123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- #ifndef ZEPHYR_INCLUDE_NET_VIRTUAL_H_
- #define ZEPHYR_INCLUDE_NET_VIRTUAL_H_
- #include <kernel.h>
- #include <zephyr/types.h>
- #include <stdbool.h>
- #include <sys/atomic.h>
- #include <net/net_ip.h>
- #include <net/net_pkt.h>
- #include <sys/util.h>
- #include <net/net_if.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- enum virtual_interface_caps {
-
- VIRTUAL_INTERFACE_IPIP = BIT(1),
-
- VIRTUAL_INTERFACE_NUM_CAPS
- };
- enum virtual_interface_config_type {
- VIRTUAL_INTERFACE_CONFIG_TYPE_PEER_ADDRESS,
- VIRTUAL_INTERFACE_CONFIG_TYPE_MTU,
- };
- struct virtual_interface_config {
- sa_family_t family;
- union {
- struct in_addr peer4addr;
- struct in6_addr peer6addr;
- int mtu;
- };
- };
- #if defined(CONFIG_NET_L2_VIRTUAL)
- #define VIRTUAL_MAX_NAME_LEN CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
- #else
- #define VIRTUAL_MAX_NAME_LEN 0
- #endif
- struct virtual_interface_api {
-
- struct net_if_api iface_api;
-
- enum virtual_interface_caps (*get_capabilities)(struct net_if *iface);
-
- int (*start)(const struct device *dev);
-
- int (*stop)(const struct device *dev);
-
- int (*send)(struct net_if *iface, struct net_pkt *pkt);
-
- enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
-
- enum net_verdict (*input)(struct net_if *input_iface,
- struct net_if *iface,
- struct net_addr *remote_addr,
- struct net_pkt *pkt);
-
- int (*attach)(struct net_if *virtual_iface, struct net_if *iface);
-
- int (*set_config)(struct net_if *iface,
- enum virtual_interface_config_type type,
- const struct virtual_interface_config *config);
-
- int (*get_config)(struct net_if *iface,
- enum virtual_interface_config_type type,
- struct virtual_interface_config *config);
- };
- BUILD_ASSERT(offsetof(struct virtual_interface_api, iface_api) == 0);
- struct virtual_interface_context {
-
- sys_snode_t node;
-
- struct net_if *virtual_iface;
-
- struct net_if *iface;
-
- enum net_l2_flags virtual_l2_flags;
-
- bool is_init;
-
- struct net_linkaddr_storage lladdr;
-
- char name[VIRTUAL_MAX_NAME_LEN];
- };
- int net_virtual_interface_attach(struct net_if *virtual_iface,
- struct net_if *iface);
- struct net_if *net_virtual_get_iface(struct net_if *iface);
- char *net_virtual_get_name(struct net_if *iface, char *buf, size_t len);
- void net_virtual_set_name(struct net_if *iface, const char *name);
- enum net_l2_flags net_virtual_set_flags(struct net_if *iface,
- enum net_l2_flags flags);
- enum net_verdict net_virtual_input(struct net_if *input_iface,
- struct net_addr *remote_addr,
- struct net_pkt *pkt);
- #if defined(CONFIG_NET_L2_VIRTUAL)
- void net_virtual_init(struct net_if *iface);
- #else
- static inline void net_virtual_init(struct net_if *iface)
- {
- ARG_UNUSED(iface);
- }
- #endif
- #if defined(CONFIG_NET_L2_VIRTUAL)
- void net_virtual_disable(struct net_if *iface);
- #else
- static inline void net_virtual_disable(struct net_if *iface)
- {
- ARG_UNUSED(iface);
- }
- #endif
- #define VIRTUAL_L2_CTX_TYPE struct virtual_interface_context
- static inline enum virtual_interface_caps
- net_virtual_get_iface_capabilities(struct net_if *iface)
- {
- const struct virtual_interface_api *virt =
- (struct virtual_interface_api *)net_if_get_device(iface)->api;
- if (!virt->get_capabilities) {
- return (enum virtual_interface_caps)0;
- }
- return virt->get_capabilities(iface);
- }
- #define Z_NET_VIRTUAL_INTERFACE_INIT(node_id, dev_name, drv_name, \
- init_fn, pm_control_fn, data, cfg, \
- prio, api, mtu) \
- Z_NET_DEVICE_INIT(node_id, dev_name, drv_name, init_fn, \
- pm_control_fn, data, cfg, prio, api, \
- VIRTUAL_L2, NET_L2_GET_CTX_TYPE(VIRTUAL_L2), \
- mtu)
- #define NET_VIRTUAL_INTERFACE_INIT(dev_name, drv_name, init_fn, \
- pm_control_fn, \
- data, cfg, prio, api, mtu) \
- Z_NET_VIRTUAL_INTERFACE_INIT(DT_INVALID_NODE, dev_name, \
- drv_name, init_fn, pm_control_fn, \
- data, cfg, prio, api, mtu)
- #ifdef __cplusplus
- }
- #endif
- #endif
|