xref: /openbmc/linux/drivers/net/netdevsim/netdev.c (revision 90d299138dd42579084715d6d25f9c27b63211af)
183c9e13aSJakub Kicinski /*
283c9e13aSJakub Kicinski  * Copyright (C) 2017 Netronome Systems, Inc.
383c9e13aSJakub Kicinski  *
483c9e13aSJakub Kicinski  * This software is licensed under the GNU General License Version 2,
583c9e13aSJakub Kicinski  * June 1991 as shown in the file COPYING in the top-level directory of this
683c9e13aSJakub Kicinski  * source tree.
783c9e13aSJakub Kicinski  *
883c9e13aSJakub Kicinski  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
983c9e13aSJakub Kicinski  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
1083c9e13aSJakub Kicinski  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1183c9e13aSJakub Kicinski  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
1283c9e13aSJakub Kicinski  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
1383c9e13aSJakub Kicinski  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
1483c9e13aSJakub Kicinski  */
1583c9e13aSJakub Kicinski 
1631d3ad83SJakub Kicinski #include <linux/debugfs.h>
1783c9e13aSJakub Kicinski #include <linux/etherdevice.h>
1883c9e13aSJakub Kicinski #include <linux/kernel.h>
1983c9e13aSJakub Kicinski #include <linux/module.h>
2083c9e13aSJakub Kicinski #include <linux/netdevice.h>
2183c9e13aSJakub Kicinski #include <linux/slab.h>
2283c9e13aSJakub Kicinski #include <net/netlink.h>
2331d3ad83SJakub Kicinski #include <net/pkt_cls.h>
2483c9e13aSJakub Kicinski #include <net/rtnetlink.h>
2583c9e13aSJakub Kicinski 
2683c9e13aSJakub Kicinski #include "netdevsim.h"
2783c9e13aSJakub Kicinski 
2883c9e13aSJakub Kicinski static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
2983c9e13aSJakub Kicinski {
3083c9e13aSJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
3183c9e13aSJakub Kicinski 
327699353dSShannon Nelson 	if (!nsim_ipsec_tx(ns, skb))
337699353dSShannon Nelson 		goto out;
347699353dSShannon Nelson 
3583c9e13aSJakub Kicinski 	u64_stats_update_begin(&ns->syncp);
3683c9e13aSJakub Kicinski 	ns->tx_packets++;
3783c9e13aSJakub Kicinski 	ns->tx_bytes += skb->len;
3883c9e13aSJakub Kicinski 	u64_stats_update_end(&ns->syncp);
3983c9e13aSJakub Kicinski 
407699353dSShannon Nelson out:
4183c9e13aSJakub Kicinski 	dev_kfree_skb(skb);
4283c9e13aSJakub Kicinski 
4383c9e13aSJakub Kicinski 	return NETDEV_TX_OK;
4483c9e13aSJakub Kicinski }
4583c9e13aSJakub Kicinski 
4683c9e13aSJakub Kicinski static void nsim_set_rx_mode(struct net_device *dev)
4783c9e13aSJakub Kicinski {
4883c9e13aSJakub Kicinski }
4983c9e13aSJakub Kicinski 
5031d3ad83SJakub Kicinski static int nsim_change_mtu(struct net_device *dev, int new_mtu)
5131d3ad83SJakub Kicinski {
5231d3ad83SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
5331d3ad83SJakub Kicinski 
54799e173dSJakub Kicinski 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
5531d3ad83SJakub Kicinski 		return -EBUSY;
5631d3ad83SJakub Kicinski 
5731d3ad83SJakub Kicinski 	dev->mtu = new_mtu;
5831d3ad83SJakub Kicinski 
5931d3ad83SJakub Kicinski 	return 0;
6031d3ad83SJakub Kicinski }
6131d3ad83SJakub Kicinski 
6283c9e13aSJakub Kicinski static void
6383c9e13aSJakub Kicinski nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6483c9e13aSJakub Kicinski {
6583c9e13aSJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
6683c9e13aSJakub Kicinski 	unsigned int start;
6783c9e13aSJakub Kicinski 
6883c9e13aSJakub Kicinski 	do {
6983c9e13aSJakub Kicinski 		start = u64_stats_fetch_begin(&ns->syncp);
7083c9e13aSJakub Kicinski 		stats->tx_bytes = ns->tx_bytes;
7183c9e13aSJakub Kicinski 		stats->tx_packets = ns->tx_packets;
7283c9e13aSJakub Kicinski 	} while (u64_stats_fetch_retry(&ns->syncp, start));
7383c9e13aSJakub Kicinski }
7483c9e13aSJakub Kicinski 
7531d3ad83SJakub Kicinski static int
7631d3ad83SJakub Kicinski nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7731d3ad83SJakub Kicinski {
7831d3ad83SJakub Kicinski 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
7931d3ad83SJakub Kicinski }
8031d3ad83SJakub Kicinski 
8179579220SJakub Kicinski static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
8279579220SJakub Kicinski {
8379579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
8440e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
8579579220SJakub Kicinski 
8679579220SJakub Kicinski 	/* Only refuse multicast addresses, zero address can mean unset/any. */
8740e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
8879579220SJakub Kicinski 		return -EINVAL;
8940e4fe4cSJiri Pirko 	memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
9079579220SJakub Kicinski 
9179579220SJakub Kicinski 	return 0;
9279579220SJakub Kicinski }
9379579220SJakub Kicinski 
9479579220SJakub Kicinski static int nsim_set_vf_vlan(struct net_device *dev, int vf,
9579579220SJakub Kicinski 			    u16 vlan, u8 qos, __be16 vlan_proto)
9679579220SJakub Kicinski {
9779579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
9840e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
9979579220SJakub Kicinski 
10040e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
10179579220SJakub Kicinski 		return -EINVAL;
10279579220SJakub Kicinski 
10340e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].vlan = vlan;
10440e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].qos = qos;
10540e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
10679579220SJakub Kicinski 
10779579220SJakub Kicinski 	return 0;
10879579220SJakub Kicinski }
10979579220SJakub Kicinski 
11079579220SJakub Kicinski static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
11179579220SJakub Kicinski {
11279579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
11340e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
11479579220SJakub Kicinski 
11540e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
11679579220SJakub Kicinski 		return -EINVAL;
11779579220SJakub Kicinski 
11840e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
11940e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
12079579220SJakub Kicinski 
12179579220SJakub Kicinski 	return 0;
12279579220SJakub Kicinski }
12379579220SJakub Kicinski 
12479579220SJakub Kicinski static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
12579579220SJakub Kicinski {
12679579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
12740e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
12879579220SJakub Kicinski 
12940e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
13079579220SJakub Kicinski 		return -EINVAL;
13140e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
13279579220SJakub Kicinski 
13379579220SJakub Kicinski 	return 0;
13479579220SJakub Kicinski }
13579579220SJakub Kicinski 
13679579220SJakub Kicinski static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
13779579220SJakub Kicinski {
13879579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
13940e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
14079579220SJakub Kicinski 
14140e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
14279579220SJakub Kicinski 		return -EINVAL;
14340e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
14479579220SJakub Kicinski 
14579579220SJakub Kicinski 	return 0;
14679579220SJakub Kicinski }
14779579220SJakub Kicinski 
14879579220SJakub Kicinski static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
14979579220SJakub Kicinski {
15079579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
15140e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
15279579220SJakub Kicinski 
15340e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
15479579220SJakub Kicinski 		return -EINVAL;
15540e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].trusted = val;
15679579220SJakub Kicinski 
15779579220SJakub Kicinski 	return 0;
15879579220SJakub Kicinski }
15979579220SJakub Kicinski 
16079579220SJakub Kicinski static int
16179579220SJakub Kicinski nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
16279579220SJakub Kicinski {
16379579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
16440e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
16579579220SJakub Kicinski 
16640e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
16779579220SJakub Kicinski 		return -EINVAL;
16879579220SJakub Kicinski 
16979579220SJakub Kicinski 	ivi->vf = vf;
17040e4fe4cSJiri Pirko 	ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
17140e4fe4cSJiri Pirko 	ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
17240e4fe4cSJiri Pirko 	ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
17340e4fe4cSJiri Pirko 	ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
17440e4fe4cSJiri Pirko 	ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
17540e4fe4cSJiri Pirko 	ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
17640e4fe4cSJiri Pirko 	memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
17740e4fe4cSJiri Pirko 	ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
17840e4fe4cSJiri Pirko 	ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
17940e4fe4cSJiri Pirko 	ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
18079579220SJakub Kicinski 
18179579220SJakub Kicinski 	return 0;
18279579220SJakub Kicinski }
18379579220SJakub Kicinski 
18479579220SJakub Kicinski static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
18579579220SJakub Kicinski {
18679579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
18740e4fe4cSJiri Pirko 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
18879579220SJakub Kicinski 
18940e4fe4cSJiri Pirko 	if (vf >= nsim_bus_dev->num_vfs)
19079579220SJakub Kicinski 		return -EINVAL;
19179579220SJakub Kicinski 
19279579220SJakub Kicinski 	switch (state) {
19379579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_AUTO:
19479579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_ENABLE:
19579579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_DISABLE:
19679579220SJakub Kicinski 		break;
19779579220SJakub Kicinski 	default:
19879579220SJakub Kicinski 		return -EINVAL;
19979579220SJakub Kicinski 	}
20079579220SJakub Kicinski 
20140e4fe4cSJiri Pirko 	nsim_bus_dev->vfconfigs[vf].link_state = state;
20279579220SJakub Kicinski 
20379579220SJakub Kicinski 	return 0;
20479579220SJakub Kicinski }
20579579220SJakub Kicinski 
206955bcb6eSPablo Neira Ayuso static LIST_HEAD(nsim_block_cb_list);
207955bcb6eSPablo Neira Ayuso 
20831d3ad83SJakub Kicinski static int
20931d3ad83SJakub Kicinski nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
21031d3ad83SJakub Kicinski {
2114e95bc26SPablo Neira Ayuso 	struct netdevsim *ns = netdev_priv(dev);
2124e95bc26SPablo Neira Ayuso 
21331d3ad83SJakub Kicinski 	switch (type) {
21431d3ad83SJakub Kicinski 	case TC_SETUP_BLOCK:
215955bcb6eSPablo Neira Ayuso 		return flow_block_cb_setup_simple(type_data,
216955bcb6eSPablo Neira Ayuso 						  &nsim_block_cb_list,
2174e95bc26SPablo Neira Ayuso 						  nsim_setup_tc_block_cb,
2184e95bc26SPablo Neira Ayuso 						  ns, ns, true);
21931d3ad83SJakub Kicinski 	default:
22031d3ad83SJakub Kicinski 		return -EOPNOTSUPP;
22131d3ad83SJakub Kicinski 	}
22231d3ad83SJakub Kicinski }
22331d3ad83SJakub Kicinski 
22431d3ad83SJakub Kicinski static int
22531d3ad83SJakub Kicinski nsim_set_features(struct net_device *dev, netdev_features_t features)
22631d3ad83SJakub Kicinski {
22731d3ad83SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
22831d3ad83SJakub Kicinski 
22931d3ad83SJakub Kicinski 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
23031d3ad83SJakub Kicinski 		return nsim_bpf_disable_tc(ns);
23131d3ad83SJakub Kicinski 
23231d3ad83SJakub Kicinski 	return 0;
23331d3ad83SJakub Kicinski }
23431d3ad83SJakub Kicinski 
235a62fdbbeSJiri Pirko static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
236a62fdbbeSJiri Pirko {
237a62fdbbeSJiri Pirko 	struct netdevsim *ns = netdev_priv(dev);
238a62fdbbeSJiri Pirko 
239a62fdbbeSJiri Pirko 	return &ns->nsim_dev_port->devlink_port;
240a62fdbbeSJiri Pirko }
241a62fdbbeSJiri Pirko 
24283c9e13aSJakub Kicinski static const struct net_device_ops nsim_netdev_ops = {
24383c9e13aSJakub Kicinski 	.ndo_start_xmit		= nsim_start_xmit,
24483c9e13aSJakub Kicinski 	.ndo_set_rx_mode	= nsim_set_rx_mode,
24583c9e13aSJakub Kicinski 	.ndo_set_mac_address	= eth_mac_addr,
24683c9e13aSJakub Kicinski 	.ndo_validate_addr	= eth_validate_addr,
24731d3ad83SJakub Kicinski 	.ndo_change_mtu		= nsim_change_mtu,
24883c9e13aSJakub Kicinski 	.ndo_get_stats64	= nsim_get_stats64,
24979579220SJakub Kicinski 	.ndo_set_vf_mac		= nsim_set_vf_mac,
25079579220SJakub Kicinski 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
25179579220SJakub Kicinski 	.ndo_set_vf_rate	= nsim_set_vf_rate,
25279579220SJakub Kicinski 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
25379579220SJakub Kicinski 	.ndo_set_vf_trust	= nsim_set_vf_trust,
25479579220SJakub Kicinski 	.ndo_get_vf_config	= nsim_get_vf_config,
25579579220SJakub Kicinski 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
25679579220SJakub Kicinski 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
25731d3ad83SJakub Kicinski 	.ndo_setup_tc		= nsim_setup_tc,
25831d3ad83SJakub Kicinski 	.ndo_set_features	= nsim_set_features,
25931d3ad83SJakub Kicinski 	.ndo_bpf		= nsim_bpf,
260a62fdbbeSJiri Pirko 	.ndo_get_devlink_port	= nsim_get_devlink_port,
26183c9e13aSJakub Kicinski };
26283c9e13aSJakub Kicinski 
26383c9e13aSJakub Kicinski static void nsim_setup(struct net_device *dev)
26483c9e13aSJakub Kicinski {
26583c9e13aSJakub Kicinski 	ether_setup(dev);
26683c9e13aSJakub Kicinski 	eth_hw_addr_random(dev);
26783c9e13aSJakub Kicinski 
26883c9e13aSJakub Kicinski 	dev->tx_queue_len = 0;
26983c9e13aSJakub Kicinski 	dev->flags |= IFF_NOARP;
27083c9e13aSJakub Kicinski 	dev->flags &= ~IFF_MULTICAST;
27183c9e13aSJakub Kicinski 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
27283c9e13aSJakub Kicinski 			   IFF_NO_QUEUE;
27383c9e13aSJakub Kicinski 	dev->features |= NETIF_F_HIGHDMA |
27483c9e13aSJakub Kicinski 			 NETIF_F_SG |
27583c9e13aSJakub Kicinski 			 NETIF_F_FRAGLIST |
27683c9e13aSJakub Kicinski 			 NETIF_F_HW_CSUM |
27783c9e13aSJakub Kicinski 			 NETIF_F_TSO;
27831d3ad83SJakub Kicinski 	dev->hw_features |= NETIF_F_HW_TC;
27983c9e13aSJakub Kicinski 	dev->max_mtu = ETH_MAX_MTU;
28083c9e13aSJakub Kicinski }
28183c9e13aSJakub Kicinski 
282e05b2d14SJiri Pirko struct netdevsim *
283e05b2d14SJiri Pirko nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
28483c9e13aSJakub Kicinski {
285e05b2d14SJiri Pirko 	struct net_device *dev;
286e05b2d14SJiri Pirko 	struct netdevsim *ns;
287af9095f0SJiri Pirko 	int err;
288eeeaaf18SJakub Kicinski 
289e05b2d14SJiri Pirko 	dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
290e05b2d14SJiri Pirko 	if (!dev)
291e05b2d14SJiri Pirko 		return ERR_PTR(-ENOMEM);
292e05b2d14SJiri Pirko 
293*90d29913SJiri Pirko 	dev_net_set(dev, nsim_dev_net(nsim_dev));
294e05b2d14SJiri Pirko 	ns = netdev_priv(dev);
2958320d145SJiri Pirko 	ns->netdev = dev;
296e05b2d14SJiri Pirko 	ns->nsim_dev = nsim_dev;
297e05b2d14SJiri Pirko 	ns->nsim_dev_port = nsim_dev_port;
298e05b2d14SJiri Pirko 	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
29940e4fe4cSJiri Pirko 	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
300e05b2d14SJiri Pirko 	dev->netdev_ops = &nsim_netdev_ops;
3015fc49422SJiri Pirko 
302e05b2d14SJiri Pirko 	rtnl_lock();
303e05b2d14SJiri Pirko 	err = nsim_bpf_init(ns);
304e05b2d14SJiri Pirko 	if (err)
305e05b2d14SJiri Pirko 		goto err_free_netdev;
306e05b2d14SJiri Pirko 
307e05b2d14SJiri Pirko 	nsim_ipsec_init(ns);
3081daf36c0SJiri Pirko 
3091daf36c0SJiri Pirko 	err = register_netdevice(dev);
3101daf36c0SJiri Pirko 	if (err)
311e05b2d14SJiri Pirko 		goto err_ipsec_teardown;
312e05b2d14SJiri Pirko 	rtnl_unlock();
313af9095f0SJiri Pirko 
314e05b2d14SJiri Pirko 	return ns;
315e05b2d14SJiri Pirko 
316e05b2d14SJiri Pirko err_ipsec_teardown:
317e05b2d14SJiri Pirko 	nsim_ipsec_teardown(ns);
318e05b2d14SJiri Pirko 	nsim_bpf_uninit(ns);
319e05b2d14SJiri Pirko 	rtnl_unlock();
320e05b2d14SJiri Pirko err_free_netdev:
321e05b2d14SJiri Pirko 	free_netdev(dev);
322e05b2d14SJiri Pirko 	return ERR_PTR(err);
323e05b2d14SJiri Pirko }
324e05b2d14SJiri Pirko 
325e05b2d14SJiri Pirko void nsim_destroy(struct netdevsim *ns)
326e05b2d14SJiri Pirko {
327e05b2d14SJiri Pirko 	struct net_device *dev = ns->netdev;
328e05b2d14SJiri Pirko 
329e05b2d14SJiri Pirko 	rtnl_lock();
330e05b2d14SJiri Pirko 	unregister_netdevice(dev);
331e05b2d14SJiri Pirko 	nsim_ipsec_teardown(ns);
332e05b2d14SJiri Pirko 	nsim_bpf_uninit(ns);
333e05b2d14SJiri Pirko 	rtnl_unlock();
334e05b2d14SJiri Pirko 	free_netdev(dev);
335e05b2d14SJiri Pirko }
336e05b2d14SJiri Pirko 
337e05b2d14SJiri Pirko static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
338e05b2d14SJiri Pirko 			 struct netlink_ext_ack *extack)
339e05b2d14SJiri Pirko {
340e05b2d14SJiri Pirko 	NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
341e05b2d14SJiri Pirko 	return -EOPNOTSUPP;
342eeeaaf18SJakub Kicinski }
343eeeaaf18SJakub Kicinski 
34483c9e13aSJakub Kicinski static struct rtnl_link_ops nsim_link_ops __read_mostly = {
34583c9e13aSJakub Kicinski 	.kind		= DRV_NAME,
34683c9e13aSJakub Kicinski 	.validate	= nsim_validate,
34783c9e13aSJakub Kicinski };
34883c9e13aSJakub Kicinski 
34983c9e13aSJakub Kicinski static int __init nsim_module_init(void)
35083c9e13aSJakub Kicinski {
35131d3ad83SJakub Kicinski 	int err;
35231d3ad83SJakub Kicinski 
353d514f41eSJiri Pirko 	err = nsim_dev_init();
354af9095f0SJiri Pirko 	if (err)
355ab1d0cc0SJiri Pirko 		return err;
356eeeaaf18SJakub Kicinski 
357925f5afeSJiri Pirko 	err = nsim_bus_init();
35831d3ad83SJakub Kicinski 	if (err)
359d514f41eSJiri Pirko 		goto err_dev_exit;
36031d3ad83SJakub Kicinski 
361a5facc4cSJiri Pirko 	err = rtnl_link_register(&nsim_link_ops);
36279579220SJakub Kicinski 	if (err)
363925f5afeSJiri Pirko 		goto err_bus_exit;
36479579220SJakub Kicinski 
36531d3ad83SJakub Kicinski 	return 0;
36631d3ad83SJakub Kicinski 
367925f5afeSJiri Pirko err_bus_exit:
368925f5afeSJiri Pirko 	nsim_bus_exit();
369d514f41eSJiri Pirko err_dev_exit:
370d514f41eSJiri Pirko 	nsim_dev_exit();
37131d3ad83SJakub Kicinski 	return err;
37283c9e13aSJakub Kicinski }
37383c9e13aSJakub Kicinski 
37483c9e13aSJakub Kicinski static void __exit nsim_module_exit(void)
37583c9e13aSJakub Kicinski {
37683c9e13aSJakub Kicinski 	rtnl_link_unregister(&nsim_link_ops);
377925f5afeSJiri Pirko 	nsim_bus_exit();
378d514f41eSJiri Pirko 	nsim_dev_exit();
37983c9e13aSJakub Kicinski }
38083c9e13aSJakub Kicinski 
38183c9e13aSJakub Kicinski module_init(nsim_module_init);
38283c9e13aSJakub Kicinski module_exit(nsim_module_exit);
38383c9e13aSJakub Kicinski MODULE_LICENSE("GPL");
38483c9e13aSJakub Kicinski MODULE_ALIAS_RTNL_LINK(DRV_NAME);
385