| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 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 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 16 | #include <linux/debugfs.h> | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 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> | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 23 | #include <net/pkt_cls.h> | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 24 | #include <net/rtnetlink.h> | 
 | 25 |  | 
 | 26 | #include "netdevsim.h" | 
 | 27 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 28 | struct 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 |  | 
 | 41 | static u32 nsim_dev_id; | 
 | 42 |  | 
 | 43 | static int nsim_num_vf(struct device *dev) | 
 | 44 | { | 
 | 45 | 	struct netdevsim *ns = to_nsim(dev); | 
 | 46 |  | 
 | 47 | 	return ns->num_vfs; | 
 | 48 | } | 
 | 49 |  | 
 | 50 | static struct bus_type nsim_bus = { | 
 | 51 | 	.name		= DRV_NAME, | 
 | 52 | 	.dev_name	= DRV_NAME, | 
 | 53 | 	.num_vf		= nsim_num_vf, | 
 | 54 | }; | 
 | 55 |  | 
 | 56 | static 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 |  | 
 | 67 | static void nsim_vfs_disable(struct netdevsim *ns) | 
 | 68 | { | 
 | 69 | 	kfree(ns->vfconfigs); | 
 | 70 | 	ns->vfconfigs = NULL; | 
 | 71 | 	ns->num_vfs = 0; | 
 | 72 | } | 
 | 73 |  | 
 | 74 | static ssize_t | 
 | 75 | nsim_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 | 	} | 
 | 101 | exit_good: | 
 | 102 | 	ret = count; | 
 | 103 | exit_unlock: | 
 | 104 | 	rtnl_unlock(); | 
 | 105 |  | 
 | 106 | 	return ret; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | static ssize_t | 
 | 110 | nsim_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 |  | 
 | 117 | static struct device_attribute nsim_numvfs_attr = | 
 | 118 | 	__ATTR(sriov_numvfs, 0664, nsim_numvfs_show, nsim_numvfs_store); | 
 | 119 |  | 
 | 120 | static struct attribute *nsim_dev_attrs[] = { | 
 | 121 | 	&nsim_numvfs_attr.attr, | 
 | 122 | 	NULL, | 
 | 123 | }; | 
 | 124 |  | 
 | 125 | static const struct attribute_group nsim_dev_attr_group = { | 
 | 126 | 	.attrs = nsim_dev_attrs, | 
 | 127 | }; | 
 | 128 |  | 
 | 129 | static const struct attribute_group *nsim_dev_attr_groups[] = { | 
 | 130 | 	&nsim_dev_attr_group, | 
 | 131 | 	NULL, | 
 | 132 | }; | 
 | 133 |  | 
 | 134 | static 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 |  | 
| Jakub Kicinski | fd5ebbc | 2017-12-19 12:10:42 -0800 | [diff] [blame^] | 142 | static struct device_type nsim_dev_type = { | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 143 | 	.groups = nsim_dev_attr_groups, | 
 | 144 | 	.release = nsim_dev_release, | 
 | 145 | }; | 
 | 146 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 147 | static 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 |  | 
 | 155 | 	err = nsim_bpf_init(ns); | 
 | 156 | 	if (err) | 
 | 157 | 		goto err_debugfs_destroy; | 
 | 158 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 159 | 	ns->dev.id = nsim_dev_id++; | 
 | 160 | 	ns->dev.bus = &nsim_bus; | 
 | 161 | 	ns->dev.type = &nsim_dev_type; | 
 | 162 | 	err = device_register(&ns->dev); | 
 | 163 | 	if (err) | 
 | 164 | 		goto err_bpf_uninit; | 
 | 165 |  | 
 | 166 | 	SET_NETDEV_DEV(dev, &ns->dev); | 
 | 167 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 168 | 	return 0; | 
 | 169 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 170 | err_bpf_uninit: | 
 | 171 | 	nsim_bpf_uninit(ns); | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 172 | err_debugfs_destroy: | 
 | 173 | 	debugfs_remove_recursive(ns->ddir); | 
 | 174 | 	return err; | 
 | 175 | } | 
 | 176 |  | 
 | 177 | static void nsim_uninit(struct net_device *dev) | 
 | 178 | { | 
 | 179 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 180 |  | 
 | 181 | 	debugfs_remove_recursive(ns->ddir); | 
 | 182 | 	nsim_bpf_uninit(ns); | 
 | 183 | } | 
 | 184 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 185 | static void nsim_free(struct net_device *dev) | 
 | 186 | { | 
 | 187 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 188 |  | 
 | 189 | 	device_unregister(&ns->dev); | 
 | 190 | 	/* netdev and vf state will be freed out of device_release() */ | 
 | 191 | } | 
 | 192 |  | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 193 | static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) | 
 | 194 | { | 
 | 195 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 196 |  | 
 | 197 | 	u64_stats_update_begin(&ns->syncp); | 
 | 198 | 	ns->tx_packets++; | 
 | 199 | 	ns->tx_bytes += skb->len; | 
 | 200 | 	u64_stats_update_end(&ns->syncp); | 
 | 201 |  | 
 | 202 | 	dev_kfree_skb(skb); | 
 | 203 |  | 
 | 204 | 	return NETDEV_TX_OK; | 
 | 205 | } | 
 | 206 |  | 
 | 207 | static void nsim_set_rx_mode(struct net_device *dev) | 
 | 208 | { | 
 | 209 | } | 
 | 210 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 211 | static int nsim_change_mtu(struct net_device *dev, int new_mtu) | 
 | 212 | { | 
 | 213 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 214 |  | 
 | 215 | 	if (ns->xdp_prog_mode == XDP_ATTACHED_DRV && | 
 | 216 | 	    new_mtu > NSIM_XDP_MAX_MTU) | 
 | 217 | 		return -EBUSY; | 
 | 218 |  | 
 | 219 | 	dev->mtu = new_mtu; | 
 | 220 |  | 
 | 221 | 	return 0; | 
 | 222 | } | 
 | 223 |  | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 224 | static void | 
 | 225 | nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) | 
 | 226 | { | 
 | 227 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 228 | 	unsigned int start; | 
 | 229 |  | 
 | 230 | 	do { | 
 | 231 | 		start = u64_stats_fetch_begin(&ns->syncp); | 
 | 232 | 		stats->tx_bytes = ns->tx_bytes; | 
 | 233 | 		stats->tx_packets = ns->tx_packets; | 
 | 234 | 	} while (u64_stats_fetch_retry(&ns->syncp, start)); | 
 | 235 | } | 
 | 236 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 237 | static int | 
 | 238 | nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv) | 
 | 239 | { | 
 | 240 | 	return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv); | 
 | 241 | } | 
 | 242 |  | 
 | 243 | static int | 
 | 244 | nsim_setup_tc_block(struct net_device *dev, struct tc_block_offload *f) | 
 | 245 | { | 
 | 246 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 247 |  | 
 | 248 | 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) | 
 | 249 | 		return -EOPNOTSUPP; | 
 | 250 |  | 
 | 251 | 	switch (f->command) { | 
 | 252 | 	case TC_BLOCK_BIND: | 
 | 253 | 		return tcf_block_cb_register(f->block, nsim_setup_tc_block_cb, | 
 | 254 | 					     ns, ns); | 
 | 255 | 	case TC_BLOCK_UNBIND: | 
 | 256 | 		tcf_block_cb_unregister(f->block, nsim_setup_tc_block_cb, ns); | 
 | 257 | 		return 0; | 
 | 258 | 	default: | 
 | 259 | 		return -EOPNOTSUPP; | 
 | 260 | 	} | 
 | 261 | } | 
 | 262 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 263 | static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac) | 
 | 264 | { | 
 | 265 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 266 |  | 
 | 267 | 	/* Only refuse multicast addresses, zero address can mean unset/any. */ | 
 | 268 | 	if (vf >= ns->num_vfs || is_multicast_ether_addr(mac)) | 
 | 269 | 		return -EINVAL; | 
 | 270 | 	memcpy(ns->vfconfigs[vf].vf_mac, mac, ETH_ALEN); | 
 | 271 |  | 
 | 272 | 	return 0; | 
 | 273 | } | 
 | 274 |  | 
 | 275 | static int nsim_set_vf_vlan(struct net_device *dev, int vf, | 
 | 276 | 			    u16 vlan, u8 qos, __be16 vlan_proto) | 
 | 277 | { | 
 | 278 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 279 |  | 
 | 280 | 	if (vf >= ns->num_vfs || vlan > 4095 || qos > 7) | 
 | 281 | 		return -EINVAL; | 
 | 282 |  | 
 | 283 | 	ns->vfconfigs[vf].vlan = vlan; | 
 | 284 | 	ns->vfconfigs[vf].qos = qos; | 
 | 285 | 	ns->vfconfigs[vf].vlan_proto = vlan_proto; | 
 | 286 |  | 
 | 287 | 	return 0; | 
 | 288 | } | 
 | 289 |  | 
 | 290 | static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max) | 
 | 291 | { | 
 | 292 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 293 |  | 
 | 294 | 	if (vf >= ns->num_vfs) | 
 | 295 | 		return -EINVAL; | 
 | 296 |  | 
 | 297 | 	ns->vfconfigs[vf].min_tx_rate = min; | 
 | 298 | 	ns->vfconfigs[vf].max_tx_rate = max; | 
 | 299 |  | 
 | 300 | 	return 0; | 
 | 301 | } | 
 | 302 |  | 
 | 303 | static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val) | 
 | 304 | { | 
 | 305 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 306 |  | 
 | 307 | 	if (vf >= ns->num_vfs) | 
 | 308 | 		return -EINVAL; | 
 | 309 | 	ns->vfconfigs[vf].spoofchk_enabled = val; | 
 | 310 |  | 
 | 311 | 	return 0; | 
 | 312 | } | 
 | 313 |  | 
 | 314 | static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val) | 
 | 315 | { | 
 | 316 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 317 |  | 
 | 318 | 	if (vf >= ns->num_vfs) | 
 | 319 | 		return -EINVAL; | 
 | 320 | 	ns->vfconfigs[vf].rss_query_enabled = val; | 
 | 321 |  | 
 | 322 | 	return 0; | 
 | 323 | } | 
 | 324 |  | 
 | 325 | static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val) | 
 | 326 | { | 
 | 327 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 328 |  | 
 | 329 | 	if (vf >= ns->num_vfs) | 
 | 330 | 		return -EINVAL; | 
 | 331 | 	ns->vfconfigs[vf].trusted = val; | 
 | 332 |  | 
 | 333 | 	return 0; | 
 | 334 | } | 
 | 335 |  | 
 | 336 | static int | 
 | 337 | nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi) | 
 | 338 | { | 
 | 339 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 340 |  | 
 | 341 | 	if (vf >= ns->num_vfs) | 
 | 342 | 		return -EINVAL; | 
 | 343 |  | 
 | 344 | 	ivi->vf = vf; | 
 | 345 | 	ivi->linkstate = ns->vfconfigs[vf].link_state; | 
 | 346 | 	ivi->min_tx_rate = ns->vfconfigs[vf].min_tx_rate; | 
 | 347 | 	ivi->max_tx_rate = ns->vfconfigs[vf].max_tx_rate; | 
 | 348 | 	ivi->vlan = ns->vfconfigs[vf].vlan; | 
 | 349 | 	ivi->vlan_proto = ns->vfconfigs[vf].vlan_proto; | 
 | 350 | 	ivi->qos = ns->vfconfigs[vf].qos; | 
 | 351 | 	memcpy(&ivi->mac, ns->vfconfigs[vf].vf_mac, ETH_ALEN); | 
 | 352 | 	ivi->spoofchk = ns->vfconfigs[vf].spoofchk_enabled; | 
 | 353 | 	ivi->trusted = ns->vfconfigs[vf].trusted; | 
 | 354 | 	ivi->rss_query_en = ns->vfconfigs[vf].rss_query_enabled; | 
 | 355 |  | 
 | 356 | 	return 0; | 
 | 357 | } | 
 | 358 |  | 
 | 359 | static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state) | 
 | 360 | { | 
 | 361 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 362 |  | 
 | 363 | 	if (vf >= ns->num_vfs) | 
 | 364 | 		return -EINVAL; | 
 | 365 |  | 
 | 366 | 	switch (state) { | 
 | 367 | 	case IFLA_VF_LINK_STATE_AUTO: | 
 | 368 | 	case IFLA_VF_LINK_STATE_ENABLE: | 
 | 369 | 	case IFLA_VF_LINK_STATE_DISABLE: | 
 | 370 | 		break; | 
 | 371 | 	default: | 
 | 372 | 		return -EINVAL; | 
 | 373 | 	} | 
 | 374 |  | 
 | 375 | 	ns->vfconfigs[vf].link_state = state; | 
 | 376 |  | 
 | 377 | 	return 0; | 
 | 378 | } | 
 | 379 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 380 | static int | 
 | 381 | nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data) | 
 | 382 | { | 
 | 383 | 	switch (type) { | 
 | 384 | 	case TC_SETUP_BLOCK: | 
 | 385 | 		return nsim_setup_tc_block(dev, type_data); | 
 | 386 | 	default: | 
 | 387 | 		return -EOPNOTSUPP; | 
 | 388 | 	} | 
 | 389 | } | 
 | 390 |  | 
 | 391 | static int | 
 | 392 | nsim_set_features(struct net_device *dev, netdev_features_t features) | 
 | 393 | { | 
 | 394 | 	struct netdevsim *ns = netdev_priv(dev); | 
 | 395 |  | 
 | 396 | 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC)) | 
 | 397 | 		return nsim_bpf_disable_tc(ns); | 
 | 398 |  | 
 | 399 | 	return 0; | 
 | 400 | } | 
 | 401 |  | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 402 | static const struct net_device_ops nsim_netdev_ops = { | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 403 | 	.ndo_init		= nsim_init, | 
 | 404 | 	.ndo_uninit		= nsim_uninit, | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 405 | 	.ndo_start_xmit		= nsim_start_xmit, | 
 | 406 | 	.ndo_set_rx_mode	= nsim_set_rx_mode, | 
 | 407 | 	.ndo_set_mac_address	= eth_mac_addr, | 
 | 408 | 	.ndo_validate_addr	= eth_validate_addr, | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 409 | 	.ndo_change_mtu		= nsim_change_mtu, | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 410 | 	.ndo_get_stats64	= nsim_get_stats64, | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 411 | 	.ndo_set_vf_mac		= nsim_set_vf_mac, | 
 | 412 | 	.ndo_set_vf_vlan	= nsim_set_vf_vlan, | 
 | 413 | 	.ndo_set_vf_rate	= nsim_set_vf_rate, | 
 | 414 | 	.ndo_set_vf_spoofchk	= nsim_set_vf_spoofchk, | 
 | 415 | 	.ndo_set_vf_trust	= nsim_set_vf_trust, | 
 | 416 | 	.ndo_get_vf_config	= nsim_get_vf_config, | 
 | 417 | 	.ndo_set_vf_link_state	= nsim_set_vf_link_state, | 
 | 418 | 	.ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 419 | 	.ndo_setup_tc		= nsim_setup_tc, | 
 | 420 | 	.ndo_set_features	= nsim_set_features, | 
 | 421 | 	.ndo_bpf		= nsim_bpf, | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 422 | }; | 
 | 423 |  | 
 | 424 | static void nsim_setup(struct net_device *dev) | 
 | 425 | { | 
 | 426 | 	ether_setup(dev); | 
 | 427 | 	eth_hw_addr_random(dev); | 
 | 428 |  | 
 | 429 | 	dev->netdev_ops = &nsim_netdev_ops; | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 430 | 	dev->priv_destructor = nsim_free; | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 431 |  | 
 | 432 | 	dev->tx_queue_len = 0; | 
 | 433 | 	dev->flags |= IFF_NOARP; | 
 | 434 | 	dev->flags &= ~IFF_MULTICAST; | 
 | 435 | 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | | 
 | 436 | 			   IFF_NO_QUEUE; | 
 | 437 | 	dev->features |= NETIF_F_HIGHDMA | | 
 | 438 | 			 NETIF_F_SG | | 
 | 439 | 			 NETIF_F_FRAGLIST | | 
 | 440 | 			 NETIF_F_HW_CSUM | | 
 | 441 | 			 NETIF_F_TSO; | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 442 | 	dev->hw_features |= NETIF_F_HW_TC; | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 443 | 	dev->max_mtu = ETH_MAX_MTU; | 
 | 444 | } | 
 | 445 |  | 
 | 446 | static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], | 
 | 447 | 			 struct netlink_ext_ack *extack) | 
 | 448 | { | 
 | 449 | 	if (tb[IFLA_ADDRESS]) { | 
 | 450 | 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | 
 | 451 | 			return -EINVAL; | 
 | 452 | 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | 
 | 453 | 			return -EADDRNOTAVAIL; | 
 | 454 | 	} | 
 | 455 | 	return 0; | 
 | 456 | } | 
 | 457 |  | 
 | 458 | static struct rtnl_link_ops nsim_link_ops __read_mostly = { | 
 | 459 | 	.kind		= DRV_NAME, | 
 | 460 | 	.priv_size	= sizeof(struct netdevsim), | 
 | 461 | 	.setup		= nsim_setup, | 
 | 462 | 	.validate	= nsim_validate, | 
 | 463 | }; | 
 | 464 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 465 | struct dentry *nsim_ddir; | 
 | 466 |  | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 467 | static int __init nsim_module_init(void) | 
 | 468 | { | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 469 | 	int err; | 
 | 470 |  | 
 | 471 | 	nsim_ddir = debugfs_create_dir(DRV_NAME, NULL); | 
 | 472 | 	if (IS_ERR(nsim_ddir)) | 
 | 473 | 		return PTR_ERR(nsim_ddir); | 
 | 474 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 475 | 	err = bus_register(&nsim_bus); | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 476 | 	if (err) | 
 | 477 | 		goto err_debugfs_destroy; | 
 | 478 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 479 | 	err = rtnl_link_register(&nsim_link_ops); | 
 | 480 | 	if (err) | 
 | 481 | 		goto err_unreg_bus; | 
 | 482 |  | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 483 | 	return 0; | 
 | 484 |  | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 485 | err_unreg_bus: | 
 | 486 | 	bus_unregister(&nsim_bus); | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 487 | err_debugfs_destroy: | 
 | 488 | 	debugfs_remove_recursive(nsim_ddir); | 
 | 489 | 	return err; | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 490 | } | 
 | 491 |  | 
 | 492 | static void __exit nsim_module_exit(void) | 
 | 493 | { | 
 | 494 | 	rtnl_link_unregister(&nsim_link_ops); | 
| Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 495 | 	bus_unregister(&nsim_bus); | 
| Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 496 | 	debugfs_remove_recursive(nsim_ddir); | 
| Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 497 | } | 
 | 498 |  | 
 | 499 | module_init(nsim_module_init); | 
 | 500 | module_exit(nsim_module_exit); | 
 | 501 | MODULE_LICENSE("GPL"); | 
 | 502 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |