aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/dummy.c
blob: 58483af80bdb99bcced68e8938338e7dc5514f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* dummy.c: a dummy net driver

	The purpose of this driver is to provide a device to point a
	route through, but not to actually transmit packets.

	Why?  If you have a machine whose only connection is an occasional
	PPP/SLIP/PLIP link, you can only connect to your own hostname
	when the link is up.  Otherwise you have to use localhost.
	This isn't very consistent.

	One solution is to set up a dummy link using PPP/SLIP/PLIP,
	but this seems (to me) too much overhead for too little gain.
	This driver provides a small alternative. Thus you can do

	[when not running slip]
		ifconfig dummy slip.addr.ess.here up
	[to go to slip]
		ifconfig dummy down
		dip whatever

	This was written by looking at Donald Becker's skeleton driver
	and the loopback driver.  I then threw away anything that didn't
	apply!	Thanks to Alan Cox for the key clue on what to do with
	misguided packets.

			Nick Holloway, 27th May 1994
	[I tweaked this explanation a little but that's all]
			Alan Cox, 30th May 1994
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/rtnetlink.h>
#include <linux/net_tstamp.h>
#include <net/rtnetlink.h>
#include <linux/u64_stats_sync.h>

#define DRV_NAME	"dummy"
#define DRV_VERSION	"1.0"

#undef pr_fmt
#define pr_fmt(fmt) DRV_NAME ": " fmt

static int numdummies = 1;
static int num_vfs;

struct vf_data_storage {
	u8	vf_mac[ETH_ALEN];
	u16	pf_vlan; /* When set, guest VLAN config not allowed. */
	u16	pf_qos;
	__be16	vlan_proto;
	u16	min_tx_rate;
	u16	max_tx_rate;
	u8	spoofchk_enabled;
	bool	rss_query_enabled;
	u8	trusted;
	int	link_state;
};

struct dummy_priv {
	struct vf_data_storage	*vfinfo;
};

static int dummy_num_vf(struct device *dev)
{
	return num_vfs;
}

static struct bus_type dummy_bus = {
	.name	= "dummy",
	.num_vf	= dummy_num_vf,
};

static void release_dummy_parent(struct device *dev)
{
}

static struct device dummy_parent = {
	.init_name	= "dummy",
	.bus		= &dummy_bus,
	.release	= release_dummy_parent,
};

/* fake multicast ability */
static void set_multicast_list(struct net_device *dev)
{
}

struct pcpu_dstats {
	u64			tx_packets;
	u64			tx_bytes;
	struct u64_stats_sync	syncp;
};

static void dummy_get_stats64(struct net_device *dev,
			      struct rtnl_link_stats64 *stats)
{
	int i;

	for_each_possible_cpu(i) {
		const struct pcpu_dstats *dstats;
		u64 tbytes, tpackets;
		unsigned int start;

		dstats = per_cpu_ptr(dev->dstats, i);
		do {
			start = u64_stats_fetch_begin_irq(&dstats->syncp);
			tbytes = dstats->tx_bytes;
			tpackets = dstats->tx_packets;
		} while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
		stats->tx_bytes += tbytes;
		stats->tx_packets += tpackets;
	}
}

static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);

	u64_stats_update_begin(&dstats->syncp);
	dstats->tx_packets++;
	dstats->tx_bytes += skb->len;
	u64_stats_update_end(&dstats->syncp);

	skb_tx_timestamp(skb);
	dev_kfree_skb(skb);
	return NETDEV_TX_OK;
}

static int dummy_dev_init(struct net_device *dev)
{
	struct dummy_priv *priv = netdev_priv(dev);

	dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
	if (!dev->dstats)
		return -ENOMEM;

	priv->vfinfo = NULL;

	if (!num_vfs)
		return 0;

	dev->dev.parent = &dummy_parent;
	priv->vfinfo = kcalloc(num_vfs, sizeof(struct vf_data_storage),
			       GFP_KERNEL);
	if (!priv->vfinfo) {
		free_percpu(dev->dstats);
		return -ENOMEM;
	}

	return 0;
}

static void dummy_dev_uninit(struct net_device *dev)
{
	free_percpu(dev->dstats);
}

static int dummy_change_carrier(struct net_device *dev, bool new_carrier)
{
	if (new_carrier)
		netif_carrier_on(dev);
	else
		netif_carrier_off(dev);
	return 0;
}

static int dummy_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
		return -EINVAL;

	memcpy(priv->vfinfo[vf].vf_mac, mac, ETH_ALEN);

	return 0;
}

static int dummy_set_vf_vlan(struct net_device *dev, int vf,
			     u16 vlan, u8 qos, __be16 vlan_proto)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if ((vf >= num_vfs) || (vlan > 4095) || (qos > 7))
		return -EINVAL;

	priv->vfinfo[vf].pf_vlan = vlan;
	priv->vfinfo[vf].pf_qos = qos;
	priv->vfinfo[vf].vlan_proto = vlan_proto;

	return 0;
}

static int dummy_set_vf_rate(struct net_device *dev, int vf, int min, int max)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	priv->vfinfo[vf].min_tx_rate = min;
	priv->vfinfo[vf].max_tx_rate = max;

	return 0;
}

static int dummy_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	priv->vfinfo[vf].spoofchk_enabled = val;

	return 0;
}

static int dummy_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	priv->vfinfo[vf].rss_query_enabled = val;

	return 0;
}

static int dummy_set_vf_trust(struct net_device *dev, int vf, bool val)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	priv->vfinfo[vf].trusted = val;

	return 0;
}

static int dummy_get_vf_config(struct net_device *dev,
			       int vf, struct ifla_vf_info *ivi)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	ivi->vf = vf;
	memcpy(&ivi->mac, priv->vfinfo[vf].vf_mac, ETH_ALEN);
	ivi->vlan = priv->vfinfo[vf].pf_vlan;
	ivi->qos = priv->vfinfo[vf].pf_qos;
	ivi->spoofchk = priv->vfinfo[vf].spoofchk_enabled;
	ivi->linkstate = priv->vfinfo[vf].link_state;
	ivi->min_tx_rate = priv->vfinfo[vf].min_tx_rate;
	ivi->max_tx_rate = priv->vfinfo[vf].max_tx_rate;
	ivi->rss_query_en = priv->vfinfo[vf].rss_query_enabled;
	ivi->trusted = priv->vfinfo[vf].trusted;
	ivi->vlan_proto = priv->vfinfo[vf].vlan_proto;

	return 0;
}

static int dummy_set_vf_link_state(struct net_device *dev, int vf, int state)
{
	struct dummy_priv *priv = netdev_priv(dev);

	if (vf >= num_vfs)
		return -EINVAL;

	priv->vfinfo[vf].link_state = state;

	return 0;
}

static const struct net_device_ops dummy_netdev_ops = {
	.ndo_init		= dummy_dev_init,
	.ndo_uninit		= dummy_dev_uninit,
	.ndo_start_xmit		= dummy_xmit,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_rx_mode	= set_multicast_list,
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_get_stats64	= dummy_get_stats64,
	.ndo_change_carrier	= dummy_change_carrier,
	.ndo_set_vf_mac		= dummy_set_vf_mac,
	.ndo_set_vf_vlan	= dummy_set_vf_vlan,
	.ndo_set_vf_rate	= dummy_set_vf_rate,
	.ndo_set_vf_spoofchk	= dummy_set_vf_spoofchk,
	.ndo_set_vf_trust	= dummy_set_vf_trust,
	.ndo_get_vf_config	= dummy_get_vf_config,
	.ndo_set_vf_link_state	= dummy_set_vf_link_state,
	.ndo_set_vf_rss_query_en = dummy_set_vf_rss_query_en,
};

static void dummy_get_drvinfo(struct net_device *dev,
			      struct ethtool_drvinfo *info)
{
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
}

static int dummy_get_ts_info(struct net_device *dev,
			      struct ethtool_ts_info *ts_info)
{
	ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
				   SOF_TIMESTAMPING_RX_SOFTWARE |
				   SOF_TIMESTAMPING_SOFTWARE;

	ts_info->phc_index = -1;

	return 0;
};

static const struct ethtool_ops dummy_ethtool_ops = {
	.get_drvinfo            = dummy_get_drvinfo,
	.get_ts_info		= dummy_get_ts_info,
};

static void dummy_free_netdev(struct net_device *dev)
{
	struct dummy_priv *priv = netdev_priv(dev);

	kfree(priv->vfinfo);
}

static void dummy_setup(struct net_device *dev)
{
	ether_setup(dev);

	/* Initialize the device structure. */
	dev->netdev_ops = &dummy_netdev_ops;
	dev->ethtool_ops = &dummy_ethtool_ops;
	dev->needs_free_netdev = true;
	dev->priv_destructor = dummy_free_netdev;

	/* Fill in device structure with ethernet-generic values. */
	dev->flags |= IFF_NOARP;
	dev->flags &= ~IFF_MULTICAST;
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
	dev->features	|= NETIF_F_SG | NETIF_F_FRAGLIST;
	dev->features	|= NETIF_F_ALL_TSO;
	dev->features	|= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
	dev->features	|= NETIF_F_GSO_ENCAP_ALL;
	dev->hw_features |= dev->features;
	dev->hw_enc_features |= dev->features;
	eth_hw_addr_random(dev);

	dev->min_mtu = 0;
	dev->max_mtu = 0;
}

static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
			  struct netlink_ext_ack *extack)
{
	if (tb[IFLA_ADDRESS]) {
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
			return -EINVAL;
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
			return -EADDRNOTAVAIL;
	}
	return 0;
}

static struct rtnl_link_ops dummy_link_ops __read_mostly = {
	.kind		= DRV_NAME,
	.priv_size	= sizeof(struct dummy_priv),
	.setup		= dummy_setup,
	.validate	= dummy_validate,
};

/* Number of dummy devices to be set up by this module. */
module_param(numdummies, int, 0);
MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");

module_param(num_vfs, int, 0);
MODULE_PARM_DESC(num_vfs, "Number of dummy VFs per dummy device");

static int __init dummy_init_one(void)
{
	struct net_device *dev_dummy;
	int err;

	dev_dummy = alloc_netdev(sizeof(struct dummy_priv),
				 "dummy%d", NET_NAME_ENUM, dummy_setup);
	if (!dev_dummy)
		return -ENOMEM;

	dev_dummy->rtnl_link_ops = &dummy_link_ops;
	err = register_netdevice(dev_dummy);
	if (err < 0)
		goto err;
	return 0;

err:
	free_netdev(dev_dummy);
	return err;
}

static int __init dummy_init_module(void)
{
	int i, err = 0;

	if (num_vfs) {
		err = bus_register(&dummy_bus);
		if (err < 0) {
			pr_err("registering dummy bus failed\n");
			return err;
		}

		err = device_register(&dummy_parent);
		if (err < 0) {
			pr_err("registering dummy parent device failed\n");
			bus_unregister(&dummy_bus);
			return err;
		}
	}

	rtnl_lock();
	err = __rtnl_link_register(&dummy_link_ops);
	if (err < 0)
		goto out;

	for (i = 0; i < numdummies && !err; i++) {
		err = dummy_init_one();
		cond_resched();
	}
	if (err < 0)
		__rtnl_link_unregister(&dummy_link_ops);

out:
	rtnl_unlock();

	if (err && num_vfs) {
		device_unregister(&dummy_parent);
		bus_unregister(&dummy_bus);
	}

	return err;
}

static void __exit dummy_cleanup_module(void)
{
	rtnl_link_unregister(&dummy_link_ops);

	if (num_vfs) {
		device_unregister(&dummy_parent);
		bus_unregister(&dummy_bus);
	}
}

module_init(dummy_init_module);
module_exit(dummy_cleanup_module);
MODULE_LICENSE("GPL");
MODULE_ALIAS_RTNL_LINK(DRV_NAME);
MODULE_VERSION(DRV_VERSION);