forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofproto-dpif.c
5848 lines (5002 loc) · 181 KB
/
ofproto-dpif.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) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <errno.h>
#include "bfd.h"
#include "bond.h"
#include "bundle.h"
#include "byte-order.h"
#include "connectivity.h"
#include "connmgr.h"
#include "coverage.h"
#include "cfm.h"
#include "ct-dpif.h"
#include "fail-open.h"
#include "guarded-list.h"
#include "hmapx.h"
#include "lacp.h"
#include "learn.h"
#include "mac-learning.h"
#include "mcast-snooping.h"
#include "multipath.h"
#include "netdev-vport.h"
#include "netdev.h"
#include "netlink.h"
#include "nx-match.h"
#include "odp-util.h"
#include "odp-execute.h"
#include "ofproto/ofproto-dpif.h"
#include "ofproto/ofproto-provider.h"
#include "ofproto-dpif-ipfix.h"
#include "ofproto-dpif-mirror.h"
#include "ofproto-dpif-monitor.h"
#include "ofproto-dpif-rid.h"
#include "ofproto-dpif-sflow.h"
#include "ofproto-dpif-trace.h"
#include "ofproto-dpif-upcall.h"
#include "ofproto-dpif-xlate.h"
#include "ofproto-dpif-xlate-cache.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/meta-flow.h"
#include "openvswitch/ofp-parse.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "ovs-lldp.h"
#include "ovs-rcu.h"
#include "ovs-router.h"
#include "poll-loop.h"
#include "seq.h"
#include "simap.h"
#include "smap.h"
#include "timer.h"
#include "tunnel.h"
#include "unaligned.h"
#include "unixctl.h"
#include "util.h"
#include "vlan-bitmap.h"
VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
COVERAGE_DEFINE(ofproto_dpif_expired);
COVERAGE_DEFINE(packet_in_overflow);
struct flow_miss;
static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
long long int *used);
static struct rule_dpif *rule_dpif_cast(const struct rule *);
static void rule_expire(struct rule_dpif *, long long now);
struct ofbundle {
struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
struct ofproto_dpif *ofproto; /* Owning ofproto. */
void *aux; /* Key supplied by ofproto's client. */
char *name; /* Identifier for log messages. */
/* Configuration. */
struct ovs_list ports; /* Contains "struct ofport"s. */
enum port_vlan_mode vlan_mode; /* VLAN mode */
uint16_t qinq_ethtype;
int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
* NULL if all VLANs are trunked. */
unsigned long *cvlans;
struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
struct bond *bond; /* Nonnull iff more than one port. */
bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
bool protected; /* Protected port mode */
/* Status. */
bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
};
static void bundle_remove(struct ofport *);
static void bundle_update(struct ofbundle *);
static void bundle_destroy(struct ofbundle *);
static void bundle_del_port(struct ofport_dpif *);
static void bundle_run(struct ofbundle *);
static void bundle_wait(struct ofbundle *);
static void bundle_flush_macs(struct ofbundle *, bool);
static void bundle_move(struct ofbundle *, struct ofbundle *);
static void stp_run(struct ofproto_dpif *ofproto);
static void stp_wait(struct ofproto_dpif *ofproto);
static int set_stp_port(struct ofport *,
const struct ofproto_port_stp_settings *);
static void rstp_run(struct ofproto_dpif *ofproto);
static void set_rstp_port(struct ofport *,
const struct ofproto_port_rstp_settings *);
struct ofport_dpif {
struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
struct ofport up;
odp_port_t odp_port;
struct ofbundle *bundle; /* Bundle that contains this port, if any. */
struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
struct cfm *cfm; /* Connectivity Fault Management, if any. */
struct bfd *bfd; /* BFD, if any. */
struct lldp *lldp; /* lldp, if any. */
bool may_enable; /* May be enabled in bonds. */
bool is_tunnel; /* This port is a tunnel. */
long long int carrier_seq; /* Carrier status changes. */
struct ofport_dpif *peer; /* Peer if patch port. */
/* Spanning tree. */
struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
long long int stp_state_entered;
/* Rapid Spanning Tree. */
struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
/* Queue to DSCP mapping. */
struct ofproto_port_queue *qdscp;
size_t n_qdscp;
};
static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
ofp_port_t);
static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
odp_port_t);
static struct ofport_dpif *
ofport_dpif_cast(const struct ofport *ofport)
{
return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
}
static void port_run(struct ofport_dpif *);
static int set_bfd(struct ofport *, const struct smap *);
static int set_cfm(struct ofport *, const struct cfm_settings *);
static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
static void ofport_update_peer(struct ofport_dpif *);
COVERAGE_DEFINE(rev_reconfigure);
COVERAGE_DEFINE(rev_stp);
COVERAGE_DEFINE(rev_rstp);
COVERAGE_DEFINE(rev_bond);
COVERAGE_DEFINE(rev_port_toggled);
COVERAGE_DEFINE(rev_flow_table);
COVERAGE_DEFINE(rev_mac_learning);
COVERAGE_DEFINE(rev_mcast_snooping);
/* All existing ofproto_backer instances, indexed by ofproto->up.type. */
struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
/* All existing ofproto_dpif instances, indexed by ->up.name. */
struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
static bool ofproto_use_tnl_push_pop = true;
static void ofproto_unixctl_init(void);
static inline struct ofproto_dpif *
ofproto_dpif_cast(const struct ofproto *ofproto)
{
ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
}
/* Global variables. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
/* Initial mappings of port to bridge mappings. */
static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
/* Initialize 'ofm' for a learn action. If the rule already existed, reference
* to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
* rule reference in both cases. */
enum ofperr
ofproto_dpif_flow_mod_init_for_learn(struct ofproto_dpif *ofproto,
const struct ofputil_flow_mod *fm,
struct ofproto_flow_mod *ofm)
{
/* This will not take the global 'ofproto_mutex'. */
return ofproto_flow_mod_init_for_learn(&ofproto->up, fm, ofm);
}
/* Appends 'am' to the queue of asynchronous messages to be sent to the
* controller. Takes ownership of 'am' and any data it points to. */
void
ofproto_dpif_send_async_msg(struct ofproto_dpif *ofproto,
struct ofproto_async_msg *am)
{
if (!guarded_list_push_back(&ofproto->ams, &am->list_node, 1024)) {
COVERAGE_INC(packet_in_overflow);
ofproto_async_msg_free(am);
}
/* Wakes up main thread for packet-in I/O. */
seq_change(ofproto->ams_seq);
}
/* Factory functions. */
static void
init(const struct shash *iface_hints)
{
struct shash_node *node;
/* Make a local copy, since we don't own 'iface_hints' elements. */
SHASH_FOR_EACH(node, iface_hints) {
const struct iface_hint *orig_hint = node->data;
struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
new_hint->br_name = xstrdup(orig_hint->br_name);
new_hint->br_type = xstrdup(orig_hint->br_type);
new_hint->ofp_port = orig_hint->ofp_port;
shash_add(&init_ofp_ports, node->name, new_hint);
}
ofproto_unixctl_init();
ofproto_dpif_trace_init();
udpif_init();
}
static void
enumerate_types(struct sset *types)
{
dp_enumerate_types(types);
}
static int
enumerate_names(const char *type, struct sset *names)
{
struct ofproto_dpif *ofproto;
sset_clear(names);
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (strcmp(type, ofproto->up.type)) {
continue;
}
sset_add(names, ofproto->up.name);
}
return 0;
}
static int
del(const char *type, const char *name)
{
struct dpif *dpif;
int error;
error = dpif_open(name, type, &dpif);
if (!error) {
error = dpif_delete(dpif);
dpif_close(dpif);
}
return error;
}
static const char *
port_open_type(const char *datapath_type, const char *port_type)
{
return dpif_port_open_type(datapath_type, port_type);
}
/* Type functions. */
static void process_dpif_port_changes(struct dpif_backer *);
static void process_dpif_all_ports_changed(struct dpif_backer *);
static void process_dpif_port_change(struct dpif_backer *,
const char *devname);
static void process_dpif_port_error(struct dpif_backer *, int error);
static struct ofproto_dpif *
lookup_ofproto_dpif_by_port_name(const char *name)
{
struct ofproto_dpif *ofproto;
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (sset_contains(&ofproto->ports, name)) {
return ofproto;
}
}
return NULL;
}
static int
type_run(const char *type)
{
struct dpif_backer *backer;
backer = shash_find_data(&all_dpif_backers, type);
if (!backer) {
/* This is not necessarily a problem, since backers are only
* created on demand. */
return 0;
}
if (dpif_run(backer->dpif)) {
backer->need_revalidate = REV_RECONFIGURE;
}
udpif_run(backer->udpif);
/* If vswitchd started with other_config:flow_restore_wait set as "true",
* and the configuration has now changed to "false", enable receiving
* packets from the datapath. */
if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
int error;
backer->recv_set_enable = true;
error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
if (error) {
VLOG_ERR("Failed to enable receiving packets in dpif.");
return error;
}
dpif_flow_flush(backer->dpif);
backer->need_revalidate = REV_RECONFIGURE;
}
if (backer->recv_set_enable) {
udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
}
if (backer->need_revalidate) {
struct ofproto_dpif *ofproto;
struct simap_node *node;
struct simap tmp_backers;
/* Handle tunnel garbage collection. */
simap_init(&tmp_backers);
simap_swap(&backer->tnl_backers, &tmp_backers);
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
struct ofport_dpif *iter;
if (backer != ofproto->backer) {
continue;
}
HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
const char *dp_port;
if (!iter->is_tunnel) {
continue;
}
dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
namebuf, sizeof namebuf);
node = simap_find(&tmp_backers, dp_port);
if (node) {
simap_put(&backer->tnl_backers, dp_port, node->data);
simap_delete(&tmp_backers, node);
node = simap_find(&backer->tnl_backers, dp_port);
} else {
node = simap_find(&backer->tnl_backers, dp_port);
if (!node) {
odp_port_t odp_port = ODPP_NONE;
if (!dpif_port_add(backer->dpif, iter->up.netdev,
&odp_port)) {
simap_put(&backer->tnl_backers, dp_port,
odp_to_u32(odp_port));
node = simap_find(&backer->tnl_backers, dp_port);
}
}
}
iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
if (tnl_port_reconfigure(iter, iter->up.netdev,
iter->odp_port,
ovs_native_tunneling_is_on(ofproto), dp_port)) {
backer->need_revalidate = REV_RECONFIGURE;
}
}
}
SIMAP_FOR_EACH (node, &tmp_backers) {
dpif_port_del(backer->dpif, u32_to_odp(node->data));
}
simap_destroy(&tmp_backers);
switch (backer->need_revalidate) {
case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
case REV_STP: COVERAGE_INC(rev_stp); break;
case REV_RSTP: COVERAGE_INC(rev_rstp); break;
case REV_BOND: COVERAGE_INC(rev_bond); break;
case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
}
backer->need_revalidate = 0;
xlate_txn_start();
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
struct ofport_dpif *ofport;
struct ofbundle *bundle;
if (ofproto->backer != backer) {
continue;
}
xlate_ofproto_set(ofproto, ofproto->up.name,
ofproto->backer->dpif, ofproto->ml,
ofproto->stp, ofproto->rstp, ofproto->ms,
ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
ofproto->netflow,
ofproto->up.forward_bpdu,
connmgr_has_in_band(ofproto->up.connmgr),
&ofproto->backer->rt_support);
HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
xlate_bundle_set(ofproto, bundle, bundle->name,
bundle->vlan_mode, bundle->qinq_ethtype,
bundle->vlan, bundle->trunks, bundle->cvlans,
bundle->use_priority_tags,
bundle->bond, bundle->lacp,
bundle->floodable, bundle->protected);
}
HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
int stp_port = ofport->stp_port
? stp_port_no(ofport->stp_port)
: -1;
xlate_ofport_set(ofproto, ofport->bundle, ofport,
ofport->up.ofp_port, ofport->odp_port,
ofport->up.netdev, ofport->cfm, ofport->bfd,
ofport->lldp, ofport->peer, stp_port,
ofport->rstp_port, ofport->qdscp,
ofport->n_qdscp, ofport->up.pp.config,
ofport->up.pp.state, ofport->is_tunnel,
ofport->may_enable);
}
}
xlate_txn_commit();
udpif_revalidate(backer->udpif);
}
process_dpif_port_changes(backer);
return 0;
}
/* Check for and handle port changes in 'backer''s dpif. */
static void
process_dpif_port_changes(struct dpif_backer *backer)
{
for (;;) {
char *devname;
int error;
error = dpif_port_poll(backer->dpif, &devname);
switch (error) {
case EAGAIN:
return;
case ENOBUFS:
process_dpif_all_ports_changed(backer);
break;
case 0:
process_dpif_port_change(backer, devname);
free(devname);
break;
default:
process_dpif_port_error(backer, error);
break;
}
}
}
static void
process_dpif_all_ports_changed(struct dpif_backer *backer)
{
struct ofproto_dpif *ofproto;
struct dpif_port dpif_port;
struct dpif_port_dump dump;
struct sset devnames;
const char *devname;
sset_init(&devnames);
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (ofproto->backer == backer) {
struct ofport *ofport;
HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
sset_add(&devnames, netdev_get_name(ofport->netdev));
}
}
}
DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
sset_add(&devnames, dpif_port.name);
}
SSET_FOR_EACH (devname, &devnames) {
process_dpif_port_change(backer, devname);
}
sset_destroy(&devnames);
}
static void
process_dpif_port_change(struct dpif_backer *backer, const char *devname)
{
struct ofproto_dpif *ofproto;
struct dpif_port port;
/* Don't report on the datapath's device. */
if (!strcmp(devname, dpif_base_name(backer->dpif))) {
return;
}
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
&all_ofproto_dpifs) {
if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
return;
}
}
ofproto = lookup_ofproto_dpif_by_port_name(devname);
if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
/* The port was removed. If we know the datapath,
* report it through poll_set(). If we don't, it may be
* notifying us of a removal we initiated, so ignore it.
* If there's a pending ENOBUFS, let it stand, since
* everything will be reevaluated. */
if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
sset_add(&ofproto->port_poll_set, devname);
ofproto->port_poll_errno = 0;
}
} else if (!ofproto) {
/* The port was added, but we don't know with which
* ofproto we should associate it. Delete it. */
dpif_port_del(backer->dpif, port.port_no);
} else {
struct ofport_dpif *ofport;
ofport = ofport_dpif_cast(shash_find_data(
&ofproto->up.port_by_name, devname));
if (ofport
&& ofport->odp_port != port.port_no
&& !odp_port_to_ofport(backer, port.port_no))
{
/* 'ofport''s datapath port number has changed from
* 'ofport->odp_port' to 'port.port_no'. Update our internal data
* structures to match. */
ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
ofport->odp_port = port.port_no;
hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
hash_odp_port(port.port_no));
ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
backer->need_revalidate = REV_RECONFIGURE;
}
}
dpif_port_destroy(&port);
}
/* Propagate 'error' to all ofprotos based on 'backer'. */
static void
process_dpif_port_error(struct dpif_backer *backer, int error)
{
struct ofproto_dpif *ofproto;
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
if (ofproto->backer == backer) {
sset_clear(&ofproto->port_poll_set);
ofproto->port_poll_errno = error;
}
}
}
static void
type_wait(const char *type)
{
struct dpif_backer *backer;
backer = shash_find_data(&all_dpif_backers, type);
if (!backer) {
/* This is not necessarily a problem, since backers are only
* created on demand. */
return;
}
dpif_wait(backer->dpif);
}
/* Basic life-cycle. */
static int add_internal_flows(struct ofproto_dpif *);
static struct ofproto *
alloc(void)
{
struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
return &ofproto->up;
}
static void
dealloc(struct ofproto *ofproto_)
{
struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
free(ofproto);
}
static void
close_dpif_backer(struct dpif_backer *backer, bool del)
{
ovs_assert(backer->refcount > 0);
if (--backer->refcount) {
return;
}
udpif_destroy(backer->udpif);
simap_destroy(&backer->tnl_backers);
ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
hmap_destroy(&backer->odp_to_ofport_map);
shash_find_and_delete(&all_dpif_backers, backer->type);
free(backer->type);
free(backer->dp_version_string);
if (del) {
dpif_delete(backer->dpif);
}
dpif_close(backer->dpif);
id_pool_destroy(backer->meter_ids);
free(backer);
}
/* Datapath port slated for removal from datapath. */
struct odp_garbage {
struct ovs_list list_node;
odp_port_t odp_port;
};
static bool check_variable_length_userdata(struct dpif_backer *backer);
static void check_support(struct dpif_backer *backer);
static int
open_dpif_backer(const char *type, struct dpif_backer **backerp)
{
struct dpif_backer *backer;
struct dpif_port_dump port_dump;
struct dpif_port port;
struct shash_node *node;
struct ovs_list garbage_list;
struct odp_garbage *garbage;
struct sset names;
char *backer_name;
const char *name;
int error;
backer = shash_find_data(&all_dpif_backers, type);
if (backer) {
backer->refcount++;
*backerp = backer;
return 0;
}
backer_name = xasprintf("ovs-%s", type);
/* Remove any existing datapaths, since we assume we're the only
* userspace controlling the datapath. */
sset_init(&names);
dp_enumerate_names(type, &names);
SSET_FOR_EACH(name, &names) {
struct dpif *old_dpif;
/* Don't remove our backer if it exists. */
if (!strcmp(name, backer_name)) {
continue;
}
if (dpif_open(name, type, &old_dpif)) {
VLOG_WARN("couldn't open old datapath %s to remove it", name);
} else {
dpif_delete(old_dpif);
dpif_close(old_dpif);
}
}
sset_destroy(&names);
backer = xmalloc(sizeof *backer);
error = dpif_create_and_open(backer_name, type, &backer->dpif);
free(backer_name);
if (error) {
VLOG_ERR("failed to open datapath of type %s: %s", type,
ovs_strerror(error));
free(backer);
return error;
}
backer->udpif = udpif_create(backer, backer->dpif);
backer->type = xstrdup(type);
backer->refcount = 1;
hmap_init(&backer->odp_to_ofport_map);
ovs_rwlock_init(&backer->odp_to_ofport_lock);
backer->need_revalidate = 0;
simap_init(&backer->tnl_backers);
backer->recv_set_enable = !ofproto_get_flow_restore_wait();
*backerp = backer;
if (backer->recv_set_enable) {
dpif_flow_flush(backer->dpif);
}
/* Loop through the ports already on the datapath and remove any
* that we don't need anymore. */
ovs_list_init(&garbage_list);
dpif_port_dump_start(&port_dump, backer->dpif);
while (dpif_port_dump_next(&port_dump, &port)) {
node = shash_find(&init_ofp_ports, port.name);
if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
garbage = xmalloc(sizeof *garbage);
garbage->odp_port = port.port_no;
ovs_list_push_front(&garbage_list, &garbage->list_node);
}
}
dpif_port_dump_done(&port_dump);
LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
dpif_port_del(backer->dpif, garbage->odp_port);
free(garbage);
}
shash_add(&all_dpif_backers, type, backer);
check_support(backer);
atomic_count_init(&backer->tnl_count, 0);
error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
if (error) {
VLOG_ERR("failed to listen on datapath of type %s: %s",
type, ovs_strerror(error));
close_dpif_backer(backer, false);
return error;
}
if (backer->recv_set_enable) {
udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
}
/* This check fails if performed before udpif threads have been set,
* as the kernel module checks that the 'pid' in userspace action
* is non-zero. */
backer->rt_support.variable_length_userdata
= check_variable_length_userdata(backer);
backer->dp_version_string = dpif_get_dp_version(backer->dpif);
/* Manage Datapath meter IDs if supported. */
struct ofputil_meter_features features;
dpif_meter_get_features(backer->dpif, &features);
if (features.max_meters) {
backer->meter_ids = id_pool_create(0, features.max_meters);
} else {
backer->meter_ids = NULL;
}
/* Make a pristine snapshot of 'support' into 'boottime_support'.
* 'boottime_support' can be checked to prevent 'support' to be changed
* beyond the datapath capabilities. In case 'support' is changed by
* the user, 'boottime_support' can be used to restore it. */
backer->bt_support = backer->rt_support;
return error;
}
bool
ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
{
return ofproto_use_tnl_push_pop
&& ofproto->backer->rt_support.tnl_push_pop
&& atomic_count_get(&ofproto->backer->tnl_count);
}
/* Tests whether 'backer''s datapath supports recirculation. Only newer
* datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
* features on older datapaths that don't support this feature.
*
* Returns false if 'backer' definitely does not support recirculation, true if
* it seems to support recirculation or if at least the error we get is
* ambiguous. */
static bool
check_recirc(struct dpif_backer *backer)
{
struct flow flow;
struct odputil_keybuf keybuf;
struct ofpbuf key;
bool enable_recirc;
struct odp_flow_key_parms odp_parms = {
.flow = &flow,
.support = {
.recirc = true,
},
};
memset(&flow, 0, sizeof flow);
flow.recirc_id = 1;
flow.dp_hash = 1;
ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
odp_flow_key_from_flow(&odp_parms, &key);
enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
NULL, NULL);
if (enable_recirc) {
VLOG_INFO("%s: Datapath supports recirculation",
dpif_name(backer->dpif));
} else {
VLOG_INFO("%s: Datapath does not support recirculation",
dpif_name(backer->dpif));
}
return enable_recirc;
}
/* Tests whether 'dpif' supports unique flow ids. We can skip serializing
* some flow attributes for datapaths that support this feature.
*
* Returns true if 'dpif' supports UFID for flow operations.
* Returns false if 'dpif' does not support UFID. */
static bool
check_ufid(struct dpif_backer *backer)
{
struct flow flow;
struct odputil_keybuf keybuf;
struct ofpbuf key;
ovs_u128 ufid;
bool enable_ufid;
struct odp_flow_key_parms odp_parms = {
.flow = &flow,
};
memset(&flow, 0, sizeof flow);
flow.dl_type = htons(0x1234);
ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
odp_flow_key_from_flow(&odp_parms, &key);
dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, NULL, &ufid);
if (enable_ufid) {
VLOG_INFO("%s: Datapath supports unique flow ids",
dpif_name(backer->dpif));
} else {
VLOG_INFO("%s: Datapath does not support unique flow ids",
dpif_name(backer->dpif));
}
return enable_ufid;
}
/* Tests whether 'backer''s datapath supports variable-length
* OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
* to disable some features on older datapaths that don't support this
* feature.
*
* Returns false if 'backer' definitely does not support variable-length
* userdata, true if it seems to support them or if at least the error we get
* is ambiguous. */
static bool
check_variable_length_userdata(struct dpif_backer *backer)
{
struct eth_header *eth;
struct ofpbuf actions;
struct dpif_execute execute;
struct dp_packet packet;
struct flow flow;
size_t start;
int error;
/* Compose a userspace action that will cause an ERANGE error on older
* datapaths that don't support variable-length userdata.
*
* We really test for using userdata longer than 8 bytes, but older
* datapaths accepted these, silently truncating the userdata to 8 bytes.
* The same older datapaths rejected userdata shorter than 8 bytes, so we
* test for that instead as a proxy for longer userdata support. */
ofpbuf_init(&actions, 64);
start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
nl_msg_end_nested(&actions, start);
/* Compose a dummy ethernet packet. */
dp_packet_init(&packet, ETH_HEADER_LEN);
eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
eth->eth_type = htons(0x1234);
flow_extract(&packet, &flow);
/* Execute the actions. On older datapaths this fails with ERANGE, on
* newer datapaths it succeeds. */
execute.actions = actions.data;
execute.actions_len = actions.size;
execute.packet = &packet;
execute.flow = &flow;
execute.needs_help = false;
execute.probe = true;
execute.mtu = 0;
error = dpif_execute(backer->dpif, &execute);
dp_packet_uninit(&packet);
ofpbuf_uninit(&actions);
switch (error) {
case 0:
return true;
case ERANGE:
/* Variable-length userdata is not supported. */
VLOG_WARN("%s: datapath does not support variable-length userdata "
"feature (needs Linux 3.10+ or kernel module from OVS "
"1..11+). The NXAST_SAMPLE action will be ignored.",
dpif_name(backer->dpif));
return false;
default:
/* Something odd happened. We're not sure whether variable-length
* userdata is supported. Default to "yes". */
VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
dpif_name(backer->dpif), ovs_strerror(error));
return true;
}
}
/* Tests number of 802.1q VLAN headers supported by 'backer''s datapath.
*
* Returns the number of elements in a struct flow's vlan
* if the datapath supports at least that many VLAN headers. */
static size_t
check_max_vlan_headers(struct dpif_backer *backer)
{
struct flow flow;
struct odp_flow_key_parms odp_parms = {
.flow = &flow,
.probe = true,
};
int n;
memset(&flow, 0, sizeof flow);
flow.dl_type = htons(ETH_TYPE_IP);
for (n = 0; n < FLOW_MAX_VLAN_HEADERS; n++) {
struct odputil_keybuf keybuf;
struct ofpbuf key;
flow_push_vlan_uninit(&flow, NULL);
flow.vlans[0].tpid = htons(ETH_TYPE_VLAN);
flow.vlans[0].tci = htons(1) | htons(VLAN_CFI);
ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
odp_flow_key_from_flow(&odp_parms, &key);
if (!dpif_probe_feature(backer->dpif, "VLAN", &key, NULL, NULL)) {
break;
}