xref: /linux-6.15/include/linux/if_macvlan.h (revision 69c7be1b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b863ceb7SPatrick McHardy #ifndef _LINUX_IF_MACVLAN_H
3b863ceb7SPatrick McHardy #define _LINUX_IF_MACVLAN_H
4b863ceb7SPatrick McHardy 
5fc0663d6SArnd Bergmann #include <linux/if_link.h>
6cdf3e274SLi RongQing #include <linux/if_vlan.h>
7fc0663d6SArnd Bergmann #include <linux/list.h>
8fc0663d6SArnd Bergmann #include <linux/netdevice.h>
9fc0663d6SArnd Bergmann #include <linux/netlink.h>
10fc0663d6SArnd Bergmann #include <net/netlink.h>
11bc66154eSEric Dumazet #include <linux/u64_stats_sync.h>
12fc0663d6SArnd Bergmann 
13fc0663d6SArnd Bergmann struct macvlan_port;
141565c7c1SKrishna Kumar 
15cd431e73SEric Dumazet #define MACVLAN_MC_FILTER_BITS	8
16cd431e73SEric Dumazet #define MACVLAN_MC_FILTER_SZ	(1 << MACVLAN_MC_FILTER_BITS)
17cd431e73SEric Dumazet 
18fc0663d6SArnd Bergmann struct macvlan_dev {
19fc0663d6SArnd Bergmann 	struct net_device	*dev;
20fc0663d6SArnd Bergmann 	struct list_head	list;
21fc0663d6SArnd Bergmann 	struct hlist_node	hlist;
22fc0663d6SArnd Bergmann 	struct macvlan_port	*port;
23fc0663d6SArnd Bergmann 	struct net_device	*lowerdev;
241f4a5983SZiyang Xuan 	netdevice_tracker	dev_tracker;
257d775f63SAlexander Duyck 	void			*accel_priv;
26cdf3e274SLi RongQing 	struct vlan_pcpu_stats __percpu *pcpu_stats;
27cd431e73SEric Dumazet 
28cd431e73SEric Dumazet 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
29cd431e73SEric Dumazet 
302be5c767SVlad Yasevich 	netdev_features_t	set_features;
31fc0663d6SArnd Bergmann 	enum macvlan_mode	mode;
32df8ef8f3SJohn Fastabend 	u16			flags;
335e54b3c1SGirish Moodalbail 	unsigned int		macaddr_count;
34d4bff72cSThomas Karlsson 	u32			bc_queue_len_req;
35688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
36688cea83Sdingtianhong 	struct netpoll		*netpoll;
37688cea83Sdingtianhong #endif
38fc0663d6SArnd Bergmann };
39fc0663d6SArnd Bergmann 
macvlan_count_rx(const struct macvlan_dev * vlan,unsigned int len,bool success,bool multicast)40fc0663d6SArnd Bergmann static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
41fc0663d6SArnd Bergmann 				    unsigned int len, bool success,
42fc0663d6SArnd Bergmann 				    bool multicast)
43fc0663d6SArnd Bergmann {
44fc0663d6SArnd Bergmann 	if (likely(success)) {
45cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
468ffab51bSEric Dumazet 
47dd4fa1daSEric Dumazet 		pcpu_stats = get_cpu_ptr(vlan->pcpu_stats);
488ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
4909cca53cSEric Dumazet 		u64_stats_inc(&pcpu_stats->rx_packets);
5009cca53cSEric Dumazet 		u64_stats_add(&pcpu_stats->rx_bytes, len);
51fc0663d6SArnd Bergmann 		if (multicast)
5209cca53cSEric Dumazet 			u64_stats_inc(&pcpu_stats->rx_multicast);
538ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
54dd4fa1daSEric Dumazet 		put_cpu_ptr(vlan->pcpu_stats);
55fc0663d6SArnd Bergmann 	} else {
568ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->rx_errors);
57fc0663d6SArnd Bergmann 	}
58fc0663d6SArnd Bergmann }
59fc0663d6SArnd Bergmann 
608a35747aSHerbert Xu extern void macvlan_common_setup(struct net_device *dev);
618a35747aSHerbert Xu 
62*69c7be1bSXiao Liang struct rtnl_newlink_params;
63*69c7be1bSXiao Liang 
64*69c7be1bSXiao Liang extern int macvlan_common_newlink(struct net_device *dev,
65*69c7be1bSXiao Liang 				  struct rtnl_newlink_params *params,
6642ab19eeSDavid Ahern 				  struct netlink_ext_ack *extack);
67fc0663d6SArnd Bergmann 
68fc0663d6SArnd Bergmann extern void macvlan_dellink(struct net_device *dev, struct list_head *head);
69fc0663d6SArnd Bergmann 
70fc0663d6SArnd Bergmann extern int macvlan_link_register(struct rtnl_link_ops *ops);
71fc0663d6SArnd Bergmann 
72be9eac48SMichal Kubeček #if IS_ENABLED(CONFIG_MACVLAN)
73be9eac48SMichal Kubeček static inline struct net_device *
macvlan_dev_real_dev(const struct net_device * dev)74be9eac48SMichal Kubeček macvlan_dev_real_dev(const struct net_device *dev)
75be9eac48SMichal Kubeček {
76be9eac48SMichal Kubeček 	struct macvlan_dev *macvlan = netdev_priv(dev);
77be9eac48SMichal Kubeček 
78be9eac48SMichal Kubeček 	return macvlan->lowerdev;
79be9eac48SMichal Kubeček }
80be9eac48SMichal Kubeček #else
81be9eac48SMichal Kubeček static inline struct net_device *
macvlan_dev_real_dev(const struct net_device * dev)82be9eac48SMichal Kubeček macvlan_dev_real_dev(const struct net_device *dev)
83be9eac48SMichal Kubeček {
84be9eac48SMichal Kubeček 	BUG();
85be9eac48SMichal Kubeček 	return NULL;
86be9eac48SMichal Kubeček }
87be9eac48SMichal Kubeček #endif
88be9eac48SMichal Kubeček 
macvlan_accel_priv(struct net_device * dev)897d775f63SAlexander Duyck static inline void *macvlan_accel_priv(struct net_device *dev)
907d775f63SAlexander Duyck {
917d775f63SAlexander Duyck 	struct macvlan_dev *macvlan = netdev_priv(dev);
927d775f63SAlexander Duyck 
937d775f63SAlexander Duyck 	return macvlan->accel_priv;
947d775f63SAlexander Duyck }
956cb1937dSAlexander Duyck 
macvlan_supports_dest_filter(struct net_device * dev)966cb1937dSAlexander Duyck static inline bool macvlan_supports_dest_filter(struct net_device *dev)
976cb1937dSAlexander Duyck {
986cb1937dSAlexander Duyck 	struct macvlan_dev *macvlan = netdev_priv(dev);
996cb1937dSAlexander Duyck 
1006cb1937dSAlexander Duyck 	return macvlan->mode == MACVLAN_MODE_PRIVATE ||
1016cb1937dSAlexander Duyck 	       macvlan->mode == MACVLAN_MODE_VEPA ||
1026cb1937dSAlexander Duyck 	       macvlan->mode == MACVLAN_MODE_BRIDGE;
1036cb1937dSAlexander Duyck }
10453cd4d8eSAlexander Duyck 
macvlan_release_l2fw_offload(struct net_device * dev)10553cd4d8eSAlexander Duyck static inline int macvlan_release_l2fw_offload(struct net_device *dev)
10653cd4d8eSAlexander Duyck {
10753cd4d8eSAlexander Duyck 	struct macvlan_dev *macvlan = netdev_priv(dev);
10853cd4d8eSAlexander Duyck 
10953cd4d8eSAlexander Duyck 	macvlan->accel_priv = NULL;
11053cd4d8eSAlexander Duyck 	return dev_uc_add(macvlan->lowerdev, dev->dev_addr);
11153cd4d8eSAlexander Duyck }
112b863ceb7SPatrick McHardy #endif /* _LINUX_IF_MACVLAN_H */
113