forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_tunnel.c
936 lines (752 loc) · 24.1 KB
/
ip_tunnel.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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*
* IPv4 tunnel commom routines and control plane codes.
*
* [email protected], Dec 2017, initial.
*/
#include <assert.h>
#include <netinet/ip.h>
#include <linux/if_ether.h>
#include "list.h"
#include "conf/common.h"
#include "netif.h"
#include "ipv4.h"
#include "icmp.h"
#include "ctrl.h"
#include "ip_tunnel.h"
#define TUNNEL
#define RTE_LOGTYPE_TUNNEL RTE_LOGTYPE_USER1
#ifndef IPV4_MIN_MTU
#define IPV4_MIN_MTU 68
#endif
static rte_rwlock_t ip_tunnel_lock;
static struct list_head ip_tunnel_ops_list;
static int tunnel_register_ops(struct ip_tunnel_ops *new)
{
struct ip_tunnel_ops *ops;
assert(new);
rte_rwlock_write_lock(&ip_tunnel_lock);
list_for_each_entry(ops, &ip_tunnel_ops_list, list) {
if (strcmp(ops->kind, new->kind) == 0) {
rte_rwlock_write_unlock(&ip_tunnel_lock);
return EDPVS_EXIST;
}
}
list_add_tail(&new->list, &ip_tunnel_ops_list);
rte_rwlock_write_unlock(&ip_tunnel_lock);
return EDPVS_OK;
}
static int tunnel_unregister_ops(struct ip_tunnel_ops *ops)
{
assert(ops);
rte_rwlock_write_lock(&ip_tunnel_lock);
list_del(&ops->list);
rte_rwlock_write_unlock(&ip_tunnel_lock);
return EDPVS_OK;
}
static struct ip_tunnel_ops *tunnel_find_ops(const char *kind)
{
struct ip_tunnel_ops *ops;
rte_rwlock_read_lock(&ip_tunnel_lock);
list_for_each_entry(ops, &ip_tunnel_ops_list, list) {
if (strcmp(ops->kind, kind) == 0) {
rte_rwlock_read_unlock(&ip_tunnel_lock);
return ops;
}
}
rte_rwlock_read_unlock(&ip_tunnel_lock);
return NULL;
}
static inline struct hlist_head *
tunnel_hash_head(struct ip_tunnel_tab *tab, __be32 key, __be32 remote)
{
uint32_t h = (uint32_t)key ^ (uint32_t)remote;
return &tab->tunnels[h % IP_TNL_HASH_SIZE];
}
static inline void tunnel_clear_rt_cache(struct ip_tunnel *tnl)
{
if (tnl->rt_cache) {
route4_put(tnl->rt_cache);
tnl->rt_cache = NULL;
}
}
/* linux:ip_tunnel_bind_dev
* return MTU of tunnel device. */
static int tunnel_bind_dev(struct netif_port *dev)
{
struct ip_tunnel *tnl = netif_priv(dev);
const struct iphdr *tiph = &tnl->params.iph;
struct netif_port *linkdev = NULL;
int mtu = ETH_DATA_LEN; /* 1500 */
int t_hlen = tnl->hlen + sizeof(struct iphdr);
/* guess output device to choose mtu and headroom */
if (tiph->daddr) {
struct route_entry *rt;
struct flow4 fl4 = {
.fl4_proto = tiph->protocol,
.fl4_daddr.s_addr = tiph->daddr,
.fl4_saddr.s_addr = tiph->saddr,
.fl4_tos = tiph->tos,
.fl4_oif = tnl->link,
};
rt = route4_output(&fl4);
if (rt) {
linkdev = rt->port;
route4_put(rt);
}
tunnel_clear_rt_cache(tnl);
}
if (!linkdev && tnl->link)
linkdev = tnl->link;
if (linkdev)
mtu = linkdev->mtu;
mtu -= dev->hw_header_len + t_hlen;
if (mtu < IPV4_MIN_MTU)
mtu = IPV4_MIN_MTU;
return mtu;
}
static struct netif_port *tunnel_create(struct ip_tunnel_tab *tab,
const struct ip_tunnel_ops *ops,
const struct ip_tunnel_param *par)
{
struct netif_port *dev;
struct ip_tunnel *tnl;
struct ip_tunnel_param params;
int err;
assert(tab && ops && par);
params = *par; /* may modified */
if (netif_port_count() >= NETIF_MAX_PORTS) {
RTE_LOG(ERR, TUNNEL, "%s: exceeding specification limits(%d)",
__func__, NETIF_MAX_PORTS);
return NULL;
}
/* set ifname template if not assigned. */
if (!strlen(params.ifname))
snprintf(params.ifname, IFNAMSIZ, "%s%%d", ops->kind);
dev = netif_alloc(ops->priv_size, params.ifname, 1, 1, ops->setup);
if (!dev)
return NULL;
/* syn back ifname, it may generated. */
snprintf(params.ifname, IFNAMSIZ, "%.15s", dev->name);
tnl = netif_priv(dev);
INIT_HLIST_NODE(&tnl->hlist);
tnl->dev = dev;
tnl->tab = tab;
tnl->params = params;
if (strlen(params.link)) {
tnl->link = netif_port_get_by_name(params.link);
if (!tnl->link) {
RTE_LOG(WARNING, TUNNEL, "%s: invalid link device\n", __func__);
tnl->params.link[0] = '\0';
}
}
dev->type = PORT_TYPE_TUNNEL;
dev->hw_header_len = 0; /* no l2 header or tunnel,
set before tunnel_bind_dev */
if (tnl->link) {
dev->flag |= tnl->link->flag;
ether_addr_copy(&tnl->link->addr, &dev->addr);
}
dev->flag |= NETIF_PORT_FLAG_RUNNING; /* XXX */
dev->flag |= NETIF_PORT_FLAG_NO_ARP;
dev->flag &= ~NETIF_PORT_FLAG_TX_IP_CSUM_OFFLOAD;
dev->flag &= ~NETIF_PORT_FLAG_TX_TCP_CSUM_OFFLOAD;
dev->flag &= ~NETIF_PORT_FLAG_TX_UDP_CSUM_OFFLOAD;
err = netif_port_register(dev);
if (err != EDPVS_OK) {
netif_free(dev);
return NULL;
}
/* set MTU after op_init, need calc tnl->hlen first */
dev->mtu = tunnel_bind_dev(dev);
/* insert to table */
hlist_add_head(&tnl->hlist, tunnel_hash_head(tab, params.i_key,
params.iph.daddr));
tab->nb_tnl++;
return dev;
}
static int tunnel_change(struct netif_port *dev,
const struct ip_tunnel_param *params)
{
struct ip_tunnel *tnl = netif_priv(dev);
struct netif_port *link;
assert(dev && dev->type == PORT_TYPE_TUNNEL && params && tnl->tab);
if (strlen(params->link)) {
link = netif_port_get_by_name(params->link);
if (!link)
return EDPVS_NODEV;
tnl->link = link;
}
tunnel_clear_rt_cache(tnl);
hlist_del(&tnl->hlist);
tnl->params = *params; /* FIXME: all params changes ! */
hlist_add_head(&tnl->hlist, tunnel_hash_head(tnl->tab, params->i_key,
params->iph.daddr));
dev->mtu = tunnel_bind_dev(dev);
if (tnl->tab->ops->change)
return tnl->tab->ops->change(dev, params);
return EDPVS_OK;
}
static int tunnel_destroy(struct ip_tunnel_tab *tab, struct netif_port *dev)
{
struct ip_tunnel *tnl = netif_priv(dev);
assert(dev && dev->type == PORT_TYPE_TUNNEL);
hlist_del(&tnl->hlist);
tab->nb_tnl--;
if (tab->fb_tunnel_dev == dev)
tab->fb_tunnel_dev = NULL;
netif_port_unregister(dev);
return netif_free(dev);
}
/* linux:ip_tunnel_key_match */
static bool tunnel_key_match(const struct ip_tunnel_param *p,
__be16 flags, __be32 key)
{
if (p->i_flags & TUNNEL_F_KEY) {
if (flags & TUNNEL_F_KEY)
return key == p->i_key;
else
return false;
} else {
return !(flags & TUNNEL_F_KEY);
}
}
static inline bool tunnel_link_match(struct ip_tunnel *tnl, portid_t port)
{
if (port == NETIF_PORT_ID_INVALID && !tnl->link)
return true;
else if (tnl->link && tnl->link->id == port)
return true;
return false;
}
static int tunnel_dump_table(struct ip_tunnel_tab *tab,
struct ip_tunnel_param *pars,
size_t npar, portid_t link)
{
int h, cnt = 0;
struct ip_tunnel *tnl;
assert(tab && pars);
for (h = 0; h < IP_TNL_HASH_SIZE; h++) {
hlist_for_each_entry(tnl, &tab->tunnels[h], hlist) {
if (cnt >= npar)
break;
assert(tnl->dev && tnl->dev->type == PORT_TYPE_TUNNEL);
if (link != NETIF_PORT_ID_INVALID &&
!tunnel_link_match(tnl, link))
continue;
memcpy(pars + cnt, &tnl->params, sizeof(*pars));
cnt++;
}
}
return cnt;
}
/* linux:tnl_update_pmtu */
static int tunnel_update_pmtu(struct netif_port *dev, struct rte_mbuf *mbuf,
struct route_entry *rt, __be16 df,
const struct iphdr *iiph)
{
struct ip_tunnel *tnl = netif_priv(dev);
int pkt_size = mbuf->pkt_len - tnl->hlen - dev->hw_header_len;
int mtu;
if (df)
mtu = rt->mtu - dev->hw_header_len - sizeof(struct iphdr) - tnl->hlen;
else
mtu = rt->mtu ? : dev->mtu;
if (mbuf->packet_type == ETHER_TYPE_IPv4) {
if ((iiph->frag_off & htons(IP_DF)) && mtu < pkt_size) {
icmp_send(mbuf, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
return EDPVS_FRAG;
}
}
return EDPVS_OK;
}
static int tunnel_xmit(struct rte_mbuf *mbuf, __be32 src, __be32 dst,
uint8_t proto, uint8_t tos, uint8_t ttl, __be16 df)
{
struct iphdr *oiph; /* outter IP header */
oiph = (struct iphdr *)rte_pktmbuf_prepend(mbuf, sizeof(*oiph));
if (!oiph) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROOM;
}
oiph->version = 4;
oiph->ihl = sizeof(struct iphdr) >> 2;
oiph->frag_off = df;
oiph->protocol = proto;
oiph->tos = tos;
oiph->daddr = dst;
oiph->saddr = src;
oiph->ttl = ttl;
oiph->id = ip4_select_id((struct ipv4_hdr *)oiph);
return ipv4_local_out(mbuf);
}
static int tunnel_so_set(sockoptid_t opt, const void *arg, size_t inlen)
{
const struct ip_tunnel_param *params = arg;
struct netif_port *dev;
struct ip_tunnel *tnl;
struct ip_tunnel_ops *ops = NULL;
int err = EDPVS_INVAL;
assert(params && inlen >= sizeof(*params));
/* find the tunnel mode first */
dev = netif_port_get_by_name(params->ifname);
if (dev) {
if (dev->type != PORT_TYPE_TUNNEL) {
RTE_LOG(ERR, TUNNEL, "%s: not tunnel device\n", __func__);
return EDPVS_INVAL;
}
tnl = netif_priv(dev);
ops = tnl->tab->ops;
} else if (strlen(params->kind)) {
ops = tunnel_find_ops(params->kind);
if (!ops) {
RTE_LOG(ERR, TUNNEL, "%s: invalid tunnel mode\n", __func__);
return EDPVS_INVAL;
}
}
if (!ops && (opt == SOCKOPT_TUNNEL_ADD || opt == SOCKOPT_TUNNEL_REPLACE)) {
RTE_LOG(ERR, TUNNEL, "%s: cannot determine tunnel mode\n", __func__);
return EDPVS_INVAL;
}
if (!dev && (opt == SOCKOPT_TUNNEL_DEL || opt == SOCKOPT_TUNNEL_CHANGE ||
opt == SOCKOPT_TUNNEL_REPLACE)) {
return EDPVS_NOTEXIST;
}
rte_rwlock_write_lock(&ip_tunnel_lock);
switch (opt) {
case SOCKOPT_TUNNEL_ADD:
if (dev) {
err = EDPVS_EXIST;
break;
}
dev = tunnel_create(ops->tab, ops, params);
err = dev ? EDPVS_OK : EDPVS_RESOURCE;
break;
case SOCKOPT_TUNNEL_DEL:
err = tunnel_destroy(ops->tab, dev);
break;
case SOCKOPT_TUNNEL_CHANGE:
err = tunnel_change(dev, params);
break;
case SOCKOPT_TUNNEL_REPLACE:
err = tunnel_destroy(ops->tab, dev);
if (err != EDPVS_OK)
break;
dev = tunnel_create(ops->tab, ops, params);
err = dev ? EDPVS_OK : EDPVS_RESOURCE;
break;
default:
err = EDPVS_NOTSUPP;
break;
}
rte_rwlock_write_unlock(&ip_tunnel_lock);
return err;
}
static int tunnel_so_get(sockoptid_t opt, const void *arg, size_t inlen,
void **out, size_t *outlen)
{
const struct ip_tunnel_param *params = arg;
struct netif_port *dev, *link = NULL;
const struct ip_tunnel *tnl;
struct ip_tunnel_ops *ops;
struct ip_tunnel_tab *tab;
struct ip_tunnel_param *tp_arr = NULL;
size_t tp_cnt = 0; /* number of tunnel param */
int err = EDPVS_OK;
assert(params && inlen >= sizeof(*params) && out && outlen);
rte_rwlock_read_lock(&ip_tunnel_lock);
/* device name is indicated */
if (strlen(params->ifname)) {
dev = netif_port_get_by_name(params->ifname);
if (!dev) {
err = EDPVS_NOTEXIST;
goto out;
}
if (dev->type != PORT_TYPE_TUNNEL) {
RTE_LOG(ERR, TUNNEL, "%s: not tunnel device\n", __func__);
err = EDPVS_INVAL;
goto out;
}
/* no more check, need we ? */
tnl = netif_priv(dev);
tp_cnt = 1;
tp_arr = rte_malloc(NULL, sizeof(*tp_arr) * tp_cnt, 0);
if (!tp_arr) {
err = EDPVS_NOMEM;
goto out;
}
memcpy(tp_arr, &tnl->params, sizeof(*tp_arr));
goto done;
}
if (strlen(params->link))
link = netif_port_get_by_name(params->link);
/* for specific table */
if (strlen(params->kind)) {
ops = tunnel_find_ops(params->kind);
if (!ops) {
RTE_LOG(ERR, TUNNEL, "%s: invalid tunnel mode\n", __func__);
err = EDPVS_INVAL;
goto out;
}
tab = ops->tab;
assert(tab);
/* rte_malloc() do not support 0 size allocate,
* we cannot return EDPVS_NOMEM for that case. */
if (!tab->nb_tnl)
goto done;
tp_arr = rte_malloc(NULL, sizeof(*tp_arr) * tab->nb_tnl, 0);
if (!tp_arr) {
err = EDPVS_NOMEM;
goto out;
}
tp_cnt = tunnel_dump_table(tab, tp_arr, tab->nb_tnl,
link ? link->id : NETIF_PORT_ID_INVALID);
goto done;
}
/* for each table */
list_for_each_entry(ops, &ip_tunnel_ops_list, list) {
void *new_ptr;
size_t size;
tab = ops->tab;
/* rte_realloc() do not support 0 size,
* we cannot return EDPVS_NOMEM. */
size = sizeof(*tp_arr) * (tab->nb_tnl + tp_cnt);
if (!size)
continue;
/* realloc could be slow, but need optimize ? */
new_ptr = rte_realloc(tp_arr, size, 0);
if (!new_ptr) {
rte_free(tp_arr);
err = EDPVS_NOMEM;
goto out;
}
tp_arr = new_ptr;
tp_cnt += tunnel_dump_table(tab, tp_arr + tp_cnt, tab->nb_tnl,
link ? link->id : NETIF_PORT_ID_INVALID);
}
done:
*out = tp_arr;
*outlen = tp_cnt * sizeof(*tp_arr);
out:
rte_rwlock_read_unlock(&ip_tunnel_lock);
return err;
}
int ip_tunnel_init_tab(struct ip_tunnel_tab *tab, struct ip_tunnel_ops *ops,
const char *fbname)
{
int i;
struct ip_tunnel_param params = {};
assert(tab && ops && fbname);
for (i = 0; i < NELEMS(tab->tunnels); i++)
INIT_HLIST_HEAD(&tab->tunnels[i]);
if (fbname)
snprintf(params.ifname, IFNAMSIZ, "%s", fbname);
snprintf(params.kind, IFNAMSIZ, "%s", ops->kind);
#if 0 /* need it ? */
rte_rwlock_write_lock(&ip_tunnel_lock);
tab->fb_tunnel_dev = tunnel_create(tab, ops, ¶ms);
if (!tab->fb_tunnel_dev) {
RTE_LOG(WARNING, TUNNEL, "%s: fail to create fb dev for %s.\n",
__func__, ops->kind);
}
rte_rwlock_write_unlock(&ip_tunnel_lock);
#endif
tab->ops = ops;
ops->tab = tab;
tunnel_register_ops(ops);
return EDPVS_OK;
}
int ip_tunnel_term_tab(struct ip_tunnel_tab *tab)
{
int h;
struct ip_tunnel *tun;
struct hlist_node *n;
assert(tab && tab->ops);
tunnel_unregister_ops(tab->ops);
rte_rwlock_write_lock(&ip_tunnel_lock);
if (tab->fb_tunnel_dev) {
tunnel_destroy(tab, tab->fb_tunnel_dev);
tab->fb_tunnel_dev = NULL;
}
for (h = 0; h < NELEMS(tab->tunnels); h++) {
hlist_for_each_entry_safe(tun, n, &tab->tunnels[h], hlist) {
hlist_del(&tun->hlist);
tunnel_destroy(tab, tun->dev);
}
}
rte_rwlock_write_unlock(&ip_tunnel_lock);
return EDPVS_OK;
}
static struct dpvs_sockopts ip_tunnel_sockopts = {
.version = SOCKOPT_VERSION,
.set_opt_min = SOCKOPT_TUNNEL_ADD,
.set_opt_max = SOCKOPT_TUNNEL_REPLACE,
.set = tunnel_so_set,
.get_opt_min = SOCKOPT_TUNNEL_SHOW,
.get_opt_max = SOCKOPT_TUNNEL_SHOW,
.get = tunnel_so_get,
};
int ip_tunnel_init(void)
{
int err;
rte_rwlock_init(&ip_tunnel_lock);
INIT_LIST_HEAD(&ip_tunnel_ops_list);
/* control plane */
err = sockopt_register(&ip_tunnel_sockopts);
if (err != EDPVS_OK)
goto so_fail;
/*
* init all ipv4 tunnels.
*/
if ((err = ipip_init()) != EDPVS_OK)
goto ipip_fail;
if ((err = gre_init()) != EDPVS_OK)
goto gre_fail;
return EDPVS_OK;
gre_fail:
ipip_term();
ipip_fail:
sockopt_unregister(&ip_tunnel_sockopts);
so_fail:
return err;
}
int ip_tunnel_term(void)
{
int err;
err = ipip_term();
if (err != EDPVS_OK)
return err;
err = gre_term();
if (err != EDPVS_OK)
return err;
err = sockopt_unregister(&ip_tunnel_sockopts);
if (err != EDPVS_OK)
return err;
return EDPVS_OK;
}
/* linux:ip_tunnel_lookup */
struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_tab *tab,
portid_t link, __be16 flags,
__be32 remote, __be32 local,
__be32 key)
{
struct hlist_head *head;
struct ip_tunnel *tnl, *cand = NULL;
head = tunnel_hash_head(tab, key, remote);
hlist_for_each_entry(tnl, head, hlist) {
if (local != tnl->params.iph.saddr ||
remote != tnl->params.iph.daddr ||
!(tnl->dev->flag & NETIF_PORT_FLAG_RUNNING))
continue;
if (!tunnel_key_match(&tnl->params, flags, key))
continue;
if (tunnel_link_match(tnl, link))
return tnl;
else
cand = tnl;
}
hlist_for_each_entry(tnl, head, hlist) {
if (remote != tnl->params.iph.daddr ||
tnl->params.iph.saddr != 0 ||
!(tnl->dev->flag & NETIF_PORT_FLAG_RUNNING))
continue;
if (!tunnel_key_match(&tnl->params, flags, key))
continue;
if (tunnel_link_match(tnl, link))
return tnl;
else if (!cand)
cand = tnl;
}
head = tunnel_hash_head(tab, key, 0);
hlist_for_each_entry(tnl, head, hlist) {
if ((local != tnl->params.iph.saddr || tnl->params.iph.daddr != 0) &&
(local != tnl->params.iph.daddr || !IN_MULTICAST(local)))
continue;
if (!(tnl->dev->flag & NETIF_PORT_FLAG_RUNNING))
continue;
if (!tunnel_key_match(&tnl->params, flags, key))
continue;
if (tunnel_link_match(tnl, link))
return tnl;
else if (!cand)
cand = tnl;
}
if (flags & TUNNEL_F_NO_KEY)
goto skip_key_lookup;
hlist_for_each_entry(tnl, head, hlist) {
if (tnl->params.i_key != key ||
tnl->params.iph.saddr != 0 ||
tnl->params.iph.daddr != 0 ||
!(tnl->dev->flag & NETIF_PORT_FLAG_RUNNING))
continue;
if (tunnel_link_match(tnl, link))
return tnl;
else if (!cand)
cand = tnl;
}
skip_key_lookup:
if (cand)
return cand;
if (tab->fb_tunnel_dev &&
tab->fb_tunnel_dev->flag & NETIF_PORT_FLAG_RUNNING)
return netif_priv(tab->fb_tunnel_dev);
return NULL;
}
/* linux:ip_tunnel_rcv */
int ip_tunnel_rcv(struct ip_tunnel *tnl, struct ip_tunnel_pktinfo *tpi,
struct rte_mbuf *mbuf)
{
const struct ip_tunnel_param *params = &tnl->params;
assert(tnl && mbuf);
if ((!(tpi->flags & TUNNEL_F_CSUM) && (params->i_flags & TUNNEL_F_CSUM)) ||
((tpi->flags & TUNNEL_F_CSUM) && !(params->i_flags & TUNNEL_F_CSUM))) {
goto drop;
}
if (tnl->params.i_flags & TUNNEL_F_SEQ) {
if (!(tpi->flags & TUNNEL_F_SEQ) ||
(tnl->i_seqno && (int32_t)(ntohl(tpi->seq) - tnl->i_seqno) < 0)) {
goto drop;
}
tnl->i_seqno = ntohl(tpi->seq) + 1;
}
mbuf->port = tnl->dev->id;
return netif_rcv(tnl->dev, htons(ETH_P_IP), mbuf);
drop:
rte_pktmbuf_free(mbuf);
return EDPVS_DROP;
}
/* linux: ip_tunnel_xmit */
int ip_tunnel_xmit(struct rte_mbuf *mbuf, struct netif_port *dev,
const struct iphdr *tiph, uint8_t proto)
{
struct ip_tunnel *tnl = netif_priv(dev);
const struct iphdr *iiph = NULL; /* inner ip header*/
struct route_entry *rt;
struct flow4 fl4 = {};
int err = EDPVS_DROP;
uint8_t tos, ttl;
bool connected;
__be16 df;
__be32 dip;
assert(mbuf && dev && tiph);
if (mbuf->packet_type == ETHER_TYPE_IPv4)
iiph = rte_pktmbuf_mtod_offset(mbuf, struct iphdr *, tnl->hlen);
connected = tiph->daddr != 0;
dip = tiph->daddr;
if (!dip) {
/* TODO: NBMA tunnel */
RTE_LOG(DEBUG, TUNNEL, "%s: NBMA dev not support\n", __func__);
err = EDPVS_NOTSUPP;
goto errout;
}
tos = tiph->tos;
if (tos & 0x1) {
tos &= ~0x1;
if (iiph)
tos = iiph->tos;
connected = false;
}
/* try cache first then route lookup */
rt = connected ? tnl->rt_cache : NULL;
if (!rt) {
/* not connected or no route cache */
fl4.fl4_proto = proto;
fl4.fl4_daddr.s_addr = dip;
fl4.fl4_saddr.s_addr = tiph->saddr;
fl4.fl4_tos = tos;
fl4.fl4_oif = tnl->link;
rt = route4_output(&fl4);
if (!rt) {
err = EDPVS_NOROUTE;
goto errout;
}
tunnel_clear_rt_cache(tnl);
tnl->rt_cache = rt;
/* refer route in tunnel do not put it. */
}
if (rt->port == dev)
goto errout;
/* refer route in mbuf and this reference will be put later. */
route4_get(rt);
mbuf->userdata = (void *)rt;
err = tunnel_update_pmtu(dev, mbuf, rt, tiph->frag_off, iiph);
if (err != EDPVS_OK)
goto errout;
ttl = tiph->ttl;
if (!ttl) {
if (iiph)
ttl = iiph->ttl;
else
ttl = INET_DEF_TTL;
}
df = tiph->frag_off;
if (iiph)
df |= (iiph->frag_off & htons(IP_DF));
if (!rt->src.s_addr)
RTE_LOG(WARNING, TUNNEL, "%s: xmit with no source IP\n", __func__);
return tunnel_xmit(mbuf, rt->src.s_addr, dip, proto, tos, ttl, df);
errout:
rte_pktmbuf_free(mbuf);
return err;
}
int ip_tunnel_pull_header(struct rte_mbuf *mbuf, int hlen, __be16 in_proto)
{
/* pull inner header */
if (mbuf_may_pull(mbuf, hlen) != 0)
return EDPVS_NOROOM;
if (rte_pktmbuf_adj(mbuf, hlen) == NULL)
return EDPVS_INVPKT;
/* clean up vlan info, it should be cleared by vlan module. */
if (unlikely(mbuf->ol_flags & PKT_RX_VLAN_STRIPPED)) {
mbuf->vlan_tci = 0;
mbuf->ol_flags &= (~PKT_RX_VLAN_STRIPPED);
}
return EDPVS_OK;
}
int ip_tunnel_get_link(struct netif_port *dev, struct rte_eth_link *link)
{
struct ip_tunnel *tnl = netif_priv(dev);
if (tnl->link) {
return netif_get_link(tnl->link, link);
} else {
memset(link, 0, sizeof(*link));
return EDPVS_OK;
}
}
int ip_tunnel_get_stats(struct netif_port *dev, struct rte_eth_stats *stats)
{
struct ip_tunnel *tnl = netif_priv(dev);
if (tnl->link) {
return netif_get_stats(tnl->link, stats);
} else {
/* TODO: support per-lcore stats */
memset(stats, 0, sizeof(*stats));
return EDPVS_OK;
}
}
int ip_tunnel_get_promisc(struct netif_port *dev, bool *promisc)
{
struct ip_tunnel *tnl = netif_priv(dev);
if (tnl->link) {
return netif_get_promisc(tnl->link, promisc);
} else {
*promisc = false;
return EDPVS_OK;
}
}