forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofproto-dpif.c
5681 lines (4841 loc) · 174 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 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 "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-upcall.h"
#include "ofproto-dpif-xlate.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 "vlan-bitmap.h"
VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
COVERAGE_DEFINE(ofproto_dpif_expired);
COVERAGE_DEFINE(packet_in_overflow);
struct flow_miss;
struct rule_dpif {
struct rule up;
/* These statistics:
*
* - Do include packets and bytes from datapath flows which have not
* recently been processed by a revalidator. */
struct ovs_mutex stats_mutex;
struct dpif_flow_stats stats OVS_GUARDED;
/* In non-NULL, will point to a new rule (for which a reference is held) to
* which all the stats updates should be forwarded. This exists only
* transitionally when flows are replaced.
*
* Protected by stats_mutex. If both 'rule->stats_mutex' and
* 'rule->new_rule->stats_mutex' must be held together, acquire them in that
* order, */
struct rule_dpif *new_rule OVS_GUARDED;
/* If non-zero then the recirculation id that has
* been allocated for use with this rule.
* The recirculation id and associated internal flow should
* be freed when the rule is freed */
uint32_t recirc_id;
};
/* RULE_CAST() depends on this. */
BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
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 group_dpif {
struct ofgroup up;
/* These statistics:
*
* - Do include packets and bytes from datapath flows which have not
* recently been processed by a revalidator. */
struct ovs_mutex stats_mutex;
uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
};
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 */
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. */
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? */
/* 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. */
bool is_layer3; /* This is a layer 3 port. */
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 *);
/* Reasons that we might need to revalidate every datapath flow, and
* corresponding coverage counters.
*
* A value of 0 means that there is no need to revalidate.
*
* It would be nice to have some cleaner way to integrate with coverage
* counters, but with only a few reasons I guess this is good enough for
* now. */
enum revalidate_reason {
REV_RECONFIGURE = 1, /* Switch configuration changed. */
REV_STP, /* Spanning tree protocol port status change. */
REV_RSTP, /* RSTP port status change. */
REV_BOND, /* Bonding changed. */
REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
REV_FLOW_TABLE, /* Flow table changed. */
REV_MAC_LEARNING, /* Mac learning changed. */
REV_MCAST_SNOOPING, /* Multicast snooping changed. */
};
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 datapaths of a given type share a single dpif backer instance. */
struct dpif_backer {
char *type;
int refcount;
struct dpif *dpif;
struct udpif *udpif;
struct ovs_rwlock odp_to_ofport_lock;
struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
enum revalidate_reason need_revalidate; /* Revalidate all flows. */
bool recv_set_enable; /* Enables or disables receiving packets. */
/* Version string of the datapath stored in OVSDB. */
char *dp_version_string;
/* Datapath feature support. */
struct dpif_backer_support support;
struct atomic_count tnl_count;
};
/* All existing ofproto_backer instances, indexed by ofproto->up.type. */
static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
struct ofproto_dpif {
struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
struct ofproto up;
struct dpif_backer *backer;
/* Unique identifier for this instantiation of this bridge in this running
* process. */
struct uuid uuid;
ATOMIC(cls_version_t) tables_version; /* For classifier lookups. */
uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
/* Special OpenFlow rules. */
struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
struct rule_dpif *drop_frags_rule; /* Used in OFPUTIL_FRAG_DROP mode. */
/* Bridging. */
struct netflow *netflow;
struct dpif_sflow *sflow;
struct dpif_ipfix *ipfix;
struct hmap bundles; /* Contains "struct ofbundle"s. */
struct mac_learning *ml;
struct mcast_snooping *ms;
bool has_bonded_bundles;
bool lacp_enabled;
struct mbridge *mbridge;
struct ovs_mutex stats_mutex;
struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
* consumed in userspace. */
/* Spanning tree. */
struct stp *stp;
long long int stp_last_tick;
/* Rapid Spanning Tree. */
struct rstp *rstp;
long long int rstp_last_tick;
/* Ports. */
struct sset ports; /* Set of standard port names. */
struct sset ghost_ports; /* Ports with no datapath port. */
struct sset port_poll_set; /* Queued names for port_poll() reply. */
int port_poll_errno; /* Last errno for port_poll() reply. */
uint64_t change_seq; /* Connectivity status changes. */
/* Work queues. */
struct guarded_list ams; /* Contains "struct ofproto_async_msgs"s. */
struct seq *ams_seq; /* For notifying 'ams' reception. */
uint64_t ams_seqno;
};
/* All existing ofproto_dpif instances, indexed by ->up.name. */
static 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);
}
bool
ofproto_dpif_get_enable_ufid(const struct dpif_backer *backer)
{
return backer->support.ufid;
}
struct dpif_backer_support *
ofproto_dpif_get_support(const struct ofproto_dpif *ofproto)
{
return &ofproto->backer->support;
}
static void ofproto_trace(struct ofproto_dpif *, struct flow *,
const struct dp_packet *packet,
const struct ofpact[], size_t ofpacts_len,
struct ds *);
/* 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);
/* Executes 'fm'. The caller retains ownership of 'fm' and everything in
* it. */
void
ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
const struct ofputil_flow_mod *fm)
{
struct ofproto_flow_mod ofm;
/* Multiple threads may do this for the same 'fm' at the same time.
* Allocate ofproto_flow_mod with execution context from stack.
*
* Note: This copy could be avoided by making ofproto_flow_mod more
* complex, but that may not be desireable, and a learn action is not that
* fast to begin with. */
ofm.fm = *fm;
ofproto_flow_mod(&ofproto->up, &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);
}
/* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
* packet rather than to send the packet to the controller.
*
* This function returns false to indicate that a packet_in message
* for a "table-miss" should be sent to at least one controller.
* False otherwise. */
bool
ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
{
return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
}
/* 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();
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;
}
bool
ofproto_dpif_backer_enabled(struct dpif_backer* backer)
{
return backer->recv_set_enable;
}
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;
}
/* This must be called before dpif_run() */
dpif_poll_threads_set(backer->dpif, pmd_cpu_mask);
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;
HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
struct ofport_dpif *ofport;
struct ofbundle *bundle;
if (ofproto->backer != backer) {
continue;
}
xlate_txn_start();
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->support);
HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
xlate_bundle_set(ofproto, bundle, bundle->name,
bundle->vlan_mode, bundle->vlan,
bundle->trunks, bundle->use_priority_tags,
bundle->bond, bundle->lacp,
bundle->floodable);
}
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)
{
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);
dpif_close(backer->dpif);
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);
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->support.variable_length_userdata
= check_variable_length_userdata(backer);
backer->dp_version_string = dpif_get_dp_version(backer->dpif);
return error;
}
bool
ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
{
return ofproto_use_tnl_push_pop && ofproto->backer->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);