/
vr_dpdk_interface.c
1689 lines (1468 loc) · 56.4 KB
/
vr_dpdk_interface.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 Semihalf.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* vr_dpdk_interface.c -- vRouter interface callbacks
*
*/
#include "vr_dpdk.h"
#include "vr_dpdk_netlink.h"
#include "vr_dpdk_usocket.h"
#include "vr_dpdk_virtio.h"
#include <rte_errno.h>
#include <rte_ethdev.h>
#include <rte_ip_frag.h>
#include <rte_ip.h>
#include <rte_port_ethdev.h>
#include <rte_eth_af_packet.h>
/*
* dpdk_virtual_if_add - add a virtual (virtio) interface to vrouter.
* Returns 0 on success, < 0 otherwise.
*/
static int
dpdk_virtual_if_add(struct vr_interface *vif)
{
int ret;
uint16_t nrxqs, ntxqs;
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) virtual device %s\n",
vif->vif_idx, vif->vif_gen, vif->vif_name);
nrxqs = vr_dpdk_virtio_nrxqs(vif);
/* virtio TX is thread safe, so we assign TX queue to each lcore */
ntxqs = (uint16_t)-1;
ret = vr_dpdk_lcore_if_schedule(vif, vr_dpdk_lcore_least_used_get(),
nrxqs, &vr_dpdk_virtio_rx_queue_init,
ntxqs, &vr_dpdk_virtio_tx_queue_init);
if (ret) {
return ret;
}
/*
* When something goes wrong, vr_netlink_uvhost_vif_add() returns
* non-zero value. Then we return this value here. It is handled by
* dp-core and dpdk_virtual_if_del() is called, so there is no need
* to do it manually here.
*
* Check dp-core/vf_interface.c:eth_drv_add() for reference.
*/
return vr_netlink_uvhost_vif_add(vif->vif_name, vif->vif_idx, vif->vif_gen,
nrxqs, ntxqs);
}
/*
* dpdk_virtual_vlan_if_add - add a virtual VLAN interface to vRouter.
* Returns 0 on success, < 0 otherwise.
*/
static int
dpdk_virtual_vlan_if_add(struct vr_interface *vif)
{
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) virtual VLAN(o/i) %u/%u device %s\n",
vif->vif_idx, vif->vif_gen, vif->vif_ovlan_id, vif->vif_vlan_id,
vif->vif_name);
RTE_LOG(INFO, VROUTER, " parent vif %u device %s\n",
vif->vif_parent->vif_idx, vif->vif_parent->vif_name);
return 0;
}
/*
* dpdk_virtual_if_del - deletes a virtual (virtio) interface from vrouter.
* Returns 0 on success, -1 otherwise.
*/
static int
dpdk_virtual_if_del(struct vr_interface *vif)
{
int ret;
RTE_LOG(INFO, VROUTER, "Deleting vif %u virtual device\n",
vif->vif_idx);
vr_dpdk_lcore_if_unschedule(vif);
ret = vr_netlink_uvhost_vif_del(vif->vif_idx);
if (ret) {
RTE_LOG(ERR, VROUTER, "Error deleting vif %u virtual device %s\n",
vif->vif_idx, vif->vif_name);
}
return ret;
}
/*
* dpdk_virtual_vlan_if_del - deletes a virtual VLAN interface from vRouter.
* Returns 0 on success, < 0 otherwise.
*/
static int
dpdk_virtual_vlan_if_del(struct vr_interface *vif)
{
RTE_LOG(INFO, VROUTER, "Deleting vif %u virtual VLAN(o/i) %u/%u device\n",
vif->vif_idx, vif->vif_ovlan_id, vif->vif_vlan_id);
return 0;
}
static inline void
dpdk_dbdf_to_pci(unsigned int dbdf,
struct rte_pci_addr *address)
{
address->domain = (dbdf >> 16);
address->bus = (dbdf >> 8) & 0xff;
address->devid = (dbdf >> 3) & 0x1f;
address->function = (dbdf & 0x7);
return;
}
static inline unsigned
dpdk_pci_to_dbdf(struct rte_pci_addr *address)
{
return address->domain << 16
| address->bus << 8
| address->devid
| address->function;
}
/* mirrors the function used in bonding */
static inline uint8_t
dpdk_find_port_id_by_pci_addr(const struct rte_pci_addr *addr)
{
uint8_t i;
struct rte_pci_addr *eth_pci_addr;
for (i = 0; i < rte_eth_dev_count(); i++) {
if (rte_eth_devices[i].pci_dev == NULL)
continue;
eth_pci_addr = &(rte_eth_devices[i].pci_dev->addr);
if (addr->bus == eth_pci_addr->bus &&
addr->devid == eth_pci_addr->devid &&
addr->domain == eth_pci_addr->domain &&
addr->function == eth_pci_addr->function) {
return i;
}
}
return VR_DPDK_INVALID_PORT_ID;
}
static inline void
dpdk_find_pci_addr_by_port(struct rte_pci_addr *addr, uint8_t port_id)
{
rte_memcpy(addr, &rte_eth_devices[port_id].pci_dev->addr, sizeof(struct rte_pci_addr));
}
static inline void
dpdk_set_addr_vlan_filter_strip(uint32_t port_id, struct vr_interface *vif)
{
uint32_t i, ret;
uint8_t *port_id_ptr;
int port_num = 0;
struct vr_dpdk_ethdev *ethdev = &vr_dpdk.ethdevs[port_id];
port_id_ptr = (ethdev->ethdev_nb_slaves == -1)?
ðdev->ethdev_port_id:ethdev->ethdev_slaves;
do {
/*
* TODO: vf_lcore_id check for SR-IOV VF should be a per-interface
* check to handle the case where a bond has a VF and a PF in it.
*/
/*
* Set the MAC address of slave interfaces. Doing it from the bond driver in
* DPDK doesn't seem to work on SR-IOV VFs.
*/
if ((ethdev->ethdev_nb_slaves != -1) && vr_dpdk.vf_lcore_id) {
ret = rte_eth_dev_default_mac_addr_set(*port_id_ptr,
(struct ether_addr *)vif->vif_mac);
if (ret == 0) {
RTE_LOG(INFO, VROUTER, "Bond slave port %d now uses vif MAC "
MAC_FORMAT "\n",
*port_id_ptr, MAC_VALUE(vif->vif_mac));
} else {
RTE_LOG(ERR, VROUTER, "Error setting vif MAC to bond slave port %d: "
"%s (%d)\n",
*port_id_ptr, rte_strerror(-ret), -ret);
}
}
if ((vr_dpdk.vlan_tag != VLAN_ID_INVALID) && vr_dpdk.vf_lcore_id) {
ret = rte_eth_dev_set_vlan_offload(*port_id_ptr, ETH_VLAN_FILTER_OFFLOAD);
if (ret) {
RTE_LOG(INFO, VROUTER, "Error %d enabling vlan offload on port %d\n",
ret, *port_id_ptr);
} else {
RTE_LOG(INFO, VROUTER, "Enabled vlan offload on port %d\n",
*port_id_ptr);
}
ret = rte_eth_dev_vlan_filter(*port_id_ptr, vr_dpdk.vlan_tag, 1);
if (ret) {
RTE_LOG(INFO, VROUTER, "Error %d enabling vlan %d on port %d\n",
ret, vr_dpdk.vlan_tag, *port_id_ptr);
} else {
RTE_LOG(INFO, VROUTER, "Enabled vlan %d on port %d\n",
vr_dpdk.vlan_tag, *port_id_ptr);
}
}
for (i=0; i< rte_eth_devices[*port_id_ptr].data->nb_rx_queues; i++)
{
if (vif->vif_flags & VIF_FLAG_VLAN_OFFLOAD) {
rte_eth_dev_set_vlan_strip_on_queue(*port_id_ptr, i, 1);
} else {
rte_eth_dev_set_vlan_strip_on_queue(*port_id_ptr, i, 0);
}
}
port_num++;
port_id_ptr++;
} while (port_num < ethdev->ethdev_nb_slaves);
}
void
dpdk_vif_attach_ethdev(struct vr_interface *vif,
struct vr_dpdk_ethdev *ethdev)
{
struct ether_addr mac_addr;
struct rte_eth_dev_info dev_info;
int ret;
vif->vif_os = (void *)ethdev;
rte_eth_dev_info_get(ethdev->ethdev_port_id, &dev_info);
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM
&& dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM
&& dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM) {
vif->vif_flags |= VIF_FLAG_TX_CSUM_OFFLOAD;
} else {
vif->vif_flags &= ~VIF_FLAG_TX_CSUM_OFFLOAD;
}
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT
&& dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) {
vif->vif_flags |= VIF_FLAG_VLAN_OFFLOAD;
} else {
vif->vif_flags &= ~VIF_FLAG_VLAN_OFFLOAD;
}
/*
* Do not want to overwrite what agent had sent.
* Set only if the address is null.
*/
memset(&mac_addr, 0, sizeof(mac_addr));
if (memcmp(vif->vif_mac, mac_addr.addr_bytes, ETHER_ADDR_LEN) == 0) {
rte_eth_macaddr_get(ethdev->ethdev_port_id, &mac_addr);
memcpy(vif->vif_mac, mac_addr.addr_bytes, ETHER_ADDR_LEN);
} else {
/*
* On some hardware (e100e, virtual functions, etc) the MAC is random,
* so we check if vif and NIC MACs are match and set the NIC MAC.
*/
rte_eth_macaddr_get(ethdev->ethdev_port_id, &mac_addr);
if (memcmp(vif->vif_mac, mac_addr.addr_bytes, ETHER_ADDR_LEN) != 0) {
/* No match, so set vif MAC to NIC. */
ret = rte_eth_dev_default_mac_addr_set(ethdev->ethdev_port_id,
(struct ether_addr *)vif->vif_mac);
if (ret == 0) {
RTE_LOG(INFO, VROUTER, " eth dev %s now use vif MAC "
MAC_FORMAT "\n",
vif->vif_name, MAC_VALUE(vif->vif_mac));
} else {
RTE_LOG(ERR, VROUTER, " error setting vif MAC to eth dev %s: "
"%s (%d)\n",
vif->vif_name, rte_strerror(-ret), -ret);
}
}
}
}
/*
* dpdk_vlan_forwarding_if_add - add VLAN forwarding interface
* Returns 0 on success, < 0 otherwise.
*/
int
dpdk_vlan_forwarding_if_add(void)
{
int ret;
struct vr_interface vlan_fwd_intf;
strncpy((char *)vlan_fwd_intf.vif_name, vr_dpdk.vlan_name,
sizeof(vlan_fwd_intf.vif_name));
vlan_fwd_intf.vif_type = VIF_TYPE_VLAN;
RTE_LOG(INFO, VROUTER, "Adding VLAN forwarding interface %s\n",
vr_dpdk.vlan_name);
ret = vr_dpdk_knidev_init(0, &vlan_fwd_intf);
if (ret != 0) {
RTE_LOG(ERR, VROUTER, "Error initializing KNI for VLAN forwarding interface\n");
return ret;
}
/* Save KNI handler needed to send packets to the interface. */
vr_dpdk.vlan_kni = (struct rte_kni *)vlan_fwd_intf.vif_os;
/*
* Allocate a multi-producer single-consumer ring - a buffer for packets
* waiting to be send to the forwarding interface.
*/
vr_dpdk.vlan_ring = vr_dpdk_ring_allocate(VR_DPDK_FWD_LCORE_ID,
vr_dpdk.vlan_name, VR_DPDK_TX_RING_SZ, RING_F_SC_DEQ);
if (!vr_dpdk.vlan_ring) {
RTE_LOG(ERR, VROUTER, "Error allocating ring for VLAN forwarding interface\n");
vr_dpdk.vlan_kni = NULL;
vr_dpdk_knidev_release(&vlan_fwd_intf);
return -1;
}
return 0;
}
/*
* Add af_packet virtual device to communicate with veth namespace devices.
* The device is removed with dpdk_fabric_af_packet_if_del().
*/
static int
dpdk_af_packet_if_add(struct vr_interface *vif)
{
int ret;
char params[VR_DPDK_STR_BUF_SZ];
char name[VR_INTERFACE_NAME_LEN];
struct vr_dpdk_ethdev *ethdev;
uint8_t port_id;
int frame_size;
RTE_LOG(INFO, VROUTER,
"Adding vif %u (gen. %u) af_packet device %s\n",
vif->vif_idx, vif->vif_gen, vif->vif_name);
ret = snprintf(name, sizeof(name), "eth_af_packet_%d", vif->vif_idx);
if (ret >= sizeof(name)) {
RTE_LOG(ERR, VROUTER,
" error creating name for af_packet device %s\n", name);
return ret;
}
/* Frame size should be a multiple of page size. */
frame_size = (vr_packet_sz + getpagesize() - 1) /
getpagesize() * getpagesize();
ret = snprintf(params, sizeof(params),
/* TODO: Optional af_packet mmap parameters
* "qpairs=%d,framecnt=%d", 16, 512);
*/
"iface=%s,framesz=%d,blocksz=%d",
vif->vif_name, frame_size, frame_size);
if (ret >= sizeof(params)) {
RTE_LOG(ERR, VROUTER,
" error creating config for af_packet device %s\n", name);
return ret;
}
ret = rte_eal_vdev_init(name, params);
if (ret < 0) {
RTE_LOG(ERR, VROUTER,
" error initializing af_packet device %s\n", name);
return ret;
}
port_id = (uint8_t)(rte_eth_dev_allocated(name) - rte_eth_devices);
ethdev = &vr_dpdk.ethdevs[port_id];
if (ethdev->ethdev_ptr != NULL) {
RTE_LOG(ERR, VROUTER,
" error adding af_packet device %s: eth device %"PRIu8" already added\n",
name, port_id);
return -EEXIST;
}
ethdev->ethdev_port_id = port_id;
ret = vr_dpdk_ethdev_init(ethdev);
if (ret != 0)
return ret;
dpdk_vif_attach_ethdev(vif, ethdev);
ret = rte_eth_dev_start(port_id);
if (ret < 0) {
RTE_LOG(ERR, VROUTER,
" error starting eth device %" PRIu8": %s (%d)\n",
port_id, rte_strerror(-ret), -ret);
return ret;
}
/* schedule RX/TX queues */
return vr_dpdk_lcore_if_schedule(vif, vr_dpdk_lcore_least_used_get(),
ethdev->ethdev_nb_rss_queues, &vr_dpdk_ethdev_rx_queue_init,
ethdev->ethdev_nb_tx_queues, &vr_dpdk_ethdev_tx_queue_init);
}
/* Add fabric interface */
static int
dpdk_fabric_if_add(struct vr_interface *vif)
{
int ret;
uint8_t port_id;
struct rte_pci_addr pci_address;
struct vr_dpdk_ethdev *ethdev;
struct ether_addr mac_addr;
memset(&pci_address, 0, sizeof(pci_address));
memset(&mac_addr, 0, sizeof(mac_addr));
if (vif->vif_flags & VIF_FLAG_PMD) {
if (vif->vif_os_idx >= rte_eth_dev_count()) {
RTE_LOG(ERR, VROUTER, "Error adding vif %u eth device %s: invalid PMD %u"
" (must be less than %u)\n", vif->vif_idx, vif->vif_name,
vif->vif_os_idx, (unsigned)rte_eth_dev_count());
return -ENOENT;
}
port_id = vif->vif_os_idx;
/* TODO: does not work for host interfaces
dpdk_find_pci_addr_by_port(&pci_address, port_id);
vif->vif_os_idx = dpdk_pci_to_dbdf(&pci_address);
*/
rte_eth_macaddr_get(port_id, &mac_addr);
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) eth device %" PRIu8
" (PMD) MAC " MAC_FORMAT " (vif MAC "MAC_FORMAT")\n",
vif->vif_idx, vif->vif_gen, port_id,
MAC_VALUE(mac_addr.addr_bytes), MAC_VALUE(vif->vif_mac));
} else {
dpdk_dbdf_to_pci(vif->vif_os_idx, &pci_address);
port_id = dpdk_find_port_id_by_pci_addr(&pci_address);
if (port_id == VR_DPDK_INVALID_PORT_ID) {
RTE_LOG(ERR, VROUTER, "Error adding vif %u eth device %s:"
" no port ID found for PCI " PCI_PRI_FMT "\n",
vif->vif_idx, vif->vif_name,
pci_address.domain, pci_address.bus,
pci_address.devid, pci_address.function);
return -ENOENT;
}
rte_eth_macaddr_get(port_id, &mac_addr);
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) eth device %" PRIu8
" PCI " PCI_PRI_FMT " MAC " MAC_FORMAT " (vif MAC "MAC_FORMAT")\n",
vif->vif_idx, vif->vif_gen, port_id,
pci_address.domain, pci_address.bus,
pci_address.devid, pci_address.function,
MAC_VALUE(mac_addr.addr_bytes), MAC_VALUE(vif->vif_mac));
}
ethdev = &vr_dpdk.ethdevs[port_id];
if (ethdev->ethdev_ptr != NULL) {
RTE_LOG(ERR, VROUTER, " error adding eth dev %s: already added\n",
vif->vif_name);
return -EEXIST;
}
ethdev->ethdev_port_id = port_id;
/* init eth device */
ret = vr_dpdk_ethdev_init(ethdev);
if (ret != 0)
return ret;
dpdk_vif_attach_ethdev(vif, ethdev);
ret = rte_eth_dev_start(port_id);
if (ret < 0) {
RTE_LOG(ERR, VROUTER, " error starting eth device %" PRIu8
": %s (%d)\n", port_id, rte_strerror(-ret), -ret);
return ret;
}
ret = vr_dpdk_ethdev_rss_init(ethdev);
if (ret < 0)
return ret;
/* we need to init Flow Director after the device has started */
#if VR_DPDK_USE_HW_FILTERING
/* init hardware filtering */
ret = vr_dpdk_ethdev_filtering_init(vif, ethdev);
if (ret < 0)
return ret;
#endif
/* Set hardware VLAN stripping */
dpdk_set_addr_vlan_filter_strip(port_id, vif);
/* schedule RX/TX queues */
return vr_dpdk_lcore_if_schedule(vif, vr_dpdk_lcore_least_used_get(),
ethdev->ethdev_nb_rss_queues, &vr_dpdk_ethdev_rx_queue_init,
ethdev->ethdev_nb_tx_queues, &vr_dpdk_ethdev_tx_queue_init);
}
/* Delete fabric or af_packet interface */
static int
dpdk_fabric_af_packet_if_del(struct vr_interface *vif)
{
uint8_t port_id;
struct vr_dpdk_ethdev *ethdev;
struct rte_eth_dev *ethdev_ptr;
RTE_LOG(INFO, VROUTER, "Deleting vif %u %s device\n", vif->vif_idx,
vif_is_fabric(vif) ? "eth" : "af_packet");
/*
* If dpdk_fabric_if_add() failed before dpdk_vif_attach_ethdev,
* then vif->vif_os will be NULL.
*/
if (vif->vif_os == NULL) {
RTE_LOG(ERR, VROUTER, " error deleting %s dev: already removed\n",
vif_is_fabric(vif) ? "eth" : "af_packet");
return -EEXIST;
}
ethdev = (struct vr_dpdk_ethdev *)(vif->vif_os);
ethdev_ptr = ethdev->ethdev_ptr;
port_id = ethdev->ethdev_port_id;
/* unschedule RX/TX queues */
vr_dpdk_lcore_if_unschedule(vif);
rte_eth_dev_stop(port_id);
/* af_packet release */
if (vif_is_namespace(vif)) {
/**
* af_packet does not implement rte_driver.uninit() that should
* free memory and call rte_eth_dev_release_port(). If we ever wanted
* to move to the pcap driver, we should call rte_eth_dev_close(),
* then rte_eth_dev_detach(). _detach() will call .uninit(), that is
* implemented in pcap. .uninit() will free memory and call
* _release_port().
*/
rte_eth_dev_close(port_id);
rte_free(ethdev_ptr->data->dev_private);
rte_free(ethdev_ptr->data);
rte_free(ethdev_ptr->pci_dev);
rte_eth_dev_release_port(ethdev_ptr);
}
/* release eth device */
return vr_dpdk_ethdev_release(ethdev);
}
/* Add vhost interface */
static int
dpdk_vhost_if_add(struct vr_interface *vif)
{
uint8_t port_id;
int ret;
struct ether_addr mac_addr;
struct vr_dpdk_ethdev *ethdev;
/*
* The Agent passes xconnect fabric interface in cross_connect_idx,
* but dp-core does not copy it into vr_interface. Instead
* it looks for an interface with os_idx == cross_connect_idx
* and sets vif->vif_bridge if there is such an interface.
*/
ethdev = (struct vr_dpdk_ethdev *)(vif->vif_bridge->vif_os);
if (ethdev == NULL) {
RTE_LOG(ERR, VROUTER, "Error adding vif %u KNI device %s:"
" bridge vif %u ethdev is not initialized\n",
vif->vif_idx, vif->vif_name, vif->vif_bridge->vif_idx);
return -ENOENT;
}
port_id = ethdev->ethdev_port_id;
/* get interface MAC address */
memset(&mac_addr, 0, sizeof(mac_addr));
rte_eth_macaddr_get(port_id, &mac_addr);
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) KNI device %s at eth device %" PRIu8
" MAC " MAC_FORMAT " (vif MAC " MAC_FORMAT ")\n",
vif->vif_idx, vif->vif_gen, vif->vif_name, port_id,
MAC_VALUE(mac_addr.addr_bytes), MAC_VALUE(vif->vif_mac));
/*
* KNI does not support bond interfaces and generate random MACs,
* so we try to get a bond member instead.
*/
if (ethdev->ethdev_nb_slaves > 0) {
port_id = ethdev->ethdev_slaves[0];
memset(&mac_addr, 0, sizeof(mac_addr));
rte_eth_macaddr_get(port_id, &mac_addr);
RTE_LOG(INFO, VROUTER, " using bond slave eth device %" PRIu8
" MAC " MAC_FORMAT "\n",
port_id, MAC_VALUE(mac_addr.addr_bytes));
}
/* init KNI */
ret = vr_dpdk_knidev_init(port_id, vif);
if (ret != 0)
return ret;
return vr_dpdk_lcore_if_schedule(vif, vr_dpdk_lcore_least_used_get(),
1, &vr_dpdk_kni_rx_queue_init,
1, &vr_dpdk_kni_tx_queue_init);
}
/* Delete vhost interface */
static int
dpdk_vhost_if_del(struct vr_interface *vif)
{
RTE_LOG(INFO, VROUTER, "Deleting vif %u KNI device %s\n",
vif->vif_idx, vif->vif_name);
vr_dpdk_lcore_if_unschedule(vif);
/* release KNI */
return vr_dpdk_knidev_release(vif);
}
/* Start interface monitoring */
static void
dpdk_monitoring_start(struct vr_interface *monitored_vif,
struct vr_interface *monitoring_vif)
{
uint8_t port_id;
/* set monitoring redirection */
vr_dpdk.monitorings[monitored_vif->vif_idx] = monitoring_vif->vif_idx;
/* set vif flag */
rte_wmb();
monitored_vif->vif_flags |= VIF_FLAG_MONITORED;
if(vif_is_fabric(monitored_vif)) {
port_id = (((struct vr_dpdk_ethdev *)(monitored_vif->vif_os))->ethdev_port_id);
rte_eth_promiscuous_enable(port_id);
}
}
/* Stop interface monitoring */
static void
dpdk_monitoring_stop(struct vr_interface *monitored_vif,
struct vr_interface *monitoring_vif)
{
#if !VR_DPDK_ENABLE_PROMISC
uint8_t port_id;
#endif
/* check if the monitored vif was reused */
if (vr_dpdk.monitorings[monitored_vif->vif_idx] != monitoring_vif->vif_idx)
return;
/* clear vif flag */
monitored_vif->vif_flags &= ~((unsigned int)VIF_FLAG_MONITORED);
rte_wmb();
/* clear monitoring redirection */
vr_dpdk.monitorings[monitored_vif->vif_idx] = VR_MAX_INTERFACES;
#if !VR_DPDK_ENABLE_PROMISC
if(vif_is_fabric(monitored_vif)) {
port_id = (((struct vr_dpdk_ethdev *)(monitored_vif->vif_os))->ethdev_port_id);
rte_eth_promiscuous_disable(port_id);
}
#endif
}
/* Add monitoring interface */
static int
dpdk_monitoring_if_add(struct vr_interface *vif)
{
int ret;
unsigned short monitored_vif_id = vif->vif_os_idx;
struct vr_interface *monitored_vif;
struct vrouter *router = vrouter_get(vif->vif_rid);
RTE_LOG(INFO, VROUTER, "Adding monitoring vif %u (gen. %u) KNI device %s"
" to monitor vif %u\n",
vif->vif_idx, vif->vif_gen, vif->vif_name, monitored_vif_id);
/* Check if vif exist.
* We don't need vif reference in order to monitor it.
* We use the VIF_FLAG_MONITORED to copy in/out packet to the
* monitoring interface. If the monitored vif get deleted, we simply
* get no more packets.
*/
monitored_vif = __vrouter_get_interface(router, monitored_vif_id);
if (!monitored_vif) {
RTE_LOG(ERR, VROUTER, " error getting vif to monitor:"
" vif %u does not exist\n", monitored_vif_id);
return -EINVAL;
}
/*
* TODO: we always use DPDK port 0 for monitoring KNI
* DPDK numerates all the detected Ethernet devices starting from 0.
* So we might only get into an issue if we have no eth devices at all
* or we have few eth ports and don't want to use the first one.
*/
ret = vr_dpdk_knidev_init(0, vif);
if (ret != 0)
return ret;
/* write-only interface */
ret = vr_dpdk_lcore_if_schedule(vif, vr_dpdk_lcore_least_used_get(),
0, NULL,
1, &vr_dpdk_kni_tx_queue_init);
if (ret != 0)
return ret;
/* start monitoring */
dpdk_monitoring_start(monitored_vif, vif);
return 0;
}
/* Delete monitoring interface */
static int
dpdk_monitoring_if_del(struct vr_interface *vif)
{
unsigned short monitored_vif_id = vif->vif_os_idx;
struct vr_interface *monitored_vif;
RTE_LOG(INFO, VROUTER, "Deleting monitoring vif %u KNI device"
" to monitor vif %u\n",
vif->vif_idx, monitored_vif_id);
/* check if vif exist */
monitored_vif = __vrouter_get_interface(vrouter_get(vif->vif_rid),
monitored_vif_id);
if (!monitored_vif) {
RTE_LOG(ERR, VROUTER, " error getting vif to monitor:"
" vif %u does not exist\n", monitored_vif_id);
} else {
/* stop monitoring */
dpdk_monitoring_stop(monitored_vif, vif);
}
vr_dpdk_lcore_if_unschedule(vif);
/* release KNI */
return vr_dpdk_knidev_release(vif);
}
/* Add agent interface */
static int
dpdk_agent_if_add(struct vr_interface *vif)
{
int ret;
RTE_LOG(INFO, VROUTER, "Adding vif %u (gen. %u) packet device %s\n",
vif->vif_idx, vif->vif_gen, vif->vif_name);
/* check if packet device is already added */
if (vr_dpdk.packet_transport != NULL) {
RTE_LOG(ERR, VROUTER, " error adding packet device %s: already exist\n",
vif->vif_name);
return -EEXIST;
}
/* init packet device */
ret = dpdk_packet_socket_init();
if (ret < 0) {
RTE_LOG(ERR, VROUTER, " error initializing packet socket: %s (%d)\n",
rte_strerror(errno), errno);
return ret;
}
vr_usocket_attach_vif(vr_dpdk.packet_transport, vif);
/* No need to schedule the pkt0 at the moment, since we RX from the
* socket and TX to the global packet_ring.
*/
return 0;
}
/* Delete agent interface */
static int
dpdk_agent_if_del(struct vr_interface *vif)
{
RTE_LOG(INFO, VROUTER, "Deleting vif %u packet device\n",
vif->vif_idx);
dpdk_packet_socket_close();
return 0;
}
extern void vhost_remove_xconnect(void);
/* vRouter callback */
static int
dpdk_if_add(struct vr_interface *vif)
{
if (vr_dpdk_is_stop_flag_set())
return -EINPROGRESS;
if (vif_is_fabric(vif)) return dpdk_fabric_if_add(vif);
else if (vif_is_vm(vif)) return dpdk_virtual_if_add(vif);
else if (vif_is_vlan(vif)) return dpdk_virtual_vlan_if_add(vif);
else if (vif_is_namespace(vif)) return dpdk_af_packet_if_add(vif);
else if (vif_is_vhost(vif)) return dpdk_vhost_if_add(vif);
else if (vif_is_agent(vif)) return dpdk_agent_if_add(vif);
else if (vif_is_monitoring(vif)) return dpdk_monitoring_if_add(vif);
RTE_LOG(ERR, VROUTER,
"Error adding vif %d (%s): unsupported interface type %d transport %d\n",
vif->vif_idx, vif->vif_name, vif->vif_type, vif->vif_transport);
return -EFAULT;
}
static int
dpdk_if_del(struct vr_interface *vif)
{
if (vr_dpdk_is_stop_flag_set())
return -EINPROGRESS;
if (vif_is_fabric(vif) ||
vif_is_namespace(vif)) return dpdk_fabric_af_packet_if_del(vif);
else if (vif_is_vm(vif)) return dpdk_virtual_if_del(vif);
else if (vif_is_vlan(vif)) return dpdk_virtual_vlan_if_del(vif);
else if (vif_is_vhost(vif)) return dpdk_vhost_if_del(vif);
else if (vif_is_agent(vif)) return dpdk_agent_if_del(vif);
else if (vif_is_monitoring(vif)) return dpdk_monitoring_if_del(vif);
RTE_LOG(ERR, VROUTER,
"Error deleting vif %d: unsupported interface type %d transport %d\n",
vif->vif_idx, vif->vif_type, vif->vif_transport);
return -EFAULT;
}
/* vRouter callback */
static int
dpdk_if_del_tap(struct vr_interface *vif)
{
/* TODO: we untap interfaces at if_del */
return 0;
}
/* vRouter callback */
static int
dpdk_if_add_tap(struct vr_interface *vif)
{
/* TODO: we tap interfaces at if_add */
return 0;
}
static inline void
dpdk_hw_checksum_at_offset(struct vr_packet *pkt, unsigned offset)
{
struct rte_mbuf *m = vr_dpdk_pkt_to_mbuf(pkt);
struct vr_ip *iph = NULL;
struct vr_ip6 *ip6h = NULL;
unsigned char iph_len = 0, iph_proto = 0;
struct vr_tcp *tcph;
struct vr_udp *udph;
RTE_VERIFY(0 < offset);
if (pkt->vp_type == VP_TYPE_IP || pkt->vp_type == VP_TYPE_IPOIP) {
iph = (struct vr_ip *)pkt_data_at_offset(pkt, offset);
iph_len = iph->ip_hl * 4;
iph_proto = iph->ip_proto;
m->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
iph->ip_csum = 0;
} else if (pkt->vp_type == VP_TYPE_IP6 || pkt->vp_type == VP_TYPE_IP6OIP) {
ip6h = (struct vr_ip6 *)pkt_data_at_offset(pkt, offset);
iph_len = sizeof(struct vr_ip6);
iph_proto = ip6h->ip6_nxt;
m->ol_flags |= PKT_TX_IPV6;
} else {
/* Nothing to do if the packet is neither IPv4 nor IPv6. */
return;
}
/* Note: Intel NICs need the checksum set to zero
* and proper l2/l3 lens to be set.
*/
m->l3_len = iph_len;
m->l2_len = offset - rte_pktmbuf_headroom(m);
/* calculate TCP/UDP checksum */
if (likely(iph_proto == VR_IP_PROTO_UDP)) {
m->ol_flags |= PKT_TX_UDP_CKSUM;
udph = (struct vr_udp *)pkt_data_at_offset(pkt, offset + iph_len);
udph->udp_csum = 0;
if (iph)
udph->udp_csum = rte_ipv4_phdr_cksum((struct ipv4_hdr *)iph, m->ol_flags);
else if (ip6h)
udph->udp_csum = rte_ipv6_phdr_cksum((struct ipv6_hdr *)ip6h, m->ol_flags);
} else if (likely(iph_proto == VR_IP_PROTO_TCP)) {
m->ol_flags |= PKT_TX_TCP_CKSUM;
tcph = (struct vr_tcp *)pkt_data_at_offset(pkt, offset + iph_len);
tcph->tcp_csum = 0;
if (iph)
tcph->tcp_csum = rte_ipv4_phdr_cksum((struct ipv4_hdr *)iph, m->ol_flags);
else if (ip6h)
tcph->tcp_csum = rte_ipv6_phdr_cksum((struct ipv6_hdr *)ip6h, m->ol_flags);
}
}
static inline void
dpdk_ipv4_sw_iphdr_checksum_at_offset(struct vr_packet *pkt, unsigned offset)
{
struct vr_ip *iph;
RTE_VERIFY(0 < offset);
iph = (struct vr_ip *)pkt_data_at_offset(pkt, offset);
iph->ip_csum = vr_ip_csum(iph);
}
static inline void
dpdk_sw_checksum_at_offset(struct vr_packet *pkt, unsigned offset)
{
struct vr_ip *iph = NULL;
struct vr_ip6 *ip6h = NULL;
unsigned char iph_len = 0, iph_proto = 0;
struct vr_udp *udph;
struct vr_tcp *tcph;
RTE_VERIFY(0 < offset);
if (pkt->vp_type == VP_TYPE_IP || pkt->vp_type == VP_TYPE_IPOIP) {
iph = (struct vr_ip *)pkt_data_at_offset(pkt, offset);
iph->ip_csum = vr_ip_csum(iph);
iph_len = iph->ip_hl * 4;
iph_proto = iph->ip_proto;
} else if (pkt->vp_type == VP_TYPE_IP6 || pkt->vp_type == VP_TYPE_IP6OIP) {
ip6h = (struct vr_ip6 *)pkt_data_at_offset(pkt, offset);
iph_len = sizeof(struct vr_ip6);
iph_proto = ip6h->ip6_nxt;
} else {
/* Nothing to do if the packet is neither IPv4 nor IPv6. */
return;
}
if (iph_proto == VR_IP_PROTO_UDP) {
udph = (struct vr_udp *)pkt_data_at_offset(pkt, offset + iph_len);
udph->udp_csum = 0;
if (iph)
udph->udp_csum = rte_ipv4_udptcp_cksum((struct ipv4_hdr *)iph, udph);
else if (ip6h)
udph->udp_csum = rte_ipv6_udptcp_cksum((struct ipv6_hdr *)ip6h, udph);
} else if (iph_proto == VR_IP_PROTO_TCP) {
tcph = (struct vr_tcp *)pkt_data_at_offset(pkt, offset + iph_len);
tcph->tcp_csum = 0;
if (iph)
tcph->tcp_csum = rte_ipv4_udptcp_cksum((struct ipv4_hdr *)iph, tcph);
else if (ip6h)
tcph->tcp_csum = rte_ipv6_udptcp_cksum((struct ipv6_hdr *)ip6h, tcph);
}
}
static inline void
dpdk_ipv4_outer_tunnel_hw_checksum(struct vr_packet *pkt)
{
struct rte_mbuf *m = vr_dpdk_pkt_to_mbuf(pkt);
unsigned offset = pkt->vp_data + sizeof(struct ether_hdr);
struct vr_ip *iph = (struct vr_ip *)pkt_data_at_offset(pkt, offset);
unsigned iph_len = iph->ip_hl * 4;
m->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
iph->ip_csum = 0;
m->l3_len = iph_len;
m->l2_len = offset - rte_pktmbuf_headroom(m);
}
static inline void
dpdk_ipv4_outer_tunnel_sw_checksum(struct vr_packet *pkt)
{
unsigned offset = pkt->vp_data + sizeof(struct ether_hdr);
struct vr_ip *iph = (struct vr_ip *)pkt_data_at_offset(pkt, offset);
iph->ip_csum = vr_ip_csum(iph);
}
static inline void
dpdk_hw_checksum(struct vr_packet *pkt)
{
/* if a tunnel */
if (vr_pkt_type_is_overlay(pkt->vp_type)) {
/* calculate outer checksum in soft */
dpdk_ipv4_sw_iphdr_checksum_at_offset(pkt,
pkt->vp_data + sizeof(struct ether_hdr));
/* calculate inner checksum in hardware */
dpdk_hw_checksum_at_offset(pkt, pkt_get_inner_network_header_off(pkt));
} else if (VP_TYPE_IP == pkt->vp_type || VP_TYPE_IP6 == pkt->vp_type) {
/* normal IPv4 or IPv6 packet */
dpdk_hw_checksum_at_offset(pkt, pkt->vp_data + sizeof(struct ether_hdr));
}
}