xref: /openbmc/linux/drivers/net/netdevsim/netdev.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
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>
25424be63aSJakub Kicinski #include <net/udp_tunnel.h>
2683c9e13aSJakub Kicinski 
2783c9e13aSJakub Kicinski #include "netdevsim.h"
2883c9e13aSJakub Kicinski 
nsim_start_xmit(struct sk_buff * skb,struct net_device * dev)2983c9e13aSJakub Kicinski static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
3083c9e13aSJakub Kicinski {
3183c9e13aSJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
3283c9e13aSJakub Kicinski 
337699353dSShannon Nelson 	if (!nsim_ipsec_tx(ns, skb))
347699353dSShannon Nelson 		goto out;
357699353dSShannon Nelson 
3683c9e13aSJakub Kicinski 	u64_stats_update_begin(&ns->syncp);
3783c9e13aSJakub Kicinski 	ns->tx_packets++;
3883c9e13aSJakub Kicinski 	ns->tx_bytes += skb->len;
3983c9e13aSJakub Kicinski 	u64_stats_update_end(&ns->syncp);
4083c9e13aSJakub Kicinski 
417699353dSShannon Nelson out:
4283c9e13aSJakub Kicinski 	dev_kfree_skb(skb);
4383c9e13aSJakub Kicinski 
4483c9e13aSJakub Kicinski 	return NETDEV_TX_OK;
4583c9e13aSJakub Kicinski }
4683c9e13aSJakub Kicinski 
nsim_set_rx_mode(struct net_device * dev)4783c9e13aSJakub Kicinski static void nsim_set_rx_mode(struct net_device *dev)
4883c9e13aSJakub Kicinski {
4983c9e13aSJakub Kicinski }
5083c9e13aSJakub Kicinski 
nsim_change_mtu(struct net_device * dev,int new_mtu)5131d3ad83SJakub Kicinski static int nsim_change_mtu(struct net_device *dev, int new_mtu)
5231d3ad83SJakub Kicinski {
5331d3ad83SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
5431d3ad83SJakub Kicinski 
55799e173dSJakub Kicinski 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
5631d3ad83SJakub Kicinski 		return -EBUSY;
5731d3ad83SJakub Kicinski 
5831d3ad83SJakub Kicinski 	dev->mtu = new_mtu;
5931d3ad83SJakub Kicinski 
6031d3ad83SJakub Kicinski 	return 0;
6131d3ad83SJakub Kicinski }
6231d3ad83SJakub Kicinski 
6383c9e13aSJakub Kicinski static void
nsim_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)6483c9e13aSJakub Kicinski nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6583c9e13aSJakub Kicinski {
6683c9e13aSJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
6783c9e13aSJakub Kicinski 	unsigned int start;
6883c9e13aSJakub Kicinski 
6983c9e13aSJakub Kicinski 	do {
70068c38adSThomas Gleixner 		start = u64_stats_fetch_begin(&ns->syncp);
7183c9e13aSJakub Kicinski 		stats->tx_bytes = ns->tx_bytes;
7283c9e13aSJakub Kicinski 		stats->tx_packets = ns->tx_packets;
73068c38adSThomas Gleixner 	} while (u64_stats_fetch_retry(&ns->syncp, start));
7483c9e13aSJakub Kicinski }
7583c9e13aSJakub Kicinski 
7631d3ad83SJakub Kicinski static int
nsim_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)7731d3ad83SJakub Kicinski nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7831d3ad83SJakub Kicinski {
7931d3ad83SJakub Kicinski 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
8031d3ad83SJakub Kicinski }
8131d3ad83SJakub Kicinski 
nsim_set_vf_mac(struct net_device * dev,int vf,u8 * mac)8279579220SJakub Kicinski static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
8379579220SJakub Kicinski {
8479579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
855e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
8679579220SJakub Kicinski 
8779579220SJakub Kicinski 	/* Only refuse multicast addresses, zero address can mean unset/any. */
885e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev) || is_multicast_ether_addr(mac))
8979579220SJakub Kicinski 		return -EINVAL;
905e388f3dSJakub Kicinski 	memcpy(nsim_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
9179579220SJakub Kicinski 
9279579220SJakub Kicinski 	return 0;
9379579220SJakub Kicinski }
9479579220SJakub Kicinski 
nsim_set_vf_vlan(struct net_device * dev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)9579579220SJakub Kicinski static int nsim_set_vf_vlan(struct net_device *dev, int vf,
9679579220SJakub Kicinski 			    u16 vlan, u8 qos, __be16 vlan_proto)
9779579220SJakub Kicinski {
9879579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
995e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
10079579220SJakub Kicinski 
1015e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev) || vlan > 4095 || qos > 7)
10279579220SJakub Kicinski 		return -EINVAL;
10379579220SJakub Kicinski 
1045e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].vlan = vlan;
1055e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].qos = qos;
1065e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].vlan_proto = vlan_proto;
10779579220SJakub Kicinski 
10879579220SJakub Kicinski 	return 0;
10979579220SJakub Kicinski }
11079579220SJakub Kicinski 
nsim_set_vf_rate(struct net_device * dev,int vf,int min,int max)11179579220SJakub Kicinski static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
11279579220SJakub Kicinski {
11379579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1145e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
11579579220SJakub Kicinski 
116160dc373SDmytro Linkin 	if (nsim_esw_mode_is_switchdev(ns->nsim_dev)) {
117160dc373SDmytro Linkin 		pr_err("Not supported in switchdev mode. Please use devlink API.\n");
118160dc373SDmytro Linkin 		return -EOPNOTSUPP;
119160dc373SDmytro Linkin 	}
120160dc373SDmytro Linkin 
1215e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
12279579220SJakub Kicinski 		return -EINVAL;
12379579220SJakub Kicinski 
1245e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].min_tx_rate = min;
1255e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].max_tx_rate = max;
12679579220SJakub Kicinski 
12779579220SJakub Kicinski 	return 0;
12879579220SJakub Kicinski }
12979579220SJakub Kicinski 
nsim_set_vf_spoofchk(struct net_device * dev,int vf,bool val)13079579220SJakub Kicinski static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
13179579220SJakub Kicinski {
13279579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1335e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
13479579220SJakub Kicinski 
1355e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
13679579220SJakub Kicinski 		return -EINVAL;
1375e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].spoofchk_enabled = val;
13879579220SJakub Kicinski 
13979579220SJakub Kicinski 	return 0;
14079579220SJakub Kicinski }
14179579220SJakub Kicinski 
nsim_set_vf_rss_query_en(struct net_device * dev,int vf,bool val)14279579220SJakub Kicinski static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
14379579220SJakub Kicinski {
14479579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1455e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
14679579220SJakub Kicinski 
1475e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
14879579220SJakub Kicinski 		return -EINVAL;
1495e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].rss_query_enabled = val;
15079579220SJakub Kicinski 
15179579220SJakub Kicinski 	return 0;
15279579220SJakub Kicinski }
15379579220SJakub Kicinski 
nsim_set_vf_trust(struct net_device * dev,int vf,bool val)15479579220SJakub Kicinski static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
15579579220SJakub Kicinski {
15679579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1575e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
15879579220SJakub Kicinski 
1595e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
16079579220SJakub Kicinski 		return -EINVAL;
1615e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].trusted = val;
16279579220SJakub Kicinski 
16379579220SJakub Kicinski 	return 0;
16479579220SJakub Kicinski }
16579579220SJakub Kicinski 
16679579220SJakub Kicinski static int
nsim_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivi)16779579220SJakub Kicinski nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
16879579220SJakub Kicinski {
16979579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1705e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
17179579220SJakub Kicinski 
1725e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
17379579220SJakub Kicinski 		return -EINVAL;
17479579220SJakub Kicinski 
17579579220SJakub Kicinski 	ivi->vf = vf;
1765e388f3dSJakub Kicinski 	ivi->linkstate = nsim_dev->vfconfigs[vf].link_state;
1775e388f3dSJakub Kicinski 	ivi->min_tx_rate = nsim_dev->vfconfigs[vf].min_tx_rate;
1785e388f3dSJakub Kicinski 	ivi->max_tx_rate = nsim_dev->vfconfigs[vf].max_tx_rate;
1795e388f3dSJakub Kicinski 	ivi->vlan = nsim_dev->vfconfigs[vf].vlan;
1805e388f3dSJakub Kicinski 	ivi->vlan_proto = nsim_dev->vfconfigs[vf].vlan_proto;
1815e388f3dSJakub Kicinski 	ivi->qos = nsim_dev->vfconfigs[vf].qos;
1825e388f3dSJakub Kicinski 	memcpy(&ivi->mac, nsim_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
1835e388f3dSJakub Kicinski 	ivi->spoofchk = nsim_dev->vfconfigs[vf].spoofchk_enabled;
1845e388f3dSJakub Kicinski 	ivi->trusted = nsim_dev->vfconfigs[vf].trusted;
1855e388f3dSJakub Kicinski 	ivi->rss_query_en = nsim_dev->vfconfigs[vf].rss_query_enabled;
18679579220SJakub Kicinski 
18779579220SJakub Kicinski 	return 0;
18879579220SJakub Kicinski }
18979579220SJakub Kicinski 
nsim_set_vf_link_state(struct net_device * dev,int vf,int state)19079579220SJakub Kicinski static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
19179579220SJakub Kicinski {
19279579220SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
1935e388f3dSJakub Kicinski 	struct nsim_dev *nsim_dev = ns->nsim_dev;
19479579220SJakub Kicinski 
1955e388f3dSJakub Kicinski 	if (vf >= nsim_dev_get_vfs(nsim_dev))
19679579220SJakub Kicinski 		return -EINVAL;
19779579220SJakub Kicinski 
19879579220SJakub Kicinski 	switch (state) {
19979579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_AUTO:
20079579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_ENABLE:
20179579220SJakub Kicinski 	case IFLA_VF_LINK_STATE_DISABLE:
20279579220SJakub Kicinski 		break;
20379579220SJakub Kicinski 	default:
20479579220SJakub Kicinski 		return -EINVAL;
20579579220SJakub Kicinski 	}
20679579220SJakub Kicinski 
2075e388f3dSJakub Kicinski 	nsim_dev->vfconfigs[vf].link_state = state;
20879579220SJakub Kicinski 
20979579220SJakub Kicinski 	return 0;
21079579220SJakub Kicinski }
21179579220SJakub Kicinski 
nsim_taprio_stats(struct tc_taprio_qopt_stats * stats)21235da47feSVladimir Oltean static void nsim_taprio_stats(struct tc_taprio_qopt_stats *stats)
21335da47feSVladimir Oltean {
21435da47feSVladimir Oltean 	stats->window_drops = 0;
21535da47feSVladimir Oltean 	stats->tx_overruns = 0;
21635da47feSVladimir Oltean }
21735da47feSVladimir Oltean 
nsim_setup_tc_taprio(struct net_device * dev,struct tc_taprio_qopt_offload * offload)21835da47feSVladimir Oltean static int nsim_setup_tc_taprio(struct net_device *dev,
21935da47feSVladimir Oltean 				struct tc_taprio_qopt_offload *offload)
22035da47feSVladimir Oltean {
22135da47feSVladimir Oltean 	int err = 0;
22235da47feSVladimir Oltean 
22335da47feSVladimir Oltean 	switch (offload->cmd) {
22435da47feSVladimir Oltean 	case TAPRIO_CMD_REPLACE:
22535da47feSVladimir Oltean 	case TAPRIO_CMD_DESTROY:
22635da47feSVladimir Oltean 		break;
22735da47feSVladimir Oltean 	case TAPRIO_CMD_STATS:
22835da47feSVladimir Oltean 		nsim_taprio_stats(&offload->stats);
22935da47feSVladimir Oltean 		break;
23035da47feSVladimir Oltean 	default:
23135da47feSVladimir Oltean 		err = -EOPNOTSUPP;
23235da47feSVladimir Oltean 	}
23335da47feSVladimir Oltean 
23435da47feSVladimir Oltean 	return err;
23535da47feSVladimir Oltean }
23635da47feSVladimir Oltean 
237955bcb6eSPablo Neira Ayuso static LIST_HEAD(nsim_block_cb_list);
238955bcb6eSPablo Neira Ayuso 
23931d3ad83SJakub Kicinski static int
nsim_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)24031d3ad83SJakub Kicinski nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
24131d3ad83SJakub Kicinski {
2424e95bc26SPablo Neira Ayuso 	struct netdevsim *ns = netdev_priv(dev);
2434e95bc26SPablo Neira Ayuso 
24431d3ad83SJakub Kicinski 	switch (type) {
24535da47feSVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
24635da47feSVladimir Oltean 		return nsim_setup_tc_taprio(dev, type_data);
24731d3ad83SJakub Kicinski 	case TC_SETUP_BLOCK:
248955bcb6eSPablo Neira Ayuso 		return flow_block_cb_setup_simple(type_data,
249955bcb6eSPablo Neira Ayuso 						  &nsim_block_cb_list,
2504e95bc26SPablo Neira Ayuso 						  nsim_setup_tc_block_cb,
2514e95bc26SPablo Neira Ayuso 						  ns, ns, true);
25231d3ad83SJakub Kicinski 	default:
25331d3ad83SJakub Kicinski 		return -EOPNOTSUPP;
25431d3ad83SJakub Kicinski 	}
25531d3ad83SJakub Kicinski }
25631d3ad83SJakub Kicinski 
25731d3ad83SJakub Kicinski static int
nsim_set_features(struct net_device * dev,netdev_features_t features)25831d3ad83SJakub Kicinski nsim_set_features(struct net_device *dev, netdev_features_t features)
25931d3ad83SJakub Kicinski {
26031d3ad83SJakub Kicinski 	struct netdevsim *ns = netdev_priv(dev);
26131d3ad83SJakub Kicinski 
26231d3ad83SJakub Kicinski 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
26331d3ad83SJakub Kicinski 		return nsim_bpf_disable_tc(ns);
26431d3ad83SJakub Kicinski 
26531d3ad83SJakub Kicinski 	return 0;
26631d3ad83SJakub Kicinski }
26731d3ad83SJakub Kicinski 
26883c9e13aSJakub Kicinski static const struct net_device_ops nsim_netdev_ops = {
26983c9e13aSJakub Kicinski 	.ndo_start_xmit		= nsim_start_xmit,
27083c9e13aSJakub Kicinski 	.ndo_set_rx_mode	= nsim_set_rx_mode,
27183c9e13aSJakub Kicinski 	.ndo_set_mac_address	= eth_mac_addr,
27283c9e13aSJakub Kicinski 	.ndo_validate_addr	= eth_validate_addr,
27331d3ad83SJakub Kicinski 	.ndo_change_mtu		= nsim_change_mtu,
27483c9e13aSJakub Kicinski 	.ndo_get_stats64	= nsim_get_stats64,
27579579220SJakub Kicinski 	.ndo_set_vf_mac		= nsim_set_vf_mac,
27679579220SJakub Kicinski 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
27779579220SJakub Kicinski 	.ndo_set_vf_rate	= nsim_set_vf_rate,
27879579220SJakub Kicinski 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
27979579220SJakub Kicinski 	.ndo_set_vf_trust	= nsim_set_vf_trust,
28079579220SJakub Kicinski 	.ndo_get_vf_config	= nsim_get_vf_config,
28179579220SJakub Kicinski 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
28279579220SJakub Kicinski 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
28331d3ad83SJakub Kicinski 	.ndo_setup_tc		= nsim_setup_tc,
28431d3ad83SJakub Kicinski 	.ndo_set_features	= nsim_set_features,
28531d3ad83SJakub Kicinski 	.ndo_bpf		= nsim_bpf,
28683c9e13aSJakub Kicinski };
28783c9e13aSJakub Kicinski 
28892ba1f29SDmytro Linkin static const struct net_device_ops nsim_vf_netdev_ops = {
28992ba1f29SDmytro Linkin 	.ndo_start_xmit		= nsim_start_xmit,
29092ba1f29SDmytro Linkin 	.ndo_set_rx_mode	= nsim_set_rx_mode,
29192ba1f29SDmytro Linkin 	.ndo_set_mac_address	= eth_mac_addr,
29292ba1f29SDmytro Linkin 	.ndo_validate_addr	= eth_validate_addr,
29392ba1f29SDmytro Linkin 	.ndo_change_mtu		= nsim_change_mtu,
29492ba1f29SDmytro Linkin 	.ndo_get_stats64	= nsim_get_stats64,
29592ba1f29SDmytro Linkin 	.ndo_setup_tc		= nsim_setup_tc,
29692ba1f29SDmytro Linkin 	.ndo_set_features	= nsim_set_features,
29792ba1f29SDmytro Linkin };
29892ba1f29SDmytro Linkin 
nsim_setup(struct net_device * dev)29983c9e13aSJakub Kicinski static void nsim_setup(struct net_device *dev)
30083c9e13aSJakub Kicinski {
30183c9e13aSJakub Kicinski 	ether_setup(dev);
30283c9e13aSJakub Kicinski 	eth_hw_addr_random(dev);
30383c9e13aSJakub Kicinski 
30483c9e13aSJakub Kicinski 	dev->tx_queue_len = 0;
30583c9e13aSJakub Kicinski 	dev->flags |= IFF_NOARP;
30683c9e13aSJakub Kicinski 	dev->flags &= ~IFF_MULTICAST;
30783c9e13aSJakub Kicinski 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
30883c9e13aSJakub Kicinski 			   IFF_NO_QUEUE;
30983c9e13aSJakub Kicinski 	dev->features |= NETIF_F_HIGHDMA |
31083c9e13aSJakub Kicinski 			 NETIF_F_SG |
31183c9e13aSJakub Kicinski 			 NETIF_F_FRAGLIST |
31283c9e13aSJakub Kicinski 			 NETIF_F_HW_CSUM |
31383c9e13aSJakub Kicinski 			 NETIF_F_TSO;
31431d3ad83SJakub Kicinski 	dev->hw_features |= NETIF_F_HW_TC;
31583c9e13aSJakub Kicinski 	dev->max_mtu = ETH_MAX_MTU;
31666c0e13aSMarek Majtyka 	dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
31783c9e13aSJakub Kicinski }
31883c9e13aSJakub Kicinski 
nsim_init_netdevsim(struct netdevsim * ns)31992ba1f29SDmytro Linkin static int nsim_init_netdevsim(struct netdevsim *ns)
32092ba1f29SDmytro Linkin {
321b63e78fcSVladimir Oltean 	struct mock_phc *phc;
32292ba1f29SDmytro Linkin 	int err;
32392ba1f29SDmytro Linkin 
324b63e78fcSVladimir Oltean 	phc = mock_phc_create(&ns->nsim_bus_dev->dev);
325b63e78fcSVladimir Oltean 	if (IS_ERR(phc))
326b63e78fcSVladimir Oltean 		return PTR_ERR(phc);
327b63e78fcSVladimir Oltean 
328b63e78fcSVladimir Oltean 	ns->phc = phc;
32992ba1f29SDmytro Linkin 	ns->netdev->netdev_ops = &nsim_netdev_ops;
33092ba1f29SDmytro Linkin 
33192ba1f29SDmytro Linkin 	err = nsim_udp_tunnels_info_create(ns->nsim_dev, ns->netdev);
33292ba1f29SDmytro Linkin 	if (err)
333b63e78fcSVladimir Oltean 		goto err_phc_destroy;
33492ba1f29SDmytro Linkin 
33592ba1f29SDmytro Linkin 	rtnl_lock();
33692ba1f29SDmytro Linkin 	err = nsim_bpf_init(ns);
33792ba1f29SDmytro Linkin 	if (err)
33892ba1f29SDmytro Linkin 		goto err_utn_destroy;
33992ba1f29SDmytro Linkin 
34002b34d03SSabrina Dubroca 	nsim_macsec_init(ns);
34192ba1f29SDmytro Linkin 	nsim_ipsec_init(ns);
34292ba1f29SDmytro Linkin 
34392ba1f29SDmytro Linkin 	err = register_netdevice(ns->netdev);
34492ba1f29SDmytro Linkin 	if (err)
34592ba1f29SDmytro Linkin 		goto err_ipsec_teardown;
34692ba1f29SDmytro Linkin 	rtnl_unlock();
34792ba1f29SDmytro Linkin 	return 0;
34892ba1f29SDmytro Linkin 
34992ba1f29SDmytro Linkin err_ipsec_teardown:
35092ba1f29SDmytro Linkin 	nsim_ipsec_teardown(ns);
35102b34d03SSabrina Dubroca 	nsim_macsec_teardown(ns);
35292ba1f29SDmytro Linkin 	nsim_bpf_uninit(ns);
35392ba1f29SDmytro Linkin err_utn_destroy:
35492ba1f29SDmytro Linkin 	rtnl_unlock();
35592ba1f29SDmytro Linkin 	nsim_udp_tunnels_info_destroy(ns->netdev);
356b63e78fcSVladimir Oltean err_phc_destroy:
357b63e78fcSVladimir Oltean 	mock_phc_destroy(ns->phc);
35892ba1f29SDmytro Linkin 	return err;
35992ba1f29SDmytro Linkin }
36092ba1f29SDmytro Linkin 
nsim_init_netdevsim_vf(struct netdevsim * ns)36192ba1f29SDmytro Linkin static int nsim_init_netdevsim_vf(struct netdevsim *ns)
36292ba1f29SDmytro Linkin {
36392ba1f29SDmytro Linkin 	int err;
36492ba1f29SDmytro Linkin 
36592ba1f29SDmytro Linkin 	ns->netdev->netdev_ops = &nsim_vf_netdev_ops;
36692ba1f29SDmytro Linkin 	rtnl_lock();
36792ba1f29SDmytro Linkin 	err = register_netdevice(ns->netdev);
36892ba1f29SDmytro Linkin 	rtnl_unlock();
36992ba1f29SDmytro Linkin 	return err;
37092ba1f29SDmytro Linkin }
37192ba1f29SDmytro Linkin 
nsim_exit_netdevsim(struct netdevsim * ns)372*08aca659SJakub Kicinski static void nsim_exit_netdevsim(struct netdevsim *ns)
373*08aca659SJakub Kicinski {
374*08aca659SJakub Kicinski 	nsim_udp_tunnels_info_destroy(ns->netdev);
375*08aca659SJakub Kicinski 	mock_phc_destroy(ns->phc);
376*08aca659SJakub Kicinski }
377*08aca659SJakub Kicinski 
378e05b2d14SJiri Pirko struct netdevsim *
nsim_create(struct nsim_dev * nsim_dev,struct nsim_dev_port * nsim_dev_port)379e05b2d14SJiri Pirko nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
38083c9e13aSJakub Kicinski {
381e05b2d14SJiri Pirko 	struct net_device *dev;
382e05b2d14SJiri Pirko 	struct netdevsim *ns;
383af9095f0SJiri Pirko 	int err;
384eeeaaf18SJakub Kicinski 
385d4861fc6SPeilin Ye 	dev = alloc_netdev_mq(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup,
386d4861fc6SPeilin Ye 			      nsim_dev->nsim_bus_dev->num_queues);
387e05b2d14SJiri Pirko 	if (!dev)
388e05b2d14SJiri Pirko 		return ERR_PTR(-ENOMEM);
389e05b2d14SJiri Pirko 
39090d29913SJiri Pirko 	dev_net_set(dev, nsim_dev_net(nsim_dev));
391e05b2d14SJiri Pirko 	ns = netdev_priv(dev);
3928320d145SJiri Pirko 	ns->netdev = dev;
393863a42b2SHillf Danton 	u64_stats_init(&ns->syncp);
394e05b2d14SJiri Pirko 	ns->nsim_dev = nsim_dev;
395e05b2d14SJiri Pirko 	ns->nsim_dev_port = nsim_dev_port;
396e05b2d14SJiri Pirko 	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
39740e4fe4cSJiri Pirko 	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
398ac73d4bfSJiri Pirko 	SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port);
399ff1f7c17SJakub Kicinski 	nsim_ethtool_init(ns);
40092ba1f29SDmytro Linkin 	if (nsim_dev_port_is_pf(nsim_dev_port))
40192ba1f29SDmytro Linkin 		err = nsim_init_netdevsim(ns);
40292ba1f29SDmytro Linkin 	else
40392ba1f29SDmytro Linkin 		err = nsim_init_netdevsim_vf(ns);
404424be63aSJakub Kicinski 	if (err)
405424be63aSJakub Kicinski 		goto err_free_netdev;
406e05b2d14SJiri Pirko 	return ns;
407e05b2d14SJiri Pirko 
408e05b2d14SJiri Pirko err_free_netdev:
409e05b2d14SJiri Pirko 	free_netdev(dev);
410e05b2d14SJiri Pirko 	return ERR_PTR(err);
411e05b2d14SJiri Pirko }
412e05b2d14SJiri Pirko 
nsim_destroy(struct netdevsim * ns)413e05b2d14SJiri Pirko void nsim_destroy(struct netdevsim *ns)
414e05b2d14SJiri Pirko {
415e05b2d14SJiri Pirko 	struct net_device *dev = ns->netdev;
416e05b2d14SJiri Pirko 
417e05b2d14SJiri Pirko 	rtnl_lock();
418e05b2d14SJiri Pirko 	unregister_netdevice(dev);
41992ba1f29SDmytro Linkin 	if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
42002b34d03SSabrina Dubroca 		nsim_macsec_teardown(ns);
421e05b2d14SJiri Pirko 		nsim_ipsec_teardown(ns);
422e05b2d14SJiri Pirko 		nsim_bpf_uninit(ns);
42392ba1f29SDmytro Linkin 	}
424e05b2d14SJiri Pirko 	rtnl_unlock();
42592ba1f29SDmytro Linkin 	if (nsim_dev_port_is_pf(ns->nsim_dev_port))
426*08aca659SJakub Kicinski 		nsim_exit_netdevsim(ns);
427e05b2d14SJiri Pirko 	free_netdev(dev);
428e05b2d14SJiri Pirko }
429e05b2d14SJiri Pirko 
nsim_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)430e05b2d14SJiri Pirko static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
431e05b2d14SJiri Pirko 			 struct netlink_ext_ack *extack)
432e05b2d14SJiri Pirko {
433d4861fc6SPeilin Ye 	NL_SET_ERR_MSG_MOD(extack,
434d4861fc6SPeilin Ye 			   "Please use: echo \"[ID] [PORT_COUNT] [NUM_QUEUES]\" > /sys/bus/netdevsim/new_device");
435e05b2d14SJiri Pirko 	return -EOPNOTSUPP;
436eeeaaf18SJakub Kicinski }
437eeeaaf18SJakub Kicinski 
43883c9e13aSJakub Kicinski static struct rtnl_link_ops nsim_link_ops __read_mostly = {
43983c9e13aSJakub Kicinski 	.kind		= DRV_NAME,
44083c9e13aSJakub Kicinski 	.validate	= nsim_validate,
44183c9e13aSJakub Kicinski };
44283c9e13aSJakub Kicinski 
nsim_module_init(void)44383c9e13aSJakub Kicinski static int __init nsim_module_init(void)
44483c9e13aSJakub Kicinski {
44531d3ad83SJakub Kicinski 	int err;
44631d3ad83SJakub Kicinski 
447d514f41eSJiri Pirko 	err = nsim_dev_init();
448af9095f0SJiri Pirko 	if (err)
449ab1d0cc0SJiri Pirko 		return err;
450eeeaaf18SJakub Kicinski 
451925f5afeSJiri Pirko 	err = nsim_bus_init();
45231d3ad83SJakub Kicinski 	if (err)
453d514f41eSJiri Pirko 		goto err_dev_exit;
45431d3ad83SJakub Kicinski 
455a5facc4cSJiri Pirko 	err = rtnl_link_register(&nsim_link_ops);
45679579220SJakub Kicinski 	if (err)
457925f5afeSJiri Pirko 		goto err_bus_exit;
45879579220SJakub Kicinski 
45931d3ad83SJakub Kicinski 	return 0;
46031d3ad83SJakub Kicinski 
461925f5afeSJiri Pirko err_bus_exit:
462925f5afeSJiri Pirko 	nsim_bus_exit();
463d514f41eSJiri Pirko err_dev_exit:
464d514f41eSJiri Pirko 	nsim_dev_exit();
46531d3ad83SJakub Kicinski 	return err;
46683c9e13aSJakub Kicinski }
46783c9e13aSJakub Kicinski 
nsim_module_exit(void)46883c9e13aSJakub Kicinski static void __exit nsim_module_exit(void)
46983c9e13aSJakub Kicinski {
47083c9e13aSJakub Kicinski 	rtnl_link_unregister(&nsim_link_ops);
471925f5afeSJiri Pirko 	nsim_bus_exit();
472d514f41eSJiri Pirko 	nsim_dev_exit();
47383c9e13aSJakub Kicinski }
47483c9e13aSJakub Kicinski 
47583c9e13aSJakub Kicinski module_init(nsim_module_init);
47683c9e13aSJakub Kicinski module_exit(nsim_module_exit);
47783c9e13aSJakub Kicinski MODULE_LICENSE("GPL");
47883c9e13aSJakub Kicinski MODULE_ALIAS_RTNL_LINK(DRV_NAME);
479