Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | /** @file * @brief Virtual Network Interface */ /* * Copyright (c) 2021 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #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 /** * @brief Virtual network interface support functions * @defgroup virtual Virtual Network Interface Support Functions * @ingroup networking * @{ */ /** Virtual interface capabilities */ enum virtual_interface_caps { /** IPIP tunnel */ VIRTUAL_INTERFACE_IPIP = BIT(1), /** @cond INTERNAL_HIDDEN */ /* Marker for capabilities - must be at the end of the enum. * It is here because the capability list cannot be empty. */ VIRTUAL_INTERFACE_NUM_CAPS /** @endcond */ }; /** @cond INTERNAL_HIDDEN */ 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 /** @endcond */ struct virtual_interface_api { /** * The net_if_api must be placed in first position in this * struct so that we are compatible with network interface API. */ struct net_if_api iface_api; /** Get the virtual interface capabilities */ enum virtual_interface_caps (*get_capabilities)(struct net_if *iface); /** Start the device */ int (*start)(const struct device *dev); /** Stop the device */ int (*stop)(const struct device *dev); /** Send a network packet */ int (*send)(struct net_if *iface, struct net_pkt *pkt); /** Receive a network packet */ enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt); /** Check if this received network packet is for this interface. * The callback returns NET_CONTINUE if this interface will accept the * packet and pass it upper layers, NET_DROP if the packet is to be * dropped and NET_OK to pass it to next interface. */ enum net_verdict (*input)(struct net_if *input_iface, struct net_if *iface, struct net_addr *remote_addr, struct net_pkt *pkt); /** Pass the attachment information to virtual interface */ int (*attach)(struct net_if *virtual_iface, struct net_if *iface); /** Set specific L2 configuration */ int (*set_config)(struct net_if *iface, enum virtual_interface_config_type type, const struct virtual_interface_config *config); /** Get specific L2 configuration */ int (*get_config)(struct net_if *iface, enum virtual_interface_config_type type, struct virtual_interface_config *config); }; /* Make sure that the network interface API is properly setup inside * Virtual API struct (it is the first one). */ BUILD_ASSERT(offsetof(struct virtual_interface_api, iface_api) == 0); /** Virtual L2 context that is needed to binding to the real network interface */ struct virtual_interface_context { /** @cond INTERNAL_HIDDEN */ /* Keep track of contexts */ sys_snode_t node; /* My virtual network interface */ struct net_if *virtual_iface; /** @endcond */ /** * Other network interface this virtual network interface is * attached to. These values can be chained so virtual network * interfaces can run on top of other virtual interfaces. */ struct net_if *iface; /** * This tells what L2 features does virtual support. */ enum net_l2_flags virtual_l2_flags; /** Is this context already initialized */ bool is_init; /** Link address for this network interface */ struct net_linkaddr_storage lladdr; /** User friendly name of this L2 layer. */ char name[VIRTUAL_MAX_NAME_LEN]; }; /** * @brief Attach virtual network interface to the given network interface. * * @param virtual_iface Virtual network interface. * @param iface Network interface we are attached to. This can be NULL, * if we want to detach. * * @return 0 if ok, <0 if attaching failed */ int net_virtual_interface_attach(struct net_if *virtual_iface, struct net_if *iface); /** * @brief Return network interface related to this virtual network interface. * The returned network interface is below this virtual network interface. * * @param iface Virtual network interface. * * @return Network interface related to this virtual interface or * NULL if no such interface exists. */ struct net_if *net_virtual_get_iface(struct net_if *iface); /** * @brief Return the name of the virtual network interface L2. * * @param iface Virtual network interface. * @param buf Buffer to store the name * @param len Max buffer length * * @return Name of the virtual network interface. */ char *net_virtual_get_name(struct net_if *iface, char *buf, size_t len); /** * @brief Set the name of the virtual network interface L2. * * @param iface Virtual network interface. * @param name Name of the virtual L2 layer. */ void net_virtual_set_name(struct net_if *iface, const char *name); /** * @brief Set the L2 flags of the virtual network interface. * * @param iface Virtual network interface. * @param flags L2 flags to set. * * @return Previous flags that were set. */ enum net_l2_flags net_virtual_set_flags(struct net_if *iface, enum net_l2_flags flags); /** * @brief Feed the IP pkt to stack if tunneling is enabled. * * @param input_iface Network interface receiving the pkt. * @param remote_addr IP address of the sender. * @param pkt Network packet. * * @return Verdict what to do with the packet. */ enum net_verdict net_virtual_input(struct net_if *input_iface, struct net_addr *remote_addr, struct net_pkt *pkt); /** @cond INTERNAL_HIDDEN */ /** * @brief Initialize the network interface so that a virtual * interface can be attached to it. * * @param iface Network interface */ #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 /** * @brief Take virtual network interface down. This is called * if the underlying interface is going down. * * @param iface Network interface */ #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 /** * @brief Return virtual device hardware capability information. * * @param iface Network interface * * @return Hardware capabilities */ 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) /** @endcond */ /** * @def NET_VIRTUAL_INTERFACE_INIT * * @brief Create a virtual network interface. Binding to another interface * is done at runtime by calling net_virtual_interface_attach(). * The attaching is done automatically when setting up tunneling * when peer IP address is set in IP tunneling driver. * * @param dev_name Network device name. * @param drv_name The name this instance of the driver exposes to * the system. * @param init_fn Address to the init function of the driver. * @param pm_control_fn Pointer to pm_control function. * Can be NULL if not implemented. * @param data Pointer to the device's private data. * @param cfg The address to the structure containing the * configuration information for this instance of the driver. * @param prio The initialization level at which configuration occurs. * @param api Provides an initial pointer to the API function struct * used by the driver. Can be NULL. * @param mtu Maximum transfer unit in bytes for this network interface. * This is the default value and its value can be tweaked at runtime. */ #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 /* ZEPHYR_INCLUDE_NET_VIRTUAL_H_ */ |