1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_IF_MACVLAN_H
3#define _LINUX_IF_MACVLAN_H
4
5#include <linux/if_link.h>
6#include <linux/if_vlan.h>
7#include <linux/list.h>
8#include <linux/netdevice.h>
9#include <linux/netlink.h>
10#include <net/netlink.h>
11#include <linux/u64_stats_sync.h>
12
13struct macvlan_port;
14
15#define MACVLAN_MC_FILTER_BITS 8
16#define MACVLAN_MC_FILTER_SZ (1 << MACVLAN_MC_FILTER_BITS)
17
18struct macvlan_dev {
19 struct net_device *dev;
20 struct list_head list;
21 struct hlist_node hlist;
22 struct macvlan_port *port;
23 struct net_device *lowerdev;
24 netdevice_tracker dev_tracker;
25 void *accel_priv;
26 struct vlan_pcpu_stats __percpu *pcpu_stats;
27
28 DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
29
30 netdev_features_t set_features;
31 enum macvlan_mode mode;
32 u16 flags;
33 unsigned int macaddr_count;
34 u32 bc_queue_len_req;
35#ifdef CONFIG_NET_POLL_CONTROLLER
36 struct netpoll *netpoll;
37#endif
38};
39
40static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
41 unsigned int len, bool success,
42 bool multicast)
43{
44 if (likely(success)) {
45 struct vlan_pcpu_stats *pcpu_stats;
46
47 pcpu_stats = get_cpu_ptr(vlan->pcpu_stats);
48 u64_stats_update_begin(syncp: &pcpu_stats->syncp);
49 u64_stats_inc(p: &pcpu_stats->rx_packets);
50 u64_stats_add(p: &pcpu_stats->rx_bytes, val: len);
51 if (multicast)
52 u64_stats_inc(p: &pcpu_stats->rx_multicast);
53 u64_stats_update_end(syncp: &pcpu_stats->syncp);
54 put_cpu_ptr(vlan->pcpu_stats);
55 } else {
56 this_cpu_inc(vlan->pcpu_stats->rx_errors);
57 }
58}
59
60extern void macvlan_common_setup(struct net_device *dev);
61
62struct rtnl_newlink_params;
63
64extern int macvlan_common_newlink(struct net_device *dev,
65 struct rtnl_newlink_params *params,
66 struct netlink_ext_ack *extack);
67
68extern void macvlan_dellink(struct net_device *dev, struct list_head *head);
69
70extern int macvlan_link_register(struct rtnl_link_ops *ops);
71
72#if IS_ENABLED(CONFIG_MACVLAN)
73static inline struct net_device *
74macvlan_dev_real_dev(const struct net_device *dev)
75{
76 struct macvlan_dev *macvlan = netdev_priv(dev);
77
78 return macvlan->lowerdev;
79}
80#else
81static inline struct net_device *
82macvlan_dev_real_dev(const struct net_device *dev)
83{
84 BUG();
85 return NULL;
86}
87#endif
88
89static inline void *macvlan_accel_priv(struct net_device *dev)
90{
91 struct macvlan_dev *macvlan = netdev_priv(dev);
92
93 return macvlan->accel_priv;
94}
95
96static inline bool macvlan_supports_dest_filter(struct net_device *dev)
97{
98 struct macvlan_dev *macvlan = netdev_priv(dev);
99
100 return macvlan->mode == MACVLAN_MODE_PRIVATE ||
101 macvlan->mode == MACVLAN_MODE_VEPA ||
102 macvlan->mode == MACVLAN_MODE_BRIDGE;
103}
104
105static inline int macvlan_release_l2fw_offload(struct net_device *dev)
106{
107 struct macvlan_dev *macvlan = netdev_priv(dev);
108
109 macvlan->accel_priv = NULL;
110 return dev_uc_add(dev: macvlan->lowerdev, addr: dev->dev_addr);
111}
112#endif /* _LINUX_IF_MACVLAN_H */
113