Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (C) 2017 Netronome Systems, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This software is licensed under the GNU General License Version 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * June 1991 as shown in the file COPYING in the top-level directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * source tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <net/udp_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "netdevsim.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (!nsim_ipsec_tx(ns, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u64_stats_update_begin(&ns->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	ns->tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	ns->tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u64_stats_update_end(&ns->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static void nsim_set_rx_mode(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int nsim_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	dev->mtu = new_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned int start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		start = u64_stats_fetch_begin(&ns->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		stats->tx_bytes = ns->tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		stats->tx_packets = ns->tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	} while (u64_stats_fetch_retry(&ns->syncp, start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* Only refuse multicast addresses, zero address can mean unset/any. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int nsim_set_vf_vlan(struct net_device *dev, int vf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			    u16 vlan, u8 qos, __be16 vlan_proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	nsim_bus_dev->vfconfigs[vf].vlan = vlan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	nsim_bus_dev->vfconfigs[vf].qos = qos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	nsim_bus_dev->vfconfigs[vf].trusted = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ivi->vf = vf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (vf >= nsim_bus_dev->num_vfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	case IFLA_VF_LINK_STATE_AUTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	case IFLA_VF_LINK_STATE_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	case IFLA_VF_LINK_STATE_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	nsim_bus_dev->vfconfigs[vf].link_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static LIST_HEAD(nsim_block_cb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	case TC_SETUP_BLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return flow_block_cb_setup_simple(type_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 						  &nsim_block_cb_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 						  nsim_setup_tc_block_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 						  ns, ns, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) nsim_set_features(struct net_device *dev, netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return nsim_bpf_disable_tc(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct netdevsim *ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return &ns->nsim_dev_port->devlink_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static const struct net_device_ops nsim_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	.ndo_start_xmit		= nsim_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	.ndo_set_rx_mode	= nsim_set_rx_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	.ndo_set_mac_address	= eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	.ndo_validate_addr	= eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	.ndo_change_mtu		= nsim_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	.ndo_get_stats64	= nsim_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.ndo_set_vf_mac		= nsim_set_vf_mac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.ndo_set_vf_vlan	= nsim_set_vf_vlan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	.ndo_set_vf_rate	= nsim_set_vf_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	.ndo_set_vf_trust	= nsim_set_vf_trust,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.ndo_get_vf_config	= nsim_get_vf_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	.ndo_set_vf_link_state	= nsim_set_vf_link_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	.ndo_setup_tc		= nsim_setup_tc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	.ndo_set_features	= nsim_set_features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	.ndo_bpf		= nsim_bpf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	.ndo_get_devlink_port	= nsim_get_devlink_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static void nsim_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	dev->tx_queue_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	dev->flags |= IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	dev->flags &= ~IFF_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			   IFF_NO_QUEUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	dev->features |= NETIF_F_HIGHDMA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			 NETIF_F_SG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			 NETIF_F_FRAGLIST |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			 NETIF_F_HW_CSUM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			 NETIF_F_TSO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	dev->hw_features |= NETIF_F_HW_TC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	dev->max_mtu = ETH_MAX_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct netdevsim *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct netdevsim *ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	dev_net_set(dev, nsim_dev_net(nsim_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	ns = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ns->netdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	u64_stats_init(&ns->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ns->nsim_dev = nsim_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	ns->nsim_dev_port = nsim_dev_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	dev->netdev_ops = &nsim_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	nsim_ethtool_init(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	err = nsim_udp_tunnels_info_create(nsim_dev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		goto err_free_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	err = nsim_bpf_init(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		goto err_utn_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	nsim_ipsec_init(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		goto err_ipsec_teardown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) err_ipsec_teardown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	nsim_ipsec_teardown(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	nsim_bpf_uninit(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) err_utn_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	nsim_udp_tunnels_info_destroy(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) err_free_netdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) void nsim_destroy(struct netdevsim *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct net_device *dev = ns->netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	nsim_ipsec_teardown(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	nsim_bpf_uninit(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	nsim_udp_tunnels_info_destroy(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static struct rtnl_link_ops nsim_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	.kind		= DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.validate	= nsim_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int __init nsim_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	err = nsim_dev_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	err = nsim_bus_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		goto err_dev_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	err = rtnl_link_register(&nsim_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		goto err_bus_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) err_bus_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	nsim_bus_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) err_dev_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	nsim_dev_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void __exit nsim_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	rtnl_link_unregister(&nsim_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	nsim_bus_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	nsim_dev_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) module_init(nsim_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) module_exit(nsim_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) MODULE_ALIAS_RTNL_LINK(DRV_NAME);