xref: /linux-6.15/include/linux/if_macvlan.h (revision bb7e5ce7)
1 #ifndef _LINUX_IF_MACVLAN_H
2 #define _LINUX_IF_MACVLAN_H
3 
4 #include <linux/if_link.h>
5 #include <linux/if_vlan.h>
6 #include <linux/list.h>
7 #include <linux/netdevice.h>
8 #include <linux/netlink.h>
9 #include <net/netlink.h>
10 #include <linux/u64_stats_sync.h>
11 
12 struct macvlan_port;
13 struct macvtap_queue;
14 
15 /*
16  * Maximum times a macvtap device can be opened. This can be used to
17  * configure the number of receive queue, e.g. for multiqueue virtio.
18  */
19 #define MAX_TAP_QUEUES	256
20 
21 #define MACVLAN_MC_FILTER_BITS	8
22 #define MACVLAN_MC_FILTER_SZ	(1 << MACVLAN_MC_FILTER_BITS)
23 
24 struct macvlan_dev {
25 	struct net_device	*dev;
26 	struct list_head	list;
27 	struct hlist_node	hlist;
28 	struct macvlan_port	*port;
29 	struct net_device	*lowerdev;
30 	void			*fwd_priv;
31 	struct vlan_pcpu_stats __percpu *pcpu_stats;
32 
33 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
34 
35 	netdev_features_t	set_features;
36 	enum macvlan_mode	mode;
37 	u16			flags;
38 	/* This array tracks active taps. */
39 	struct tap_queue	__rcu *taps[MAX_TAP_QUEUES];
40 	/* This list tracks all taps (both enabled and disabled) */
41 	struct list_head	queue_list;
42 	int			numvtaps;
43 	int			numqueues;
44 	netdev_features_t	tap_features;
45 	int			minor;
46 	int			nest_level;
47 #ifdef CONFIG_NET_POLL_CONTROLLER
48 	struct netpoll		*netpoll;
49 #endif
50 	unsigned int		macaddr_count;
51 };
52 
53 static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
54 				    unsigned int len, bool success,
55 				    bool multicast)
56 {
57 	if (likely(success)) {
58 		struct vlan_pcpu_stats *pcpu_stats;
59 
60 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
61 		u64_stats_update_begin(&pcpu_stats->syncp);
62 		pcpu_stats->rx_packets++;
63 		pcpu_stats->rx_bytes += len;
64 		if (multicast)
65 			pcpu_stats->rx_multicast++;
66 		u64_stats_update_end(&pcpu_stats->syncp);
67 	} else {
68 		this_cpu_inc(vlan->pcpu_stats->rx_errors);
69 	}
70 }
71 
72 extern void macvlan_common_setup(struct net_device *dev);
73 
74 extern int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
75 				  struct nlattr *tb[], struct nlattr *data[]);
76 
77 extern void macvlan_count_rx(const struct macvlan_dev *vlan,
78 			     unsigned int len, bool success,
79 			     bool multicast);
80 
81 extern void macvlan_dellink(struct net_device *dev, struct list_head *head);
82 
83 extern int macvlan_link_register(struct rtnl_link_ops *ops);
84 
85 #if IS_ENABLED(CONFIG_MACVLAN)
86 static inline struct net_device *
87 macvlan_dev_real_dev(const struct net_device *dev)
88 {
89 	struct macvlan_dev *macvlan = netdev_priv(dev);
90 
91 	return macvlan->lowerdev;
92 }
93 #else
94 static inline struct net_device *
95 macvlan_dev_real_dev(const struct net_device *dev)
96 {
97 	BUG();
98 	return NULL;
99 }
100 #endif
101 
102 #endif /* _LINUX_IF_MACVLAN_H */
103