Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
virtual.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2021 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#ifndef ZEPHYR_INCLUDE_NET_VIRTUAL_H_
12#define ZEPHYR_INCLUDE_NET_VIRTUAL_H_
13
14#include <zephyr/kernel.h>
15#include <zephyr/types.h>
16#include <stdbool.h>
17#include <zephyr/sys/atomic.h>
18
19#include <zephyr/net/net_ip.h>
20#include <zephyr/net/net_pkt.h>
21
22#include <zephyr/sys/util.h>
23#include <zephyr/net/net_if.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
42
45
48
50 /* Marker for capabilities - must be at the end of the enum.
51 * It is here because the capability list cannot be empty.
52 */
53 VIRTUAL_INTERFACE_NUM_CAPS
55};
56
59enum virtual_interface_config_type {
60 VIRTUAL_INTERFACE_CONFIG_TYPE_PEER_ADDRESS,
61 VIRTUAL_INTERFACE_CONFIG_TYPE_MTU,
62 VIRTUAL_INTERFACE_CONFIG_TYPE_LINK_TYPE,
63};
64
65struct virtual_interface_link_types {
66 int count;
67 uint16_t type[COND_CODE_1(CONFIG_NET_CAPTURE_COOKED_MODE,
68 (CONFIG_NET_CAPTURE_COOKED_MODE_MAX_LINK_TYPES),
69 (1))];
70};
71
72struct virtual_interface_config {
73 sa_family_t family;
74 union {
75 struct in_addr peer4addr;
76 struct in6_addr peer6addr;
77 int mtu;
78 struct virtual_interface_link_types link_types;
79 };
80};
81
82#if defined(CONFIG_NET_L2_VIRTUAL)
83#define VIRTUAL_MAX_NAME_LEN CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
84#else
85#define VIRTUAL_MAX_NAME_LEN 0
86#endif
95 struct net_if_api iface_api;
96
98 enum virtual_interface_caps (*get_capabilities)(struct net_if *iface);
99
101 int (*start)(const struct device *dev);
102
104 int (*stop)(const struct device *dev);
105
107 int (*send)(struct net_if *iface, struct net_pkt *pkt);
108
115 enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
116
118 int (*attach)(struct net_if *virtual_iface, struct net_if *iface);
119
121 int (*set_config)(struct net_if *iface,
122 enum virtual_interface_config_type type,
123 const struct virtual_interface_config *config);
124
126 int (*get_config)(struct net_if *iface,
127 enum virtual_interface_config_type type,
128 struct virtual_interface_config *config);
129};
130
131/* Make sure that the network interface API is properly setup inside
132 * Virtual API struct (it is the first one).
133 */
134BUILD_ASSERT(offsetof(struct virtual_interface_api, iface_api) == 0);
135
140 /* Keep track of contexts */
141 sys_snode_t node;
142
143 /* My virtual network interface */
144 struct net_if *virtual_iface;
152 struct net_if *iface;
153
158
161
164
166 char name[VIRTUAL_MAX_NAME_LEN];
167};
168
178int net_virtual_interface_attach(struct net_if *virtual_iface,
179 struct net_if *iface);
180
190struct net_if *net_virtual_get_iface(struct net_if *iface);
191
201char *net_virtual_get_name(struct net_if *iface, char *buf, size_t len);
202
209void net_virtual_set_name(struct net_if *iface, const char *name);
210
220 enum net_l2_flags flags);
221
231enum net_verdict net_virtual_input(struct net_if *input_iface,
232 struct net_addr *remote_addr,
233 struct net_pkt *pkt);
234
243#if defined(CONFIG_NET_L2_VIRTUAL)
244void net_virtual_init(struct net_if *iface);
245#else
246static inline void net_virtual_init(struct net_if *iface)
247{
248 ARG_UNUSED(iface);
249}
250#endif
251
258#if defined(CONFIG_NET_L2_VIRTUAL)
259void net_virtual_disable(struct net_if *iface);
260#else
261static inline void net_virtual_disable(struct net_if *iface)
262{
263 ARG_UNUSED(iface);
264}
265#endif
266
273#if defined(CONFIG_NET_L2_VIRTUAL)
274void net_virtual_enable(struct net_if *iface);
275#else
276static inline void net_virtual_enable(struct net_if *iface)
277{
278 ARG_UNUSED(iface);
279}
280#endif
281
282#define VIRTUAL_L2_CTX_TYPE struct virtual_interface_context
283
291static inline enum virtual_interface_caps
292net_virtual_get_iface_capabilities(struct net_if *iface)
293{
294 const struct virtual_interface_api *virt =
296
297 if (!virt->get_capabilities) {
298 return (enum virtual_interface_caps)0;
299 }
300
301 return virt->get_capabilities(iface);
302}
303
304#define Z_NET_VIRTUAL_INTERFACE_INIT(node_id, dev_id, name, init_fn, \
305 pm, data, config, prio, api, mtu) \
306 Z_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data, \
307 config, prio, api, VIRTUAL_L2, \
308 NET_L2_GET_CTX_TYPE(VIRTUAL_L2), mtu)
309
310#define Z_NET_VIRTUAL_INTERFACE_INIT_INSTANCE(node_id, dev_id, name, \
311 inst, init_fn, pm, data, \
312 config, prio, api, mtu) \
313 Z_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, inst, \
314 init_fn, pm, data, \
315 config, prio, api, VIRTUAL_L2, \
316 NET_L2_GET_CTX_TYPE(VIRTUAL_L2), mtu)
340#define NET_VIRTUAL_INTERFACE_INIT(dev_id, name, init_fn, pm, data, \
341 config, prio, api, mtu) \
342 Z_NET_VIRTUAL_INTERFACE_INIT(DT_INVALID_NODE, dev_id, name, \
343 init_fn, pm, data, config, prio, \
344 api, mtu)
345
368#define NET_VIRTUAL_INTERFACE_INIT_INSTANCE(dev_id, name, inst, \
369 init_fn, pm, data, \
370 config, prio, api, mtu) \
371 Z_NET_VIRTUAL_INTERFACE_INIT_INSTANCE(DT_INVALID_NODE, dev_id, \
372 name, inst, \
373 init_fn, pm, data, config, \
374 prio, api, mtu)
375
380#ifdef __cplusplus
381}
382#endif
383
384#endif /* ZEPHYR_INCLUDE_NET_VIRTUAL_H_ */
unsigned short int sa_family_t
Socket address family type.
Definition net_ip.h:168
net_verdict
Net Verdict.
Definition net_core.h:102
static const struct device * net_if_get_device(struct net_if *iface)
Get an network interface's device.
Definition net_if.h:954
net_l2_flags
L2 flags.
Definition net_l2.h:36
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition util_macro.h:195
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
void net_virtual_set_name(struct net_if *iface, const char *name)
Set the name of the virtual network interface L2.
enum net_l2_flags net_virtual_set_flags(struct net_if *iface, enum net_l2_flags flags)
Set the L2 flags of the virtual network interface.
int net_virtual_interface_attach(struct net_if *virtual_iface, struct net_if *iface)
Attach virtual network interface to the given network interface.
enum net_verdict net_virtual_input(struct net_if *input_iface, struct net_addr *remote_addr, struct net_pkt *pkt)
Feed the IP pkt to stack if tunneling is enabled.
virtual_interface_caps
Virtual interface capabilities.
Definition virtual.h:39
char * net_virtual_get_name(struct net_if *iface, char *buf, size_t len)
Return the name of the virtual network interface L2.
struct net_if * net_virtual_get_iface(struct net_if *iface)
Return network interface related to this virtual network interface.
@ VIRTUAL_INTERFACE_IPIP
IPIP tunnel.
Definition virtual.h:41
@ VIRTUAL_INTERFACE_BRIDGE
Virtual Ethernet bridge interface.
Definition virtual.h:47
@ VIRTUAL_INTERFACE_VLAN
Virtual LAN interface (VLAN)
Definition virtual.h:44
Public kernel APIs.
Public API for network interface.
IPv6 and IPv4 definitions.
Network packet buffer descriptor API.
flags
Definition parser.h:96
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
IPv6 address struct.
Definition net_ip.h:143
IPv4 address struct.
Definition net_ip.h:155
Network Interface structure.
Definition net_if.h:690
Hardware link address structure.
Definition net_linkaddr.h:90
Network packet.
Definition net_pkt.h:91
Virtual L2 API operations.
Definition virtual.h:90
int(* stop)(const struct device *dev)
Stop the device.
Definition virtual.h:104
int(* get_config)(struct net_if *iface, enum virtual_interface_config_type type, struct virtual_interface_config *config)
Get specific L2 configuration.
Definition virtual.h:126
int(* set_config)(struct net_if *iface, enum virtual_interface_config_type type, const struct virtual_interface_config *config)
Set specific L2 configuration.
Definition virtual.h:121
enum net_verdict(* recv)(struct net_if *iface, struct net_pkt *pkt)
Receive a network packet.
Definition virtual.h:115
int(* send)(struct net_if *iface, struct net_pkt *pkt)
Send a network packet.
Definition virtual.h:107
int(* attach)(struct net_if *virtual_iface, struct net_if *iface)
Pass the attachment information to virtual interface.
Definition virtual.h:118
enum virtual_interface_caps(* get_capabilities)(struct net_if *iface)
Get the virtual interface capabilities.
Definition virtual.h:98
struct net_if_api iface_api
The net_if_api must be placed in first position in this struct so that we are compatible with network...
Definition virtual.h:95
int(* start)(const struct device *dev)
Start the device.
Definition virtual.h:101
Virtual L2 context that is needed to binding to the real network interface.
Definition virtual.h:138
enum net_l2_flags virtual_l2_flags
This tells what L2 features does virtual support.
Definition virtual.h:157
struct net_if * iface
Other network interface this virtual network interface is attached to.
Definition virtual.h:152
char name[VIRTUAL_MAX_NAME_LEN]
User friendly name of this L2 layer.
Definition virtual.h:166
bool is_init
Is this context already initialized.
Definition virtual.h:160
struct net_linkaddr_storage lladdr
Link address for this network interface.
Definition virtual.h:163
Misc utilities.