Linux Audio

Check our new training course

Loading...
v6.9.4
  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	unsigned int len = skb->len;
 33	struct netdevsim *peer_ns;
 34
 35	rcu_read_lock();
 36	if (!nsim_ipsec_tx(ns, skb))
 37		goto out_drop_free;
 38
 39	peer_ns = rcu_dereference(ns->peer);
 40	if (!peer_ns)
 41		goto out_drop_free;
 42
 43	skb_tx_timestamp(skb);
 44	if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP))
 45		goto out_drop_cnt;
 46
 47	rcu_read_unlock();
 48	u64_stats_update_begin(&ns->syncp);
 49	ns->tx_packets++;
 50	ns->tx_bytes += len;
 51	u64_stats_update_end(&ns->syncp);
 52	return NETDEV_TX_OK;
 53
 54out_drop_free:
 55	dev_kfree_skb(skb);
 56out_drop_cnt:
 57	rcu_read_unlock();
 58	u64_stats_update_begin(&ns->syncp);
 59	ns->tx_dropped++;
 60	u64_stats_update_end(&ns->syncp);
 61	return NETDEV_TX_OK;
 62}
 63
 64static void nsim_set_rx_mode(struct net_device *dev)
 65{
 66}
 67
 68static int nsim_change_mtu(struct net_device *dev, int new_mtu)
 69{
 70	struct netdevsim *ns = netdev_priv(dev);
 71
 72	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
 73		return -EBUSY;
 74
 75	dev->mtu = new_mtu;
 76
 77	return 0;
 78}
 79
 80static void
 81nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 82{
 83	struct netdevsim *ns = netdev_priv(dev);
 84	unsigned int start;
 85
 86	do {
 87		start = u64_stats_fetch_begin(&ns->syncp);
 88		stats->tx_bytes = ns->tx_bytes;
 89		stats->tx_packets = ns->tx_packets;
 90		stats->tx_dropped = ns->tx_dropped;
 91	} while (u64_stats_fetch_retry(&ns->syncp, start));
 92}
 93
 94static int
 95nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
 96{
 97	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
 98}
 99
100static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
101{
102	struct netdevsim *ns = netdev_priv(dev);
103	struct nsim_dev *nsim_dev = ns->nsim_dev;
104
105	/* Only refuse multicast addresses, zero address can mean unset/any. */
106	if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
107		return -EINVAL;
108	memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
109
110	return 0;
111}
112
113static int nsim_set_vf_vlan(struct net_device *dev, int vf,
114			    u16 vlan, u8 qos, __be16 vlan_proto)
115{
116	struct netdevsim *ns = netdev_priv(dev);
117	struct nsim_dev *nsim_dev = ns->nsim_dev;
118
119	if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
120		return -EINVAL;
121
122	nsim_dev->vfconfigs[vf].vlan = vlan;
123	nsim_dev->vfconfigs[vf].qos = qos;
124	nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
125
126	return 0;
127}
128
129static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
130{
131	struct netdevsim *ns = netdev_priv(dev);
132	struct nsim_dev *nsim_dev = ns->nsim_dev;
133
134	if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
135		pr_err("Not supported in switchdev mode. Please use devlink API.\n");
136		return -EOPNOTSUPP;
137	}
138
139	if (vf >= nsim_dev_get_vfs(nsim_dev))
140		return -EINVAL;
141
142	nsim_dev->vfconfigs[vf].min_tx_rate = min;
143	nsim_dev->vfconfigs[vf].max_tx_rate = max;
144
145	return 0;
146}
147
148static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
149{
150	struct netdevsim *ns = netdev_priv(dev);
151	struct nsim_dev *nsim_dev = ns->nsim_dev;
152
153	if (vf >= nsim_dev_get_vfs(nsim_dev))
154		return -EINVAL;
155	nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
156
157	return 0;
158}
159
160static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
161{
162	struct netdevsim *ns = netdev_priv(dev);
163	struct nsim_dev *nsim_dev = ns->nsim_dev;
164
165	if (vf >= nsim_dev_get_vfs(nsim_dev))
166		return -EINVAL;
167	nsim_dev->vfconfigs[vf].rss_query_enabled = val;
168
169	return 0;
170}
171
172static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
173{
174	struct netdevsim *ns = netdev_priv(dev);
175	struct nsim_dev *nsim_dev = ns->nsim_dev;
176
177	if (vf >= nsim_dev_get_vfs(nsim_dev))
178		return -EINVAL;
179	nsim_dev->vfconfigs[vf].trusted = val;
180
181	return 0;
182}
183
184static int
185nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
186{
187	struct netdevsim *ns = netdev_priv(dev);
188	struct nsim_dev *nsim_dev = ns->nsim_dev;
189
190	if (vf >= nsim_dev_get_vfs(nsim_dev))
191		return -EINVAL;
192
193	ivi->vf = vf;
194	ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
195	ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
196	ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
197	ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
198	ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
199	ivi->qos = nsim_dev->vfconfigs[vf].qos;
200	memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
201	ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
202	ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
203	ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
204
205	return 0;
206}
207
208static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
209{
210	struct netdevsim *ns = netdev_priv(dev);
211	struct nsim_dev *nsim_dev = ns->nsim_dev;
212
213	if (vf >= nsim_dev_get_vfs(nsim_dev))
214		return -EINVAL;
215
216	switch (state) {
217	case IFLA_VF_LINK_STATE_AUTO:
218	case IFLA_VF_LINK_STATE_ENABLE:
219	case IFLA_VF_LINK_STATE_DISABLE:
220		break;
221	default:
222		return -EINVAL;
223	}
224
225	nsim_dev->vfconfigs[vf].link_state = state;
226
227	return 0;
228}
229
230static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats)
231{
232	stats->window_drops = 0;
233	stats->tx_overruns = 0;
234}
235
236static int nsim_setup_tc_taprio(struct net_device *dev,
237				struct tc_taprio_qopt_offload *offload)
238{
239	int err = 0;
240
241	switch (offload->cmd) {
242	case TAPRIO_CMD_REPLACE:
243	case TAPRIO_CMD_DESTROY:
244		break;
245	case TAPRIO_CMD_STATS:
246		nsim_taprio_stats(&offload->stats);
247		break;
248	default:
249		err = -EOPNOTSUPP;
250	}
251
252	return err;
253}
254
255static LIST_HEAD(nsim_block_cb_list);
256
257static int
258nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
259{
260	struct netdevsim *ns = netdev_priv(dev);
261
262	switch (type) {
263	case TC_SETUP_QDISC_TAPRIO:
264		return nsim_setup_tc_taprio(dev, type_data);
265	case TC_SETUP_BLOCK:
266		return flow_block_cb_setup_simple(type_data,
267						  &nsim_block_cb_list,
268						  nsim_setup_tc_block_cb,
269						  ns, ns, true);
270	default:
271		return -EOPNOTSUPP;
272	}
273}
274
275static int
276nsim_set_features(struct net_device *dev, netdev_features_t features)
277{
278	struct netdevsim *ns = netdev_priv(dev);
279
280	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
281		return nsim_bpf_disable_tc(ns);
282
283	return 0;
284}
285
286static int nsim_get_iflink(const struct net_device *dev)
287{
288	struct netdevsim *nsim, *peer;
289	int iflink;
290
291	nsim = netdev_priv(dev);
292
293	rcu_read_lock();
294	peer = rcu_dereference(nsim->peer);
295	iflink = peer ? READ_ONCE(peer->netdev->ifindex) : 0;
296	rcu_read_unlock();
297
298	return iflink;
299}
300
301static const struct net_device_ops nsim_netdev_ops = {
302	.ndo_start_xmit		= nsim_start_xmit,
303	.ndo_set_rx_mode	= nsim_set_rx_mode,
304	.ndo_set_mac_address	= eth_mac_addr,
305	.ndo_validate_addr	= eth_validate_addr,
306	.ndo_change_mtu		= nsim_change_mtu,
307	.ndo_get_stats64	= nsim_get_stats64,
308	.ndo_set_vf_mac		= nsim_set_vf_mac,
309	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
310	.ndo_set_vf_rate	= nsim_set_vf_rate,
311	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
312	.ndo_set_vf_trust	= nsim_set_vf_trust,
313	.ndo_get_vf_config	= nsim_get_vf_config,
314	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
315	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
316	.ndo_setup_tc		= nsim_setup_tc,
317	.ndo_set_features	= nsim_set_features,
318	.ndo_get_iflink		= nsim_get_iflink,
319	.ndo_bpf		= nsim_bpf,
320};
321
322static const struct net_device_ops nsim_vf_netdev_ops = {
323	.ndo_start_xmit		= nsim_start_xmit,
324	.ndo_set_rx_mode	= nsim_set_rx_mode,
325	.ndo_set_mac_address	= eth_mac_addr,
326	.ndo_validate_addr	= eth_validate_addr,
327	.ndo_change_mtu		= nsim_change_mtu,
328	.ndo_get_stats64	= nsim_get_stats64,
329	.ndo_setup_tc		= nsim_setup_tc,
330	.ndo_set_features	= nsim_set_features,
331};
332
333static void nsim_setup(struct net_device *dev)
334{
335	ether_setup(dev);
336	eth_hw_addr_random(dev);
337
338	dev->tx_queue_len = 0;
 
339	dev->flags &= ~IFF_MULTICAST;
340	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
341			   IFF_NO_QUEUE;
342	dev->features |= NETIF_F_HIGHDMA |
343			 NETIF_F_SG |
344			 NETIF_F_FRAGLIST |
345			 NETIF_F_HW_CSUM |
346			 NETIF_F_TSO;
347	dev->hw_features |= NETIF_F_HW_TC;
348	dev->max_mtu = ETH_MAX_MTU;
349	dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
350}
351
352static int nsim_init_netdevsim(struct netdevsim *ns)
353{
354	struct mock_phc *phc;
355	int err;
356
357	phc = mock_phc_create(&ns->nsim_bus_dev->dev);
358	if (IS_ERR(phc))
359		return PTR_ERR(phc);
360
361	ns->phc = phc;
362	ns->netdev->netdev_ops = &nsim_netdev_ops;
363
364	err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
365	if (err)
366		goto err_phc_destroy;
367
368	rtnl_lock();
369	err = nsim_bpf_init(ns);
370	if (err)
371		goto err_utn_destroy;
372
373	nsim_macsec_init(ns);
374	nsim_ipsec_init(ns);
375
376	err = register_netdevice(ns->netdev);
377	if (err)
378		goto err_ipsec_teardown;
379	rtnl_unlock();
380	return 0;
381
382err_ipsec_teardown:
383	nsim_ipsec_teardown(ns);
384	nsim_macsec_teardown(ns);
385	nsim_bpf_uninit(ns);
386err_utn_destroy:
387	rtnl_unlock();
388	nsim_udp_tunnels_info_destroy(ns->netdev);
389err_phc_destroy:
390	mock_phc_destroy(ns->phc);
391	return err;
392}
393
394static int nsim_init_netdevsim_vf(struct netdevsim *ns)
395{
396	int err;
397
398	ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
399	rtnl_lock();
400	err = register_netdevice(ns->netdev);
401	rtnl_unlock();
402	return err;
403}
404
405static void nsim_exit_netdevsim(struct netdevsim *ns)
406{
407	nsim_udp_tunnels_info_destroy(ns->netdev);
408	mock_phc_destroy(ns->phc);
409}
410
411struct netdevsim *
412nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
413{
414	struct net_device *dev;
415	struct netdevsim *ns;
416	int err;
417
418	dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
419			      nsim_dev->nsim_bus_dev->num_queues);
420	if (!dev)
421		return ERR_PTR(-ENOMEM);
422
423	dev_net_set(dev, nsim_dev_net(nsim_dev));
424	ns = netdev_priv(dev);
425	ns->netdev = dev;
426	u64_stats_init(&ns->syncp);
427	ns->nsim_dev = nsim_dev;
428	ns->nsim_dev_port = nsim_dev_port;
429	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
430	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
431	SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port);
432	nsim_ethtool_init(ns);
433	if (nsim_dev_port_is_pf(nsim_dev_port))
434		err = nsim_init_netdevsim(ns);
435	else
436		err = nsim_init_netdevsim_vf(ns);
437	if (err)
438		goto err_free_netdev;
 
 
 
 
 
 
 
 
439	return ns;
440
 
 
 
 
441err_free_netdev:
442	free_netdev(dev);
443	return ERR_PTR(err);
444}
445
446void nsim_destroy(struct netdevsim *ns)
447{
448	struct net_device *dev = ns->netdev;
449	struct netdevsim *peer;
450
451	rtnl_lock();
452	peer = rtnl_dereference(ns->peer);
453	if (peer)
454		RCU_INIT_POINTER(peer->peer, NULL);
455	RCU_INIT_POINTER(ns->peer, NULL);
456	unregister_netdevice(dev);
457	if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
458		nsim_macsec_teardown(ns);
459		nsim_ipsec_teardown(ns);
460		nsim_bpf_uninit(ns);
461	}
462	rtnl_unlock();
463	if (nsim_dev_port_is_pf(ns->nsim_dev_port))
464		nsim_exit_netdevsim(ns);
465	free_netdev(dev);
466}
467
468bool netdev_is_nsim(struct net_device *dev)
469{
470	return dev->netdev_ops == &nsim_netdev_ops;
471}
472
473static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
474			 struct netlink_ext_ack *extack)
475{
476	NL_SET_ERR_MSG_MOD(extack,
477			   "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
478	return -EOPNOTSUPP;
479}
480
481static struct rtnl_link_ops nsim_link_ops __read_mostly = {
482	.kind		= DRV_NAME,
483	.validate	= nsim_validate,
484};
485
486static int __init nsim_module_init(void)
487{
488	int err;
489
490	err = nsim_dev_init();
491	if (err)
492		return err;
493
494	err = nsim_bus_init();
495	if (err)
496		goto err_dev_exit;
497
498	err = rtnl_link_register(&nsim_link_ops);
499	if (err)
500		goto err_bus_exit;
501
 
 
 
 
502	return 0;
503
 
 
504err_bus_exit:
505	nsim_bus_exit();
506err_dev_exit:
507	nsim_dev_exit();
508	return err;
509}
510
511static void __exit nsim_module_exit(void)
512{
513	rtnl_link_unregister(&nsim_link_ops);
 
514	nsim_bus_exit();
515	nsim_dev_exit();
516}
517
518module_init(nsim_module_init);
519module_exit(nsim_module_exit);
520MODULE_LICENSE("GPL");
521MODULE_DESCRIPTION("Simulated networking device for testing");
522MODULE_ALIAS_RTNL_LINK(DRV_NAME);
v5.4
  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
 26#include "netdevsim.h"
 27
 28static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
 29{
 30	struct netdevsim *ns = netdev_priv(dev);
 
 
 31
 
 32	if (!nsim_ipsec_tx(ns, skb))
 33		goto out;
 34
 
 
 
 
 
 
 
 
 
 35	u64_stats_update_begin(&ns->syncp);
 36	ns->tx_packets++;
 37	ns->tx_bytes += skb->len;
 38	u64_stats_update_end(&ns->syncp);
 
 39
 40out:
 41	dev_kfree_skb(skb);
 42
 
 
 
 
 43	return NETDEV_TX_OK;
 44}
 45
 46static void nsim_set_rx_mode(struct net_device *dev)
 47{
 48}
 49
 50static int nsim_change_mtu(struct net_device *dev, int new_mtu)
 51{
 52	struct netdevsim *ns = netdev_priv(dev);
 53
 54	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
 55		return -EBUSY;
 56
 57	dev->mtu = new_mtu;
 58
 59	return 0;
 60}
 61
 62static void
 63nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 64{
 65	struct netdevsim *ns = netdev_priv(dev);
 66	unsigned int start;
 67
 68	do {
 69		start = u64_stats_fetch_begin(&ns->syncp);
 70		stats->tx_bytes = ns->tx_bytes;
 71		stats->tx_packets = ns->tx_packets;
 
 72	} while (u64_stats_fetch_retry(&ns->syncp, start));
 73}
 74
 75static int
 76nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
 77{
 78	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
 79}
 80
 81static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
 82{
 83	struct netdevsim *ns = netdev_priv(dev);
 84	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
 85
 86	/* Only refuse multicast addresses, zero address can mean unset/any. */
 87	if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
 88		return -EINVAL;
 89	memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
 90
 91	return 0;
 92}
 93
 94static int nsim_set_vf_vlan(struct net_device *dev, int vf,
 95			    u16 vlan, u8 qos, __be16 vlan_proto)
 96{
 97	struct netdevsim *ns = netdev_priv(dev);
 98	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
 99
100	if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
101		return -EINVAL;
102
103	nsim_bus_dev->vfconfigs[vf].vlan = vlan;
104	nsim_bus_dev->vfconfigs[vf].qos = qos;
105	nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
106
107	return 0;
108}
109
110static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
111{
112	struct netdevsim *ns = netdev_priv(dev);
113	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
114
115	if (vf >= nsim_bus_dev->num_vfs)
 
 
 
 
 
116		return -EINVAL;
117
118	nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
119	nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
120
121	return 0;
122}
123
124static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
125{
126	struct netdevsim *ns = netdev_priv(dev);
127	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
128
129	if (vf >= nsim_bus_dev->num_vfs)
130		return -EINVAL;
131	nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
132
133	return 0;
134}
135
136static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
137{
138	struct netdevsim *ns = netdev_priv(dev);
139	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
140
141	if (vf >= nsim_bus_dev->num_vfs)
142		return -EINVAL;
143	nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
144
145	return 0;
146}
147
148static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
149{
150	struct netdevsim *ns = netdev_priv(dev);
151	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
152
153	if (vf >= nsim_bus_dev->num_vfs)
154		return -EINVAL;
155	nsim_bus_dev->vfconfigs[vf].trusted = val;
156
157	return 0;
158}
159
160static int
161nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
162{
163	struct netdevsim *ns = netdev_priv(dev);
164	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
165
166	if (vf >= nsim_bus_dev->num_vfs)
167		return -EINVAL;
168
169	ivi->vf = vf;
170	ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
171	ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
172	ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
173	ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
174	ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
175	ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
176	memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
177	ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
178	ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
179	ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
180
181	return 0;
182}
183
184static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
185{
186	struct netdevsim *ns = netdev_priv(dev);
187	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
188
189	if (vf >= nsim_bus_dev->num_vfs)
190		return -EINVAL;
191
192	switch (state) {
193	case IFLA_VF_LINK_STATE_AUTO:
194	case IFLA_VF_LINK_STATE_ENABLE:
195	case IFLA_VF_LINK_STATE_DISABLE:
196		break;
197	default:
198		return -EINVAL;
199	}
200
201	nsim_bus_dev->vfconfigs[vf].link_state = state;
202
203	return 0;
204}
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206static LIST_HEAD(nsim_block_cb_list);
207
208static int
209nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
210{
211	struct netdevsim *ns = netdev_priv(dev);
212
213	switch (type) {
 
 
214	case TC_SETUP_BLOCK:
215		return flow_block_cb_setup_simple(type_data,
216						  &nsim_block_cb_list,
217						  nsim_setup_tc_block_cb,
218						  ns, ns, true);
219	default:
220		return -EOPNOTSUPP;
221	}
222}
223
224static int
225nsim_set_features(struct net_device *dev, netdev_features_t features)
226{
227	struct netdevsim *ns = netdev_priv(dev);
228
229	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
230		return nsim_bpf_disable_tc(ns);
231
232	return 0;
233}
234
235static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
236{
237	struct netdevsim *ns = netdev_priv(dev);
 
 
 
 
 
 
 
 
238
239	return &ns->nsim_dev_port->devlink_port;
240}
241
242static const struct net_device_ops nsim_netdev_ops = {
243	.ndo_start_xmit		= nsim_start_xmit,
244	.ndo_set_rx_mode	= nsim_set_rx_mode,
245	.ndo_set_mac_address	= eth_mac_addr,
246	.ndo_validate_addr	= eth_validate_addr,
247	.ndo_change_mtu		= nsim_change_mtu,
248	.ndo_get_stats64	= nsim_get_stats64,
249	.ndo_set_vf_mac		= nsim_set_vf_mac,
250	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
251	.ndo_set_vf_rate	= nsim_set_vf_rate,
252	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
253	.ndo_set_vf_trust	= nsim_set_vf_trust,
254	.ndo_get_vf_config	= nsim_get_vf_config,
255	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
256	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
257	.ndo_setup_tc		= nsim_setup_tc,
258	.ndo_set_features	= nsim_set_features,
 
259	.ndo_bpf		= nsim_bpf,
260	.ndo_get_devlink_port	= nsim_get_devlink_port,
 
 
 
 
 
 
 
 
 
 
261};
262
263static void nsim_setup(struct net_device *dev)
264{
265	ether_setup(dev);
266	eth_hw_addr_random(dev);
267
268	dev->tx_queue_len = 0;
269	dev->flags |= IFF_NOARP;
270	dev->flags &= ~IFF_MULTICAST;
271	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
272			   IFF_NO_QUEUE;
273	dev->features |= NETIF_F_HIGHDMA |
274			 NETIF_F_SG |
275			 NETIF_F_FRAGLIST |
276			 NETIF_F_HW_CSUM |
277			 NETIF_F_TSO;
278	dev->hw_features |= NETIF_F_HW_TC;
279	dev->max_mtu = ETH_MAX_MTU;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280}
281
282struct netdevsim *
283nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
284{
285	struct net_device *dev;
286	struct netdevsim *ns;
287	int err;
288
289	dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
 
290	if (!dev)
291		return ERR_PTR(-ENOMEM);
292
 
293	ns = netdev_priv(dev);
294	ns->netdev = dev;
 
295	ns->nsim_dev = nsim_dev;
296	ns->nsim_dev_port = nsim_dev_port;
297	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
298	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
299	dev->netdev_ops = &nsim_netdev_ops;
300
301	rtnl_lock();
302	err = nsim_bpf_init(ns);
 
 
303	if (err)
304		goto err_free_netdev;
305
306	nsim_ipsec_init(ns);
307
308	err = register_netdevice(dev);
309	if (err)
310		goto err_ipsec_teardown;
311	rtnl_unlock();
312
313	return ns;
314
315err_ipsec_teardown:
316	nsim_ipsec_teardown(ns);
317	nsim_bpf_uninit(ns);
318	rtnl_unlock();
319err_free_netdev:
320	free_netdev(dev);
321	return ERR_PTR(err);
322}
323
324void nsim_destroy(struct netdevsim *ns)
325{
326	struct net_device *dev = ns->netdev;
 
327
328	rtnl_lock();
 
 
 
 
329	unregister_netdevice(dev);
330	nsim_ipsec_teardown(ns);
331	nsim_bpf_uninit(ns);
 
 
 
332	rtnl_unlock();
 
 
333	free_netdev(dev);
334}
335
 
 
 
 
 
336static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
337			 struct netlink_ext_ack *extack)
338{
339	NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
 
340	return -EOPNOTSUPP;
341}
342
343static struct rtnl_link_ops nsim_link_ops __read_mostly = {
344	.kind		= DRV_NAME,
345	.validate	= nsim_validate,
346};
347
348static int __init nsim_module_init(void)
349{
350	int err;
351
352	err = nsim_dev_init();
353	if (err)
354		return err;
355
356	err = nsim_bus_init();
357	if (err)
358		goto err_dev_exit;
359
360	err = nsim_fib_init();
361	if (err)
362		goto err_bus_exit;
363
364	err = rtnl_link_register(&nsim_link_ops);
365	if (err)
366		goto err_fib_exit;
367
368	return 0;
369
370err_fib_exit:
371	nsim_fib_exit();
372err_bus_exit:
373	nsim_bus_exit();
374err_dev_exit:
375	nsim_dev_exit();
376	return err;
377}
378
379static void __exit nsim_module_exit(void)
380{
381	rtnl_link_unregister(&nsim_link_ops);
382	nsim_fib_exit();
383	nsim_bus_exit();
384	nsim_dev_exit();
385}
386
387module_init(nsim_module_init);
388module_exit(nsim_module_exit);
389MODULE_LICENSE("GPL");
 
390MODULE_ALIAS_RTNL_LINK(DRV_NAME);