forked from ovn-org/ovn
/
ofctrl.c
2339 lines (2087 loc) · 78.3 KB
/
ofctrl.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) 2015, 2016, 2017 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "bitmap.h"
#include "byte-order.h"
#include "dirs.h"
#include "dp-packet.h"
#include "flow.h"
#include "hash.h"
#include "hindex.h"
#include "lflow.h"
#include "ofctrl.h"
#include "openflow/openflow.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/hmap.h"
#include "openvswitch/list.h"
#include "openvswitch/match.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-flow.h"
#include "openvswitch/ofp-group.h"
#include "openvswitch/ofp-match.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-meter.h"
#include "openvswitch/ofp-packet.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "ovn-controller.h"
#include "ovn/actions.h"
#include "lib/extend-table.h"
#include "openvswitch/poll-loop.h"
#include "physical.h"
#include "openvswitch/rconn.h"
#include "socket-util.h"
#include "util.h"
#include "vswitch-idl.h"
VLOG_DEFINE_THIS_MODULE(ofctrl);
/* An OpenFlow flow. */
struct ovn_flow {
/* Key. */
uint8_t table_id;
uint16_t priority;
struct minimatch match;
/* Hash. */
uint32_t hash;
/* Data. */
struct ofpact *ofpacts;
size_t ofpacts_len;
uint64_t cookie;
};
/* A desired flow, in struct ovn_desired_flow_table, calculated by the
* incremental processing engine.
* - They are added/removed incrementally when I-P engine is able to process
* the changes incrementally, or
* - Completely cleared and recomputed by I-P engine when recompute happens.
*
* Links are maintained between desired flows and SB data. The relationship
* is M to N. The struct sb_flow_ref is used to link a pair of desired flow
* and SB UUID. The below diagram depicts the data structure.
*
* SB UUIDs
* +-----+-----+-----+-----+-----+-----+-----+
* | | | | | | | |
* +--+--+--+--+--+--+-----+--+--+--+--+--+--+
* | | | | | |
* Desired Flows | | | | | |
* +----+ +-+-+ | +-+-+ | +-+-+ |
* | +-------+ +-------+ +-------------+ | |
* +----+ +---+ | +-+-+ | +---+ |
* | | | | | |
* +----+ | | +-+-+ |
* | +-------------------------------+ | |
* +----+ +---+ | +---+ |
* | +-------------+ | | |
* +----+ +---+ | |
* | | | |
* +----+ +-+-+ +-+-+
* | +-------------------+ +-------------------+ |
* +----+ +---+ +---+
* | |
* +----+
*
* The links are updated whenever there is a change in desired flows, which is
* usually triggered by a SB data change in I-P engine.
*
* ** Tracking **
*
* A desired flow can be tracked - listed in ovn_desired_flow_table's
* tracked_flows.
*
* Tracked flows is initially empty, and stays empty after the first run of I-P
* engine when installed flows are initially populated. After that, flow
* changes are tracked when I-P engine incrementally computes flow changes.
* Tracked flows are then processed and removed completely in ofctrl_put.
* ("processed" means OpenFlow change messages are composed and sent/queued to
* OVS, which ensures flows in OVS is always in sync (eventually) with the
* installed flows table).
*
* In case of full recompute of I-P engine, tracked flows are not
* added/removed, and ofctrl_put will not rely on tracked flows. (It is I-P
* engine's responsibility to ensure the tracked flows are cleared before
* recompute).
*
* Tracked flows can be preserved across multiple I-P engine runs - if in some
* iterations ofctrl_put() is skipped. Tracked flows are cleared only when it
* is consumed or when flow recompute happens.
*
* The "change_tracked" member of desired flow table maintains the status of
* whether flow changes are tracked or not. It is always set to true when
* ofctrl_put is completed, and transition to false whenever
* ovn_desired_flow_table_clear is called.
*
* NOTE: A tracked flow is just a reference to a desired flow, instead of a new
* copy. When a desired flow is removed and tracked, it is removed from the
* match_flow_table and uuid_flow_table indexes, and added to the tracked_flows
* list, marking is_deleted = true, but not immediately destroyed. It is
* destroyed when the tracking is processed for installed flow updates.
*/
struct desired_flow {
struct ovn_flow flow;
struct hmap_node match_hmap_node; /* For match based hashing. */
struct ovs_list list_node; /* For handling lists of flows. */
/* A list of struct sb_flow_ref nodes, which references this flow. (There
* are cases that multiple SB entities share the same desired OpenFlow
* flow, e.g. when conjunction is used.) */
struct ovs_list references;
/* The corresponding flow in installed table. */
struct installed_flow *installed_flow;
/* Node in installed_flow.desired_refs list. */
struct ovs_list installed_ref_list_node;
/* For tracking. */
struct ovs_list track_list_node; /* node in ovn_desired_flow_table's
* tracked_flows list. */
bool is_deleted; /* If the tracked flow is deleted. */
};
struct sb_to_flow {
struct hmap_node hmap_node; /* Node in
ovn_desired_flow_table.uuid_flow_table. */
struct uuid sb_uuid;
struct ovs_list flows; /* A list of struct sb_flow_ref nodes that
are referenced by the sb_uuid. */
};
struct sb_flow_ref {
struct ovs_list sb_list; /* List node in desired_flow.references. */
struct ovs_list flow_list; /* List node in sb_to_flow.desired_flows. */
struct desired_flow *flow;
struct uuid sb_uuid;
};
/* A installed flow, in static variable installed_flows.
*
* Installed flows are updated in ofctrl_put for maintaining the flow
* installation to OVS. They are updated according to desired flows: either by
* processing the tracked desired flow changes, or by comparing desired flows
* with currently installed flows when tracked desired flows changes are not
* available.
*
* In addition, when ofctrl state machine enters S_CLEAR, the installed flows
* will be cleared. (This happens in initialization phase and also when
* ovs-vswitchd is disconnected/reconnected).
*
* Links are maintained between installed flows and desired flows. The
* relationship is 1 to N. A link is added when a flow addition is processed.
* A link is removed when a flow deletion is processed, the desired flow
* table is cleared, or the installed flow table is cleared.
*
* To ensure predictable behavior, the list of desired flows is maintained
* partially sorted in the following way (from least restrictive to most
* restrictive wrt. match):
* - allow flows without action conjunction.
* - drop flows without action conjunction.
* - a single flow with action conjunction.
*
* The first desired_flow in the list is the active one, the one that is
* actually installed.
*/
struct installed_flow {
struct ovn_flow flow;
struct hmap_node match_hmap_node; /* For match based hashing. */
/* A list of desired ovn_flow nodes (linked by
* desired_flow.installed_ref_list_node), which reference this installed
* flow. (There are cases that multiple desired flows reference the same
* installed flow, e.g. when there are conflict/duplicated ACLs that
* generates same match conditions). */
struct ovs_list desired_refs;
};
typedef bool
(*desired_flow_match_cb)(const struct desired_flow *candidate,
const void *arg);
static struct desired_flow *desired_flow_alloc(
uint8_t table_id,
uint16_t priority,
uint64_t cookie,
const struct match *match,
const struct ofpbuf *actions);
static struct desired_flow *desired_flow_lookup(
struct ovn_desired_flow_table *,
const struct ovn_flow *target);
static struct desired_flow *desired_flow_lookup_check_uuid(
struct ovn_desired_flow_table *,
const struct ovn_flow *target,
const struct uuid *);
static struct desired_flow *desired_flow_lookup_conjunctive(
struct ovn_desired_flow_table *,
const struct ovn_flow *target);
static void desired_flow_destroy(struct desired_flow *);
static struct installed_flow *installed_flow_lookup(
const struct ovn_flow *target);
static void installed_flow_destroy(struct installed_flow *);
static struct installed_flow *installed_flow_dup(struct desired_flow *);
static struct desired_flow *installed_flow_get_active(struct installed_flow *);
static uint32_t ovn_flow_match_hash(const struct ovn_flow *);
static char *ovn_flow_to_string(const struct ovn_flow *);
static void ovn_flow_log(const struct ovn_flow *, const char *action);
/* OpenFlow connection to the switch. */
static struct rconn *swconn;
/* Symbol table for OVN expressions. */
static struct shash symtab;
/* Last seen sequence number for 'swconn'. When this differs from
* rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
static unsigned int seqno;
/* Connection state machine. */
#define STATES \
STATE(S_NEW) \
STATE(S_TLV_TABLE_REQUESTED) \
STATE(S_TLV_TABLE_MOD_SENT) \
STATE(S_CLEAR_FLOWS) \
STATE(S_UPDATE_FLOWS)
enum ofctrl_state {
#define STATE(NAME) NAME,
STATES
#undef STATE
};
/* An in-flight update to the switch's flow table.
*
* When we receive a barrier reply from the switch with the given 'xid', we
* know that the switch is caught up to northbound database sequence number
* 'nb_cfg' (and make that available to the client via ofctrl_get_cur_cfg(), so
* that it can store it into our Chassis record's nb_cfg column). */
struct ofctrl_flow_update {
struct ovs_list list_node; /* In 'flow_updates'. */
ovs_be32 xid; /* OpenFlow transaction ID for barrier. */
int64_t nb_cfg; /* Northbound database sequence number. */
};
static struct ofctrl_flow_update *
ofctrl_flow_update_from_list_node(const struct ovs_list *list_node)
{
return CONTAINER_OF(list_node, struct ofctrl_flow_update, list_node);
}
/* Currently in-flight updates. */
static struct ovs_list flow_updates;
/* nb_cfg of latest committed flow update. */
static int64_t cur_cfg;
/* Current state. */
static enum ofctrl_state state;
/* Transaction IDs for messages in flight to the switch. */
static ovs_be32 xid, xid2;
/* Counter for in-flight OpenFlow messages on 'swconn'. We only send a new
* round of flow table modifications to the switch when the counter falls to
* zero, to avoid unbounded buffering. */
static struct rconn_packet_counter *tx_counter;
/* Flow table of "struct ovn_flow"s, that holds the flow table currently
* installed in the switch. */
static struct hmap installed_flows;
/* A reference to the group_table. */
static struct ovn_extend_table *groups;
/* A reference to the meter_table. */
static struct ovn_extend_table *meters;
/* MFF_* field ID for our Geneve option. In S_TLV_TABLE_MOD_SENT, this is
* the option we requested (we don't know whether we obtained it yet). In
* S_CLEAR_FLOWS or S_UPDATE_FLOWS, this is really the option we have. */
static enum mf_field_id mff_ovn_geneve;
/* Indicates if flows need to be reinstalled for scenarios when ovs
* is restarted, even if there is no change in the desired flow table. */
static bool need_reinstall_flows;
static ovs_be32 queue_msg(struct ofpbuf *);
static struct ofpbuf *encode_flow_mod(struct ofputil_flow_mod *);
static struct ofpbuf *encode_group_mod(const struct ofputil_group_mod *);
static struct ofpbuf *encode_meter_mod(const struct ofputil_meter_mod *);
static void ovn_installed_flow_table_clear(void);
static void ovn_installed_flow_table_destroy(void);
static void ofctrl_recv(const struct ofp_header *, enum ofptype);
void
ofctrl_init(struct ovn_extend_table *group_table,
struct ovn_extend_table *meter_table,
int inactivity_probe_interval)
{
swconn = rconn_create(inactivity_probe_interval, 0,
DSCP_DEFAULT, 1 << OFP15_VERSION);
tx_counter = rconn_packet_counter_create();
hmap_init(&installed_flows);
ovs_list_init(&flow_updates);
ovn_init_symtab(&symtab);
groups = group_table;
meters = meter_table;
}
/* S_NEW, for a new connection.
*
* Sends NXT_TLV_TABLE_REQUEST and transitions to
* S_TLV_TABLE_REQUESTED. */
static void
run_S_NEW(void)
{
struct ofpbuf *buf = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_REQUEST,
rconn_get_version(swconn), 0);
xid = queue_msg(buf);
state = S_TLV_TABLE_REQUESTED;
}
static void
recv_S_NEW(const struct ofp_header *oh OVS_UNUSED,
enum ofptype type OVS_UNUSED,
struct shash *pending_ct_zones OVS_UNUSED)
{
OVS_NOT_REACHED();
}
/* S_TLV_TABLE_REQUESTED, when NXT_TLV_TABLE_REQUEST has been sent
* and we're waiting for a reply.
*
* If we receive an NXT_TLV_TABLE_REPLY:
*
* - If it contains our tunnel metadata option, assign its field ID to
* mff_ovn_geneve and transition to S_CLEAR_FLOWS.
*
* - Otherwise, if there is an unused tunnel metadata field ID, send
* NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST, and transition to
* S_TLV_TABLE_MOD_SENT.
*
* - Otherwise, log an error, disable Geneve, and transition to
* S_CLEAR_FLOWS.
*
* If we receive an OFPT_ERROR:
*
* - Log an error, disable Geneve, and transition to S_CLEAR_FLOWS. */
static void
run_S_TLV_TABLE_REQUESTED(void)
{
}
static bool
process_tlv_table_reply(const struct ofputil_tlv_table_reply *reply)
{
const struct ofputil_tlv_map *map;
uint64_t md_free = UINT64_MAX;
BUILD_ASSERT(TUN_METADATA_NUM_OPTS == 64);
LIST_FOR_EACH (map, list_node, &reply->mappings) {
if (map->option_class == OVN_GENEVE_CLASS
&& map->option_type == OVN_GENEVE_TYPE
&& map->option_len == OVN_GENEVE_LEN) {
if (map->index >= TUN_METADATA_NUM_OPTS) {
VLOG_ERR("desired Geneve tunnel option 0x%"PRIx16","
"%"PRIu8",%"PRIu8" already in use with "
"unsupported index %"PRIu16,
map->option_class, map->option_type,
map->option_len, map->index);
return false;
} else {
mff_ovn_geneve = MFF_TUN_METADATA0 + map->index;
state = S_CLEAR_FLOWS;
return true;
}
}
if (map->index < TUN_METADATA_NUM_OPTS) {
md_free &= ~(UINT64_C(1) << map->index);
}
}
VLOG_DBG("OVN Geneve option not found");
if (!md_free) {
VLOG_ERR("no Geneve options free for use by OVN");
return false;
}
unsigned int index = rightmost_1bit_idx(md_free);
mff_ovn_geneve = MFF_TUN_METADATA0 + index;
struct ofputil_tlv_map tm;
tm.option_class = OVN_GENEVE_CLASS;
tm.option_type = OVN_GENEVE_TYPE;
tm.option_len = OVN_GENEVE_LEN;
tm.index = index;
struct ofputil_tlv_table_mod ttm;
ttm.command = NXTTMC_ADD;
ovs_list_init(&ttm.mappings);
ovs_list_push_back(&ttm.mappings, &tm.list_node);
xid = queue_msg(ofputil_encode_tlv_table_mod(OFP15_VERSION, &ttm));
xid2 = queue_msg(ofputil_encode_barrier_request(OFP15_VERSION));
state = S_TLV_TABLE_MOD_SENT;
return true;
}
static void
recv_S_TLV_TABLE_REQUESTED(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED)
{
if (oh->xid != xid) {
ofctrl_recv(oh, type);
return;
} else if (type == OFPTYPE_NXT_TLV_TABLE_REPLY) {
struct ofputil_tlv_table_reply reply;
enum ofperr error = ofputil_decode_tlv_table_reply(oh, &reply);
if (!error) {
bool ok = process_tlv_table_reply(&reply);
ofputil_uninit_tlv_table(&reply.mappings);
if (ok) {
return;
}
} else {
VLOG_ERR("failed to decode TLV table request (%s)",
ofperr_to_string(error));
}
} else if (type == OFPTYPE_ERROR) {
VLOG_ERR("switch refused to allocate Geneve option (%s)",
ofperr_to_string(ofperr_decode_msg(oh, NULL)));
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_ERR("unexpected reply to TLV table request (%s)", s);
free(s);
}
/* Error path. */
mff_ovn_geneve = 0;
state = S_CLEAR_FLOWS;
}
/* S_TLV_TABLE_MOD_SENT, when NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST
* have been sent and we're waiting for a reply to one or the other.
*
* If we receive an OFPT_ERROR:
*
* - If the error is NXTTMFC_ALREADY_MAPPED or NXTTMFC_DUP_ENTRY, we
* raced with some other controller. Transition to S_NEW.
*
* - Otherwise, log an error, disable Geneve, and transition to
* S_CLEAR_FLOWS.
*
* If we receive OFPT_BARRIER_REPLY:
*
* - Set the tunnel metadata field ID to the one that we requested.
* Transition to S_CLEAR_FLOWS.
*/
static void
run_S_TLV_TABLE_MOD_SENT(void)
{
}
static void
recv_S_TLV_TABLE_MOD_SENT(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED)
{
if (oh->xid != xid && oh->xid != xid2) {
ofctrl_recv(oh, type);
} else if (oh->xid == xid2 && type == OFPTYPE_BARRIER_REPLY) {
state = S_CLEAR_FLOWS;
} else if (oh->xid == xid && type == OFPTYPE_ERROR) {
enum ofperr error = ofperr_decode_msg(oh, NULL);
if (error == OFPERR_NXTTMFC_ALREADY_MAPPED ||
error == OFPERR_NXTTMFC_DUP_ENTRY) {
VLOG_INFO("raced with another controller adding "
"Geneve option (%s); trying again",
ofperr_to_string(error));
state = S_NEW;
} else {
VLOG_ERR("error adding Geneve option (%s)",
ofperr_to_string(error));
goto error;
}
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_ERR("unexpected reply to Geneve option allocation request (%s)",
s);
free(s);
goto error;
}
return;
error:
state = S_CLEAR_FLOWS;
}
/* S_CLEAR_FLOWS, after we've established a Geneve metadata field ID and it's
* time to set up some flows.
*
* Sends an OFPT_TABLE_MOD to clear all flows, then transitions to
* S_UPDATE_FLOWS. */
static void
run_S_CLEAR_FLOWS(void)
{
VLOG_DBG("clearing all flows");
need_reinstall_flows = true;
/* Send a flow_mod to delete all flows. */
struct ofputil_flow_mod fm = {
.table_id = OFPTT_ALL,
.command = OFPFC_DELETE,
};
minimatch_init_catchall(&fm.match);
queue_msg(encode_flow_mod(&fm));
minimatch_destroy(&fm.match);
/* Send a group_mod to delete all groups. */
struct ofputil_group_mod gm;
memset(&gm, 0, sizeof gm);
gm.command = OFPGC11_DELETE;
gm.group_id = OFPG_ALL;
gm.command_bucket_id = OFPG15_BUCKET_ALL;
ovs_list_init(&gm.buckets);
queue_msg(encode_group_mod(&gm));
ofputil_uninit_group_mod(&gm);
/* Clear installed_flows, to match the state of the switch. */
ovn_installed_flow_table_clear();
/* Clear existing groups, to match the state of the switch. */
if (groups) {
ovn_extend_table_clear(groups, true);
}
/* Send a meter_mod to delete all meters. */
struct ofputil_meter_mod mm;
memset(&mm, 0, sizeof mm);
mm.command = OFPMC13_DELETE;
mm.meter.meter_id = OFPM13_ALL;
queue_msg(encode_meter_mod(&mm));
/* Clear existing meters, to match the state of the switch. */
if (meters) {
ovn_extend_table_clear(meters, true);
}
/* All flow updates are irrelevant now. */
struct ofctrl_flow_update *fup, *next;
LIST_FOR_EACH_SAFE (fup, next, list_node, &flow_updates) {
ovs_list_remove(&fup->list_node);
free(fup);
}
state = S_UPDATE_FLOWS;
}
static void
recv_S_CLEAR_FLOWS(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED)
{
ofctrl_recv(oh, type);
}
/* S_UPDATE_FLOWS, for maintaining the flow table over time.
*
* Compare the installed flows to the ones we want. Send OFPT_FLOW_MOD as
* necessary.
*
* This is a terminal state. We only transition out of it if the connection
* drops. */
static void
run_S_UPDATE_FLOWS(void)
{
/* Nothing to do here.
*
* Being in this state enables ofctrl_put() to work, however. */
}
static void
recv_S_UPDATE_FLOWS(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones)
{
if (type == OFPTYPE_BARRIER_REPLY && !ovs_list_is_empty(&flow_updates)) {
struct ofctrl_flow_update *fup = ofctrl_flow_update_from_list_node(
ovs_list_front(&flow_updates));
if (fup->xid == oh->xid) {
if (fup->nb_cfg >= cur_cfg) {
cur_cfg = fup->nb_cfg;
}
ovs_list_remove(&fup->list_node);
free(fup);
}
/* If the barrier xid is associated with an outstanding conntrack
* flush, the flush succeeded. Move the pending ct zone entry
* to the next stage. */
struct shash_node *iter;
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_SENT && ctzpe->of_xid == oh->xid) {
ctzpe->state = CT_ZONE_DB_QUEUED;
}
}
} else {
ofctrl_recv(oh, type);
}
}
enum mf_field_id
ofctrl_get_mf_field_id(void)
{
if (!rconn_is_connected(swconn)) {
return 0;
}
return (state == S_CLEAR_FLOWS || state == S_UPDATE_FLOWS
? mff_ovn_geneve : 0);
}
/* Runs the OpenFlow state machine against 'br_int', which is local to the
* hypervisor on which we are running. Attempts to negotiate a Geneve option
* field for class OVN_GENEVE_CLASS, type OVN_GENEVE_TYPE. */
void
ofctrl_run(const struct ovsrec_bridge *br_int, struct shash *pending_ct_zones)
{
char *target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
if (strcmp(target, rconn_get_target(swconn))) {
VLOG_INFO("%s: connecting to switch", target);
rconn_connect(swconn, target, target);
}
free(target);
rconn_run(swconn);
if (!rconn_is_connected(swconn)) {
return;
}
if (seqno != rconn_get_connection_seqno(swconn)) {
seqno = rconn_get_connection_seqno(swconn);
state = S_NEW;
/* Reset the state of any outstanding ct flushes to resend them. */
struct shash_node *iter;
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_SENT) {
ctzpe->state = CT_ZONE_OF_QUEUED;
}
}
}
bool progress = true;
for (int i = 0; progress && i < 50; i++) {
/* Allow the state machine to run. */
enum ofctrl_state old_state = state;
switch (state) {
#define STATE(NAME) case NAME: run_##NAME(); break;
STATES
#undef STATE
default:
OVS_NOT_REACHED();
}
/* Try to process a received packet. */
struct ofpbuf *msg = rconn_recv(swconn);
if (msg) {
const struct ofp_header *oh = msg->data;
enum ofptype type;
enum ofperr error;
error = ofptype_decode(&type, oh);
if (!error) {
switch (state) {
#define STATE(NAME) case NAME: recv_##NAME(oh, type, pending_ct_zones); break;
STATES
#undef STATE
default:
OVS_NOT_REACHED();
}
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_WARN("could not decode OpenFlow message (%s): %s",
ofperr_to_string(error), s);
free(s);
}
ofpbuf_delete(msg);
}
/* If we did some work, plan to go around again. */
progress = old_state != state || msg;
}
if (progress) {
/* We bailed out to limit the amount of work we do in one go, to allow
* other code a chance to run. We were still making progress at that
* point, so ensure that we come back again without waiting. */
poll_immediate_wake();
}
}
void
ofctrl_wait(void)
{
rconn_run_wait(swconn);
rconn_recv_wait(swconn);
}
void
ofctrl_destroy(void)
{
rconn_destroy(swconn);
ovn_installed_flow_table_destroy();
rconn_packet_counter_destroy(tx_counter);
expr_symtab_destroy(&symtab);
shash_destroy(&symtab);
}
int64_t
ofctrl_get_cur_cfg(void)
{
return cur_cfg;
}
static ovs_be32
queue_msg(struct ofpbuf *msg)
{
const struct ofp_header *oh = msg->data;
ovs_be32 xid_ = oh->xid;
rconn_send(swconn, msg, tx_counter);
return xid_;
}
static void
log_openflow_rl(struct vlog_rate_limit *rl, enum vlog_level level,
const struct ofp_header *oh, const char *title)
{
if (!vlog_should_drop(&this_module, level, rl)) {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 2);
vlog(&this_module, level, "%s: %s", title, s);
free(s);
}
}
static void
ofctrl_recv(const struct ofp_header *oh, enum ofptype type)
{
if (type == OFPTYPE_ECHO_REQUEST) {
queue_msg(ofputil_encode_echo_reply(oh));
} else if (type == OFPTYPE_ERROR) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
log_openflow_rl(&rl, VLL_INFO, oh, "OpenFlow error");
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
log_openflow_rl(&rl, VLL_DBG, oh, "OpenFlow packet ignored");
}
}
static bool
flow_action_has_drop(const struct ovn_flow *f)
{
return f->ofpacts_len == 0;
}
static bool
flow_action_has_conj(const struct ovn_flow *f)
{
const struct ofpact *a = NULL;
OFPACT_FOR_EACH (a, f->ofpacts, f->ofpacts_len) {
if (a->type == OFPACT_CONJUNCTION) {
return true;
}
}
return false;
}
static bool
flow_action_has_allow(const struct ovn_flow *f)
{
return !flow_action_has_drop(f) && !flow_action_has_conj(f);
}
/* Returns true if flow 'a' is preferred over flow 'b'. */
static bool
flow_is_preferred(const struct ovn_flow *a, const struct ovn_flow *b)
{
if (flow_action_has_allow(b)) {
return false;
}
if (flow_action_has_allow(a)) {
return true;
}
if (flow_action_has_drop(b)) {
return false;
}
if (flow_action_has_drop(a)) {
return true;
}
/* Flows 'a' and 'b' should never both have action conjunction. */
OVS_NOT_REACHED();
}
/* Adds the desired flow to the list of desired flows that have same match
* conditions as the installed flow.
*
* It is caller's responsibility to make sure the link between the pair didn't
* exist before.
*
* Returns true if the newly added desired flow is selected to be the active
* one.
*/
static bool
link_installed_to_desired(struct installed_flow *i, struct desired_flow *d)
{
struct desired_flow *f;
/* Find first 'f' such that 'd' is preferred over 'f'. If no such desired
* flow exists then 'f' will point after the last element of the list.
*/
LIST_FOR_EACH (f, installed_ref_list_node, &i->desired_refs) {
if (flow_is_preferred(&d->flow, &f->flow)) {
break;
}
}
ovs_list_insert(&f->installed_ref_list_node, &d->installed_ref_list_node);
d->installed_flow = i;
return installed_flow_get_active(i) == d;
}
/* Replaces 'old_desired' with 'new_desired' in the list of desired flows
* that have same match conditions as the installed flow.
*/
static void
replace_installed_to_desired(struct installed_flow *i,
struct desired_flow *old_desired,
struct desired_flow *new_desired)
{
ovs_assert(old_desired->installed_flow == i);
ovs_list_replace(&new_desired->installed_ref_list_node,
&old_desired->installed_ref_list_node);
old_desired->installed_flow = NULL;
new_desired->installed_flow = i;
}
/* Removes the desired flow from the list of desired flows that have the same
* match conditions as the installed flow.
*
* Returns true if the desired flow was the previously active flow.
*/
static bool
unlink_installed_to_desired(struct installed_flow *i, struct desired_flow *d)
{
struct desired_flow *old_active = installed_flow_get_active(i);
ovs_assert(d && d->installed_flow == i);
ovs_list_remove(&d->installed_ref_list_node);
d->installed_flow = NULL;
return old_active == d;
}
static void
unlink_all_refs_for_installed_flow(struct installed_flow *i)
{
struct desired_flow *d, *next;
LIST_FOR_EACH_SAFE (d, next, installed_ref_list_node, &i->desired_refs) {
unlink_installed_to_desired(i, d);
}
}
static void
track_flow_add_or_modify(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (!flow_table->change_tracked) {
return;
}
/* If same node (flow adding/modifying) was tracked, remove it from
* tracking first. */
if (!ovs_list_is_empty(&f->track_list_node)) {
ovs_list_remove(&f->track_list_node);
}
f->is_deleted = false;
ovs_list_push_back(&flow_table->tracked_flows, &f->track_list_node);
}
static void
track_flow_del(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (!flow_table->change_tracked) {
return;
}
/* If same node (flow adding/modifying) was tracked, remove it from
* tracking first. */
if (!ovs_list_is_empty(&f->track_list_node)) {
ovs_list_remove(&f->track_list_node);
if (!f->installed_flow) {
/* If it is not installed yet, simply destroy it. */
desired_flow_destroy(f);
return;
}
}
f->is_deleted = true;
ovs_list_push_back(&flow_table->tracked_flows, &f->track_list_node);
}
/* When a desired flow is being removed, depending on "change_tracked", this
* function either unlinks a desired flow from installed flow and destroy it,
* or do nothing but track it. */
static void
track_or_destroy_for_flow_del(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (flow_table->change_tracked) {
track_flow_del(flow_table, f);
} else {
if (f->installed_flow) {
unlink_installed_to_desired(f->installed_flow, f);
}
desired_flow_destroy(f);
}
}
static struct sb_to_flow *
sb_to_flow_find(struct hmap *uuid_flow_table, const struct uuid *sb_uuid)
{
struct sb_to_flow *stf;
HMAP_FOR_EACH_WITH_HASH (stf, hmap_node, uuid_hash(sb_uuid),
uuid_flow_table) {
if (uuid_equals(sb_uuid, &stf->sb_uuid)) {
return stf;
}
}
return NULL;
}
static void
link_flow_to_sb(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f, const struct uuid *sb_uuid)
{
struct sb_flow_ref *sfr = xmalloc(sizeof *sfr);
sfr->flow = f;
sfr->sb_uuid = *sb_uuid;
ovs_list_insert(&f->references, &sfr->sb_list);
struct sb_to_flow *stf = sb_to_flow_find(&flow_table->uuid_flow_table,
sb_uuid);
if (!stf) {
stf = xmalloc(sizeof *stf);
stf->sb_uuid = *sb_uuid;