/
vr_interface.c
2887 lines (2351 loc) · 76.8 KB
/
vr_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
/*
* vr_interface.c -- router interface management
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <vr_os.h>
#include <vr_types.h>
#include <vr_packet.h>
#include <vr_interface.h>
#include "vr_message.h"
#include "vr_sandesh.h"
#include "vr_mirror.h"
#include "vr_htable.h"
#include "vr_datapath.h"
#include "vr_bridge.h"
volatile bool agent_alive = false;
static struct vr_host_interface_ops *hif_ops;
static int vm_srx(struct vr_interface *, struct vr_packet *, unsigned short);
static int vm_rx(struct vr_interface *, struct vr_packet *, unsigned short);
static int eth_rx(struct vr_interface *, struct vr_packet *, unsigned short);
static mac_response_t vm_mac_request(struct vr_interface *, struct vr_packet *,
struct vr_forwarding_md *, unsigned char *);
static void vif_fat_flow_free(uint8_t **);
static int vif_fat_flow_add(struct vr_interface *, vr_interface_req *);
static bool vif_fat_flow_port_is_set(struct vr_interface *, uint8_t,
uint16_t);
void vif_attach(struct vr_interface *);
void vif_detach(struct vr_interface *);
int vr_gro_vif_add(struct vrouter *, unsigned int, char *, unsigned short);
struct vr_interface_stats *vif_get_stats(struct vr_interface *, unsigned short);
struct vr_interface *__vrouter_get_interface_os(struct vrouter *, unsigned int);
extern struct vr_host_interface_ops *vr_host_interface_init(void);
extern void vr_host_interface_exit(void);
extern void vr_host_vif_init(struct vrouter *);
extern struct vr_interface *vif_bridge_get_sub_interface(vr_htable_t,
unsigned short, unsigned char *);
extern int vif_bridge_get_index(struct vr_interface *, struct vr_interface *);
extern int vif_bridge_init(struct vr_interface *);
extern void vif_bridge_deinit(struct vr_interface *);
extern int vif_bridge_delete(struct vr_interface *, struct vr_interface *);
extern int vif_bridge_add(struct vr_interface *, struct vr_interface *);
extern void vhost_remove_xconnect(void);
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
struct vr_interface_stats *
vif_get_stats(struct vr_interface *vif, unsigned short cpu)
{
return &vif->vif_stats[cpu & VR_CPU_MASK];
}
static int
vif_discard_tx(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
vr_pfree(pkt, VP_DROP_INTERFACE_TX_DISCARD);
return 0;
}
static int
vif_discard_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id __attribute__((unused)))
{
vr_pfree(pkt, VP_DROP_INTERFACE_RX_DISCARD);
return 0;
}
void
vif_drop_pkt(struct vr_interface *vif, struct vr_packet *pkt, bool input)
{
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
if (input)
stats->vis_ierrors++;
else
stats->vis_oerrors++;
vr_pfree(pkt, VP_DROP_INTERFACE_DROP);
return;
}
/*
* in the rewrite case, we will assume the positive case of caller
* passing us valid rewrite ptr and len and will not check for those
*/
static unsigned char *
vif_cmn_rewrite(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *rewrite,
unsigned short len)
{
unsigned char *head;
if (!len)
return pkt_data(pkt);
if (pkt_head_space(pkt) < len) {
pkt = vr_pexpand_head(pkt, len - pkt_head_space(pkt));
if (!pkt)
return NULL;
}
head = pkt_push(pkt, len);
if (!head)
return NULL;
memcpy(head, rewrite, len);
return head;
}
static int
vr_interface_service_enable(struct vr_interface *vif)
{
int i;
/*
* handle the case of existing vrf assign table, which
* was not freed because the table was not empty. also,
* note that for existing vrf table, we do not reset the
* entries and the table users count, as per requirements
* from agent
*/
if (!vif->vif_vrf_table) {
vif->vif_vrf_table = vr_malloc(sizeof(struct vr_vrf_assign) *
VIF_VRF_TABLE_ENTRIES, VR_INTERFACE_VRF_TABLE_OBJECT);
if (!vif->vif_vrf_table)
return -ENOMEM;
for (i = 0; i < VIF_VRF_TABLE_ENTRIES; i++) {
vif->vif_vrf_table[i].va_vrf = -1;
vif->vif_vrf_table[i].va_nh_id = 0;
}
/* for the new table, there are no users */
vif->vif_vrf_table_users = 0;
}
vif->vif_rx = vm_srx;
return 0;
}
static void
vr_interface_service_disable(struct vr_interface *vif)
{
if (vif_is_virtual(vif))
vif->vif_rx = vm_rx;
else
vif->vif_rx = eth_rx;
/*
* once everybody sees the change, we are free to do whatever
* we want with the vrf assign table
*/
if (!vr_not_ready)
vr_delay_op();
/*
* it is possible that when this function is called from
* interface delete, the table users are +ve, and hence
* the memory will not be freed here. our saving grace
* is vif_free (called from last put operation), which
* takes care of freeing the memory
*/
if (vif->vif_vrf_table && !vif->vif_vrf_table_users) {
vr_free(vif->vif_vrf_table, VR_INTERFACE_VRF_TABLE_OBJECT);
vif->vif_vrf_table = NULL;
}
return;
}
/*
* xconnect mode
*
* xconnect mode, as of now, is relevant only for physical (ethX) and vhost
* interfaces. the purpose of xconnect mode is to make sure that the host
* management interface does not lose connectivity to network when agent is
* either not started or not alive.
*
* the xconnect mode works differently in different devices. since for ethernet
* interfaces, we would still like to process all packets destined to VMs,
* packets will have to be processed by the vrouter stack. however, for vhost
* interfaces, we just need the packets to be pushed to ethernet interface,
* and hence the packets are switched to the corresponding physical interface
*/
void
vif_set_xconnect(struct vr_interface *vif)
{
if (!vif)
return;
vif->vif_flags |= VIF_FLAG_XCONNECT;
return;
}
void
vif_remove_xconnect(struct vr_interface *vif)
{
if (!vif)
return;
vif->vif_flags &= ~VIF_FLAG_XCONNECT;
return;
}
int
vif_xconnect(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
struct vr_interface *bridge;
if (!vif)
goto free_pkt;
bridge = vif->vif_bridge;
if (bridge) {
vr_preset(pkt);
return bridge->vif_tx(bridge, pkt, fmd);
}
free_pkt:
if (vif)
vif_drop_pkt(vif, pkt, 1);
return 0;
}
/* agent driver */
#define AGENT_PKT_HEAD_SPACE (sizeof(struct vr_eth) + \
sizeof(struct agent_hdr))
static unsigned char *
agent_set_rewrite(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *rewrite,
unsigned short len)
{
unsigned char *head;
unsigned int hdr_len;
struct agent_hdr *hdr;
vr_preset(pkt);
hdr_len = sizeof(struct agent_hdr) + len;
if (pkt_head_space(pkt) < hdr_len) {
pkt = vr_pexpand_head(pkt, hdr_len - pkt_head_space(pkt));
if (!pkt)
return NULL;
}
head = pkt_push(pkt, hdr_len);
if (!head)
return NULL;
/* copy the rewrite first */
memcpy(head, rewrite, len);
hdr = (struct agent_hdr *)(head + len);
hdr->hdr_ifindex = htons(pkt->vp_if->vif_idx);
hdr->hdr_vrf = htons(fmd->fmd_dvrf);
/* this needs some thought */
hdr->hdr_cmd = htons(AGENT_TRAP_NEXTHOP);
hdr->hdr_cmd_param = 0;
return head;
}
static int
agent_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id __attribute__((unused)))
{
unsigned short cmd;
struct agent_hdr *hdr;
struct vr_forwarding_md fmd;
struct vr_interface *agent_vif;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
hdr = (struct agent_hdr *)pkt_pull(pkt, sizeof(struct vr_eth));
if (!hdr || !pkt_pull(pkt, sizeof(*hdr))) {
stats->vis_ierrors++;
vr_pfree(pkt, VP_DROP_PULL);
return 0;
}
/*
* Update the original (OS visible) packet to point to the
* l2 header of the injected packet
*/
vr_pset_data(pkt, pkt->vp_data);
cmd = ntohs(hdr->hdr_cmd);
switch (cmd) {
case AGENT_CMD_ROUTE:
/*
* XXX
* Packet with command "route" from agent may
* result in flow setup, this breaks the
* assumption that all packets for a flow will
* reach same CPU. Need a better way to handle this
*/
agent_vif = __vrouter_get_interface(vrouter_get(0),
ntohs(hdr->hdr_ifindex));
if (!agent_vif) {
agent_vif = vif;
}
pkt->vp_if = agent_vif;
if (ntohl(hdr->hdr_cmd_param) == CMD_PARAM_PACKET_CTRL) {
if (ntohl(hdr->hdr_cmd_param_1) == CMD_PARAM_1_DIAG) {
pkt->vp_flags |= VP_FLAG_DIAG;
}
}
vr_virtual_input(ntohs(hdr->hdr_vrf), agent_vif, pkt, VLAN_ID_INVALID);
break;
case AGENT_CMD_SWITCH:
vif = __vrouter_get_interface(vrouter_get(0), ntohs(hdr->hdr_ifindex));
if (!vif) {
stats->vis_ierrors++;
vr_pfree(pkt, VP_DROP_INVALID_IF);
return 0;
}
vr_init_forwarding_md(&fmd);
fmd.fmd_dvrf = ntohs(hdr->hdr_vrf);
pkt->vp_type = VP_TYPE_AGENT;
pkt_set_network_header(pkt, pkt->vp_data + sizeof(struct vr_eth));
pkt_set_inner_network_header(pkt,
pkt->vp_data + sizeof(struct vr_eth));
return vif->vif_tx(vif, pkt, &fmd);
break;
default:
vr_pfree(pkt, VP_DROP_INVALID_PACKET);
break;
}
return 0;
}
static int
agent_tx(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
stats->vis_obytes += pkt_len(pkt);
stats->vis_opackets++;
ret = hif_ops->hif_tx(vif, pkt);
if (ret != 0) {
ret = 0;
stats->vis_oerrors++;
}
return ret;
}
/*
* Return true if the packet may be truncated.
*/
static int
agent_trap_may_truncate(int trap_reason)
{
switch (trap_reason) {
case AGENT_TRAP_NEXTHOP:
case AGENT_TRAP_RESOLVE:
case AGENT_TRAP_FLOW_MISS:
case AGENT_TRAP_FLOW_ACTION_HOLD:
case AGENT_TRAP_ECMP_RESOLVE:
case AGENT_TRAP_HANDLE_DF:
case AGENT_TRAP_ZERO_TTL:
return 1;
case AGENT_TRAP_ARP:
case AGENT_TRAP_L2_PROTOCOLS:
case AGENT_TRAP_L3_PROTOCOLS:
case AGENT_TRAP_DIAG:
default:
return 0;
}
}
static int
agent_send(struct vr_interface *vif, struct vr_packet *pkt,
void *ifspecific)
{
unsigned short encap = VIF_ENCAP_TYPE_ETHER;
int len, head_space;
struct vr_eth *eth;
struct vr_forwarding_md fmd;
struct agent_hdr *hdr;
unsigned char *rewrite;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
struct vr_packet *pkt_c;
struct agent_send_params *params =
(struct agent_send_params *)ifspecific;
struct vr_flow_trap_arg *fta;
struct vr_df_trap_arg *dta;
bool truncate = false;
vr_preset(pkt);
if (pkt_is_gso(pkt)) {
truncate = true;
}
if ((params->trap_reason == AGENT_TRAP_HANDLE_DF) ||
(params->trap_reason == AGENT_TRAP_ZERO_TTL)) {
if (pkt_len(pkt) > VR_AGENT_MIN_PACKET_LEN)
truncate = true;
}
if (hif_ops->hif_get_encap)
encap = hif_ops->hif_get_encap(pkt->vp_if);
head_space = AGENT_PKT_HEAD_SPACE;
if (encap == VIF_ENCAP_TYPE_L3)
head_space += VR_ETHER_HLEN;
if (truncate || (pkt_head_space(pkt) < head_space)) {
len = pkt_len(pkt);
if (agent_trap_may_truncate(params->trap_reason)) {
len = MINIMUM(len, VR_AGENT_MIN_PACKET_LEN);
}
pkt_c = pkt_copy(pkt, 0, len);
if (pkt_c) {
vr_pfree(pkt, VP_DROP_DUPLICATED);
pkt = pkt_c;
}
}
pkt->vp_type = VP_TYPE_AGENT;
if (encap == VIF_ENCAP_TYPE_L3) {
eth = (struct vr_eth *)(pkt_push(pkt, VR_ETHER_HLEN));
if (!eth)
goto drop;
memcpy(eth->eth_dmac, vif->vif_mac, VR_ETHER_ALEN);
memcpy(eth->eth_smac, vif->vif_mac, VR_ETHER_ALEN);
if (pkt->vp_type == VP_TYPE_IP6)
eth->eth_proto = htons(VR_ETH_PROTO_IP6);
else
eth->eth_proto = htons(VR_ETH_PROTO_IP);
}
pkt_set_network_header(pkt, pkt->vp_data + sizeof(struct vr_eth));
pkt_set_inner_network_header(pkt,
pkt->vp_data + sizeof(struct vr_eth));
hdr = (struct agent_hdr *)pkt_push(pkt, sizeof(struct agent_hdr));
if (!hdr)
goto drop;
hdr->hdr_ifindex = htons(pkt->vp_if->vif_idx);
hdr->hdr_vrf = htons(params->trap_vrf);
hdr->hdr_cmd = htons(params->trap_reason);
switch (params->trap_reason) {
case AGENT_TRAP_FLOW_MISS:
case AGENT_TRAP_FLOW_ACTION_HOLD:
if (params->trap_param) {
fta = (struct vr_flow_trap_arg *)(params->trap_param);
hdr->hdr_cmd_param = htonl(fta->vfta_index);
hdr->hdr_cmd_param_1 = htonl(fta->vfta_nh_index);
hdr->hdr_cmd_param_2 = htonl(fta->vfta_stats.flow_bytes);
hdr->hdr_cmd_param_3 = htonl(fta->vfta_stats.flow_packets);
hdr->hdr_cmd_param_4 = htonl((fta->vfta_stats.flow_bytes_oflow |
(fta->vfta_stats.flow_packets_oflow << 16)));
hdr->hdr_cmd_param_5 = fta->vfta_gen_id;
}
break;
case AGENT_TRAP_ECMP_RESOLVE:
case AGENT_TRAP_SOURCE_MISMATCH:
if (params->trap_param)
hdr->hdr_cmd_param = htonl(*(unsigned int *)(params->trap_param));
break;
case AGENT_TRAP_DIAG:
if (params->trap_param)
hdr->hdr_cmd_param = htonl(*(unsigned int *)(params->trap_param));
break;
case AGENT_TRAP_HANDLE_DF:
dta = (struct vr_df_trap_arg *)(params->trap_param);
hdr->hdr_cmd_param = htonl(dta->df_mtu);
hdr->hdr_cmd_param_1 = htonl(dta->df_flow_index);
break;
default:
hdr->hdr_cmd_param = 0;
hdr->hdr_cmd_param_1 = 0;
break;
}
rewrite = pkt_push(pkt, VR_ETHER_HLEN);
if (!rewrite)
goto drop;
memcpy(rewrite, vif->vif_rewrite, VR_ETHER_HLEN);
vr_init_forwarding_md(&fmd);
return vif->vif_tx(vif, pkt, &fmd);
drop:
stats->vis_oerrors++;
vr_pfree(pkt, VP_DROP_PUSH);
return 0;
}
static int
agent_drv_del(struct vr_interface *vif)
{
hif_ops->hif_del_tap(vif);
vif->vif_tx = vif_discard_tx;
vif->vif_rx = vif_discard_rx;
return hif_ops->hif_del(vif);
}
static int
agent_drv_add(struct vr_interface *vif,
vr_interface_req *vifr __attribute__((unused)))
{
int ret;
if (!vif->vif_mtu)
vif->vif_mtu = 1514;
vif->vif_set_rewrite = agent_set_rewrite;
/**
* At this moment, vif_tx() and vif_rx() are vif_discard_tx() and
* vif_discard_rx(). Let them stay this way until we succesfully call
* platform-dependent implementation of hif_add(). On some platfoms it may
* set up resoruces needed by hif_tx(), which is called by agent_tx().
*/
ret = hif_ops->hif_add(vif);
if (ret) {
return ret;
}
vif->vif_tx = agent_tx;
vif->vif_rx = agent_rx;
vif->vif_send = agent_send;
ret = hif_ops->hif_add_tap(vif);
if (ret)
hif_ops->hif_del(vif);
return ret;
}
/* end agent driver */
/* vhost driver */
static mac_response_t
vhost_mac_request(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *dmac)
{
struct vr_arp *sarp;
if (pkt->vp_type == VP_TYPE_ARP) {
sarp = (struct vr_arp *)pkt_data(pkt);
if (IS_LINK_LOCAL_IP(sarp->arp_dpa) ||
(vif->vif_type == VIF_TYPE_GATEWAY)) {
VR_MAC_COPY(dmac, vif->vif_mac);
return MR_PROXY;
}
}
if (vif->vif_type == VIF_TYPE_GATEWAY)
return MR_DROP;
return MR_XCONNECT;
}
static int
vhost_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id __attribute__((unused)))
{
struct vr_forwarding_md fmd;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
/* please see the text on xconnect mode */
vr_init_forwarding_md(&fmd);
fmd.fmd_dvrf = vif->vif_vrf;
if (vif_mode_xconnect(vif))
return vif_xconnect(vif, pkt, &fmd);
return vr_fabric_input(vif, pkt, vlan_id);
}
static int
vhost_tx(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
unsigned short eth_proto, pull_len = 0;
unsigned char *new_eth, *eth;
struct vr_eth *eth_hdr;
struct vr_vlan_hdr *vlan;
struct vr_interface *in_vif;
stats->vis_obytes += pkt_len(pkt);
stats->vis_opackets++;
if (vif->vif_type == VIF_TYPE_XEN_LL_HOST)
memcpy(pkt_data(pkt), vif->vif_mac, sizeof(vif->vif_mac));
else if (vif->vif_type == VIF_TYPE_HOST) {
in_vif = pkt->vp_if;
if (!hif_ops->hif_get_encap ||
(hif_ops->hif_get_encap(in_vif) == VIF_ENCAP_TYPE_ETHER)) {
/* Untag any tagged packets */
eth = pkt_data(pkt);
eth_proto = ntohs(*(unsigned short *)(eth + VR_ETHER_PROTO_OFF));
while (eth_proto == VR_ETH_PROTO_VLAN) {
vlan = (struct vr_vlan_hdr *)(pkt_data(pkt) + pull_len +
VR_ETHER_HLEN);
eth_proto = ntohs(vlan->vlan_proto);
pull_len += sizeof(*vlan);
}
/* If there are any vlan tags */
if (pull_len) {
new_eth = pkt_pull(pkt, pull_len);
if (!new_eth) {
vr_pfree(pkt, VP_DROP_PULL);
return 0;
}
memmove(new_eth, eth, (2 * VR_ETHER_ALEN));
}
/*
* Rewrite dst MAC for the link local (169.254/16) addresses.
* The replies from VMs are destined to VRRP MAC, hence
* they are discarded by the IP stack.
* For the Linux platform we fix it by setting skb->pkt_type
* to PACKET_HOST in linux_if_rx() function.
* The following code fixes MACs for all the platforms.
*/
if (vif_is_virtual(in_vif)) {
eth_hdr = (struct vr_eth *)pkt_data(pkt);
memcpy(eth_hdr->eth_dmac, vif->vif_mac, VR_ETHER_ALEN);
}
} else {
eth_hdr = (struct vr_eth *)pkt_push(pkt, VR_ETHER_HLEN);
memcpy(eth_hdr->eth_dmac, vif->vif_mac, VR_ETHER_ALEN);
memcpy(eth_hdr->eth_smac, vif->vif_mac, VR_ETHER_ALEN);
if (pkt->vp_type == VP_TYPE_IP6)
eth_hdr->eth_proto = htons(VR_ETH_PROTO_IP6);
else
eth_hdr->eth_proto = htons(VR_ETH_PROTO_IP);
}
}
ret = hif_ops->hif_rx(vif, pkt);
if (ret < 0) {
ret = 0;
stats->vis_oerrors++;
}
return ret;
}
static int
vhost_drv_del(struct vr_interface *vif)
{
vif->vif_tx = vif_discard_tx;
vif->vif_rx = vif_discard_rx;
return hif_ops->hif_del(vif);
}
static int
vhost_drv_add(struct vr_interface *vif,
vr_interface_req *vifr __attribute__((unused)))
{
int ret = 0;
if (!vif->vif_mtu)
vif->vif_mtu = 1514;
vif->vif_set_rewrite = vif_cmn_rewrite;
vif->vif_tx = vhost_tx;
vif->vif_rx = vhost_rx;
vif->vif_mac_request = vhost_mac_request;
ret = hif_ops->hif_add(vif);
if (ret)
return ret;
/*
* add tap to the corresponding physical interface, now
* that vhost is functional
*/
if (vif->vif_bridge) {
ret = hif_ops->hif_add_tap(vif->vif_bridge);
if (ret)
return ret;
}
return 0;
}
/* end vhost driver */
/* vlan driver */
static int
vlan_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id __attribute__((unused)))
{
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
pkt->vp_if = vif;
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
if (vr_untag_pkt(pkt)) {
stats->vis_ierrors++;
vr_pfree(pkt, VP_DROP_PULL);
return 0;
}
vr_pset_data(pkt, pkt->vp_data);
return vr_virtual_input(vif->vif_vrf, vif, pkt, VLAN_ID_INVALID);
}
static int
vlan_tx(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret = 0;
struct vr_interface *pvif;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
if (!(pkt->vp_flags & VP_FLAG_GRO)) {
stats->vis_obytes += pkt_len(pkt);
stats->vis_opackets++;
}
fmd->fmd_vlan = vif->vif_vlan_id;
if (vif_is_vlan(vif) && vif->vif_ovlan_id) {
fmd->fmd_vlan = vif->vif_ovlan_id;
if (vr_tag_pkt(pkt, vif->vif_ovlan_id)) {
goto drop;
}
vr_pset_data(pkt, pkt->vp_data);
}
pvif = vif->vif_parent;
if (!pvif)
goto drop;
ret = pvif->vif_tx(pvif, pkt, fmd);
if (ret < 0) {
ret = 0;
goto drop;
}
return ret;
drop:
vr_pfree(pkt, VP_DROP_INVALID_IF);
stats->vis_oerrors++;
return ret;
}
static int
vlan_drv_del(struct vr_interface *vif)
{
int ret = 0;
struct vr_interface *pvif;
pvif = vif->vif_parent;
if (!pvif)
return 0;
if (pvif->vif_driver->drv_delete_sub_interface) {
ret = pvif->vif_driver->drv_delete_sub_interface(pvif, vif);
if (ret)
goto exit_del;
}
exit_del:
if (vif->vif_src_mac) {
vr_free(vif->vif_src_mac, VR_INTERFACE_MAC_OBJECT);
vif->vif_src_mac = NULL;
}
return ret;
}
static int
vlan_drv_add(struct vr_interface *vif, vr_interface_req *vifr)
{
struct vr_interface *pvif = NULL;
if ((unsigned int)(vifr->vifr_parent_vif_idx) > VR_MAX_INTERFACES)
return -EINVAL;
if (((unsigned short)(vifr->vifr_vlan_id) >= VLAN_ID_MAX) ||
((unsigned short)(vifr->vifr_ovlan_id) >= VLAN_ID_MAX))
return -EINVAL;
if (vifr->vifr_src_mac_size && vifr->vifr_src_mac) {
if (vifr->vifr_src_mac_size != VR_ETHER_ALEN)
return -EINVAL;
vif->vif_src_mac = vr_malloc(VR_ETHER_ALEN, VR_INTERFACE_MAC_OBJECT);
if (!vif->vif_src_mac)
return -ENOMEM;
memcpy(vif->vif_src_mac, vifr->vifr_src_mac, VR_ETHER_ALEN);
}
if (!vif->vif_mtu)
vif->vif_mtu = 1514;
vif->vif_set_rewrite = vif_cmn_rewrite;
vif->vif_tx = vlan_tx;
vif->vif_rx = vlan_rx;
vif->vif_mac_request = vm_mac_request;
vif->vif_vlan_id = vifr->vifr_vlan_id;
vif->vif_ovlan_id = vifr->vifr_ovlan_id;
pvif = vrouter_get_interface(vifr->vifr_rid, vifr->vifr_parent_vif_idx);
if (!pvif)
return -EINVAL;
vif->vif_parent = pvif;
if (!pvif->vif_driver->drv_add_sub_interface)
return -EINVAL;
return pvif->vif_driver->drv_add_sub_interface(pvif, vif);
}
/* end vlan driver */
/* eth driver */
static int
vm_srx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id)
{
unsigned short vrf;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
if (vlan_id >= VIF_VRF_TABLE_ENTRIES)
vrf = vif->vif_vrf;
else
vrf = vif->vif_vrf_table[vlan_id].va_vrf;
return vr_virtual_input(vrf, vif, pkt, vlan_id);
}
static mac_response_t
vm_mac_request(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *dmac)
{
if (pkt->vp_type == VP_TYPE_ARP) {
return vm_arp_request(vif, pkt, fmd, dmac);
} else if (pkt->vp_type == VP_TYPE_IP6) {
return vm_neighbor_request(vif, pkt, fmd, dmac);
}
return MR_DROP;
}
static int
vm_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id)
{
struct vr_interface *sub_vif = NULL;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
struct vr_eth *eth = (struct vr_eth *)pkt_data(pkt);
if (vlan_id != VLAN_ID_INVALID && vlan_id < VLAN_ID_MAX) {
if (vif->vif_btable) {
sub_vif = vif_bridge_get_sub_interface(vif->vif_btable, vlan_id,
eth->eth_smac);
} else {
if (vif->vif_sub_interfaces)
sub_vif = vif->vif_sub_interfaces[vlan_id];
}
if (sub_vif)
return sub_vif->vif_rx(sub_vif, pkt, VLAN_ID_INVALID);
}
/*
* there is a requirement that stats be not counted in both the
* underlying interface and in the sub-interface (double count).
* this behavior is a specific requirement for VM interfaces since
* the VM interface and the sub-interface on top are considered
* two different VMIs and can be in two different VRFs. Counting
* stats in both interfaces will wrongly put statistics in a VRF
* where the statistics should not have been counted. hence the
* stats count block is below and not the first thing that we do
* in this function.
*/
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
return vr_virtual_input(vif->vif_vrf, vif, pkt, vlan_id);
}
static int
tun_rx(struct vr_interface *vif, struct vr_packet *pkt,
unsigned short vlan_id)
{
unsigned char first_b;
struct vr_forwarding_md fmd;
struct vr_interface_stats *stats = vif_get_stats(vif, pkt->vp_cpu);
stats->vis_ibytes += pkt_len(pkt);
stats->vis_ipackets++;
if (vif_mode_xconnect(vif))
pkt->vp_flags |= VP_FLAG_TO_ME;
first_b = *pkt_data(pkt);
first_b &= 0xf0;
switch (first_b) {
case 0x40:
pkt->vp_type = VP_TYPE_IP;
break;
case 0x60:
pkt->vp_type = VP_TYPE_IP6;
break;
default:
vr_pfree(pkt, VP_DROP_INVALID_PACKET);
return 0;
}
pkt_set_network_header(pkt, pkt->vp_data);
vr_init_forwarding_md(&fmd);
fmd.fmd_vlan = vlan_id;
fmd.fmd_dvrf = vif->vif_vrf;
vr_l3_input(pkt, &fmd);
return 0;
}
static unsigned char *
eth_set_rewrite(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *rewrite,
unsigned short len)
{
if (!len)
return pkt_data(pkt);
if ((pkt->vp_if->vif_type == VIF_TYPE_HOST) &&
!(pkt->vp_flags & VP_FLAG_FROM_DP)) {
vr_preset(pkt);
return pkt_data(pkt);
}
return vif_cmn_rewrite(vif, pkt, fmd, rewrite, len);
}
static mac_response_t
eth_mac_request(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *dmac)
{