2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
8 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/u64_stats_sync.h>
23 #define DRV_NAME "netdevsim"
25 #define NSIM_XDP_MAX_MTU 4000
27 #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)
30 struct bpf_offload_dev
;
32 struct nsim_vf_config
;
34 struct netdevsim_shared_dev
{
40 struct bpf_offload_dev
*bpf_dev
;
42 struct dentry
*ddir_bpf_bound_progs
;
45 struct list_head bpf_bound_progs
;
46 struct list_head bpf_bound_maps
;
49 #define NSIM_IPSEC_MAX_SA_COUNT 33
50 #define NSIM_IPSEC_VALID BIT(31)
53 struct xfrm_state
*xs
;
63 struct nsim_sa sa
[NSIM_IPSEC_MAX_SA_COUNT
];
71 struct net_device
*netdev
;
75 struct u64_stats_sync syncp
;
78 struct netdevsim_shared_dev
*sdev
;
83 struct nsim_vf_config
*vfconfigs
;
85 struct bpf_prog
*bpf_offloaded
;
88 struct xdp_attachment_info xdp
;
89 struct xdp_attachment_info xdp_hw
;
92 u32 bpf_bind_verifier_delay
;
95 bool bpf_tc_non_bound_accept
;
96 bool bpf_xdpdrv_accept
;
97 bool bpf_xdpoffload_accept
;
100 #if IS_ENABLED(CONFIG_NET_DEVLINK)
101 struct devlink
*devlink
;
103 struct nsim_ipsec ipsec
;
106 #ifdef CONFIG_BPF_SYSCALL
107 int nsim_bpf_init(struct netdevsim
*ns
);
108 void nsim_bpf_uninit(struct netdevsim
*ns
);
109 int nsim_bpf(struct net_device
*dev
, struct netdev_bpf
*bpf
);
110 int nsim_bpf_disable_tc(struct netdevsim
*ns
);
111 int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type
,
112 void *type_data
, void *cb_priv
);
114 static inline int nsim_bpf_init(struct netdevsim
*ns
)
119 static inline void nsim_bpf_uninit(struct netdevsim
*ns
)
123 static inline int nsim_bpf(struct net_device
*dev
, struct netdev_bpf
*bpf
)
125 return bpf
->command
== XDP_QUERY_PROG
? 0 : -EOPNOTSUPP
;
128 static inline int nsim_bpf_disable_tc(struct netdevsim
*ns
)
134 nsim_bpf_setup_tc_block_cb(enum tc_setup_type type
, void *type_data
,
141 #if IS_ENABLED(CONFIG_NET_DEVLINK)
142 enum nsim_resource_id
{
143 NSIM_RESOURCE_NONE
, /* DEVLINK_RESOURCE_ID_PARENT_TOP */
145 NSIM_RESOURCE_IPV4_FIB
,
146 NSIM_RESOURCE_IPV4_FIB_RULES
,
148 NSIM_RESOURCE_IPV6_FIB
,
149 NSIM_RESOURCE_IPV6_FIB_RULES
,
152 int nsim_devlink_setup(struct netdevsim
*ns
);
153 void nsim_devlink_teardown(struct netdevsim
*ns
);
155 int nsim_devlink_init(void);
156 void nsim_devlink_exit(void);
158 int nsim_fib_init(void);
159 void nsim_fib_exit(void);
160 u64
nsim_fib_get_val(struct net
*net
, enum nsim_resource_id res_id
, bool max
);
161 int nsim_fib_set_max(struct net
*net
, enum nsim_resource_id res_id
, u64 val
,
162 struct netlink_ext_ack
*extack
);
164 static inline int nsim_devlink_setup(struct netdevsim
*ns
)
169 static inline void nsim_devlink_teardown(struct netdevsim
*ns
)
173 static inline int nsim_devlink_init(void)
178 static inline void nsim_devlink_exit(void)
183 #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
184 void nsim_ipsec_init(struct netdevsim
*ns
);
185 void nsim_ipsec_teardown(struct netdevsim
*ns
);
186 bool nsim_ipsec_tx(struct netdevsim
*ns
, struct sk_buff
*skb
);
188 static inline void nsim_ipsec_init(struct netdevsim
*ns
)
192 static inline void nsim_ipsec_teardown(struct netdevsim
*ns
)
196 static inline bool nsim_ipsec_tx(struct netdevsim
*ns
, struct sk_buff
*skb
)
202 static inline struct netdevsim
*to_nsim(struct device
*ptr
)
204 return container_of(ptr
, struct netdevsim
, dev
);