/
vhost_dev.c
589 lines (475 loc) · 12.8 KB
/
vhost_dev.c
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* vhost_dev.c -- interface in the host OS
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <linux/init.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/inetdevice.h>
#include <linux/etherdevice.h>
#include <linux/types.h>
#include <net/rtnetlink.h>
#include <asm/bug.h>
#include "vr_os.h"
#include "vr_packet.h"
#include "vr_interface.h"
#include "vrouter.h"
#include "vhost.h"
/*
* When agent dies, cross connect logic would need the list of vhost
* interfaces which it should put in cross connect. Also, used in cases
* when physical interface goes away from the system.
*/
struct vhost_priv **vhost_priv_db;
unsigned int vhost_num_interfaces;
extern struct vr_interface vr_reset_interface;
extern int linux_to_vr(struct vr_interface *, struct sk_buff *);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
extern rx_handler_result_t linux_rx_handler(struct sk_buff **);
#endif
static bool vhost_drv_inited;
static void vhost_ethtool_get_info(struct net_device *netdev,
struct ethtool_drvinfo *info)
{
strcpy(info->driver, "vrouter");
strcpy(info->version, "N/A");
strcpy(info->fw_version, "N/A");
strcpy(info->bus_info, "N/A");
}
static const struct ethtool_ops vhost_ethtool_ops = {
.get_drvinfo = vhost_ethtool_get_info,
.get_link = ethtool_op_get_link,
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)) && defined(CONFIG_XEN)
.get_tso = ethtool_op_get_tso,
.set_tso = ethtool_op_set_tso,
.get_flags = ethtool_op_get_flags,
#endif
};
unsigned int
vhost_get_ip(struct vr_interface *vif)
{
struct net_device *dev = (struct net_device *)vif->vif_os;
struct in_device *in_dev = rcu_dereference(dev->ip_ptr);
struct in_ifaddr *ifa;
if (!in_dev)
return 0;
ifa = in_dev->ifa_list;
if (ifa)
return ifa->ifa_address;
return 0;
}
static void
vhost_dev_destructor(struct net_device *dev)
{
free_netdev(dev);
return;
}
static int
vhost_dev_open(struct net_device *dev)
{
netif_start_queue(dev);
return 0;
}
static int
vhost_dev_stop(struct net_device *dev)
{
netif_stop_queue(dev);
return 0;
}
static int
vhost_dev_set_mac_address(struct net_device *dev, void *addr)
{
struct sockaddr *mac = addr;
if (!is_valid_ether_addr(mac->sa_data))
return -EADDRNOTAVAIL;
memcpy(dev->dev_addr, mac->sa_data, ETH_ALEN);
return 0;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
/*
* this handler comes into play only for a brief moment: when the agent resets
* vrouter, a process in which all the vifs are removed, this rx handler is
* installed on the physical device with argument (rx_handler_data) set to the
* vhost device
*/
rx_handler_result_t
vhost_rx_handler(struct sk_buff **pskb)
{
struct net_device *vdev;
struct sk_buff *skb = *pskb;
vdev = rcu_dereference(skb->dev->rx_handler_data);
if (!vdev) {
kfree_skb(*pskb);
return RX_HANDLER_CONSUMED;
}
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
return RX_HANDLER_PASS;
skb->dev = vdev;
(void)__sync_fetch_and_add(&vdev->stats.rx_bytes, skb->len);
(void)__sync_fetch_and_add(&vdev->stats.rx_packets, 1);
return RX_HANDLER_ANOTHER;
}
#endif
void
vhost_del_tap_phys(struct net_device *pdev)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
if (rcu_dereference(pdev->rx_handler) == vhost_rx_handler)
netdev_rx_handler_unregister(pdev);
#else
vr_set_vif_ptr(pdev, NULL);
#endif
return;
}
/*
* register an rx handler which will bypass all vrouter processing. Helpful
* at the time of vrouter soft reset. For kernels that does not support rx
* handler tap, the logic is handled in linux_rx_handler, albeit in an
* inefficient way (but works :)
*/
void
vhost_tap_phys(struct net_device *vdev, struct net_device *pdev)
{
struct vhost_priv *vp;
if (!vdev || !pdev)
return;
vp = netdev_priv(vdev);
/*
* with vhost_attach_phys, it is possible that everything was OK except
* that the physical went out and came back. if that is the case, do not
* tap here
*/
if (vp->vp_vifp)
goto exit_tap_phys;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39))
/*
* it can so happen (unlikely) that vhost is deleted from vrouter
* before the physical, in which case we will have to unregister
* the rx handler in physical and install the vhost_rx_handler
*/
if (rcu_dereference(pdev->rx_handler) == linux_rx_handler)
netdev_rx_handler_unregister(pdev);
if (!rcu_dereference(pdev->rx_handler))
netdev_rx_handler_register(pdev, vhost_rx_handler, (void *)vdev);
#else
vr_set_vif_ptr(pdev, &vr_reset_interface);
#endif
exit_tap_phys:
return;
}
void
vhost_if_del(struct net_device *dev)
{
struct vhost_priv *vp;
if (!dev)
return;
vp = netdev_priv(dev);
vp->vp_vifp = NULL;
if (vp->vp_phys_dev)
vhost_tap_phys(dev, vp->vp_phys_dev);
return;
}
void
vhost_if_add(struct vr_interface *vif)
{
int i;
struct net_device *dev = (struct net_device *)vif->vif_os;
struct vhost_priv *vp = netdev_priv(dev);
vp->vp_vifp = vif;
if (vif->vif_type == VIF_TYPE_HOST) {
dev->features |= (NETIF_F_GSO | NETIF_F_TSO |
NETIF_F_SG | NETIF_F_IP_CSUM);
if (vif->vif_bridge) {
/*
* if there already was an association, need to remove that
*/
if ((vp->vp_phys_dev) &&
(vp->vp_phys_dev !=
((struct net_device *)vif->vif_bridge->vif_os))) {
vhost_del_tap_phys(vp->vp_phys_dev);
}
vp->vp_phys_dev =
(struct net_device *)vif->vif_bridge->vif_os;
strncpy(vp->vp_phys_name, vp->vp_phys_dev->name,
sizeof(vp->vp_phys_name) - 1);
if (vp->vp_phys_dev->type != ARPHRD_ETHER) {
dev->flags |= IFF_NOARP;
} else {
dev->flags &= ~IFF_NOARP;
}
if (vp->vp_db_index >= 0)
return;
/* ...may be a bitmap? */
for (i = 0; i < VHOST_MAX_INTERFACES; i++)
if (!vhost_priv_db[i])
break;
if (i < VHOST_MAX_INTERFACES) {
vp->vp_db_index = i;
vhost_priv_db[i] = vp;
} else {
vr_printf("%s not added to vhost database. ",
vp->vp_dev->name);
vr_printf("Cross connect will not work\n");
}
}
}
return;
}
void
vhost_attach_phys(struct net_device *dev)
{
int i;
struct vhost_priv *vp;
if (!vhost_priv_db)
return;
for (i = 0; i < VHOST_MAX_INTERFACES; i++) {
vp = vhost_priv_db[i];
if (vp) {
if (!strncmp(dev->name, vp->vp_phys_name, VR_INTERFACE_NAME_LEN)) {
vp->vp_phys_dev = dev;
vhost_tap_phys(vp->vp_dev, dev);
break;
}
}
}
return;
}
static struct vhost_priv *
vhost_get_priv_for_phys(struct net_device *dev)
{
int i;
struct vhost_priv *vp;
if (!vhost_priv_db)
return NULL;
for (i = 0; i < VHOST_MAX_INTERFACES; i++) {
vp = vhost_priv_db[i];
if (vp)
if (vp->vp_phys_dev == dev)
return vp;
}
return NULL;
}
struct net_device *
vhost_get_vhost_for_phys(struct net_device *dev)
{
struct vhost_priv *vp;
vp = vhost_get_priv_for_phys(dev);
if (!vp)
return NULL;
return vp->vp_dev;
}
/*
* when the vif is deleted, we need to bypass vrouter and start sending
* packets directly to vhost interface
*/
void
vhost_if_del_phys(struct net_device *dev)
{
struct vhost_priv *vp;
if (!dev)
return;
vp = vhost_get_priv_for_phys(dev);
if (vp)
vhost_tap_phys(vp->vp_dev, vp->vp_phys_dev);
return;
}
/*
* it is quite possible that the physical interface goes away from the
* system. when the interface goes away, we need to release the tap
*/
void
vhost_detach_phys(struct net_device *dev)
{
struct vhost_priv *vp;
vp = vhost_get_priv_for_phys(dev);
if (vp) {
/*
* if vrouter was left uninited post reset and then the
* physical interface went away, we need to detach the tap
*/
vhost_del_tap_phys(vp->vp_phys_dev);
vp->vp_phys_dev = NULL;
return;
}
return;
}
void
vhost_remove_xconnect(void)
{
int i;
struct vhost_priv *vp;
struct vr_interface *bridge;
if (!vhost_priv_db)
return;
for (i = 0; i < VHOST_MAX_INTERFACES; i++) {
vp = vhost_priv_db[i];
if (vp) {
if (vp->vp_vifp) {
vif_remove_xconnect(vp->vp_vifp);
if ((bridge = vp->vp_vifp->vif_bridge))
vif_remove_xconnect(bridge);
}
}
}
return;
}
void
vhost_xconnect(void)
{
int i;
struct vhost_priv *vp;
struct vr_interface *bridge;
if (!vhost_priv_db)
return;
for (i = 0; i < VHOST_MAX_INTERFACES; i++) {
vp = vhost_priv_db[i];
if (vp) {
if (vp->vp_vifp) {
vif_set_xconnect(vp->vp_vifp);
if ((bridge = vp->vp_vifp->vif_bridge))
vif_set_xconnect(bridge);
}
}
}
return;
}
netdev_tx_t
vhost_dev_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct vhost_priv *vp;
struct vr_interface *vifp;
struct net_device *pdev;
vp = netdev_priv(dev);
vifp = vp->vp_vifp;
if (!vifp) {
if (!(pdev = vp->vp_phys_dev)) {
(void)__sync_fetch_and_add(&dev->stats.tx_dropped, 1);
kfree_skb(skb);
return NETDEV_TX_OK;
}
skb->dev = pdev;
dev_queue_xmit(skb);
} else {
linux_to_vr(vifp, skb);
}
(void)__sync_fetch_and_add(&dev->stats.tx_packets, 1);
(void)__sync_fetch_and_add(&dev->stats.tx_bytes, skb->len);
return NETDEV_TX_OK;
}
static struct net_device_ops vhost_dev_ops = {
.ndo_open = vhost_dev_open,
.ndo_stop = vhost_dev_stop,
.ndo_start_xmit = vhost_dev_xmit,
.ndo_set_mac_address = vhost_dev_set_mac_address,
};
void
vhost_setup(struct net_device *dev)
{
struct vhost_priv *vp = netdev_priv(dev);
/* follow the standard steps */
random_ether_addr(dev->dev_addr);
ether_setup(dev);
dev->needed_headroom = sizeof(struct vr_eth) + sizeof(struct agent_hdr);
dev->netdev_ops = &vhost_dev_ops;
dev->destructor = vhost_dev_destructor;
#ifdef CONFIG_XEN
dev->ethtool_ops = &vhost_ethtool_ops;
dev->features |= NETIF_F_GRO;
#endif
vp->vp_db_index = -1;
vp->vp_dev = dev;
vhost_num_interfaces++;
return;
}
#if (LINUX_VERSION_CODE == KERNEL_VERSION(2,6,32))
static void
vhost_dellink(struct net_device *dev)
#else
static void
vhost_dellink(struct net_device *dev, struct list_head *head)
#endif
{
struct vhost_priv *vp;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,33))
unregister_netdevice_queue(dev, head);
#else
unregister_netdevice(dev);
#endif
vp = netdev_priv(dev);
if (vp) {
if (vhost_priv_db && vp->vp_db_index >= 0)
vhost_priv_db[vp->vp_db_index] = NULL;
vp->vp_db_index = -1;
if (vp->vp_phys_dev) {
vhost_del_tap_phys(vp->vp_phys_dev);
vp->vp_phys_dev = NULL;
}
vp->vp_phys_name[0] = '\0';
}
if (!vhost_num_interfaces)
BUG();
vhost_num_interfaces--;
return;
}
static int
vhost_validate(struct nlattr *tb[], struct nlattr *data[])
{
if (vhost_num_interfaces >= VHOST_MAX_INTERFACES)
return -ENOMEM;
return 0;
}
static struct rtnl_link_ops vhost_link_ops = {
.kind = VHOST_KIND,
.priv_size = sizeof(struct vhost_priv),
.setup = vhost_setup,
.validate = vhost_validate,
.dellink = vhost_dellink,
};
static void
vhost_netlink_exit(void)
{
if (vhost_drv_inited) {
rtnl_link_unregister(&vhost_link_ops);
}
vhost_drv_inited = false;
return;
}
static int
vhost_netlink_init(void)
{
int ret;
if (vhost_drv_inited)
return 0;
ret = rtnl_link_register(&vhost_link_ops);
if (ret) {
return ret;
}
vhost_drv_inited = true;
return 0;
}
void
vhost_exit(void)
{
vhost_netlink_exit();
if (vhost_priv_db) {
kfree(vhost_priv_db);
vhost_priv_db = NULL;
}
return;
}
int
vhost_init(void)
{
if (!vhost_priv_db) {
vhost_priv_db =
kzalloc(sizeof(struct vhost_priv *) * VHOST_MAX_INTERFACES,
GFP_KERNEL);
if (!vhost_priv_db)
return -ENOMEM;
}
return vhost_netlink_init();
}