blob: 7178468302c8f18c726321d8a91206c63f351330 [file] [log] [blame]
Jakub Kicinski83c9e132017-12-01 15:08:58 -08001/*
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 Kicinski31d3ad82017-12-01 15:08:59 -080016#include <linux/debugfs.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080017#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 Kicinski31d3ad82017-12-01 15:08:59 -080023#include <net/pkt_cls.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080024#include <net/rtnetlink.h>
Jakub Kicinski424be632020-07-09 17:42:48 -070025#include <net/udp_tunnel.h>
Jakub Kicinski83c9e132017-12-01 15:08:58 -080026
27#include "netdevsim.h"
28
29static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
30{
31 struct netdevsim *ns = netdev_priv(dev);
32
Shannon Nelson76993532018-06-26 10:07:54 -070033 if (!nsim_ipsec_tx(ns, skb))
34 goto out;
35
Jakub Kicinski83c9e132017-12-01 15:08:58 -080036 u64_stats_update_begin(&ns->syncp);
37 ns->tx_packets++;
38 ns->tx_bytes += skb->len;
39 u64_stats_update_end(&ns->syncp);
40
Shannon Nelson76993532018-06-26 10:07:54 -070041out:
Jakub Kicinski83c9e132017-12-01 15:08:58 -080042 dev_kfree_skb(skb);
43
44 return NETDEV_TX_OK;
45}
46
47static void nsim_set_rx_mode(struct net_device *dev)
48{
49}
50
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080051static int nsim_change_mtu(struct net_device *dev, int new_mtu)
52{
53 struct netdevsim *ns = netdev_priv(dev);
54
Jakub Kicinski799e1732018-07-11 20:36:42 -070055 if (ns->xdp.prog && new_mtu > NSIM_XDP_MAX_MTU)
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080056 return -EBUSY;
57
58 dev->mtu = new_mtu;
59
60 return 0;
61}
62
Jakub Kicinski83c9e132017-12-01 15:08:58 -080063static void
64nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
65{
66 struct netdevsim *ns = netdev_priv(dev);
67 unsigned int start;
68
69 do {
70 start = u64_stats_fetch_begin(&ns->syncp);
71 stats->tx_bytes = ns->tx_bytes;
72 stats->tx_packets = ns->tx_packets;
73 } while (u64_stats_fetch_retry(&ns->syncp, start));
74}
75
Jakub Kicinski31d3ad82017-12-01 15:08:59 -080076static int
77nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
78{
79 return nsim_bpf_setup_tc_block_cb(type, type_data, cb_priv);
80}
81
Jakub Kicinski79579222017-12-01 15:09:01 -080082static int nsim_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
83{
84 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +020085 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -080086
87 /* Only refuse multicast addresses, zero address can mean unset/any. */
Jiri Pirko40e4fe42019-04-25 15:59:45 +020088 if (vf >= nsim_bus_dev->num_vfs || is_multicast_ether_addr(mac))
Jakub Kicinski79579222017-12-01 15:09:01 -080089 return -EINVAL;
Jiri Pirko40e4fe42019-04-25 15:59:45 +020090 memcpy(nsim_bus_dev->vfconfigs[vf].vf_mac, mac, ETH_ALEN);
Jakub Kicinski79579222017-12-01 15:09:01 -080091
92 return 0;
93}
94
95static int nsim_set_vf_vlan(struct net_device *dev, int vf,
96 u16 vlan, u8 qos, __be16 vlan_proto)
97{
98 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +020099 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800100
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200101 if (vf >= nsim_bus_dev->num_vfs || vlan > 4095 || qos > 7)
Jakub Kicinski79579222017-12-01 15:09:01 -0800102 return -EINVAL;
103
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200104 nsim_bus_dev->vfconfigs[vf].vlan = vlan;
105 nsim_bus_dev->vfconfigs[vf].qos = qos;
106 nsim_bus_dev->vfconfigs[vf].vlan_proto = vlan_proto;
Jakub Kicinski79579222017-12-01 15:09:01 -0800107
108 return 0;
109}
110
111static int nsim_set_vf_rate(struct net_device *dev, int vf, int min, int max)
112{
113 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200114 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800115
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200116 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800117 return -EINVAL;
118
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200119 nsim_bus_dev->vfconfigs[vf].min_tx_rate = min;
120 nsim_bus_dev->vfconfigs[vf].max_tx_rate = max;
Jakub Kicinski79579222017-12-01 15:09:01 -0800121
122 return 0;
123}
124
125static int nsim_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
126{
127 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200128 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800129
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200130 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800131 return -EINVAL;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200132 nsim_bus_dev->vfconfigs[vf].spoofchk_enabled = val;
Jakub Kicinski79579222017-12-01 15:09:01 -0800133
134 return 0;
135}
136
137static int nsim_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
138{
139 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200140 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800141
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200142 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800143 return -EINVAL;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200144 nsim_bus_dev->vfconfigs[vf].rss_query_enabled = val;
Jakub Kicinski79579222017-12-01 15:09:01 -0800145
146 return 0;
147}
148
149static int nsim_set_vf_trust(struct net_device *dev, int vf, bool val)
150{
151 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200152 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800153
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200154 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800155 return -EINVAL;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200156 nsim_bus_dev->vfconfigs[vf].trusted = val;
Jakub Kicinski79579222017-12-01 15:09:01 -0800157
158 return 0;
159}
160
161static int
162nsim_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivi)
163{
164 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200165 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800166
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200167 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800168 return -EINVAL;
169
170 ivi->vf = vf;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200171 ivi->linkstate = nsim_bus_dev->vfconfigs[vf].link_state;
172 ivi->min_tx_rate = nsim_bus_dev->vfconfigs[vf].min_tx_rate;
173 ivi->max_tx_rate = nsim_bus_dev->vfconfigs[vf].max_tx_rate;
174 ivi->vlan = nsim_bus_dev->vfconfigs[vf].vlan;
175 ivi->vlan_proto = nsim_bus_dev->vfconfigs[vf].vlan_proto;
176 ivi->qos = nsim_bus_dev->vfconfigs[vf].qos;
177 memcpy(&ivi->mac, nsim_bus_dev->vfconfigs[vf].vf_mac, ETH_ALEN);
178 ivi->spoofchk = nsim_bus_dev->vfconfigs[vf].spoofchk_enabled;
179 ivi->trusted = nsim_bus_dev->vfconfigs[vf].trusted;
180 ivi->rss_query_en = nsim_bus_dev->vfconfigs[vf].rss_query_enabled;
Jakub Kicinski79579222017-12-01 15:09:01 -0800181
182 return 0;
183}
184
185static int nsim_set_vf_link_state(struct net_device *dev, int vf, int state)
186{
187 struct netdevsim *ns = netdev_priv(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200188 struct nsim_bus_dev *nsim_bus_dev = ns->nsim_bus_dev;
Jakub Kicinski79579222017-12-01 15:09:01 -0800189
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200190 if (vf >= nsim_bus_dev->num_vfs)
Jakub Kicinski79579222017-12-01 15:09:01 -0800191 return -EINVAL;
192
193 switch (state) {
194 case IFLA_VF_LINK_STATE_AUTO:
195 case IFLA_VF_LINK_STATE_ENABLE:
196 case IFLA_VF_LINK_STATE_DISABLE:
197 break;
198 default:
199 return -EINVAL;
200 }
201
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200202 nsim_bus_dev->vfconfigs[vf].link_state = state;
Jakub Kicinski79579222017-12-01 15:09:01 -0800203
204 return 0;
205}
206
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200207static LIST_HEAD(nsim_block_cb_list);
208
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800209static int
210nsim_setup_tc(struct net_device *dev, enum tc_setup_type type, void *type_data)
211{
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200212 struct netdevsim *ns = netdev_priv(dev);
213
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800214 switch (type) {
215 case TC_SETUP_BLOCK:
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200216 return flow_block_cb_setup_simple(type_data,
217 &nsim_block_cb_list,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200218 nsim_setup_tc_block_cb,
219 ns, ns, true);
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800220 default:
221 return -EOPNOTSUPP;
222 }
223}
224
225static int
226nsim_set_features(struct net_device *dev, netdev_features_t features)
227{
228 struct netdevsim *ns = netdev_priv(dev);
229
230 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC))
231 return nsim_bpf_disable_tc(ns);
232
233 return 0;
234}
235
Jiri Pirkoa62fdbb2019-04-25 15:59:56 +0200236static struct devlink_port *nsim_get_devlink_port(struct net_device *dev)
237{
238 struct netdevsim *ns = netdev_priv(dev);
239
240 return &ns->nsim_dev_port->devlink_port;
241}
242
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800243static const struct net_device_ops nsim_netdev_ops = {
244 .ndo_start_xmit = nsim_start_xmit,
245 .ndo_set_rx_mode = nsim_set_rx_mode,
246 .ndo_set_mac_address = eth_mac_addr,
247 .ndo_validate_addr = eth_validate_addr,
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800248 .ndo_change_mtu = nsim_change_mtu,
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800249 .ndo_get_stats64 = nsim_get_stats64,
Jakub Kicinski79579222017-12-01 15:09:01 -0800250 .ndo_set_vf_mac = nsim_set_vf_mac,
251 .ndo_set_vf_vlan = nsim_set_vf_vlan,
252 .ndo_set_vf_rate = nsim_set_vf_rate,
253 .ndo_set_vf_spoofchk = nsim_set_vf_spoofchk,
254 .ndo_set_vf_trust = nsim_set_vf_trust,
255 .ndo_get_vf_config = nsim_get_vf_config,
256 .ndo_set_vf_link_state = nsim_set_vf_link_state,
257 .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en,
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800258 .ndo_setup_tc = nsim_setup_tc,
259 .ndo_set_features = nsim_set_features,
260 .ndo_bpf = nsim_bpf,
Jakub Kicinski424be632020-07-09 17:42:48 -0700261 .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
262 .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
Jiri Pirkoa62fdbb2019-04-25 15:59:56 +0200263 .ndo_get_devlink_port = nsim_get_devlink_port,
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800264};
265
266static void nsim_setup(struct net_device *dev)
267{
268 ether_setup(dev);
269 eth_hw_addr_random(dev);
270
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800271 dev->tx_queue_len = 0;
272 dev->flags |= IFF_NOARP;
273 dev->flags &= ~IFF_MULTICAST;
274 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
275 IFF_NO_QUEUE;
276 dev->features |= NETIF_F_HIGHDMA |
277 NETIF_F_SG |
278 NETIF_F_FRAGLIST |
279 NETIF_F_HW_CSUM |
280 NETIF_F_TSO;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800281 dev->hw_features |= NETIF_F_HW_TC;
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800282 dev->max_mtu = ETH_MAX_MTU;
283}
284
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200285struct netdevsim *
286nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800287{
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200288 struct net_device *dev;
289 struct netdevsim *ns;
Jiri Pirkoaf9095f2019-04-12 14:49:26 +0200290 int err;
Jakub Kicinskieeeaaf12018-07-17 10:53:20 -0700291
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200292 dev = alloc_netdev(sizeof(*ns), "eth%d", NET_NAME_UNKNOWN, nsim_setup);
293 if (!dev)
294 return ERR_PTR(-ENOMEM);
295
Jiri Pirko90d29912019-10-03 11:49:37 +0200296 dev_net_set(dev, nsim_dev_net(nsim_dev));
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200297 ns = netdev_priv(dev);
Jiri Pirko8320d142019-04-25 15:59:53 +0200298 ns->netdev = dev;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200299 ns->nsim_dev = nsim_dev;
300 ns->nsim_dev_port = nsim_dev_port;
301 ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200302 SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200303 dev->netdev_ops = &nsim_netdev_ops;
Jakub Kicinskiff1f7c12020-09-14 17:11:54 -0700304 nsim_ethtool_init(ns);
Jiri Pirko5fc49422019-04-25 15:59:42 +0200305
Jakub Kicinski424be632020-07-09 17:42:48 -0700306 err = nsim_udp_tunnels_info_create(nsim_dev, dev);
307 if (err)
308 goto err_free_netdev;
309
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200310 rtnl_lock();
311 err = nsim_bpf_init(ns);
312 if (err)
Jakub Kicinski424be632020-07-09 17:42:48 -0700313 goto err_utn_destroy;
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200314
315 nsim_ipsec_init(ns);
Jiri Pirko1daf36c2019-04-25 15:59:41 +0200316
317 err = register_netdevice(dev);
318 if (err)
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200319 goto err_ipsec_teardown;
320 rtnl_unlock();
Jiri Pirkoaf9095f2019-04-12 14:49:26 +0200321
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200322 return ns;
323
324err_ipsec_teardown:
325 nsim_ipsec_teardown(ns);
326 nsim_bpf_uninit(ns);
Jakub Kicinski424be632020-07-09 17:42:48 -0700327err_utn_destroy:
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200328 rtnl_unlock();
Jakub Kicinski424be632020-07-09 17:42:48 -0700329 nsim_udp_tunnels_info_destroy(dev);
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200330err_free_netdev:
331 free_netdev(dev);
332 return ERR_PTR(err);
333}
334
335void nsim_destroy(struct netdevsim *ns)
336{
337 struct net_device *dev = ns->netdev;
338
339 rtnl_lock();
340 unregister_netdevice(dev);
341 nsim_ipsec_teardown(ns);
342 nsim_bpf_uninit(ns);
343 rtnl_unlock();
Jakub Kicinski424be632020-07-09 17:42:48 -0700344 nsim_udp_tunnels_info_destroy(dev);
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200345 free_netdev(dev);
346}
347
348static int nsim_validate(struct nlattr *tb[], struct nlattr *data[],
349 struct netlink_ext_ack *extack)
350{
351 NL_SET_ERR_MSG_MOD(extack, "Please use: echo \"[ID] [PORT_COUNT]\" > /sys/bus/netdevsim/new_device");
352 return -EOPNOTSUPP;
Jakub Kicinskieeeaaf12018-07-17 10:53:20 -0700353}
354
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800355static struct rtnl_link_ops nsim_link_ops __read_mostly = {
356 .kind = DRV_NAME,
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800357 .validate = nsim_validate,
358};
359
360static int __init nsim_module_init(void)
361{
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800362 int err;
363
Jiri Pirkod514f412019-04-25 15:59:50 +0200364 err = nsim_dev_init();
Jiri Pirkoaf9095f2019-04-12 14:49:26 +0200365 if (err)
Jiri Pirkoab1d0cc2019-04-25 15:59:52 +0200366 return err;
Jakub Kicinskieeeaaf12018-07-17 10:53:20 -0700367
Jiri Pirko925f5af2019-04-25 15:59:44 +0200368 err = nsim_bus_init();
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800369 if (err)
Jiri Pirkod514f412019-04-25 15:59:50 +0200370 goto err_dev_exit;
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800371
Jiri Pirkoa5facc42019-10-03 11:49:26 +0200372 err = rtnl_link_register(&nsim_link_ops);
Jakub Kicinski79579222017-12-01 15:09:01 -0800373 if (err)
Jiri Pirko925f5af2019-04-25 15:59:44 +0200374 goto err_bus_exit;
Jakub Kicinski79579222017-12-01 15:09:01 -0800375
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800376 return 0;
377
Jiri Pirko925f5af2019-04-25 15:59:44 +0200378err_bus_exit:
379 nsim_bus_exit();
Jiri Pirkod514f412019-04-25 15:59:50 +0200380err_dev_exit:
381 nsim_dev_exit();
Jakub Kicinski31d3ad82017-12-01 15:08:59 -0800382 return err;
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800383}
384
385static void __exit nsim_module_exit(void)
386{
387 rtnl_link_unregister(&nsim_link_ops);
Jiri Pirko925f5af2019-04-25 15:59:44 +0200388 nsim_bus_exit();
Jiri Pirkod514f412019-04-25 15:59:50 +0200389 nsim_dev_exit();
Jakub Kicinski83c9e132017-12-01 15:08:58 -0800390}
391
392module_init(nsim_module_init);
393module_exit(nsim_module_exit);
394MODULE_LICENSE("GPL");
395MODULE_ALIAS_RTNL_LINK(DRV_NAME);