Linux Audio

Check our new training course

Loading...
v4.17
  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
 28struct nsim_vf_config {
 29	int link_state;
 30	u16 min_tx_rate;
 31	u16 max_tx_rate;
 32	u16 vlan;
 33	__be16 vlan_proto;
 34	u16 qos;
 35	u8 vf_mac[ETH_ALEN];
 36	bool spoofchk_enabled;
 37	bool trusted;
 38	bool rss_query_enabled;
 39};
 40
 41static u32 nsim_dev_id;
 42
 43static int nsim_num_vf(struct device *dev)
 44{
 45	struct netdevsim *ns = to_nsim(dev);
 46
 47	return ns->num_vfs;
 48}
 49
 50static struct bus_type nsim_bus = {
 51	.name		= DRV_NAME,
 52	.dev_name	= DRV_NAME,
 53	.num_vf		= nsim_num_vf,
 54};
 55
 56static int nsim_vfs_enable(struct netdevsim *ns, unsigned int num_vfs)
 57{
 58	ns->vfconfigs = kcalloc(num_vfs, sizeof(struct nsim_vf_config),
 59				GFP_KERNEL);
 60	if (!ns->vfconfigs)
 61		return -ENOMEM;
 62	ns->num_vfs = num_vfs;
 63
 64	return 0;
 65}
 66
 67static void nsim_vfs_disable(struct netdevsim *ns)
 68{
 69	kfree(ns->vfconfigs);
 70	ns->vfconfigs = NULL;
 71	ns->num_vfs = 0;
 72}
 73
 74static ssize_t
 75nsim_numvfs_store(struct device *dev, struct device_attribute *attr,
 76		  const char *buf, size_t count)
 77{
 78	struct netdevsim *ns = to_nsim(dev);
 79	unsigned int num_vfs;
 80	int ret;
 81
 82	ret = kstrtouint(buf, 0, &num_vfs);
 83	if (ret)
 84		return ret;
 85
 86	rtnl_lock();
 87	if (ns->num_vfs == num_vfs)
 88		goto exit_good;
 89	if (ns->num_vfs && num_vfs) {
 90		ret = -EBUSY;
 91		goto exit_unlock;
 92	}
 93
 94	if (num_vfs) {
 95		ret = nsim_vfs_enable(ns, num_vfs);
 96		if (ret)
 97			goto exit_unlock;
 98	} else {
 99		nsim_vfs_disable(ns);
100	}
101exit_good:
102	ret = count;
103exit_unlock:
104	rtnl_unlock();
105
106	return ret;
107}
108
109static ssize_t
110nsim_numvfs_show(struct device *dev, struct device_attribute *attr, char *buf)
111{
112	struct netdevsim *ns = to_nsim(dev);
113
114	return sprintf(buf, "%u\n", ns->num_vfs);
115}
116
117static struct device_attribute nsim_numvfs_attr =
118	__ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store);
119
120static struct attribute *nsim_dev_attrs[] = {
121	&nsim_numvfs_attr.attr,
122	NULL,
123};
124
125static const struct attribute_group nsim_dev_attr_group = {
126	.attrs = nsim_dev_attrs,
127};
128
129static const struct attribute_group *nsim_dev_attr_groups[] = {
130	&nsim_dev_attr_group,
131	NULL,
132};
133
134static void nsim_dev_release(struct device *dev)
135{
136	struct netdevsim *ns = to_nsim(dev);
137
138	nsim_vfs_disable(ns);
139	free_netdev(ns->netdev);
140}
141
142static struct device_type nsim_dev_type = {
143	.groups = nsim_dev_attr_groups,
144	.release = nsim_dev_release,
145};
146
147static int nsim_init(struct net_device *dev)
148{
149	struct netdevsim *ns = netdev_priv(dev);
150	int err;
151
152	ns->netdev = dev;
153	ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir);
154	if (IS_ERR_OR_NULL(ns->ddir))
155		return -ENOMEM;
156
157	err = nsim_bpf_init(ns);
158	if (err)
159		goto err_debugfs_destroy;
160
161	ns->dev.id = nsim_dev_id++;
162	ns->dev.bus = &nsim_bus;
163	ns->dev.type = &nsim_dev_type;
164	err = device_register(&ns->dev);
165	if (err)
166		goto err_bpf_uninit;
167
168	SET_NETDEV_DEV(dev, &ns->dev);
169
170	err = nsim_devlink_setup(ns);
171	if (err)
172		goto err_unreg_dev;
173
174	return 0;
175
176err_unreg_dev:
177	device_unregister(&ns->dev);
178err_bpf_uninit:
179	nsim_bpf_uninit(ns);
180err_debugfs_destroy:
181	debugfs_remove_recursive(ns->ddir);
182	return err;
183}
184
185static void nsim_uninit(struct net_device *dev)
186{
187	struct netdevsim *ns = netdev_priv(dev);
188
189	nsim_devlink_teardown(ns);
190	debugfs_remove_recursive(ns->ddir);
191	nsim_bpf_uninit(ns);
192}
193
194static void nsim_free(struct net_device *dev)
195{
196	struct netdevsim *ns = netdev_priv(dev);
197
198	device_unregister(&ns->dev);
199	/* netdev and vf state will be freed out of device_release() */
200}
201
202static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
203{
204	struct netdevsim *ns = netdev_priv(dev);
205
 
 
 
206	u64_stats_update_begin(&ns->syncp);
207	ns->tx_packets++;
208	ns->tx_bytes += skb->len;
209	u64_stats_update_end(&ns->syncp);
210
 
211	dev_kfree_skb(skb);
212
213	return NETDEV_TX_OK;
214}
215
216static void nsim_set_rx_mode(struct net_device *dev)
217{
218}
219
220static int nsim_change_mtu(struct net_device *dev, int new_mtu)
221{
222	struct netdevsim *ns = netdev_priv(dev);
223
224	if (ns->xdp_prog_mode == XDP_ATTACHED_DRV &&
225	    new_mtu > NSIM_XDP_MAX_MTU)
226		return -EBUSY;
227
228	dev->mtu = new_mtu;
229
230	return 0;
231}
232
233static void
234nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
235{
236	struct netdevsim *ns = netdev_priv(dev);
237	unsigned int start;
238
239	do {
240		start = u64_stats_fetch_begin(&ns->syncp);
241		stats->tx_bytes = ns->tx_bytes;
242		stats->tx_packets = ns->tx_packets;
243	} while (u64_stats_fetch_retry(&ns->syncp, start));
244}
245
246static int
247nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
248{
249	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
250}
251
252static int
253nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f)
254{
255	struct netdevsim *ns = netdev_priv(dev);
256
257	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
258		return -EOPNOTSUPP;
259
260	switch (f->command) {
261	case TC_BLOCK_BIND:
262		return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb,
263					     ns, ns);
264	case TC_BLOCK_UNBIND:
265		tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns);
266		return 0;
267	default:
268		return -EOPNOTSUPP;
269	}
270}
271
272static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
273{
274	struct netdevsim *ns = netdev_priv(dev);
 
275
276	/* Only refuse multicast addresses, zero address can mean unset/any. */
277	if (vf >= ns->num_vfs || is_multicast_ether_addr(mac))
278		return -EINVAL;
279	memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
280
281	return 0;
282}
283
284static int nsim_set_vf_vlan(struct net_device *dev, int vf,
285			    u16 vlan, u8 qos, __be16 vlan_proto)
286{
287	struct netdevsim *ns = netdev_priv(dev);
 
288
289	if (vf >= ns->num_vfs || vlan > 4095 || qos > 7)
290		return -EINVAL;
291
292	ns->vfconfigs[vf].vlan = vlan;
293	ns->vfconfigs[vf].qos = qos;
294	ns->vfconfigs[vf].vlan_proto = vlan_proto;
295
296	return 0;
297}
298
299static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
300{
301	struct netdevsim *ns = netdev_priv(dev);
 
302
303	if (vf >= ns->num_vfs)
304		return -EINVAL;
305
306	ns->vfconfigs[vf].min_tx_rate = min;
307	ns->vfconfigs[vf].max_tx_rate = max;
308
309	return 0;
310}
311
312static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
313{
314	struct netdevsim *ns = netdev_priv(dev);
 
315
316	if (vf >= ns->num_vfs)
317		return -EINVAL;
318	ns->vfconfigs[vf].spoofchk_enabled = val;
319
320	return 0;
321}
322
323static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
324{
325	struct netdevsim *ns = netdev_priv(dev);
 
326
327	if (vf >= ns->num_vfs)
328		return -EINVAL;
329	ns->vfconfigs[vf].rss_query_enabled = val;
330
331	return 0;
332}
333
334static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
335{
336	struct netdevsim *ns = netdev_priv(dev);
 
337
338	if (vf >= ns->num_vfs)
339		return -EINVAL;
340	ns->vfconfigs[vf].trusted = val;
341
342	return 0;
343}
344
345static int
346nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
347{
348	struct netdevsim *ns = netdev_priv(dev);
 
349
350	if (vf >= ns->num_vfs)
351		return -EINVAL;
352
353	ivi->vf = vf;
354	ivi->linkstate = ns->vfconfigs[vf].link_state;
355	ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate;
356	ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate;
357	ivi->vlan = ns->vfconfigs[vf].vlan;
358	ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto;
359	ivi->qos = ns->vfconfigs[vf].qos;
360	memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN);
361	ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled;
362	ivi->trusted = ns->vfconfigs[vf].trusted;
363	ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled;
364
365	return 0;
366}
367
368static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
369{
370	struct netdevsim *ns = netdev_priv(dev);
 
371
372	if (vf >= ns->num_vfs)
373		return -EINVAL;
374
375	switch (state) {
376	case IFLA_VF_LINK_STATE_AUTO:
377	case IFLA_VF_LINK_STATE_ENABLE:
378	case IFLA_VF_LINK_STATE_DISABLE:
379		break;
380	default:
381		return -EINVAL;
382	}
383
384	ns->vfconfigs[vf].link_state = state;
385
386	return 0;
387}
388
 
 
389static int
390nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
391{
 
 
392	switch (type) {
393	case TC_SETUP_BLOCK:
394		return nsim_setup_tc_block(dev, type_data);
 
 
 
395	default:
396		return -EOPNOTSUPP;
397	}
398}
399
400static int
401nsim_set_features(struct net_device *dev, netdev_features_t features)
402{
403	struct netdevsim *ns = netdev_priv(dev);
404
405	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
406		return nsim_bpf_disable_tc(ns);
407
408	return 0;
409}
410
 
 
 
 
 
 
 
411static const struct net_device_ops nsim_netdev_ops = {
412	.ndo_init		= nsim_init,
413	.ndo_uninit		= nsim_uninit,
414	.ndo_start_xmit		= nsim_start_xmit,
415	.ndo_set_rx_mode	= nsim_set_rx_mode,
416	.ndo_set_mac_address	= eth_mac_addr,
417	.ndo_validate_addr	= eth_validate_addr,
418	.ndo_change_mtu		= nsim_change_mtu,
419	.ndo_get_stats64	= nsim_get_stats64,
420	.ndo_set_vf_mac		= nsim_set_vf_mac,
421	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
422	.ndo_set_vf_rate	= nsim_set_vf_rate,
423	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
424	.ndo_set_vf_trust	= nsim_set_vf_trust,
425	.ndo_get_vf_config	= nsim_get_vf_config,
426	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
427	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
428	.ndo_setup_tc		= nsim_setup_tc,
429	.ndo_set_features	= nsim_set_features,
430	.ndo_bpf		= nsim_bpf,
 
 
 
431};
432
433static void nsim_setup(struct net_device *dev)
434{
435	ether_setup(dev);
436	eth_hw_addr_random(dev);
437
438	dev->netdev_ops = &nsim_netdev_ops;
439	dev->priv_destructor = nsim_free;
440
441	dev->tx_queue_len = 0;
442	dev->flags |= IFF_NOARP;
443	dev->flags &= ~IFF_MULTICAST;
444	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
445			   IFF_NO_QUEUE;
446	dev->features |= NETIF_F_HIGHDMA |
447			 NETIF_F_SG |
448			 NETIF_F_FRAGLIST |
449			 NETIF_F_HW_CSUM |
450			 NETIF_F_TSO;
451	dev->hw_features |= NETIF_F_HW_TC;
452	dev->max_mtu = ETH_MAX_MTU;
453}
454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
456			 struct netlink_ext_ack *extack)
457{
458	if (tb[IFLA_ADDRESS]) {
459		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
460			return -EINVAL;
461		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
462			return -EADDRNOTAVAIL;
463	}
464	return 0;
465}
466
467static struct rtnl_link_ops nsim_link_ops __read_mostly = {
468	.kind		= DRV_NAME,
469	.priv_size	= sizeof(struct netdevsim),
470	.setup		= nsim_setup,
471	.validate	= nsim_validate,
472};
473
474struct dentry *nsim_ddir;
475
476static int __init nsim_module_init(void)
477{
478	int err;
479
480	nsim_ddir = debugfs_create_dir(DRV_NAME, NULL);
481	if (IS_ERR_OR_NULL(nsim_ddir))
482		return -ENOMEM;
483
484	err = bus_register(&nsim_bus);
485	if (err)
486		goto err_debugfs_destroy;
487
488	err = nsim_devlink_init();
489	if (err)
490		goto err_unreg_bus;
491
492	err = rtnl_link_register(&nsim_link_ops);
493	if (err)
494		goto err_dl_fini;
495
496	return 0;
497
498err_dl_fini:
499	nsim_devlink_exit();
500err_unreg_bus:
501	bus_unregister(&nsim_bus);
502err_debugfs_destroy:
503	debugfs_remove_recursive(nsim_ddir);
504	return err;
505}
506
507static void __exit nsim_module_exit(void)
508{
509	rtnl_link_unregister(&nsim_link_ops);
510	nsim_devlink_exit();
511	bus_unregister(&nsim_bus);
512	debugfs_remove_recursive(nsim_ddir);
513}
514
515module_init(nsim_module_init);
516module_exit(nsim_module_exit);
517MODULE_LICENSE("GPL");
518MODULE_ALIAS_RTNL_LINK(DRV_NAME);
v5.9
  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_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
 86
 87	/* Only refuse multicast addresses, zero address can mean unset/any. */
 88	if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
 89		return -EINVAL;
 90	memcpy(nsim_bus_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_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
100
101	if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
102		return -EINVAL;
103
104	nsim_bus_dev->vfconfigs[vf].vlan = vlan;
105	nsim_bus_dev->vfconfigs[vf].qos = qos;
106	nsim_bus_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_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
115
116	if (vf >= nsim_bus_dev->num_vfs)
117		return -EINVAL;
118
119	nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
120	nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
121
122	return 0;
123}
124
125static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
126{
127	struct netdevsim *ns = netdev_priv(dev);
128	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
129
130	if (vf >= nsim_bus_dev->num_vfs)
131		return -EINVAL;
132	nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
133
134	return 0;
135}
136
137static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
138{
139	struct netdevsim *ns = netdev_priv(dev);
140	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
141
142	if (vf >= nsim_bus_dev->num_vfs)
143		return -EINVAL;
144	nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
145
146	return 0;
147}
148
149static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
150{
151	struct netdevsim *ns = netdev_priv(dev);
152	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
153
154	if (vf >= nsim_bus_dev->num_vfs)
155		return -EINVAL;
156	nsim_bus_dev->vfconfigs[vf].trusted = val;
157
158	return 0;
159}
160
161static int
162nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
163{
164	struct netdevsim *ns = netdev_priv(dev);
165	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
166
167	if (vf >= nsim_bus_dev->num_vfs)
168		return -EINVAL;
169
170	ivi->vf = vf;
171	ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
172	ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
173	ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
174	ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
175	ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
176	ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
177	memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
178	ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
179	ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
180	ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
181
182	return 0;
183}
184
185static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
186{
187	struct netdevsim *ns = netdev_priv(dev);
188	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
189
190	if (vf >= nsim_bus_dev->num_vfs)
191		return -EINVAL;
192
193	switch (state) {
194	case IFLA_VF_LINK_STATE_AUTO:
195	case IFLA_VF_LINK_STATE_ENABLE:
196	case IFLA_VF_LINK_STATE_DISABLE:
197		break;
198	default:
199		return -EINVAL;
200	}
201
202	nsim_bus_dev->vfconfigs[vf].link_state = state;
203
204	return 0;
205}
206
207static LIST_HEAD(nsim_block_cb_list);
208
209static int
210nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
211{
212	struct netdevsim *ns = netdev_priv(dev);
213
214	switch (type) {
215	case TC_SETUP_BLOCK:
216		return flow_block_cb_setup_simple(type_data,
217						  &nsim_block_cb_list,
218						  nsim_setup_tc_block_cb,
219						  ns, ns, true);
220	default:
221		return -EOPNOTSUPP;
222	}
223}
224
225static int
226nsim_set_features(struct net_device *dev, netdev_features_t features)
227{
228	struct netdevsim *ns = netdev_priv(dev);
229
230	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
231		return nsim_bpf_disable_tc(ns);
232
233	return 0;
234}
235
236static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
237{
238	struct netdevsim *ns = netdev_priv(dev);
239
240	return &ns->nsim_dev_port->devlink_port;
241}
242
243static const struct net_device_ops nsim_netdev_ops = {
 
 
244	.ndo_start_xmit		= nsim_start_xmit,
245	.ndo_set_rx_mode	= nsim_set_rx_mode,
246	.ndo_set_mac_address	= eth_mac_addr,
247	.ndo_validate_addr	= eth_validate_addr,
248	.ndo_change_mtu		= nsim_change_mtu,
249	.ndo_get_stats64	= nsim_get_stats64,
250	.ndo_set_vf_mac		= nsim_set_vf_mac,
251	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
252	.ndo_set_vf_rate	= nsim_set_vf_rate,
253	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
254	.ndo_set_vf_trust	= nsim_set_vf_trust,
255	.ndo_get_vf_config	= nsim_get_vf_config,
256	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
257	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
258	.ndo_setup_tc		= nsim_setup_tc,
259	.ndo_set_features	= nsim_set_features,
260	.ndo_bpf		= nsim_bpf,
261	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
262	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
263	.ndo_get_devlink_port	= nsim_get_devlink_port,
264};
265
266static void nsim_setup(struct net_device *dev)
267{
268	ether_setup(dev);
269	eth_hw_addr_random(dev);
270
 
 
 
271	dev->tx_queue_len = 0;
272	dev->flags |= IFF_NOARP;
273	dev->flags &= ~IFF_MULTICAST;
274	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
275			   IFF_NO_QUEUE;
276	dev->features |= NETIF_F_HIGHDMA |
277			 NETIF_F_SG |
278			 NETIF_F_FRAGLIST |
279			 NETIF_F_HW_CSUM |
280			 NETIF_F_TSO;
281	dev->hw_features |= NETIF_F_HW_TC;
282	dev->max_mtu = ETH_MAX_MTU;
283}
284
285struct netdevsim *
286nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
287{
288	struct net_device *dev;
289	struct netdevsim *ns;
290	int err;
291
292	dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
293	if (!dev)
294		return ERR_PTR(-ENOMEM);
295
296	dev_net_set(dev, nsim_dev_net(nsim_dev));
297	ns = netdev_priv(dev);
298	ns->netdev = dev;
299	ns->nsim_dev = nsim_dev;
300	ns->nsim_dev_port = nsim_dev_port;
301	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
302	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
303	dev->netdev_ops = &nsim_netdev_ops;
304
305	err = nsim_udp_tunnels_info_create(nsim_dev, dev);
306	if (err)
307		goto err_free_netdev;
308
309	rtnl_lock();
310	err = nsim_bpf_init(ns);
311	if (err)
312		goto err_utn_destroy;
313
314	nsim_ipsec_init(ns);
315
316	err = register_netdevice(dev);
317	if (err)
318		goto err_ipsec_teardown;
319	rtnl_unlock();
320
321	return ns;
322
323err_ipsec_teardown:
324	nsim_ipsec_teardown(ns);
325	nsim_bpf_uninit(ns);
326err_utn_destroy:
327	rtnl_unlock();
328	nsim_udp_tunnels_info_destroy(dev);
329err_free_netdev:
330	free_netdev(dev);
331	return ERR_PTR(err);
332}
333
334void nsim_destroy(struct netdevsim *ns)
335{
336	struct net_device *dev = ns->netdev;
337
338	rtnl_lock();
339	unregister_netdevice(dev);
340	nsim_ipsec_teardown(ns);
341	nsim_bpf_uninit(ns);
342	rtnl_unlock();
343	nsim_udp_tunnels_info_destroy(dev);
344	free_netdev(dev);
345}
346
347static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
348			 struct netlink_ext_ack *extack)
349{
350	NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
351	return -EOPNOTSUPP;
 
 
 
 
 
352}
353
354static struct rtnl_link_ops nsim_link_ops __read_mostly = {
355	.kind		= DRV_NAME,
 
 
356	.validate	= nsim_validate,
357};
358
 
 
359static int __init nsim_module_init(void)
360{
361	int err;
362
363	err = nsim_dev_init();
 
 
 
 
364	if (err)
365		return err;
366
367	err = nsim_bus_init();
368	if (err)
369		goto err_dev_exit;
370
371	err = rtnl_link_register(&nsim_link_ops);
372	if (err)
373		goto err_bus_exit;
374
375	return 0;
376
377err_bus_exit:
378	nsim_bus_exit();
379err_dev_exit:
380	nsim_dev_exit();
 
 
381	return err;
382}
383
384static void __exit nsim_module_exit(void)
385{
386	rtnl_link_unregister(&nsim_link_ops);
387	nsim_bus_exit();
388	nsim_dev_exit();
 
389}
390
391module_init(nsim_module_init);
392module_exit(nsim_module_exit);
393MODULE_LICENSE("GPL");
394MODULE_ALIAS_RTNL_LINK(DRV_NAME);