-
Notifications
You must be signed in to change notification settings - Fork 17
/
ofproto.c
8759 lines (7548 loc) · 278 KB
/
ofproto.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-2017 Nicira, Inc.
* Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
*
* 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 <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "bitmap.h"
#include "bundles.h"
#include "byte-order.h"
#include "classifier.h"
#include "connectivity.h"
#include "connmgr.h"
#include "coverage.h"
#include "dp-packet.h"
#include "hash.h"
#include "openvswitch/hmap.h"
#include "netdev.h"
#include "nx-match.h"
#include "ofproto.h"
#include "ofproto-provider.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/meta-flow.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-bundle.h"
#include "openvswitch/ofp-errors.h"
#include "openvswitch/ofp-match.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-monitor.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-queue.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "ovs-rcu.h"
#include "packets.h"
#include "pinsched.h"
#include "openvswitch/poll-loop.h"
#include "random.h"
#include "seq.h"
#include "openvswitch/shash.h"
#include "simap.h"
#include "smap.h"
#include "sset.h"
#include "timeval.h"
#include "tun-metadata.h"
#include "unaligned.h"
#include "unixctl.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(ofproto);
COVERAGE_DEFINE(ofproto_flush);
COVERAGE_DEFINE(ofproto_packet_out);
COVERAGE_DEFINE(ofproto_queue_req);
COVERAGE_DEFINE(ofproto_recv_openflow);
COVERAGE_DEFINE(ofproto_reinit_ports);
COVERAGE_DEFINE(ofproto_update_port);
/* Default fields to use for prefix tries in each flow table, unless something
* else is configured. */
const enum mf_field_id default_prefix_fields[2] =
{ MFF_IPV4_DST, MFF_IPV4_SRC };
/* oftable. */
static void oftable_init(struct oftable *);
static void oftable_destroy(struct oftable *);
static void oftable_set_name(struct oftable *, const char *name);
static enum ofperr evict_rules_from_table(struct oftable *)
OVS_REQUIRES(ofproto_mutex);
static void oftable_configure_eviction(struct oftable *,
unsigned int eviction,
const struct mf_subfield *fields,
size_t n_fields)
OVS_REQUIRES(ofproto_mutex);
/* This is the only combination of OpenFlow eviction flags that OVS supports: a
* combination of OF1.4+ importance, the remaining lifetime of the flow, and
* fairness based on user-specified fields. */
#define OFPROTO_EVICTION_FLAGS \
(OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME)
/* A set of rules within a single OpenFlow table (oftable) that have the same
* values for the oftable's eviction_fields. A rule to be evicted, when one is
* needed, is taken from the eviction group that contains the greatest number
* of rules.
*
* An oftable owns any number of eviction groups, each of which contains any
* number of rules.
*
* Membership in an eviction group is imprecise, based on the hash of the
* oftable's eviction_fields (in the eviction_group's id_node.hash member).
* That is, if two rules have different eviction_fields, but those
* eviction_fields hash to the same value, then they will belong to the same
* eviction_group anyway.
*
* (When eviction is not enabled on an oftable, we don't track any eviction
* groups, to save time and space.) */
struct eviction_group {
struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
struct heap rules; /* Contains "struct rule"s. */
};
static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
OVS_REQUIRES(ofproto_mutex);
static uint64_t rule_eviction_priority(struct ofproto *ofproto, struct rule *)
OVS_REQUIRES(ofproto_mutex);
static void eviction_group_add_rule(struct rule *)
OVS_REQUIRES(ofproto_mutex);
static void eviction_group_remove_rule(struct rule *)
OVS_REQUIRES(ofproto_mutex);
static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
const struct match *match, int priority,
ovs_version_t version,
ovs_be64 cookie, ovs_be64 cookie_mask,
ofp_port_t out_port, uint32_t out_group);
static void rule_criteria_require_rw(struct rule_criteria *,
bool can_write_readonly);
static void rule_criteria_destroy(struct rule_criteria *);
static enum ofperr collect_rules_loose(struct ofproto *,
const struct rule_criteria *,
struct rule_collection *);
struct learned_cookie {
union {
/* In struct ofproto's 'learned_cookies' hmap. */
struct hmap_node hmap_node OVS_GUARDED_BY(ofproto_mutex);
/* In 'dead_cookies' list when removed from hmap. */
struct ovs_list list_node;
} u;
/* Key. */
ovs_be64 cookie OVS_GUARDED_BY(ofproto_mutex);
uint8_t table_id OVS_GUARDED_BY(ofproto_mutex);
/* Number of references from "learn" actions.
*
* When this drops to 0, all of the flows in 'table_id' with the specified
* 'cookie' are deleted. */
int n OVS_GUARDED_BY(ofproto_mutex);
};
static const struct ofpact_learn *next_learn_with_delete(
const struct rule_actions *, const struct ofpact_learn *start);
static void learned_cookies_inc(struct ofproto *, const struct rule_actions *)
OVS_REQUIRES(ofproto_mutex);
static void learned_cookies_dec(struct ofproto *, const struct rule_actions *,
struct ovs_list *dead_cookies)
OVS_REQUIRES(ofproto_mutex);
static void learned_cookies_flush(struct ofproto *, struct ovs_list *dead_cookies)
OVS_REQUIRES(ofproto_mutex);
/* ofport. */
static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
static void ofport_destroy(struct ofport *, bool del);
static bool ofport_is_mtu_overridden(const struct ofproto *,
const struct ofport *);
static int update_port(struct ofproto *, const char *devname);
static int init_ports(struct ofproto *);
static void reinit_ports(struct ofproto *);
static long long int ofport_get_usage(const struct ofproto *,
ofp_port_t ofp_port);
static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
long long int last_used);
static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
/* Ofport usage.
*
* Keeps track of the currently used and recently used ofport values and is
* used to prevent immediate recycling of ofport values. */
struct ofport_usage {
struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
ofp_port_t ofp_port; /* OpenFlow port number. */
long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
represents in-use ofports. */
};
/* rule. */
static void ofproto_rule_send_removed(struct rule *)
OVS_EXCLUDED(ofproto_mutex);
static bool rule_is_readonly(const struct rule *);
static void ofproto_rule_insert__(struct ofproto *, struct rule *)
OVS_REQUIRES(ofproto_mutex);
static void ofproto_rule_remove__(struct ofproto *, struct rule *)
OVS_REQUIRES(ofproto_mutex);
/* The source of an OpenFlow request.
*
* A table modification request can be generated externally, via OpenFlow, or
* internally through a function call. This structure indicates the source of
* an OpenFlow-generated table modification. For an internal flow_mod, it
* isn't meaningful and thus supplied as NULL. */
struct openflow_mod_requester {
struct ofconn *ofconn; /* Connection on which flow_mod arrived. */
const struct ofp_header *request;
};
/* OpenFlow. */
static enum ofperr ofproto_rule_create(struct ofproto *, struct cls_rule *,
uint8_t table_id, ovs_be64 new_cookie,
uint16_t idle_timeout,
uint16_t hard_timeout,
enum ofputil_flow_mod_flags flags,
uint16_t importance,
const struct ofpact *ofpacts,
size_t ofpacts_len,
uint64_t match_tlv_bitmap,
uint64_t ofpacts_tlv_bitmap,
struct rule **new_rule)
OVS_NO_THREAD_SAFETY_ANALYSIS;
static void replace_rule_start(struct ofproto *, struct ofproto_flow_mod *,
struct rule *old_rule, struct rule *new_rule)
OVS_REQUIRES(ofproto_mutex);
static void replace_rule_revert(struct ofproto *, struct rule *old_rule,
struct rule *new_rule)
OVS_REQUIRES(ofproto_mutex);
static void replace_rule_finish(struct ofproto *, struct ofproto_flow_mod *,
const struct openflow_mod_requester *,
struct rule *old_rule, struct rule *new_rule,
struct ovs_list *dead_cookies)
OVS_REQUIRES(ofproto_mutex);
static void delete_flows__(struct rule_collection *,
enum ofp_flow_removed_reason,
const struct openflow_mod_requester *)
OVS_REQUIRES(ofproto_mutex);
static void ofproto_group_delete_all__(struct ofproto *)
OVS_REQUIRES(ofproto_mutex);
static bool ofproto_group_exists(const struct ofproto *, uint32_t group_id);
static void handle_openflow(struct ofconn *, const struct ofpbuf *);
static enum ofperr ofproto_flow_mod_init(struct ofproto *,
struct ofproto_flow_mod *,
const struct ofputil_flow_mod *fm,
struct rule *)
OVS_EXCLUDED(ofproto_mutex);
static enum ofperr ofproto_flow_mod_start(struct ofproto *,
struct ofproto_flow_mod *)
OVS_REQUIRES(ofproto_mutex);
static void ofproto_flow_mod_revert(struct ofproto *,
struct ofproto_flow_mod *)
OVS_REQUIRES(ofproto_mutex);
static void ofproto_flow_mod_finish(struct ofproto *,
struct ofproto_flow_mod *,
const struct openflow_mod_requester *)
OVS_REQUIRES(ofproto_mutex);
static enum ofperr handle_flow_mod__(struct ofproto *,
const struct ofputil_flow_mod *,
const struct openflow_mod_requester *)
OVS_EXCLUDED(ofproto_mutex);
static void calc_duration(long long int start, long long int now,
uint32_t *sec, uint32_t *nsec);
/* ofproto. */
static uint64_t pick_datapath_id(const struct ofproto *);
static uint64_t pick_fallback_dpid(void);
static void ofproto_destroy__(struct ofproto *);
static void update_mtu(struct ofproto *, struct ofport *);
static void update_mtu_ofproto(struct ofproto *);
static void meter_delete(struct ofproto *, uint32_t);
static void meter_delete_all(struct ofproto *);
static void meter_insert_rule(struct rule *);
/* unixctl. */
static void ofproto_unixctl_init(void);
/* All registered ofproto classes, in probe order. */
static const struct ofproto_class **ofproto_classes;
static size_t n_ofproto_classes;
static size_t allocated_ofproto_classes;
/* Global lock that protects all flow table operations. */
struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT;
unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT;
size_t n_handlers, n_revalidators;
/* Map from datapath name to struct ofproto, for use by unixctl commands. */
static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
/* Initial mappings of port to OpenFlow number mappings. */
static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
/* The default value of true waits for flow restore. */
static bool flow_restore_wait = true;
/* Must be called to initialize the ofproto library.
*
* The caller may pass in 'iface_hints', which contains an shash of
* "iface_hint" elements indexed by the interface's name. The provider
* may use these hints to describe the startup configuration in order to
* reinitialize its state. The caller owns the provided data, so a
* provider will make copies of anything required. An ofproto provider
* will remove any existing state that is not described by the hint, and
* may choose to remove it all. */
void
ofproto_init(const struct shash *iface_hints)
{
struct shash_node *node;
size_t i;
ofproto_class_register(&ofproto_dpif_class);
/* 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);
const char *br_type = ofproto_normalize_type(orig_hint->br_type);
new_hint->br_name = xstrdup(orig_hint->br_name);
new_hint->br_type = xstrdup(br_type);
new_hint->ofp_port = orig_hint->ofp_port;
shash_add(&init_ofp_ports, node->name, new_hint);
}
for (i = 0; i < n_ofproto_classes; i++) {
ofproto_classes[i]->init(&init_ofp_ports);
}
ofproto_unixctl_init();
}
/* 'type' should be a normalized datapath type, as returned by
* ofproto_normalize_type(). Returns the corresponding ofproto_class
* structure, or a null pointer if there is none registered for 'type'. */
static const struct ofproto_class *
ofproto_class_find__(const char *type)
{
size_t i;
for (i = 0; i < n_ofproto_classes; i++) {
const struct ofproto_class *class = ofproto_classes[i];
struct sset types;
bool found;
sset_init(&types);
class->enumerate_types(&types);
found = sset_contains(&types, type);
sset_destroy(&types);
if (found) {
return class;
}
}
VLOG_WARN("unknown datapath type %s", type);
return NULL;
}
/* Registers a new ofproto class. After successful registration, new ofprotos
* of that type can be created using ofproto_create(). */
int
ofproto_class_register(const struct ofproto_class *new_class)
{
size_t i;
for (i = 0; i < n_ofproto_classes; i++) {
if (ofproto_classes[i] == new_class) {
return EEXIST;
}
}
if (n_ofproto_classes >= allocated_ofproto_classes) {
ofproto_classes = x2nrealloc(ofproto_classes,
&allocated_ofproto_classes,
sizeof *ofproto_classes);
}
ofproto_classes[n_ofproto_classes++] = new_class;
return 0;
}
/* Unregisters a datapath provider. 'type' must have been previously
* registered and not currently be in use by any ofprotos. After
* unregistration new datapaths of that type cannot be opened using
* ofproto_create(). */
int
ofproto_class_unregister(const struct ofproto_class *class)
{
size_t i;
for (i = 0; i < n_ofproto_classes; i++) {
if (ofproto_classes[i] == class) {
for (i++; i < n_ofproto_classes; i++) {
ofproto_classes[i - 1] = ofproto_classes[i];
}
n_ofproto_classes--;
return 0;
}
}
VLOG_WARN("attempted to unregister an ofproto class that is not "
"registered");
return EAFNOSUPPORT;
}
/* Clears 'types' and enumerates all registered ofproto types into it. The
* caller must first initialize the sset. */
void
ofproto_enumerate_types(struct sset *types)
{
size_t i;
sset_clear(types);
for (i = 0; i < n_ofproto_classes; i++) {
ofproto_classes[i]->enumerate_types(types);
}
}
/* Returns the fully spelled out name for the given ofproto 'type'.
*
* Normalized type string can be compared with strcmp(). Unnormalized type
* string might be the same even if they have different spellings. */
const char *
ofproto_normalize_type(const char *type)
{
return type && type[0] ? type : "system";
}
/* Clears 'names' and enumerates the names of all known created ofprotos with
* the given 'type'. The caller must first initialize the sset. Returns 0 if
* successful, otherwise a positive errno value.
*
* Some kinds of datapaths might not be practically enumerable. This is not
* considered an error. */
int
ofproto_enumerate_names(const char *type, struct sset *names)
{
const struct ofproto_class *class = ofproto_class_find__(type);
return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
}
static void
ofproto_bump_tables_version(struct ofproto *ofproto)
{
++ofproto->tables_version;
ofproto->ofproto_class->set_tables_version(ofproto,
ofproto->tables_version);
}
int
ofproto_create(const char *datapath_name, const char *datapath_type,
struct ofproto **ofprotop)
OVS_EXCLUDED(ofproto_mutex)
{
const struct ofproto_class *class;
struct ofproto *ofproto;
int error;
int i;
*ofprotop = NULL;
datapath_type = ofproto_normalize_type(datapath_type);
class = ofproto_class_find__(datapath_type);
if (!class) {
VLOG_WARN("could not create datapath %s of unknown type %s",
datapath_name, datapath_type);
return EAFNOSUPPORT;
}
ofproto = class->alloc();
if (!ofproto) {
VLOG_ERR("failed to allocate datapath %s of type %s",
datapath_name, datapath_type);
return ENOMEM;
}
/* Initialize. */
ovs_mutex_lock(&ofproto_mutex);
memset(ofproto, 0, sizeof *ofproto);
ofproto->ofproto_class = class;
ofproto->name = xstrdup(datapath_name);
ofproto->type = xstrdup(datapath_type);
hmap_insert(&all_ofprotos, &ofproto->hmap_node,
hash_string(ofproto->name, 0));
ofproto->datapath_id = 0;
ofproto->forward_bpdu = false;
ofproto->fallback_dpid = pick_fallback_dpid();
ofproto->mfr_desc = NULL;
ofproto->hw_desc = NULL;
ofproto->sw_desc = NULL;
ofproto->serial_desc = NULL;
ofproto->dp_desc = NULL;
ofproto->frag_handling = OFPUTIL_FRAG_NORMAL;
hmap_init(&ofproto->ports);
hmap_init(&ofproto->ofport_usage);
shash_init(&ofproto->port_by_name);
simap_init(&ofproto->ofp_requests);
ofproto->max_ports = ofp_to_u16(OFPP_MAX);
ofproto->eviction_group_timer = LLONG_MIN;
ofproto->tables = NULL;
ofproto->n_tables = 0;
ofproto->tables_version = OVS_VERSION_MIN;
hindex_init(&ofproto->cookies);
hmap_init(&ofproto->learned_cookies);
ovs_list_init(&ofproto->expirable);
ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
ofproto->min_mtu = INT_MAX;
cmap_init(&ofproto->groups);
ovs_mutex_unlock(&ofproto_mutex);
ofproto->ogf.types = 0xf;
ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
OFPGFC_SELECT_WEIGHT;
for (i = 0; i < 4; i++) {
ofproto->ogf.max_groups[i] = OFPG_MAX;
ofproto->ogf.ofpacts[i] = (UINT64_C(1) << N_OFPACTS) - 1;
}
ovsrcu_set(&ofproto->metadata_tab, tun_metadata_alloc(NULL));
ovs_mutex_init(&ofproto->vl_mff_map.mutex);
cmap_init(&ofproto->vl_mff_map.cmap);
error = ofproto->ofproto_class->construct(ofproto);
if (error) {
VLOG_ERR("failed to open datapath %s: %s",
datapath_name, ovs_strerror(error));
ovs_mutex_lock(&ofproto_mutex);
connmgr_destroy(ofproto->connmgr);
ofproto->connmgr = NULL;
ovs_mutex_unlock(&ofproto_mutex);
ofproto_destroy__(ofproto);
return error;
}
/* Check that hidden tables, if any, are at the end. */
ovs_assert(ofproto->n_tables);
for (i = 0; i + 1 < ofproto->n_tables; i++) {
enum oftable_flags flags = ofproto->tables[i].flags;
enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
}
ofproto->datapath_id = pick_datapath_id(ofproto);
init_ports(ofproto);
/* Initialize meters table. */
if (ofproto->ofproto_class->meter_get_features) {
ofproto->ofproto_class->meter_get_features(ofproto,
&ofproto->meter_features);
} else {
memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
}
hmap_init(&ofproto->meters);
ofproto->slowpath_meter_id = UINT32_MAX;
ofproto->controller_meter_id = UINT32_MAX;
/* Set the initial tables version. */
ofproto_bump_tables_version(ofproto);
*ofprotop = ofproto;
return 0;
}
/* Must be called (only) by an ofproto implementation in its constructor
* function. See the large comment on 'construct' in struct ofproto_class for
* details. */
void
ofproto_init_tables(struct ofproto *ofproto, int n_tables)
{
struct oftable *table;
ovs_assert(!ofproto->n_tables);
ovs_assert(n_tables >= 1 && n_tables <= 255);
ofproto->n_tables = n_tables;
ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
OFPROTO_FOR_EACH_TABLE (table, ofproto) {
oftable_init(table);
}
}
/* To be optionally called (only) by an ofproto implementation in its
* constructor function. See the large comment on 'construct' in struct
* ofproto_class for details.
*
* Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
* will then ensure that actions passed into the ofproto implementation will
* not refer to OpenFlow ports numbered 'max_ports' or higher. If this
* function is not called, there will be no such restriction.
*
* Reserved ports numbered OFPP_MAX and higher are special and not subject to
* the 'max_ports' restriction. */
void
ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
{
ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
ofproto->max_ports = max_ports;
}
uint64_t
ofproto_get_datapath_id(const struct ofproto *ofproto)
{
return ofproto->datapath_id;
}
void
ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
{
uint64_t old_dpid = p->datapath_id;
p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
if (p->datapath_id != old_dpid) {
/* Force all active connections to reconnect, since there is no way to
* notify a controller that the datapath ID has changed. */
ofproto_reconnect_controllers(p);
}
}
void
ofproto_set_controllers(struct ofproto *p,
const struct ofproto_controller *controllers,
size_t n_controllers, uint32_t allowed_versions)
{
connmgr_set_controllers(p->connmgr, controllers, n_controllers,
allowed_versions);
}
void
ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
{
connmgr_set_fail_mode(p->connmgr, fail_mode);
}
/* Drops the connections between 'ofproto' and all of its controllers, forcing
* them to reconnect. */
void
ofproto_reconnect_controllers(struct ofproto *ofproto)
{
connmgr_reconnect(ofproto->connmgr);
}
/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
* in-band control should guarantee access, in the same way that in-band
* control guarantees access to OpenFlow controllers. */
void
ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
const struct sockaddr_in *extras, size_t n)
{
connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
}
/* Sets the OpenFlow queue used by flows set up by in-band control on
* 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
* flows will use the default queue. */
void
ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
{
connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
}
/* Sets the number of flows at which eviction from the kernel flow table
* will occur. */
void
ofproto_set_flow_limit(unsigned limit)
{
ofproto_flow_limit = limit;
}
/* Sets the maximum idle time for flows in the datapath before they are
* expired. */
void
ofproto_set_max_idle(unsigned max_idle)
{
ofproto_max_idle = max_idle;
}
/* If forward_bpdu is true, the NORMAL action will forward frames with
* reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
* the NORMAL action will drop these frames. */
void
ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
{
bool old_val = ofproto->forward_bpdu;
ofproto->forward_bpdu = forward_bpdu;
if (old_val != ofproto->forward_bpdu) {
if (ofproto->ofproto_class->forward_bpdu_changed) {
ofproto->ofproto_class->forward_bpdu_changed(ofproto);
}
}
}
/* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
* 'idle_time', in seconds, and the maximum number of MAC table entries to
* 'max_entries'. */
void
ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
size_t max_entries)
{
if (ofproto->ofproto_class->set_mac_table_config) {
ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
max_entries);
}
}
/* Multicast snooping configuration. */
/* Configures multicast snooping on 'ofproto' using the settings
* defined in 's'. If 's' is NULL, disables multicast snooping.
*
* Returns 0 if successful, otherwise a positive errno value. */
int
ofproto_set_mcast_snooping(struct ofproto *ofproto,
const struct ofproto_mcast_snooping_settings *s)
{
return (ofproto->ofproto_class->set_mcast_snooping
? ofproto->ofproto_class->set_mcast_snooping(ofproto, s)
: EOPNOTSUPP);
}
/* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'.
*
* Returns 0 if successful, otherwise a positive errno value.*/
int
ofproto_port_set_mcast_snooping(struct ofproto *ofproto, void *aux,
const struct ofproto_mcast_snooping_port_settings *s)
{
return (ofproto->ofproto_class->set_mcast_snooping_port
? ofproto->ofproto_class->set_mcast_snooping_port(ofproto, aux, s)
: EOPNOTSUPP);
}
void
ofproto_type_set_config(const char *datapath_type, const struct smap *cfg)
{
const struct ofproto_class *class;
datapath_type = ofproto_normalize_type(datapath_type);
class = ofproto_class_find__(datapath_type);
if (class->type_set_config) {
class->type_set_config(datapath_type, cfg);
}
}
void
ofproto_set_threads(int n_handlers_, int n_revalidators_)
{
int threads = MAX(count_cpu_cores(), 2);
n_revalidators = MAX(n_revalidators_, 0);
n_handlers = MAX(n_handlers_, 0);
if (!n_revalidators) {
n_revalidators = n_handlers
? MAX(threads - (int) n_handlers, 1)
: threads / 4 + 1;
}
if (!n_handlers) {
n_handlers = MAX(threads - (int) n_revalidators, 1);
}
}
void
ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
{
free(p->dp_desc);
p->dp_desc = nullable_xstrdup(dp_desc);
}
int
ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
{
return connmgr_set_snoops(ofproto->connmgr, snoops);
}
int
ofproto_set_netflow(struct ofproto *ofproto,
const struct netflow_options *nf_options)
{
if (nf_options && sset_is_empty(&nf_options->collectors)) {
nf_options = NULL;
}
if (ofproto->ofproto_class->set_netflow) {
return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
} else {
return nf_options ? EOPNOTSUPP : 0;
}
}
int
ofproto_set_sflow(struct ofproto *ofproto,
const struct ofproto_sflow_options *oso)
{
if (oso && sset_is_empty(&oso->targets)) {
oso = NULL;
}
if (ofproto->ofproto_class->set_sflow) {
return ofproto->ofproto_class->set_sflow(ofproto, oso);
} else {
return oso ? EOPNOTSUPP : 0;
}
}
int
ofproto_set_ipfix(struct ofproto *ofproto,
const struct ofproto_ipfix_bridge_exporter_options *bo,
const struct ofproto_ipfix_flow_exporter_options *fo,
size_t n_fo)
{
if (ofproto->ofproto_class->set_ipfix) {
return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
} else {
return (bo || fo) ? EOPNOTSUPP : 0;
}
}
static int
ofproto_get_ipfix_stats(struct ofproto *ofproto,
bool bridge_ipfix,
struct ovs_list *replies)
{
int error;
if (ofproto->ofproto_class->get_ipfix_stats) {
error = ofproto->ofproto_class->get_ipfix_stats(ofproto,
bridge_ipfix,
replies);
} else {
error = EOPNOTSUPP;
}
return error;
}
static enum ofperr
handle_ipfix_bridge_stats_request(struct ofconn *ofconn,
const struct ofp_header *request)
{
struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
struct ovs_list replies;
enum ofperr error;
ofpmp_init(&replies, request);
error = ofproto_get_ipfix_stats(ofproto, true, &replies);
if (!error) {
ofconn_send_replies(ofconn, &replies);
} else {
ofpbuf_list_delete(&replies);
}
return error;
}
static enum ofperr
handle_ipfix_flow_stats_request(struct ofconn *ofconn,
const struct ofp_header *request)
{
struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
struct ovs_list replies;
enum ofperr error;
ofpmp_init(&replies, request);
error = ofproto_get_ipfix_stats(ofproto, false, &replies);
if (!error) {
ofconn_send_replies(ofconn, &replies);
} else {
ofpbuf_list_delete(&replies);
}
return error;
}
static enum ofperr
handle_nxt_ct_flush_zone(struct ofconn *ofconn, const struct ofp_header *oh)
{
struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
const struct nx_zone_id *nzi = ofpmsg_body(oh);
if (!is_all_zeros(nzi->zero, sizeof nzi->zero)) {
return OFPERR_NXBRC_MUST_BE_ZERO;
}
uint16_t zone = ntohs(nzi->zone_id);
if (ofproto->ofproto_class->ct_flush) {
ofproto->ofproto_class->ct_flush(ofproto, &zone);
} else {
return EOPNOTSUPP;
}
return 0;
}
void
ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
{
flow_restore_wait = flow_restore_wait_db;
}
bool
ofproto_get_flow_restore_wait(void)
{
return flow_restore_wait;
}
/* Spanning Tree Protocol (STP) configuration. */
/* Configures STP on 'ofproto' using the settings defined in 's'. If
* 's' is NULL, disables STP.
*
* Returns 0 if successful, otherwise a positive errno value. */
int
ofproto_set_stp(struct ofproto *ofproto,
const struct ofproto_stp_settings *s)
{
return (ofproto->ofproto_class->set_stp
? ofproto->ofproto_class->set_stp(ofproto, s)
: EOPNOTSUPP);
}
/* Retrieves STP status of 'ofproto' and stores it in 's'. If the
* 'enabled' member of 's' is false, then the other members are not
* meaningful.
*
* Returns 0 if successful, otherwise a positive errno value. */
int
ofproto_get_stp_status(struct ofproto *ofproto,
struct ofproto_stp_status *s)
{
return (ofproto->ofproto_class->get_stp_status
? ofproto->ofproto_class->get_stp_status(ofproto, s)
: EOPNOTSUPP);
}
/* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
* in 's'. The caller is responsible for assigning STP port numbers
* (using the 'port_num' member in the range of 1 through 255, inclusive)
* and ensuring there are no duplicates. If the 's' is NULL, then STP
* is disabled on the port.
*
* Returns 0 if successful, otherwise a positive errno value.*/
int
ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
const struct ofproto_port_stp_settings *s)
{
struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
if (!ofport) {
VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32,
ofproto->name, ofp_port);
return ENODEV;
}
return (ofproto->ofproto_class->set_stp_port
? ofproto->ofproto_class->set_stp_port(ofport, s)
: EOPNOTSUPP);
}
/* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
* 's'. If the 'enabled' member in 's' is false, then the other members
* are not meaningful.
*
* Returns 0 if successful, otherwise a positive errno value.*/
int
ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
struct ofproto_port_stp_status *s)
{
struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
if (!ofport) {
VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
"port %"PRIu32, ofproto->name, ofp_port);
return ENODEV;
}