Loading...
1/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
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
6 * source tree.
7 *
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.
14 */
15
16#include <linux/debugfs.h>
17#include <linux/etherdevice.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/netdevice.h>
21#include <linux/slab.h>
22#include <net/netlink.h>
23#include <net/pkt_cls.h>
24#include <net/rtnetlink.h>
25#include <net/udp_tunnel.h>
26
27#include "netdevsim.h"
28
29static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
30{
31 struct netdevsim *ns = netdev_priv(dev);
32
33 if (!nsim_ipsec_tx(ns, skb))
34 goto out;
35
36 u64_stats_update_begin(&ns->syncp);
37 ns->tx_packets++;
38 ns->tx_bytes += skb->len;
39 u64_stats_update_end(&ns->syncp);
40
41out:
42 dev_kfree_skb(skb);
43
44 return NETDEV_TX_OK;
45}
46
47static void nsim_set_rx_mode(struct net_device *dev)
48{
49}
50
51static int nsim_change_mtu(struct net_device *dev, int new_mtu)
52{
53 struct netdevsim *ns = netdev_priv(dev);
54
55 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
56 return -EBUSY;
57
58 dev->mtu = new_mtu;
59
60 return 0;
61}
62
63static void
64nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
65{
66 struct netdevsim *ns = netdev_priv(dev);
67 unsigned int start;
68
69 do {
70 start = u64_stats_fetch_begin(&ns->syncp);
71 stats->tx_bytes = ns->tx_bytes;
72 stats->tx_packets = ns->tx_packets;
73 } while (u64_stats_fetch_retry(&ns->syncp, start));
74}
75
76static int
77nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
78{
79 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
80}
81
82static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
83{
84 struct netdevsim *ns = netdev_priv(dev);
85 struct nsim_dev *nsim_dev = ns->nsim_dev;
86
87 /* Only refuse multicast addresses, zero address can mean unset/any. */
88 if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
89 return -EINVAL;
90 memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
91
92 return 0;
93}
94
95static int nsim_set_vf_vlan(struct net_device *dev, int vf,
96 u16 vlan, u8 qos, __be16 vlan_proto)
97{
98 struct netdevsim *ns = netdev_priv(dev);
99 struct nsim_dev *nsim_dev = ns->nsim_dev;
100
101 if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
102 return -EINVAL;
103
104 nsim_dev->vfconfigs[vf].vlan = vlan;
105 nsim_dev->vfconfigs[vf].qos = qos;
106 nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
107
108 return 0;
109}
110
111static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
112{
113 struct netdevsim *ns = netdev_priv(dev);
114 struct nsim_dev *nsim_dev = ns->nsim_dev;
115
116 if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
117 pr_err("Not supported in switchdev mode. Please use devlink API.\n");
118 return -EOPNOTSUPP;
119 }
120
121 if (vf >= nsim_dev_get_vfs(nsim_dev))
122 return -EINVAL;
123
124 nsim_dev->vfconfigs[vf].min_tx_rate = min;
125 nsim_dev->vfconfigs[vf].max_tx_rate = max;
126
127 return 0;
128}
129
130static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
131{
132 struct netdevsim *ns = netdev_priv(dev);
133 struct nsim_dev *nsim_dev = ns->nsim_dev;
134
135 if (vf >= nsim_dev_get_vfs(nsim_dev))
136 return -EINVAL;
137 nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
138
139 return 0;
140}
141
142static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
143{
144 struct netdevsim *ns = netdev_priv(dev);
145 struct nsim_dev *nsim_dev = ns->nsim_dev;
146
147 if (vf >= nsim_dev_get_vfs(nsim_dev))
148 return -EINVAL;
149 nsim_dev->vfconfigs[vf].rss_query_enabled = val;
150
151 return 0;
152}
153
154static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
155{
156 struct netdevsim *ns = netdev_priv(dev);
157 struct nsim_dev *nsim_dev = ns->nsim_dev;
158
159 if (vf >= nsim_dev_get_vfs(nsim_dev))
160 return -EINVAL;
161 nsim_dev->vfconfigs[vf].trusted = val;
162
163 return 0;
164}
165
166static int
167nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
168{
169 struct netdevsim *ns = netdev_priv(dev);
170 struct nsim_dev *nsim_dev = ns->nsim_dev;
171
172 if (vf >= nsim_dev_get_vfs(nsim_dev))
173 return -EINVAL;
174
175 ivi->vf = vf;
176 ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
177 ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
178 ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
179 ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
180 ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
181 ivi->qos = nsim_dev->vfconfigs[vf].qos;
182 memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
183 ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
184 ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
185 ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
186
187 return 0;
188}
189
190static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
191{
192 struct netdevsim *ns = netdev_priv(dev);
193 struct nsim_dev *nsim_dev = ns->nsim_dev;
194
195 if (vf >= nsim_dev_get_vfs(nsim_dev))
196 return -EINVAL;
197
198 switch (state) {
199 case IFLA_VF_LINK_STATE_AUTO:
200 case IFLA_VF_LINK_STATE_ENABLE:
201 case IFLA_VF_LINK_STATE_DISABLE:
202 break;
203 default:
204 return -EINVAL;
205 }
206
207 nsim_dev->vfconfigs[vf].link_state = state;
208
209 return 0;
210}
211
212static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats)
213{
214 stats->window_drops = 0;
215 stats->tx_overruns = 0;
216}
217
218static int nsim_setup_tc_taprio(struct net_device *dev,
219 struct tc_taprio_qopt_offload *offload)
220{
221 int err = 0;
222
223 switch (offload->cmd) {
224 case TAPRIO_CMD_REPLACE:
225 case TAPRIO_CMD_DESTROY:
226 break;
227 case TAPRIO_CMD_STATS:
228 nsim_taprio_stats(&offload->stats);
229 break;
230 default:
231 err = -EOPNOTSUPP;
232 }
233
234 return err;
235}
236
237static LIST_HEAD(nsim_block_cb_list);
238
239static int
240nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
241{
242 struct netdevsim *ns = netdev_priv(dev);
243
244 switch (type) {
245 case TC_SETUP_QDISC_TAPRIO:
246 return nsim_setup_tc_taprio(dev, type_data);
247 case TC_SETUP_BLOCK:
248 return flow_block_cb_setup_simple(type_data,
249 &nsim_block_cb_list,
250 nsim_setup_tc_block_cb,
251 ns, ns, true);
252 default:
253 return -EOPNOTSUPP;
254 }
255}
256
257static int
258nsim_set_features(struct net_device *dev, netdev_features_t features)
259{
260 struct netdevsim *ns = netdev_priv(dev);
261
262 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
263 return nsim_bpf_disable_tc(ns);
264
265 return 0;
266}
267
268static const struct net_device_ops nsim_netdev_ops = {
269 .ndo_start_xmit = nsim_start_xmit,
270 .ndo_set_rx_mode = nsim_set_rx_mode,
271 .ndo_set_mac_address = eth_mac_addr,
272 .ndo_validate_addr = eth_validate_addr,
273 .ndo_change_mtu = nsim_change_mtu,
274 .ndo_get_stats64 = nsim_get_stats64,
275 .ndo_set_vf_mac = nsim_set_vf_mac,
276 .ndo_set_vf_vlan = nsim_set_vf_vlan,
277 .ndo_set_vf_rate = nsim_set_vf_rate,
278 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
279 .ndo_set_vf_trust = nsim_set_vf_trust,
280 .ndo_get_vf_config = nsim_get_vf_config,
281 .ndo_set_vf_link_state = nsim_set_vf_link_state,
282 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
283 .ndo_setup_tc = nsim_setup_tc,
284 .ndo_set_features = nsim_set_features,
285 .ndo_bpf = nsim_bpf,
286};
287
288static const struct net_device_ops nsim_vf_netdev_ops = {
289 .ndo_start_xmit = nsim_start_xmit,
290 .ndo_set_rx_mode = nsim_set_rx_mode,
291 .ndo_set_mac_address = eth_mac_addr,
292 .ndo_validate_addr = eth_validate_addr,
293 .ndo_change_mtu = nsim_change_mtu,
294 .ndo_get_stats64 = nsim_get_stats64,
295 .ndo_setup_tc = nsim_setup_tc,
296 .ndo_set_features = nsim_set_features,
297};
298
299static void nsim_setup(struct net_device *dev)
300{
301 ether_setup(dev);
302 eth_hw_addr_random(dev);
303
304 dev->tx_queue_len = 0;
305 dev->flags |= IFF_NOARP;
306 dev->flags &= ~IFF_MULTICAST;
307 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
308 IFF_NO_QUEUE;
309 dev->features |= NETIF_F_HIGHDMA |
310 NETIF_F_SG |
311 NETIF_F_FRAGLIST |
312 NETIF_F_HW_CSUM |
313 NETIF_F_TSO;
314 dev->hw_features |= NETIF_F_HW_TC;
315 dev->max_mtu = ETH_MAX_MTU;
316 dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
317}
318
319static int nsim_init_netdevsim(struct netdevsim *ns)
320{
321 struct mock_phc *phc;
322 int err;
323
324 phc = mock_phc_create(&ns->nsim_bus_dev->dev);
325 if (IS_ERR(phc))
326 return PTR_ERR(phc);
327
328 ns->phc = phc;
329 ns->netdev->netdev_ops = &nsim_netdev_ops;
330
331 err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
332 if (err)
333 goto err_phc_destroy;
334
335 rtnl_lock();
336 err = nsim_bpf_init(ns);
337 if (err)
338 goto err_utn_destroy;
339
340 nsim_macsec_init(ns);
341 nsim_ipsec_init(ns);
342
343 err = register_netdevice(ns->netdev);
344 if (err)
345 goto err_ipsec_teardown;
346 rtnl_unlock();
347 return 0;
348
349err_ipsec_teardown:
350 nsim_ipsec_teardown(ns);
351 nsim_macsec_teardown(ns);
352 nsim_bpf_uninit(ns);
353err_utn_destroy:
354 rtnl_unlock();
355 nsim_udp_tunnels_info_destroy(ns->netdev);
356err_phc_destroy:
357 mock_phc_destroy(ns->phc);
358 return err;
359}
360
361static int nsim_init_netdevsim_vf(struct netdevsim *ns)
362{
363 int err;
364
365 ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
366 rtnl_lock();
367 err = register_netdevice(ns->netdev);
368 rtnl_unlock();
369 return err;
370}
371
372static void nsim_exit_netdevsim(struct netdevsim *ns)
373{
374 nsim_udp_tunnels_info_destroy(ns->netdev);
375 mock_phc_destroy(ns->phc);
376}
377
378struct netdevsim *
379nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
380{
381 struct net_device *dev;
382 struct netdevsim *ns;
383 int err;
384
385 dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
386 nsim_dev->nsim_bus_dev->num_queues);
387 if (!dev)
388 return ERR_PTR(-ENOMEM);
389
390 dev_net_set(dev, nsim_dev_net(nsim_dev));
391 ns = netdev_priv(dev);
392 ns->netdev = dev;
393 u64_stats_init(&ns->syncp);
394 ns->nsim_dev = nsim_dev;
395 ns->nsim_dev_port = nsim_dev_port;
396 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
397 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
398 SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port);
399 nsim_ethtool_init(ns);
400 if (nsim_dev_port_is_pf(nsim_dev_port))
401 err = nsim_init_netdevsim(ns);
402 else
403 err = nsim_init_netdevsim_vf(ns);
404 if (err)
405 goto err_free_netdev;
406 return ns;
407
408err_free_netdev:
409 free_netdev(dev);
410 return ERR_PTR(err);
411}
412
413void nsim_destroy(struct netdevsim *ns)
414{
415 struct net_device *dev = ns->netdev;
416
417 rtnl_lock();
418 unregister_netdevice(dev);
419 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
420 nsim_macsec_teardown(ns);
421 nsim_ipsec_teardown(ns);
422 nsim_bpf_uninit(ns);
423 }
424 rtnl_unlock();
425 if (nsim_dev_port_is_pf(ns->nsim_dev_port))
426 nsim_exit_netdevsim(ns);
427 free_netdev(dev);
428}
429
430static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
431 struct netlink_ext_ack *extack)
432{
433 NL_SET_ERR_MSG_MOD(extack,
434 "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
435 return -EOPNOTSUPP;
436}
437
438static struct rtnl_link_ops nsim_link_ops __read_mostly = {
439 .kind = DRV_NAME,
440 .validate = nsim_validate,
441};
442
443static int __init nsim_module_init(void)
444{
445 int err;
446
447 err = nsim_dev_init();
448 if (err)
449 return err;
450
451 err = nsim_bus_init();
452 if (err)
453 goto err_dev_exit;
454
455 err = rtnl_link_register(&nsim_link_ops);
456 if (err)
457 goto err_bus_exit;
458
459 return 0;
460
461err_bus_exit:
462 nsim_bus_exit();
463err_dev_exit:
464 nsim_dev_exit();
465 return err;
466}
467
468static void __exit nsim_module_exit(void)
469{
470 rtnl_link_unregister(&nsim_link_ops);
471 nsim_bus_exit();
472 nsim_dev_exit();
473}
474
475module_init(nsim_module_init);
476module_exit(nsim_module_exit);
477MODULE_LICENSE("GPL");
478MODULE_DESCRIPTION("Simulated networking device for testing");
479MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
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
6 * source tree.
7 *
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.
14 */
15
16#include <linux/debugfs.h>
17#include <linux/etherdevice.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/netdevice.h>
21#include <linux/slab.h>
22#include <net/netdev_queues.h>
23#include <net/page_pool/helpers.h>
24#include <net/netlink.h>
25#include <net/net_shaper.h>
26#include <net/pkt_cls.h>
27#include <net/rtnetlink.h>
28#include <net/udp_tunnel.h>
29
30#include "netdevsim.h"
31
32#define NSIM_RING_SIZE 256
33
34static int nsim_napi_rx(struct nsim_rq *rq, struct sk_buff *skb)
35{
36 if (skb_queue_len(&rq->skb_queue) > NSIM_RING_SIZE) {
37 dev_kfree_skb_any(skb);
38 return NET_RX_DROP;
39 }
40
41 skb_queue_tail(&rq->skb_queue, skb);
42 return NET_RX_SUCCESS;
43}
44
45static int nsim_forward_skb(struct net_device *dev, struct sk_buff *skb,
46 struct nsim_rq *rq)
47{
48 return __dev_forward_skb(dev, skb) ?: nsim_napi_rx(rq, skb);
49}
50
51static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
52{
53 struct netdevsim *ns = netdev_priv(dev);
54 struct net_device *peer_dev;
55 unsigned int len = skb->len;
56 struct netdevsim *peer_ns;
57 struct nsim_rq *rq;
58 int rxq;
59
60 rcu_read_lock();
61 if (!nsim_ipsec_tx(ns, skb))
62 goto out_drop_free;
63
64 peer_ns = rcu_dereference(ns->peer);
65 if (!peer_ns)
66 goto out_drop_free;
67
68 peer_dev = peer_ns->netdev;
69 rxq = skb_get_queue_mapping(skb);
70 if (rxq >= peer_dev->num_rx_queues)
71 rxq = rxq % peer_dev->num_rx_queues;
72 rq = &peer_ns->rq[rxq];
73
74 skb_tx_timestamp(skb);
75 if (unlikely(nsim_forward_skb(peer_dev, skb, rq) == NET_RX_DROP))
76 goto out_drop_cnt;
77
78 napi_schedule(&rq->napi);
79
80 rcu_read_unlock();
81 u64_stats_update_begin(&ns->syncp);
82 ns->tx_packets++;
83 ns->tx_bytes += len;
84 u64_stats_update_end(&ns->syncp);
85 return NETDEV_TX_OK;
86
87out_drop_free:
88 dev_kfree_skb(skb);
89out_drop_cnt:
90 rcu_read_unlock();
91 u64_stats_update_begin(&ns->syncp);
92 ns->tx_dropped++;
93 u64_stats_update_end(&ns->syncp);
94 return NETDEV_TX_OK;
95}
96
97static void nsim_set_rx_mode(struct net_device *dev)
98{
99}
100
101static int nsim_change_mtu(struct net_device *dev, int new_mtu)
102{
103 struct netdevsim *ns = netdev_priv(dev);
104
105 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
106 return -EBUSY;
107
108 WRITE_ONCE(dev->mtu, new_mtu);
109
110 return 0;
111}
112
113static void
114nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
115{
116 struct netdevsim *ns = netdev_priv(dev);
117 unsigned int start;
118
119 do {
120 start = u64_stats_fetch_begin(&ns->syncp);
121 stats->tx_bytes = ns->tx_bytes;
122 stats->tx_packets = ns->tx_packets;
123 stats->tx_dropped = ns->tx_dropped;
124 } while (u64_stats_fetch_retry(&ns->syncp, start));
125}
126
127static int
128nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
129{
130 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
131}
132
133static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
134{
135 struct netdevsim *ns = netdev_priv(dev);
136 struct nsim_dev *nsim_dev = ns->nsim_dev;
137
138 /* Only refuse multicast addresses, zero address can mean unset/any. */
139 if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
140 return -EINVAL;
141 memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
142
143 return 0;
144}
145
146static int nsim_set_vf_vlan(struct net_device *dev, int vf,
147 u16 vlan, u8 qos, __be16 vlan_proto)
148{
149 struct netdevsim *ns = netdev_priv(dev);
150 struct nsim_dev *nsim_dev = ns->nsim_dev;
151
152 if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
153 return -EINVAL;
154
155 nsim_dev->vfconfigs[vf].vlan = vlan;
156 nsim_dev->vfconfigs[vf].qos = qos;
157 nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
158
159 return 0;
160}
161
162static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
163{
164 struct netdevsim *ns = netdev_priv(dev);
165 struct nsim_dev *nsim_dev = ns->nsim_dev;
166
167 if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
168 pr_err("Not supported in switchdev mode. Please use devlink API.\n");
169 return -EOPNOTSUPP;
170 }
171
172 if (vf >= nsim_dev_get_vfs(nsim_dev))
173 return -EINVAL;
174
175 nsim_dev->vfconfigs[vf].min_tx_rate = min;
176 nsim_dev->vfconfigs[vf].max_tx_rate = max;
177
178 return 0;
179}
180
181static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
182{
183 struct netdevsim *ns = netdev_priv(dev);
184 struct nsim_dev *nsim_dev = ns->nsim_dev;
185
186 if (vf >= nsim_dev_get_vfs(nsim_dev))
187 return -EINVAL;
188 nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
189
190 return 0;
191}
192
193static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
194{
195 struct netdevsim *ns = netdev_priv(dev);
196 struct nsim_dev *nsim_dev = ns->nsim_dev;
197
198 if (vf >= nsim_dev_get_vfs(nsim_dev))
199 return -EINVAL;
200 nsim_dev->vfconfigs[vf].rss_query_enabled = val;
201
202 return 0;
203}
204
205static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
206{
207 struct netdevsim *ns = netdev_priv(dev);
208 struct nsim_dev *nsim_dev = ns->nsim_dev;
209
210 if (vf >= nsim_dev_get_vfs(nsim_dev))
211 return -EINVAL;
212 nsim_dev->vfconfigs[vf].trusted = val;
213
214 return 0;
215}
216
217static int
218nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
219{
220 struct netdevsim *ns = netdev_priv(dev);
221 struct nsim_dev *nsim_dev = ns->nsim_dev;
222
223 if (vf >= nsim_dev_get_vfs(nsim_dev))
224 return -EINVAL;
225
226 ivi->vf = vf;
227 ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
228 ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
229 ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
230 ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
231 ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
232 ivi->qos = nsim_dev->vfconfigs[vf].qos;
233 memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
234 ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
235 ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
236 ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
237
238 return 0;
239}
240
241static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
242{
243 struct netdevsim *ns = netdev_priv(dev);
244 struct nsim_dev *nsim_dev = ns->nsim_dev;
245
246 if (vf >= nsim_dev_get_vfs(nsim_dev))
247 return -EINVAL;
248
249 switch (state) {
250 case IFLA_VF_LINK_STATE_AUTO:
251 case IFLA_VF_LINK_STATE_ENABLE:
252 case IFLA_VF_LINK_STATE_DISABLE:
253 break;
254 default:
255 return -EINVAL;
256 }
257
258 nsim_dev->vfconfigs[vf].link_state = state;
259
260 return 0;
261}
262
263static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats)
264{
265 stats->window_drops = 0;
266 stats->tx_overruns = 0;
267}
268
269static int nsim_setup_tc_taprio(struct net_device *dev,
270 struct tc_taprio_qopt_offload *offload)
271{
272 int err = 0;
273
274 switch (offload->cmd) {
275 case TAPRIO_CMD_REPLACE:
276 case TAPRIO_CMD_DESTROY:
277 break;
278 case TAPRIO_CMD_STATS:
279 nsim_taprio_stats(&offload->stats);
280 break;
281 default:
282 err = -EOPNOTSUPP;
283 }
284
285 return err;
286}
287
288static LIST_HEAD(nsim_block_cb_list);
289
290static int
291nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
292{
293 struct netdevsim *ns = netdev_priv(dev);
294
295 switch (type) {
296 case TC_SETUP_QDISC_TAPRIO:
297 return nsim_setup_tc_taprio(dev, type_data);
298 case TC_SETUP_BLOCK:
299 return flow_block_cb_setup_simple(type_data,
300 &nsim_block_cb_list,
301 nsim_setup_tc_block_cb,
302 ns, ns, true);
303 default:
304 return -EOPNOTSUPP;
305 }
306}
307
308static int
309nsim_set_features(struct net_device *dev, netdev_features_t features)
310{
311 struct netdevsim *ns = netdev_priv(dev);
312
313 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
314 return nsim_bpf_disable_tc(ns);
315
316 return 0;
317}
318
319static int nsim_get_iflink(const struct net_device *dev)
320{
321 struct netdevsim *nsim, *peer;
322 int iflink;
323
324 nsim = netdev_priv(dev);
325
326 rcu_read_lock();
327 peer = rcu_dereference(nsim->peer);
328 iflink = peer ? READ_ONCE(peer->netdev->ifindex) :
329 READ_ONCE(dev->ifindex);
330 rcu_read_unlock();
331
332 return iflink;
333}
334
335static int nsim_rcv(struct nsim_rq *rq, int budget)
336{
337 struct sk_buff *skb;
338 int i;
339
340 for (i = 0; i < budget; i++) {
341 if (skb_queue_empty(&rq->skb_queue))
342 break;
343
344 skb = skb_dequeue(&rq->skb_queue);
345 netif_receive_skb(skb);
346 }
347
348 return i;
349}
350
351static int nsim_poll(struct napi_struct *napi, int budget)
352{
353 struct nsim_rq *rq = container_of(napi, struct nsim_rq, napi);
354 int done;
355
356 done = nsim_rcv(rq, budget);
357 napi_complete(napi);
358
359 return done;
360}
361
362static int nsim_create_page_pool(struct nsim_rq *rq)
363{
364 struct page_pool_params p = {
365 .order = 0,
366 .pool_size = NSIM_RING_SIZE,
367 .nid = NUMA_NO_NODE,
368 .dev = &rq->napi.dev->dev,
369 .napi = &rq->napi,
370 .dma_dir = DMA_BIDIRECTIONAL,
371 .netdev = rq->napi.dev,
372 };
373
374 rq->page_pool = page_pool_create(&p);
375 if (IS_ERR(rq->page_pool)) {
376 int err = PTR_ERR(rq->page_pool);
377
378 rq->page_pool = NULL;
379 return err;
380 }
381 return 0;
382}
383
384static int nsim_init_napi(struct netdevsim *ns)
385{
386 struct net_device *dev = ns->netdev;
387 struct nsim_rq *rq;
388 int err, i;
389
390 for (i = 0; i < dev->num_rx_queues; i++) {
391 rq = &ns->rq[i];
392
393 netif_napi_add(dev, &rq->napi, nsim_poll);
394 }
395
396 for (i = 0; i < dev->num_rx_queues; i++) {
397 rq = &ns->rq[i];
398
399 err = nsim_create_page_pool(rq);
400 if (err)
401 goto err_pp_destroy;
402 }
403
404 return 0;
405
406err_pp_destroy:
407 while (i--) {
408 page_pool_destroy(ns->rq[i].page_pool);
409 ns->rq[i].page_pool = NULL;
410 }
411
412 for (i = 0; i < dev->num_rx_queues; i++)
413 __netif_napi_del(&ns->rq[i].napi);
414
415 return err;
416}
417
418static void nsim_enable_napi(struct netdevsim *ns)
419{
420 struct net_device *dev = ns->netdev;
421 int i;
422
423 for (i = 0; i < dev->num_rx_queues; i++) {
424 struct nsim_rq *rq = &ns->rq[i];
425
426 netif_queue_set_napi(dev, i, NETDEV_QUEUE_TYPE_RX, &rq->napi);
427 napi_enable(&rq->napi);
428 }
429}
430
431static int nsim_open(struct net_device *dev)
432{
433 struct netdevsim *ns = netdev_priv(dev);
434 int err;
435
436 err = nsim_init_napi(ns);
437 if (err)
438 return err;
439
440 nsim_enable_napi(ns);
441
442 return 0;
443}
444
445static void nsim_del_napi(struct netdevsim *ns)
446{
447 struct net_device *dev = ns->netdev;
448 int i;
449
450 for (i = 0; i < dev->num_rx_queues; i++) {
451 struct nsim_rq *rq = &ns->rq[i];
452
453 napi_disable(&rq->napi);
454 __netif_napi_del(&rq->napi);
455 }
456 synchronize_net();
457
458 for (i = 0; i < dev->num_rx_queues; i++) {
459 page_pool_destroy(ns->rq[i].page_pool);
460 ns->rq[i].page_pool = NULL;
461 }
462}
463
464static int nsim_stop(struct net_device *dev)
465{
466 struct netdevsim *ns = netdev_priv(dev);
467 struct netdevsim *peer;
468
469 netif_carrier_off(dev);
470 peer = rtnl_dereference(ns->peer);
471 if (peer)
472 netif_carrier_off(peer->netdev);
473
474 nsim_del_napi(ns);
475
476 return 0;
477}
478
479static int nsim_shaper_set(struct net_shaper_binding *binding,
480 const struct net_shaper *shaper,
481 struct netlink_ext_ack *extack)
482{
483 return 0;
484}
485
486static int nsim_shaper_del(struct net_shaper_binding *binding,
487 const struct net_shaper_handle *handle,
488 struct netlink_ext_ack *extack)
489{
490 return 0;
491}
492
493static int nsim_shaper_group(struct net_shaper_binding *binding,
494 int leaves_count,
495 const struct net_shaper *leaves,
496 const struct net_shaper *root,
497 struct netlink_ext_ack *extack)
498{
499 return 0;
500}
501
502static void nsim_shaper_cap(struct net_shaper_binding *binding,
503 enum net_shaper_scope scope,
504 unsigned long *flags)
505{
506 *flags = ULONG_MAX;
507}
508
509static const struct net_shaper_ops nsim_shaper_ops = {
510 .set = nsim_shaper_set,
511 .delete = nsim_shaper_del,
512 .group = nsim_shaper_group,
513 .capabilities = nsim_shaper_cap,
514};
515
516static const struct net_device_ops nsim_netdev_ops = {
517 .ndo_start_xmit = nsim_start_xmit,
518 .ndo_set_rx_mode = nsim_set_rx_mode,
519 .ndo_set_mac_address = eth_mac_addr,
520 .ndo_validate_addr = eth_validate_addr,
521 .ndo_change_mtu = nsim_change_mtu,
522 .ndo_get_stats64 = nsim_get_stats64,
523 .ndo_set_vf_mac = nsim_set_vf_mac,
524 .ndo_set_vf_vlan = nsim_set_vf_vlan,
525 .ndo_set_vf_rate = nsim_set_vf_rate,
526 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
527 .ndo_set_vf_trust = nsim_set_vf_trust,
528 .ndo_get_vf_config = nsim_get_vf_config,
529 .ndo_set_vf_link_state = nsim_set_vf_link_state,
530 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
531 .ndo_setup_tc = nsim_setup_tc,
532 .ndo_set_features = nsim_set_features,
533 .ndo_get_iflink = nsim_get_iflink,
534 .ndo_bpf = nsim_bpf,
535 .ndo_open = nsim_open,
536 .ndo_stop = nsim_stop,
537 .net_shaper_ops = &nsim_shaper_ops,
538};
539
540static const struct net_device_ops nsim_vf_netdev_ops = {
541 .ndo_start_xmit = nsim_start_xmit,
542 .ndo_set_rx_mode = nsim_set_rx_mode,
543 .ndo_set_mac_address = eth_mac_addr,
544 .ndo_validate_addr = eth_validate_addr,
545 .ndo_change_mtu = nsim_change_mtu,
546 .ndo_get_stats64 = nsim_get_stats64,
547 .ndo_setup_tc = nsim_setup_tc,
548 .ndo_set_features = nsim_set_features,
549};
550
551/* We don't have true per-queue stats, yet, so do some random fakery here.
552 * Only report stuff for queue 0.
553 */
554static void nsim_get_queue_stats_rx(struct net_device *dev, int idx,
555 struct netdev_queue_stats_rx *stats)
556{
557 struct rtnl_link_stats64 rtstats = {};
558
559 if (!idx)
560 nsim_get_stats64(dev, &rtstats);
561
562 stats->packets = rtstats.rx_packets - !!rtstats.rx_packets;
563 stats->bytes = rtstats.rx_bytes;
564}
565
566static void nsim_get_queue_stats_tx(struct net_device *dev, int idx,
567 struct netdev_queue_stats_tx *stats)
568{
569 struct rtnl_link_stats64 rtstats = {};
570
571 if (!idx)
572 nsim_get_stats64(dev, &rtstats);
573
574 stats->packets = rtstats.tx_packets - !!rtstats.tx_packets;
575 stats->bytes = rtstats.tx_bytes;
576}
577
578static void nsim_get_base_stats(struct net_device *dev,
579 struct netdev_queue_stats_rx *rx,
580 struct netdev_queue_stats_tx *tx)
581{
582 struct rtnl_link_stats64 rtstats = {};
583
584 nsim_get_stats64(dev, &rtstats);
585
586 rx->packets = !!rtstats.rx_packets;
587 rx->bytes = 0;
588 tx->packets = !!rtstats.tx_packets;
589 tx->bytes = 0;
590}
591
592static const struct netdev_stat_ops nsim_stat_ops = {
593 .get_queue_stats_tx = nsim_get_queue_stats_tx,
594 .get_queue_stats_rx = nsim_get_queue_stats_rx,
595 .get_base_stats = nsim_get_base_stats,
596};
597
598static ssize_t
599nsim_pp_hold_read(struct file *file, char __user *data,
600 size_t count, loff_t *ppos)
601{
602 struct netdevsim *ns = file->private_data;
603 char buf[3] = "n\n";
604
605 if (ns->page)
606 buf[0] = 'y';
607
608 return simple_read_from_buffer(data, count, ppos, buf, 2);
609}
610
611static ssize_t
612nsim_pp_hold_write(struct file *file, const char __user *data,
613 size_t count, loff_t *ppos)
614{
615 struct netdevsim *ns = file->private_data;
616 ssize_t ret;
617 bool val;
618
619 ret = kstrtobool_from_user(data, count, &val);
620 if (ret)
621 return ret;
622
623 rtnl_lock();
624 ret = count;
625 if (val == !!ns->page)
626 goto exit;
627
628 if (!netif_running(ns->netdev) && val) {
629 ret = -ENETDOWN;
630 } else if (val) {
631 ns->page = page_pool_dev_alloc_pages(ns->rq[0].page_pool);
632 if (!ns->page)
633 ret = -ENOMEM;
634 } else {
635 page_pool_put_full_page(ns->page->pp, ns->page, false);
636 ns->page = NULL;
637 }
638
639exit:
640 rtnl_unlock();
641 return ret;
642}
643
644static const struct file_operations nsim_pp_hold_fops = {
645 .open = simple_open,
646 .read = nsim_pp_hold_read,
647 .write = nsim_pp_hold_write,
648 .llseek = generic_file_llseek,
649 .owner = THIS_MODULE,
650};
651
652static void nsim_setup(struct net_device *dev)
653{
654 ether_setup(dev);
655 eth_hw_addr_random(dev);
656
657 dev->tx_queue_len = 0;
658 dev->flags &= ~IFF_MULTICAST;
659 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
660 IFF_NO_QUEUE;
661 dev->features |= NETIF_F_HIGHDMA |
662 NETIF_F_SG |
663 NETIF_F_FRAGLIST |
664 NETIF_F_HW_CSUM |
665 NETIF_F_TSO;
666 dev->hw_features |= NETIF_F_HW_TC |
667 NETIF_F_SG |
668 NETIF_F_FRAGLIST |
669 NETIF_F_HW_CSUM |
670 NETIF_F_TSO;
671 dev->max_mtu = ETH_MAX_MTU;
672 dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
673}
674
675static int nsim_queue_init(struct netdevsim *ns)
676{
677 struct net_device *dev = ns->netdev;
678 int i;
679
680 ns->rq = kvcalloc(dev->num_rx_queues, sizeof(*ns->rq),
681 GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
682 if (!ns->rq)
683 return -ENOMEM;
684
685 for (i = 0; i < dev->num_rx_queues; i++)
686 skb_queue_head_init(&ns->rq[i].skb_queue);
687
688 return 0;
689}
690
691static void nsim_queue_free(struct netdevsim *ns)
692{
693 struct net_device *dev = ns->netdev;
694 int i;
695
696 for (i = 0; i < dev->num_rx_queues; i++)
697 skb_queue_purge_reason(&ns->rq[i].skb_queue,
698 SKB_DROP_REASON_QUEUE_PURGE);
699
700 kvfree(ns->rq);
701 ns->rq = NULL;
702}
703
704static int nsim_init_netdevsim(struct netdevsim *ns)
705{
706 struct mock_phc *phc;
707 int err;
708
709 phc = mock_phc_create(&ns->nsim_bus_dev->dev);
710 if (IS_ERR(phc))
711 return PTR_ERR(phc);
712
713 ns->phc = phc;
714 ns->netdev->netdev_ops = &nsim_netdev_ops;
715 ns->netdev->stat_ops = &nsim_stat_ops;
716
717 err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
718 if (err)
719 goto err_phc_destroy;
720
721 rtnl_lock();
722 err = nsim_queue_init(ns);
723 if (err)
724 goto err_utn_destroy;
725
726 err = nsim_bpf_init(ns);
727 if (err)
728 goto err_rq_destroy;
729
730 nsim_macsec_init(ns);
731 nsim_ipsec_init(ns);
732
733 err = register_netdevice(ns->netdev);
734 if (err)
735 goto err_ipsec_teardown;
736 rtnl_unlock();
737 return 0;
738
739err_ipsec_teardown:
740 nsim_ipsec_teardown(ns);
741 nsim_macsec_teardown(ns);
742 nsim_bpf_uninit(ns);
743err_rq_destroy:
744 nsim_queue_free(ns);
745err_utn_destroy:
746 rtnl_unlock();
747 nsim_udp_tunnels_info_destroy(ns->netdev);
748err_phc_destroy:
749 mock_phc_destroy(ns->phc);
750 return err;
751}
752
753static int nsim_init_netdevsim_vf(struct netdevsim *ns)
754{
755 int err;
756
757 ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
758 rtnl_lock();
759 err = register_netdevice(ns->netdev);
760 rtnl_unlock();
761 return err;
762}
763
764static void nsim_exit_netdevsim(struct netdevsim *ns)
765{
766 nsim_udp_tunnels_info_destroy(ns->netdev);
767 mock_phc_destroy(ns->phc);
768}
769
770struct netdevsim *
771nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
772{
773 struct net_device *dev;
774 struct netdevsim *ns;
775 int err;
776
777 dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
778 nsim_dev->nsim_bus_dev->num_queues);
779 if (!dev)
780 return ERR_PTR(-ENOMEM);
781
782 dev_net_set(dev, nsim_dev_net(nsim_dev));
783 ns = netdev_priv(dev);
784 ns->netdev = dev;
785 u64_stats_init(&ns->syncp);
786 ns->nsim_dev = nsim_dev;
787 ns->nsim_dev_port = nsim_dev_port;
788 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
789 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
790 SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port);
791 nsim_ethtool_init(ns);
792 if (nsim_dev_port_is_pf(nsim_dev_port))
793 err = nsim_init_netdevsim(ns);
794 else
795 err = nsim_init_netdevsim_vf(ns);
796 if (err)
797 goto err_free_netdev;
798
799 ns->pp_dfs = debugfs_create_file("pp_hold", 0600, nsim_dev_port->ddir,
800 ns, &nsim_pp_hold_fops);
801
802 return ns;
803
804err_free_netdev:
805 free_netdev(dev);
806 return ERR_PTR(err);
807}
808
809void nsim_destroy(struct netdevsim *ns)
810{
811 struct net_device *dev = ns->netdev;
812 struct netdevsim *peer;
813
814 debugfs_remove(ns->pp_dfs);
815
816 rtnl_lock();
817 peer = rtnl_dereference(ns->peer);
818 if (peer)
819 RCU_INIT_POINTER(peer->peer, NULL);
820 RCU_INIT_POINTER(ns->peer, NULL);
821 unregister_netdevice(dev);
822 if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
823 nsim_macsec_teardown(ns);
824 nsim_ipsec_teardown(ns);
825 nsim_bpf_uninit(ns);
826 nsim_queue_free(ns);
827 }
828 rtnl_unlock();
829 if (nsim_dev_port_is_pf(ns->nsim_dev_port))
830 nsim_exit_netdevsim(ns);
831
832 /* Put this intentionally late to exercise the orphaning path */
833 if (ns->page) {
834 page_pool_put_full_page(ns->page->pp, ns->page, false);
835 ns->page = NULL;
836 }
837
838 free_netdev(dev);
839}
840
841bool netdev_is_nsim(struct net_device *dev)
842{
843 return dev->netdev_ops == &nsim_netdev_ops;
844}
845
846static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
847 struct netlink_ext_ack *extack)
848{
849 NL_SET_ERR_MSG_MOD(extack,
850 "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
851 return -EOPNOTSUPP;
852}
853
854static struct rtnl_link_ops nsim_link_ops __read_mostly = {
855 .kind = DRV_NAME,
856 .validate = nsim_validate,
857};
858
859static int __init nsim_module_init(void)
860{
861 int err;
862
863 err = nsim_dev_init();
864 if (err)
865 return err;
866
867 err = nsim_bus_init();
868 if (err)
869 goto err_dev_exit;
870
871 err = rtnl_link_register(&nsim_link_ops);
872 if (err)
873 goto err_bus_exit;
874
875 return 0;
876
877err_bus_exit:
878 nsim_bus_exit();
879err_dev_exit:
880 nsim_dev_exit();
881 return err;
882}
883
884static void __exit nsim_module_exit(void)
885{
886 rtnl_link_unregister(&nsim_link_ops);
887 nsim_bus_exit();
888 nsim_dev_exit();
889}
890
891module_init(nsim_module_init);
892module_exit(nsim_module_exit);
893MODULE_LICENSE("GPL");
894MODULE_DESCRIPTION("Simulated networking device for testing");
895MODULE_ALIAS_RTNL_LINK(DRV_NAME);