Skip to content

Commit 5a82d56

Browse files
rjarrychristophefontaine
authored andcommitted
iface: add callbacks for low-level configuration
Extend iface_type with optional callbacks for setting MAC addresses, MTU, up/down state, promiscuous mode, allmulticast mode, and VLAN filtering. This allows interface types to implement hardware-specific configuration while falling back to generic behavior when callbacks are not provided. Implement wrapper functions that validate the interface, check if the callback is available, and invoke it. For operations that cannot be performed without hardware support (like promiscuous mode), return EOPNOTSUPP if the callback is missing and the operation is requested. Signed-off-by: Robin Jarry <rjarry@redhat.com>
1 parent 7dc8894 commit 5a82d56

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

modules/infra/control/gr_iface.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ struct iface_type {
3636
);
3737
int (*fini)(struct iface *);
3838
int (*get_eth_addr)(const struct iface *, struct rte_ether_addr *);
39+
int (*set_eth_addr)(struct iface *, const struct rte_ether_addr *);
3940
int (*add_eth_addr)(struct iface *, const struct rte_ether_addr *);
4041
int (*del_eth_addr)(struct iface *, const struct rte_ether_addr *);
42+
int (*set_up_down)(struct iface *, bool up);
43+
int (*set_mtu)(struct iface *, uint16_t mtu);
44+
int (*set_promisc)(struct iface *, bool enabled);
45+
int (*set_allmulti)(struct iface *, bool enabled);
46+
int (*add_vlan)(struct iface *, uint16_t vlan_id);
47+
int (*del_vlan)(struct iface *, uint16_t vlan_id);
4148
void (*to_api)(void *api_info, const struct iface *);
4249
const char *name;
4350
STAILQ_ENTRY(iface_type) next;
@@ -57,8 +64,15 @@ struct iface *iface_from_id(uint16_t ifid);
5764
void iface_add_subinterface(struct iface *parent, struct iface *sub);
5865
void iface_del_subinterface(struct iface *parent, struct iface *sub);
5966
int iface_get_eth_addr(uint16_t ifid, struct rte_ether_addr *);
67+
int iface_set_eth_addr(uint16_t ifid, const struct rte_ether_addr *);
6068
int iface_add_eth_addr(uint16_t ifid, const struct rte_ether_addr *);
6169
int iface_del_eth_addr(uint16_t ifid, const struct rte_ether_addr *);
70+
int iface_set_mtu(uint16_t ifid, uint16_t mtu);
71+
int iface_set_up_down(uint16_t ifid, bool up);
72+
int iface_set_promisc(uint16_t ifid, bool enabled);
73+
int iface_set_allmulti(uint16_t ifid, bool enabled);
74+
int iface_add_vlan(uint16_t ifid, uint16_t vlan_id);
75+
int iface_del_vlan(uint16_t ifid, uint16_t vlan_id);
6276
uint16_t ifaces_count(gr_iface_type_t type_id);
6377
struct iface *iface_next(gr_iface_type_t type_id, const struct iface *prev);
6478

modules/infra/control/iface.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
// Copyright (c) 2024 Robin Jarry
33

4+
#include <gr_config.h>
45
#include <gr_event.h>
56
#include <gr_iface.h>
67
#include <gr_log.h>
@@ -257,6 +258,21 @@ void iface_del_subinterface(struct iface *parent, struct iface *sub) {
257258
}
258259
}
259260

261+
int iface_set_eth_addr(uint16_t ifid, const struct rte_ether_addr *mac) {
262+
struct iface *iface = iface_from_id(ifid);
263+
const struct iface_type *type;
264+
265+
if (iface == NULL)
266+
return -errno;
267+
268+
type = iface_type_get(iface->type);
269+
assert(type != NULL);
270+
if (type->set_eth_addr == NULL)
271+
return errno_set(EOPNOTSUPP);
272+
273+
return type->set_eth_addr(iface, mac);
274+
}
275+
260276
int iface_add_eth_addr(uint16_t ifid, const struct rte_ether_addr *mac) {
261277
struct iface *iface = iface_from_id(ifid);
262278
const struct iface_type *type;
@@ -287,6 +303,111 @@ int iface_del_eth_addr(uint16_t ifid, const struct rte_ether_addr *mac) {
287303
return type->del_eth_addr(iface, mac);
288304
}
289305

306+
int iface_set_mtu(uint16_t ifid, uint16_t mtu) {
307+
struct iface *iface = iface_from_id(ifid);
308+
const struct iface_type *type;
309+
310+
if (iface == NULL)
311+
return -errno;
312+
313+
if (mtu > gr_config.max_mtu)
314+
return errno_set(ERANGE);
315+
316+
type = iface_type_get(iface->type);
317+
assert(type != NULL);
318+
if (type->set_mtu != NULL)
319+
return type->set_mtu(iface, mtu);
320+
321+
iface->mtu = mtu;
322+
return 0;
323+
}
324+
325+
int iface_set_up_down(uint16_t ifid, bool up) {
326+
struct iface *iface = iface_from_id(ifid);
327+
const struct iface_type *type;
328+
329+
if (iface == NULL)
330+
return -errno;
331+
332+
type = iface_type_get(iface->type);
333+
assert(type != NULL);
334+
if (type->set_up_down != NULL)
335+
return type->set_up_down(iface, up);
336+
337+
if (!(iface->flags & GR_IFACE_F_UP) && up)
338+
iface->flags |= GR_IFACE_F_UP;
339+
else if ((iface->flags & GR_IFACE_F_UP) && !up)
340+
iface->flags &= ~GR_IFACE_F_UP;
341+
342+
return 0;
343+
}
344+
345+
int iface_set_promisc(uint16_t ifid, bool enabled) {
346+
struct iface *iface = iface_from_id(ifid);
347+
const struct iface_type *type;
348+
349+
if (iface == NULL)
350+
return -errno;
351+
352+
type = iface_type_get(iface->type);
353+
assert(type != NULL);
354+
if (type->set_promisc != NULL)
355+
return type->set_promisc(iface, enabled);
356+
357+
if (enabled)
358+
return errno_set(EOPNOTSUPP);
359+
360+
return 0;
361+
}
362+
363+
int iface_set_allmulti(uint16_t ifid, bool enabled) {
364+
struct iface *iface = iface_from_id(ifid);
365+
const struct iface_type *type;
366+
367+
if (iface == NULL)
368+
return -errno;
369+
370+
type = iface_type_get(iface->type);
371+
assert(type != NULL);
372+
if (type->set_allmulti != NULL)
373+
return type->set_allmulti(iface, enabled);
374+
375+
if (enabled)
376+
return errno_set(EOPNOTSUPP);
377+
378+
return 0;
379+
}
380+
381+
int iface_add_vlan(uint16_t ifid, uint16_t vlan_id) {
382+
struct iface *iface = iface_from_id(ifid);
383+
const struct iface_type *type;
384+
385+
if (iface == NULL)
386+
return -errno;
387+
388+
type = iface_type_get(iface->type);
389+
assert(type != NULL);
390+
if (type->add_vlan == NULL)
391+
return errno_set(EOPNOTSUPP);
392+
393+
return type->add_vlan(iface, vlan_id);
394+
}
395+
396+
int iface_del_vlan(uint16_t ifid, uint16_t vlan_id) {
397+
struct iface *iface = iface_from_id(ifid);
398+
const struct iface_type *type;
399+
400+
if (iface == NULL)
401+
return -errno;
402+
403+
type = iface_type_get(iface->type);
404+
assert(type != NULL);
405+
if (type->del_vlan == NULL)
406+
return errno_set(EOPNOTSUPP);
407+
408+
return type->del_vlan(iface, vlan_id);
409+
}
410+
290411
int iface_destroy(uint16_t ifid) {
291412
struct iface *iface = iface_from_id(ifid);
292413
const struct iface_type *type;

0 commit comments

Comments
 (0)