Skip to content

Commit 83c9e13

Browse files
Jakub Kicinskiborkmann
authored andcommitted
netdevsim: add software driver for testing offloads
To be able to run selftests without any hardware required we need a software model. The model can also serve as an example implementation for those implementing actual HW offloads. The dummy driver have previously been extended to test SR-IOV, but the general consensus seems to be against adding further features to it. Add a new driver for purposes of software modelling only. eBPF and SR-IOV will be added here shortly, others are invited to further extend the driver with their offload models. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent bd0b2e7 commit 83c9e13

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

MAINTAINERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9599,6 +9599,11 @@ NETWORKING [WIRELESS]
95999599
96009600
Q: http://patchwork.kernel.org/project/linux-wireless/list/
96019601

9602+
NETDEVSIM
9603+
M: Jakub Kicinski <[email protected]>
9604+
S: Maintained
9605+
F: drivers/net/netdevsim/*
9606+
96029607
NETXEN (1/10) GbE SUPPORT
96039608
M: Manish Chopra <[email protected]>
96049609
M: Rahul Verma <[email protected]>

drivers/net/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,15 @@ config THUNDERBOLT_NET
497497

498498
source "drivers/net/hyperv/Kconfig"
499499

500+
config NETDEVSIM
501+
tristate "Simulated networking device"
502+
depends on DEBUG_FS
503+
help
504+
This driver is a developer testing tool and software model that can
505+
be used to test various control path networking APIs, especially
506+
HW-offload related.
507+
508+
To compile this driver as a module, choose M here: the module
509+
will be called netdevsim.
510+
500511
endif # NETDEVICES

drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ obj-$(CONFIG_FUJITSU_ES) += fjes/
7878

7979
thunderbolt-net-y += thunderbolt.o
8080
obj-$(CONFIG_THUNDERBOLT_NET) += thunderbolt-net.o
81+
obj-$(CONFIG_NETDEVSIM) += netdevsim/

drivers/net/netdevsim/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
obj-$(CONFIG_NETDEVSIM) += netdevsim.o
4+
5+
netdevsim-objs := \
6+
netdev.o \

drivers/net/netdevsim/netdev.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) 2017 Netronome Systems, Inc.
3+
*
4+
* This software is licensed under the GNU General License Version 2,
5+
* June 1991 as shown in the file COPYING in the top-level directory of this
6+
* source tree.
7+
*
8+
* THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9+
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10+
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11+
* FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12+
* OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13+
* THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
14+
*/
15+
16+
#include <linux/etherdevice.h>
17+
#include <linux/kernel.h>
18+
#include <linux/module.h>
19+
#include <linux/netdevice.h>
20+
#include <linux/slab.h>
21+
#include <net/netlink.h>
22+
#include <net/rtnetlink.h>
23+
24+
#include "netdevsim.h"
25+
26+
static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
27+
{
28+
struct netdevsim *ns = netdev_priv(dev);
29+
30+
u64_stats_update_begin(&ns->syncp);
31+
ns->tx_packets++;
32+
ns->tx_bytes += skb->len;
33+
u64_stats_update_end(&ns->syncp);
34+
35+
dev_kfree_skb(skb);
36+
37+
return NETDEV_TX_OK;
38+
}
39+
40+
static void nsim_set_rx_mode(struct net_device *dev)
41+
{
42+
}
43+
44+
static void
45+
nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
46+
{
47+
struct netdevsim *ns = netdev_priv(dev);
48+
unsigned int start;
49+
50+
do {
51+
start = u64_stats_fetch_begin(&ns->syncp);
52+
stats->tx_bytes = ns->tx_bytes;
53+
stats->tx_packets = ns->tx_packets;
54+
} while (u64_stats_fetch_retry(&ns->syncp, start));
55+
}
56+
57+
static const struct net_device_ops nsim_netdev_ops = {
58+
.ndo_start_xmit = nsim_start_xmit,
59+
.ndo_set_rx_mode = nsim_set_rx_mode,
60+
.ndo_set_mac_address = eth_mac_addr,
61+
.ndo_validate_addr = eth_validate_addr,
62+
.ndo_get_stats64 = nsim_get_stats64,
63+
};
64+
65+
static void nsim_setup(struct net_device *dev)
66+
{
67+
ether_setup(dev);
68+
eth_hw_addr_random(dev);
69+
70+
dev->netdev_ops = &nsim_netdev_ops;
71+
dev->needs_free_netdev = true;
72+
73+
dev->tx_queue_len = 0;
74+
dev->flags |= IFF_NOARP;
75+
dev->flags &= ~IFF_MULTICAST;
76+
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
77+
IFF_NO_QUEUE;
78+
dev->features |= NETIF_F_HIGHDMA |
79+
NETIF_F_SG |
80+
NETIF_F_FRAGLIST |
81+
NETIF_F_HW_CSUM |
82+
NETIF_F_TSO;
83+
dev->max_mtu = ETH_MAX_MTU;
84+
}
85+
86+
static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
87+
struct netlink_ext_ack *extack)
88+
{
89+
if (tb[IFLA_ADDRESS]) {
90+
if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
91+
return -EINVAL;
92+
if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
93+
return -EADDRNOTAVAIL;
94+
}
95+
return 0;
96+
}
97+
98+
static struct rtnl_link_ops nsim_link_ops __read_mostly = {
99+
.kind = DRV_NAME,
100+
.priv_size = sizeof(struct netdevsim),
101+
.setup = nsim_setup,
102+
.validate = nsim_validate,
103+
};
104+
105+
static int __init nsim_module_init(void)
106+
{
107+
return rtnl_link_register(&nsim_link_ops);
108+
}
109+
110+
static void __exit nsim_module_exit(void)
111+
{
112+
rtnl_link_unregister(&nsim_link_ops);
113+
}
114+
115+
module_init(nsim_module_init);
116+
module_exit(nsim_module_exit);
117+
MODULE_LICENSE("GPL");
118+
MODULE_ALIAS_RTNL_LINK(DRV_NAME);

drivers/net/netdevsim/netdevsim.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2017 Netronome Systems, Inc.
3+
*
4+
* This software is licensed under the GNU General License Version 2,
5+
* June 1991 as shown in the file COPYING in the top-level directory of this
6+
* source tree.
7+
*
8+
* THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9+
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10+
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11+
* FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12+
* OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13+
* THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
14+
*/
15+
16+
#include <linux/kernel.h>
17+
#include <linux/netdevice.h>
18+
#include <linux/u64_stats_sync.h>
19+
20+
#define DRV_NAME "netdevsim"
21+
22+
struct netdevsim {
23+
u64 tx_packets;
24+
u64 tx_bytes;
25+
struct u64_stats_sync syncp;
26+
};

0 commit comments

Comments
 (0)