summaryrefslogtreecommitdiff
path: root/datapath/linux-2.6/compat-2.6/veth.c
blob: 3cda3365106517d7e0595035c1ee8c95675e5623 (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* veth driver port to Linux 2.6.18 */

/*
 *  drivers/net/veth.c
 *
 *  Copyright (C) 2007, 2009 OpenVZ http://openvz.org, SWsoft Inc
 *
 * Author: Pavel Emelianov <xemul@openvz.org>
 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
 *
 */

#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>

#include <net/dst.h>
#include <net/xfrm.h>

#define DRV_NAME	"veth"
#define DRV_VERSION	"1.0"

struct veth_net_stats {
	unsigned long	rx_packets;
	unsigned long	tx_packets;
	unsigned long	rx_bytes;
	unsigned long	tx_bytes;
	unsigned long	tx_dropped;
};

struct veth_priv {
	struct net_device *peer;
	struct net_device *dev;
	struct list_head list;
	struct veth_net_stats *stats;
	unsigned ip_summed;
	struct net_device_stats dev_stats;
};

static LIST_HEAD(veth_list);

/*
 * ethtool interface
 */

static struct {
	const char string[ETH_GSTRING_LEN];
} ethtool_stats_keys[] = {
	{ "peer_ifindex" },
};

static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	cmd->supported		= 0;
	cmd->advertising	= 0;
	cmd->speed		= SPEED_10000;
	cmd->duplex		= DUPLEX_FULL;
	cmd->port		= PORT_TP;
	cmd->phy_address	= 0;
	cmd->transceiver	= XCVR_INTERNAL;
	cmd->autoneg		= AUTONEG_DISABLE;
	cmd->maxtxpkt		= 0;
	cmd->maxrxpkt		= 0;
	return 0;
}

static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);
	strcpy(info->fw_version, "N/A");
}

static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
{
	switch(stringset) {
	case ETH_SS_STATS:
		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
		break;
	}
}

static void veth_get_ethtool_stats(struct net_device *dev,
		struct ethtool_stats *stats, u64 *data)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	data[0] = priv->peer->ifindex;
}

static u32 veth_get_rx_csum(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	return priv->ip_summed == CHECKSUM_UNNECESSARY;
}

static int veth_set_rx_csum(struct net_device *dev, u32 data)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
	return 0;
}

static u32 veth_get_tx_csum(struct net_device *dev)
{
	return (dev->features & NETIF_F_NO_CSUM) != 0;
}

static int veth_set_tx_csum(struct net_device *dev, u32 data)
{
	if (data)
		dev->features |= NETIF_F_NO_CSUM;
	else
		dev->features &= ~NETIF_F_NO_CSUM;
	return 0;
}

static struct ethtool_ops veth_ethtool_ops = {
	.get_settings		= veth_get_settings,
	.get_drvinfo		= veth_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_rx_csum		= veth_get_rx_csum,
	.set_rx_csum		= veth_set_rx_csum,
	.get_tx_csum		= veth_get_tx_csum,
	.set_tx_csum		= veth_set_tx_csum,
	.get_sg			= ethtool_op_get_sg,
	.set_sg			= ethtool_op_set_sg,
	.get_strings		= veth_get_strings,
	.get_ethtool_stats	= veth_get_ethtool_stats,
};

/*
 * xmit
 */

static int veth_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct net_device *rcv = NULL;
	struct veth_priv *priv, *rcv_priv;
	struct veth_net_stats *stats;
	int length, cpu;

	skb_orphan(skb);

	priv = netdev_priv(dev);
	rcv = priv->peer;
	rcv_priv = netdev_priv(rcv);

	cpu = smp_processor_id();
	stats = per_cpu_ptr(priv->stats, cpu);

	if (!(rcv->flags & IFF_UP))
		goto outf;

	skb->dev = rcv;
	skb->pkt_type = PACKET_HOST;
	skb->protocol = eth_type_trans(skb, rcv);
	if (dev->features & NETIF_F_NO_CSUM)
		skb->ip_summed = rcv_priv->ip_summed;

	dst_release(skb->dst);
	skb->dst = NULL;
	secpath_reset(skb);
	nf_reset(skb);

	length = skb->len;

	stats->tx_bytes += length;
	stats->tx_packets++;

	stats = per_cpu_ptr(rcv_priv->stats, cpu);
	stats->rx_bytes += length;
	stats->rx_packets++;

	netif_rx(skb);
	return 0;

outf:
	kfree_skb(skb);
	stats->tx_dropped++;
	return 0;
}

/*
 * general routines
 */

static struct net_device_stats *veth_get_stats(struct net_device *dev)
{
	struct veth_priv *priv;
	struct net_device_stats *dev_stats;
	int cpu;
	struct veth_net_stats *stats;

	priv = netdev_priv(dev);
	dev_stats = &priv->dev_stats;

	dev_stats->rx_packets = 0;
	dev_stats->tx_packets = 0;
	dev_stats->rx_bytes = 0;
	dev_stats->tx_bytes = 0;
	dev_stats->tx_dropped = 0;

	for_each_online_cpu(cpu) {
		stats = per_cpu_ptr(priv->stats, cpu);

		dev_stats->rx_packets += stats->rx_packets;
		dev_stats->tx_packets += stats->tx_packets;
		dev_stats->rx_bytes += stats->rx_bytes;
		dev_stats->tx_bytes += stats->tx_bytes;
		dev_stats->tx_dropped += stats->tx_dropped;
	}

	return dev_stats;
}

static int veth_open(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	if (priv->peer == NULL)
		return -ENOTCONN;

	if (priv->peer->flags & IFF_UP) {
		netif_carrier_on(dev);
		netif_carrier_on(priv->peer);
	}
	return 0;
}

static int veth_dev_init(struct net_device *dev)
{
	struct veth_net_stats *stats;
	struct veth_priv *priv;

	stats = alloc_percpu(struct veth_net_stats);
	if (stats == NULL)
		return -ENOMEM;

	priv = netdev_priv(dev);
	priv->stats = stats;
	return 0;
}

static void veth_dev_free(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	free_percpu(priv->stats);
	free_netdev(dev);
}

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

	dev->hard_start_xmit = veth_xmit;
	dev->get_stats = veth_get_stats;
	dev->open = veth_open;
	dev->ethtool_ops = &veth_ethtool_ops;
	dev->features |= NETIF_F_LLTX;
	dev->init = veth_dev_init;
	dev->destructor = veth_dev_free;
}

static void veth_change_state(struct net_device *dev)
{
	struct net_device *peer;
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	peer = priv->peer;

	if (netif_carrier_ok(peer)) {
		if (!netif_carrier_ok(dev))
			netif_carrier_on(dev);
	} else {
		if (netif_carrier_ok(dev))
			netif_carrier_off(dev);
	}
}

static int veth_device_event(struct notifier_block *unused,
			     unsigned long event, void *ptr)
{
	struct net_device *dev = ptr;

	if (dev->open != veth_open)
		goto out;

	switch (event) {
	case NETDEV_CHANGE:
		veth_change_state(dev);
		break;
	}
out:
	return NOTIFY_DONE;
}

static struct notifier_block veth_notifier_block __read_mostly = {
	.notifier_call	= veth_device_event,
};

/*
 * netlink interface
 */

static int veth_newlink(const char *devname, const char *peername)
{
	int err;
	const char *names[2];
	struct net_device *devs[2];
	int i;

	names[0] = devname;
	names[1] = peername;
	devs[0] = devs[1] = NULL;

	for (i = 0; i < 2; i++) {
		struct net_device *dev;

		err = -ENOMEM;
		devs[i] = alloc_netdev(sizeof(struct veth_priv),
				       names[i], veth_setup);
		if (!devs[i]) {
			goto err;
		}

		dev = devs[i];

		if (strchr(dev->name, '%')) {
			err = dev_alloc_name(dev, dev->name);
			if (err < 0)
				goto err;
		}
		random_ether_addr(dev->dev_addr);

		err = register_netdevice(dev);
		if (err < 0)
			goto err;

		netif_carrier_off(dev);
	}

	/*
	 * tie the devices together
	 */

	for (i = 0; i < 2; i++) {
		struct veth_priv *priv = netdev_priv(devs[i]);
		priv->dev = devs[i];
		priv->peer = devs[!i];
		if (!i)
			list_add(&priv->list, &veth_list);
		else
			INIT_LIST_HEAD(&priv->list);
	}
	return 0;

err:
	for (i = 0; i < 2; i++) {
		if (devs[i]) {
			if (devs[i]->reg_state != NETREG_UNINITIALIZED)
				unregister_netdevice(devs[i]);
			else
				free_netdev(devs[i]);
		}
	}
	return err;
}

static void veth_dellink(struct net_device *dev)
{
	struct veth_priv *priv;
	struct net_device *peer;

	priv = netdev_priv(dev);
	peer = priv->peer;

	if (!list_empty(&priv->list))
		list_del(&priv->list);

	priv = netdev_priv(peer);
	if (!list_empty(&priv->list))
		list_del(&priv->list);

	unregister_netdevice(dev);
	unregister_netdevice(peer);
}

/*
 * sysfs 
 */

/*
 * "show" function for the veth_pairs attribute.
 * The class parameter is ignored.
 */
static ssize_t veth_show_veth_pairs(struct class *cls, char *buffer)
{
	int res = 0;
	struct veth_priv *priv;

	list_for_each_entry(priv, &veth_list, list) {
		if (res > (PAGE_SIZE - (IFNAMSIZ * 2 + 1))) {
			/* not enough space for another interface name */
			if ((PAGE_SIZE - res) > 10)
				res = PAGE_SIZE - 10;
			res += sprintf(buffer + res, "++more++");
			break;
		}
		res += sprintf(buffer + res, "%s,%s ",
			       priv->dev->name, priv->peer->name);
	}
	res += sprintf(buffer + res, "\n");
	res++;
	return res;
}

/*
 * "store" function for the veth_pairs attribute.  This is what
 * creates and deletes veth pairs.
 *
 * The class parameter is ignored.
 *
 */
static ssize_t veth_store_veth_pairs(struct class *cls, const char *buffer,
				     size_t count)
{
	int c = *buffer++;
	int retval;
	printk("1\n");
	if (c == '+') {
		char devname[IFNAMSIZ + 1] = "";
		char peername[IFNAMSIZ + 1] = "";
		char *comma = strchr(buffer, ',');
		printk("2\n");
		if (!comma)
			goto err_no_cmd;
		strncat(devname, buffer,
			min_t(int, sizeof devname, comma - buffer));
		strncat(peername, comma + 1,
			min_t(int, sizeof peername, strcspn(comma + 1, "\n")));
		printk("3 '%s' '%s'\n", devname, peername);
		if (!dev_valid_name(devname) || !dev_valid_name(peername))
			goto err_no_cmd;
		printk("4\n");
		rtnl_lock();
		retval = veth_newlink(devname, peername);
		rtnl_unlock();
		return retval ? retval : count;
	} else if (c == '-') {
		struct net_device *dev;

		rtnl_lock();
		dev = dev_get_by_name(buffer);
		if (!dev)
			retval = -ENODEV;
		else if (dev->init != veth_dev_init)
			retval = -EINVAL;
		else {
			veth_dellink(dev);
			retval = count;
		}
		rtnl_unlock();

		return retval;
	}

err_no_cmd:
	printk(KERN_ERR DRV_NAME ": no command found in veth_pairs.  Use +ifname,peername or -ifname.\n");
	return -EPERM;
}

/* class attribute for veth_pairs file.  This ends up in /sys/class/net */
static CLASS_ATTR(veth_pairs,  S_IWUSR | S_IRUGO,
		  veth_show_veth_pairs, veth_store_veth_pairs);

static struct class *netdev_class;

/*
 * Initialize sysfs.  This sets up the veth_pairs file in
 * /sys/class/net.
 */
int veth_create_sysfs(void)
{
	struct net_device *dev = dev_get_by_name("lo");
	if (!dev)
		return -ESRCH;
	netdev_class = dev->class_dev.class;
	if (!netdev_class)
		return -ENODEV;
	
	return class_create_file(netdev_class, &class_attr_veth_pairs);
}

/*
 * Remove /sys/class/net/veth_pairs.
 */
void veth_destroy_sysfs(void)
{
	class_remove_file(netdev_class, &class_attr_veth_pairs);
}



/*
 * init/fini
 */

static __init int veth_init(void)
{
	int retval = veth_create_sysfs();
	if (retval)
		return retval;
	register_netdevice_notifier(&veth_notifier_block);
	return 0;
}

static __exit void veth_exit(void)
{
	unregister_netdevice_notifier(&veth_notifier_block);
}

module_init(veth_init);
module_exit(veth_exit);

MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
MODULE_LICENSE("GPL v2");