forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofproto.c
3194 lines (2783 loc) · 99.7 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, 2010, 2011 Nicira Networks.
* 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 "ofproto.h"
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include "byte-order.h"
#include "classifier.h"
#include "connmgr.h"
#include "coverage.h"
#include "dynamic-string.h"
#include "hash.h"
#include "hmap.h"
#include "netdev.h"
#include "nx-match.h"
#include "ofp-print.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "ofproto-provider.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
#include "packets.h"
#include "pinsched.h"
#include "pktbuf.h"
#include "poll-loop.h"
#include "shash.h"
#include "sset.h"
#include "timeval.h"
#include "unaligned.h"
#include "unixctl.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(ofproto);
COVERAGE_DEFINE(ofproto_error);
COVERAGE_DEFINE(ofproto_flush);
COVERAGE_DEFINE(ofproto_no_packet_in);
COVERAGE_DEFINE(ofproto_packet_out);
COVERAGE_DEFINE(ofproto_queue_req);
COVERAGE_DEFINE(ofproto_recv_openflow);
COVERAGE_DEFINE(ofproto_reinit_ports);
COVERAGE_DEFINE(ofproto_uninstallable);
COVERAGE_DEFINE(ofproto_update_port);
enum ofproto_state {
S_OPENFLOW, /* Processing OpenFlow commands. */
S_FLUSH, /* Deleting all flow table rules. */
};
enum ofoperation_type {
OFOPERATION_ADD,
OFOPERATION_DELETE,
OFOPERATION_MODIFY
};
/* A single OpenFlow request can execute any number of operations. The
* ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
* ofconn to which an error reply should be sent if necessary.
*
* ofproto initiates some operations internally. These operations are still
* assigned to groups but will not have an associated ofconn. */
struct ofopgroup {
struct ofproto *ofproto; /* Owning ofproto. */
struct list ofproto_node; /* In ofproto's "pending" list. */
struct list ops; /* List of "struct ofoperation"s. */
/* Data needed to send OpenFlow reply on failure or to send a buffered
* packet on success.
*
* If list_is_empty(ofconn_node) then this ofopgroup never had an
* associated ofconn or its ofconn's connection dropped after it initiated
* the operation. In the latter case 'ofconn' is a wild pointer that
* refers to freed memory, so the 'ofconn' member must be used only if
* !list_is_empty(ofconn_node).
*/
struct list ofconn_node; /* In ofconn's list of pending opgroups. */
struct ofconn *ofconn; /* ofconn for reply (but see note above). */
struct ofp_header *request; /* Original request (truncated at 64 bytes). */
uint32_t buffer_id; /* Buffer id from original request. */
int error; /* 0 if no error yet, otherwise error code. */
};
static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
const struct ofp_header *,
uint32_t buffer_id);
static void ofopgroup_submit(struct ofopgroup *);
static void ofopgroup_destroy(struct ofopgroup *);
/* A single flow table operation. */
struct ofoperation {
struct ofopgroup *group; /* Owning group. */
struct list group_node; /* In ofopgroup's "ops" list. */
struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
struct rule *rule; /* Rule being operated upon. */
enum ofoperation_type type; /* Type of operation. */
int status; /* -1 if pending, otherwise 0 or error code. */
struct rule *victim; /* OFOPERATION_ADDING: Replaced rule. */
union ofp_action *actions; /* OFOPERATION_MODIFYING: Replaced actions. */
int n_actions; /* OFOPERATION_MODIFYING: # of old actions. */
ovs_be64 flow_cookie; /* Rule's old flow cookie. */
};
static void ofoperation_create(struct ofopgroup *, struct rule *,
enum ofoperation_type);
static void ofoperation_destroy(struct ofoperation *);
static void ofport_destroy__(struct ofport *);
static void ofport_destroy(struct ofport *);
static uint64_t pick_datapath_id(const struct ofproto *);
static uint64_t pick_fallback_dpid(void);
static void ofproto_destroy__(struct ofproto *);
static void ofproto_rule_destroy__(struct rule *);
static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
static void ofopgroup_destroy(struct ofopgroup *);
static int add_flow(struct ofproto *, struct ofconn *,
const struct ofputil_flow_mod *,
const struct ofp_header *);
static bool handle_openflow(struct ofconn *, struct ofpbuf *);
static int handle_flow_mod__(struct ofproto *, struct ofconn *,
const struct ofputil_flow_mod *,
const struct ofp_header *);
static void update_port(struct ofproto *, const char *devname);
static int init_ports(struct ofproto *);
static void reinit_ports(struct ofproto *);
static void set_internal_devs_mtu(struct ofproto *);
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;
/* Map from datapath name to struct ofproto, for use by unixctl commands. */
static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
static void
ofproto_initialize(void)
{
static bool inited;
if (!inited) {
inited = true;
ofproto_class_register(&ofproto_dpif_class);
}
}
/* '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;
ofproto_initialize();
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;
ofproto_initialize();
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;
}
int
ofproto_create(const char *datapath_name, const char *datapath_type,
struct ofproto **ofprotop)
{
const struct ofproto_class *class;
struct classifier *table;
struct ofproto *ofproto;
int n_tables;
int error;
*ofprotop = NULL;
ofproto_initialize();
ofproto_unixctl_init();
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. */
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_set_flow_eviction_threshold(ofproto,
OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT);
ofproto->forward_bpdu = false;
ofproto->fallback_dpid = pick_fallback_dpid();
ofproto->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
ofproto->hw_desc = xstrdup(DEFAULT_HW_DESC);
ofproto->sw_desc = xstrdup(DEFAULT_SW_DESC);
ofproto->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
ofproto->dp_desc = xstrdup(DEFAULT_DP_DESC);
ofproto->frag_handling = OFPC_FRAG_NORMAL;
hmap_init(&ofproto->ports);
shash_init(&ofproto->port_by_name);
ofproto->tables = NULL;
ofproto->n_tables = 0;
ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
ofproto->state = S_OPENFLOW;
list_init(&ofproto->pending);
ofproto->n_pending = 0;
hmap_init(&ofproto->deletions);
error = ofproto->ofproto_class->construct(ofproto, &n_tables);
if (error) {
VLOG_ERR("failed to open datapath %s: %s",
datapath_name, strerror(error));
ofproto_destroy__(ofproto);
return error;
}
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) {
classifier_init(table);
}
ofproto->datapath_id = pick_datapath_id(ofproto);
VLOG_INFO("using datapath ID %016"PRIx64, ofproto->datapath_id);
init_ports(ofproto);
*ofprotop = ofproto;
return 0;
}
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) {
VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
/* 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)
{
connmgr_set_controllers(p->connmgr, controllers, n_controllers);
}
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_eviction_threshold(struct ofproto *ofproto, unsigned threshold)
{
if (threshold < OFPROTO_FLOW_EVICTION_THRESHOLD_MIN) {
ofproto->flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_MIN;
} else {
ofproto->flow_eviction_threshold = threshold;
}
}
/* 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);
}
}
}
void
ofproto_set_desc(struct ofproto *p,
const char *mfr_desc, const char *hw_desc,
const char *sw_desc, const char *serial_desc,
const char *dp_desc)
{
struct ofp_desc_stats *ods;
if (mfr_desc) {
if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
sizeof ods->mfr_desc);
}
free(p->mfr_desc);
p->mfr_desc = xstrdup(mfr_desc);
}
if (hw_desc) {
if (strlen(hw_desc) >= sizeof ods->hw_desc) {
VLOG_WARN("truncating hw_desc, must be less than %zu characters",
sizeof ods->hw_desc);
}
free(p->hw_desc);
p->hw_desc = xstrdup(hw_desc);
}
if (sw_desc) {
if (strlen(sw_desc) >= sizeof ods->sw_desc) {
VLOG_WARN("truncating sw_desc, must be less than %zu characters",
sizeof ods->sw_desc);
}
free(p->sw_desc);
p->sw_desc = xstrdup(sw_desc);
}
if (serial_desc) {
if (strlen(serial_desc) >= sizeof ods->serial_num) {
VLOG_WARN("truncating serial_desc, must be less than %zu "
"characters",
sizeof ods->serial_num);
}
free(p->serial_desc);
p->serial_desc = xstrdup(serial_desc);
}
if (dp_desc) {
if (strlen(dp_desc) >= sizeof ods->dp_desc) {
VLOG_WARN("truncating dp_desc, must be less than %zu characters",
sizeof ods->dp_desc);
}
free(p->dp_desc);
p->dp_desc = 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;
}
}
/* 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, uint16_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 %"PRIu16,
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, uint16_t ofp_port,
struct ofproto_port_stp_status *s)
{
struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
if (!ofport) {
VLOG_WARN("%s: cannot get STP status on nonexistent port %"PRIu16,
ofproto->name, ofp_port);
return ENODEV;
}
return (ofproto->ofproto_class->get_stp_port_status
? ofproto->ofproto_class->get_stp_port_status(ofport, s)
: EOPNOTSUPP);
}
/* Connectivity Fault Management configuration. */
/* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
void
ofproto_port_clear_cfm(struct ofproto *ofproto, uint16_t ofp_port)
{
struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
if (ofport && ofproto->ofproto_class->set_cfm) {
ofproto->ofproto_class->set_cfm(ofport, NULL);
}
}
/* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
* basic configuration from the configuration members in 'cfm', and the remote
* maintenance point ID from remote_mpid. Ignores the statistics members of
* 'cfm'.
*
* This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
void
ofproto_port_set_cfm(struct ofproto *ofproto, uint16_t ofp_port,
const struct cfm_settings *s)
{
struct ofport *ofport;
int error;
ofport = ofproto_get_port(ofproto, ofp_port);
if (!ofport) {
VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
ofproto->name, ofp_port);
return;
}
/* XXX: For configuration simplicity, we only support one remote_mpid
* outside of the CFM module. It's not clear if this is the correct long
* term solution or not. */
error = (ofproto->ofproto_class->set_cfm
? ofproto->ofproto_class->set_cfm(ofport, s)
: EOPNOTSUPP);
if (error) {
VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
strerror(error));
}
}
/* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
* Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
* 0 if LACP partner information is not current (generally indicating a
* connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
int
ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port)
{
struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
return (ofport && ofproto->ofproto_class->port_is_lacp_current
? ofproto->ofproto_class->port_is_lacp_current(ofport)
: -1);
}
/* Bundles. */
/* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
* A bundle is the same concept as a Port in OVSDB, that is, it consists of one
* or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
* configuration plus, if there is more than one slave, a bonding
* configuration.
*
* If 'aux' is already registered then this function updates its configuration
* to 's'. Otherwise, this function registers a new bundle.
*
* Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
* port. */
int
ofproto_bundle_register(struct ofproto *ofproto, void *aux,
const struct ofproto_bundle_settings *s)
{
return (ofproto->ofproto_class->bundle_set
? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
: EOPNOTSUPP);
}
/* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
* If no such bundle has been registered, this has no effect. */
int
ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
{
return ofproto_bundle_register(ofproto, aux, NULL);
}
/* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
* If 'aux' is already registered then this function updates its configuration
* to 's'. Otherwise, this function registers a new mirror.
*
* Mirrors affect only the treatment of packets output to the OFPP_NORMAL
* port. */
int
ofproto_mirror_register(struct ofproto *ofproto, void *aux,
const struct ofproto_mirror_settings *s)
{
return (ofproto->ofproto_class->mirror_set
? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
: EOPNOTSUPP);
}
/* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
* If no mirror has been registered, this has no effect. */
int
ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
{
return ofproto_mirror_register(ofproto, aux, NULL);
}
/* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
* which all packets are flooded, instead of using MAC learning. If
* 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
*
* Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
* port. */
int
ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
{
return (ofproto->ofproto_class->set_flood_vlans
? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
: EOPNOTSUPP);
}
/* Returns true if 'aux' is a registered bundle that is currently in use as the
* output for a mirror. */
bool
ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
{
return (ofproto->ofproto_class->is_mirror_output_bundle
? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
: false);
}
bool
ofproto_has_snoops(const struct ofproto *ofproto)
{
return connmgr_has_snoops(ofproto->connmgr);
}
void
ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
{
connmgr_get_snoops(ofproto->connmgr, snoops);
}
static void
ofproto_flush__(struct ofproto *ofproto)
{
struct classifier *table;
struct ofopgroup *group;
if (ofproto->ofproto_class->flush) {
ofproto->ofproto_class->flush(ofproto);
}
group = ofopgroup_create_unattached(ofproto);
OFPROTO_FOR_EACH_TABLE (table, ofproto) {
struct rule *rule, *next_rule;
struct cls_cursor cursor;
cls_cursor_init(&cursor, table, NULL);
CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
if (!rule->pending) {
ofoperation_create(group, rule, OFOPERATION_DELETE);
classifier_remove(table, &rule->cr);
ofproto->ofproto_class->rule_destruct(rule);
}
}
}
ofopgroup_submit(group);
}
static void
ofproto_destroy__(struct ofproto *ofproto)
{
struct classifier *table;
assert(list_is_empty(&ofproto->pending));
assert(!ofproto->n_pending);
connmgr_destroy(ofproto->connmgr);
hmap_remove(&all_ofprotos, &ofproto->hmap_node);
free(ofproto->name);
free(ofproto->type);
free(ofproto->mfr_desc);
free(ofproto->hw_desc);
free(ofproto->sw_desc);
free(ofproto->serial_desc);
free(ofproto->dp_desc);
hmap_destroy(&ofproto->ports);
shash_destroy(&ofproto->port_by_name);
OFPROTO_FOR_EACH_TABLE (table, ofproto) {
assert(classifier_is_empty(table));
classifier_destroy(table);
}
free(ofproto->tables);
hmap_destroy(&ofproto->deletions);
ofproto->ofproto_class->dealloc(ofproto);
}
void
ofproto_destroy(struct ofproto *p)
{
struct ofport *ofport, *next_ofport;
if (!p) {
return;
}
ofproto_flush__(p);
HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
ofport_destroy(ofport);
}
p->ofproto_class->destruct(p);
ofproto_destroy__(p);
}
/* Destroys the datapath with the respective 'name' and 'type'. With the Linux
* kernel datapath, for example, this destroys the datapath in the kernel, and
* with the netdev-based datapath, it tears down the data structures that
* represent the datapath.
*
* The datapath should not be currently open as an ofproto. */
int
ofproto_delete(const char *name, const char *type)
{
const struct ofproto_class *class = ofproto_class_find__(type);
return (!class ? EAFNOSUPPORT
: !class->del ? EACCES
: class->del(type, name));
}
static void
process_port_change(struct ofproto *ofproto, int error, char *devname)
{
if (error == ENOBUFS) {
reinit_ports(ofproto);
} else if (!error) {
update_port(ofproto, devname);
free(devname);
}
}
int
ofproto_run(struct ofproto *p)
{
struct ofport *ofport;
char *devname;
int error;
error = p->ofproto_class->run(p);
if (error == ENODEV) {
/* Someone destroyed the datapath behind our back. The caller
* better destroy us and give up, because we're just going to
* spin from here on out. */
static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
p->name);
return ENODEV;
}
if (p->ofproto_class->port_poll) {
while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
process_port_change(p, error, devname);
}
}
HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
unsigned int change_seq = netdev_change_seq(ofport->netdev);
if (ofport->change_seq != change_seq) {
ofport->change_seq = change_seq;
update_port(p, netdev_get_name(ofport->netdev));
}
}
switch (p->state) {
case S_OPENFLOW:
connmgr_run(p->connmgr, handle_openflow);
break;
case S_FLUSH:
connmgr_run(p->connmgr, NULL);
ofproto_flush__(p);
if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
connmgr_flushed(p->connmgr);
p->state = S_OPENFLOW;
}
break;
default:
NOT_REACHED();
}
return 0;
}
void
ofproto_wait(struct ofproto *p)
{
struct ofport *ofport;
p->ofproto_class->wait(p);
if (p->ofproto_class->port_poll_wait) {
p->ofproto_class->port_poll_wait(p);
}
HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
poll_immediate_wake();
}
}
switch (p->state) {
case S_OPENFLOW:
connmgr_wait(p->connmgr, true);
break;
case S_FLUSH:
connmgr_wait(p->connmgr, false);
if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
poll_immediate_wake();
}
break;
}
}
bool
ofproto_is_alive(const struct ofproto *p)
{
return connmgr_has_controllers(p->connmgr);
}
void
ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
struct shash *info)
{
connmgr_get_controller_info(ofproto->connmgr, info);
}
void
ofproto_free_ofproto_controller_info(struct shash *info)
{
connmgr_free_controller_info(info);
}
/* Makes a deep copy of 'old' into 'port'. */
void
ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
{
port->name = xstrdup(old->name);
port->type = xstrdup(old->type);
port->ofp_port = old->ofp_port;
}
/* Frees memory allocated to members of 'ofproto_port'.
*
* Do not call this function on an ofproto_port obtained from
* ofproto_port_dump_next(): that function retains ownership of the data in the
* ofproto_port. */
void
ofproto_port_destroy(struct ofproto_port *ofproto_port)
{
free(ofproto_port->name);
free(ofproto_port->type);
}
/* Initializes 'dump' to begin dumping the ports in an ofproto.
*
* This function provides no status indication. An error status for the entire
* dump operation is provided when it is completed by calling
* ofproto_port_dump_done().
*/
void
ofproto_port_dump_start(struct ofproto_port_dump *dump,
const struct ofproto *ofproto)
{
dump->ofproto = ofproto;
dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
&dump->state);
}
/* Attempts to retrieve another port from 'dump', which must have been created
* with ofproto_port_dump_start(). On success, stores a new ofproto_port into
* 'port' and returns true. On failure, returns false.
*
* Failure might indicate an actual error or merely that the last port has been