Move lib/ofp-print.h to include/openvswitch directory
[cascardo/ovs.git] / ovn / controller / ofctrl.c
1 /* Copyright (c) 2015 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "byte-order.h"
18 #include "dirs.h"
19 #include "hash.h"
20 #include "hmap.h"
21 #include "ofctrl.h"
22 #include "openflow/openflow.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/match.h"
25 #include "openvswitch/ofp-actions.h"
26 #include "openvswitch/ofp-msgs.h"
27 #include "openvswitch/ofp-print.h"
28 #include "openvswitch/ofp-util.h"
29 #include "openvswitch/ofpbuf.h"
30 #include "openvswitch/vlog.h"
31 #include "ovn-controller.h"
32 #include "physical.h"
33 #include "rconn.h"
34 #include "socket-util.h"
35 #include "util.h"
36 #include "vswitch-idl.h"
37
38 VLOG_DEFINE_THIS_MODULE(ofctrl);
39
40 /* An OpenFlow flow. */
41 struct ovn_flow {
42     /* Key. */
43     struct hmap_node hmap_node;
44     uint8_t table_id;
45     uint16_t priority;
46     struct match match;
47
48     /* Data. */
49     struct ofpact *ofpacts;
50     size_t ofpacts_len;
51 };
52
53 static uint32_t ovn_flow_hash(const struct ovn_flow *);
54 static struct ovn_flow *ovn_flow_lookup(struct hmap *flow_table,
55                                         const struct ovn_flow *target);
56 static char *ovn_flow_to_string(const struct ovn_flow *);
57 static void ovn_flow_log(const struct ovn_flow *, const char *action);
58 static void ovn_flow_destroy(struct ovn_flow *);
59
60 static ovs_be32 queue_msg(struct ofpbuf *);
61 static void queue_flow_mod(struct ofputil_flow_mod *);
62
63 /* OpenFlow connection to the switch. */
64 static struct rconn *swconn;
65
66 /* Last seen sequence number for 'swconn'.  When this differs from
67  * rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
68 static unsigned int seqno;
69
70 /* Connection state machine. */
71 #define STATES                                  \
72     STATE(S_NEW)                                \
73     STATE(S_TLV_TABLE_REQUESTED)                \
74     STATE(S_TLV_TABLE_MOD_SENT)                 \
75     STATE(S_CLEAR_FLOWS)                        \
76     STATE(S_UPDATE_FLOWS)
77 enum ofctrl_state {
78 #define STATE(NAME) NAME,
79     STATES
80 #undef STATE
81 };
82
83 /* Current state. */
84 static enum ofctrl_state state;
85
86 /* Transaction IDs for messages in flight to the switch. */
87 static ovs_be32 xid, xid2;
88
89 /* Counter for in-flight OpenFlow messages on 'swconn'.  We only send a new
90  * round of flow table modifications to the switch when the counter falls to
91  * zero, to avoid unbounded buffering. */
92 static struct rconn_packet_counter *tx_counter;
93
94 /* Flow table of "struct ovn_flow"s, that holds the flow table currently
95  * installed in the switch. */
96 static struct hmap installed_flows;
97
98 /* MFF_* field ID for our Geneve option.  In S_TLV_TABLE_MOD_SENT, this is
99  * the option we requested (we don't know whether we obtained it yet).  In
100  * S_CLEAR_FLOWS or S_UPDATE_FLOWS, this is really the option we have. */
101 static enum mf_field_id mff_ovn_geneve;
102
103 static void ovn_flow_table_clear(struct hmap *flow_table);
104 static void ovn_flow_table_destroy(struct hmap *flow_table);
105
106 static void ofctrl_recv(const struct ofp_header *, enum ofptype);
107
108 void
109 ofctrl_init(void)
110 {
111     swconn = rconn_create(5, 0, DSCP_DEFAULT, 1 << OFP13_VERSION);
112     tx_counter = rconn_packet_counter_create();
113     hmap_init(&installed_flows);
114 }
115 \f
116 /* S_NEW, for a new connection.
117  *
118  * Sends NXT_TLV_TABLE_REQUEST and transitions to
119  * S_TLV_TABLE_REQUESTED. */
120
121 static void
122 run_S_NEW(void)
123 {
124     struct ofpbuf *buf = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_REQUEST,
125                                       rconn_get_version(swconn), 0);
126     xid = queue_msg(buf);
127     state = S_TLV_TABLE_REQUESTED;
128 }
129
130 static void
131 recv_S_NEW(const struct ofp_header *oh OVS_UNUSED,
132            enum ofptype type OVS_UNUSED)
133 {
134     OVS_NOT_REACHED();
135 }
136 \f
137 /* S_TLV_TABLE_REQUESTED, when NXT_TLV_TABLE_REQUEST has been sent
138  * and we're waiting for a reply.
139  *
140  * If we receive an NXT_TLV_TABLE_REPLY:
141  *
142  *     - If it contains our tunnel metadata option, assign its field ID to
143  *       mff_ovn_geneve and transition to S_CLEAR_FLOWS.
144  *
145  *     - Otherwise, if there is an unused tunnel metadata field ID, send
146  *       NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST, and transition to
147  *       S_TLV_TABLE_MOD_SENT.
148  *
149  *     - Otherwise, log an error, disable Geneve, and transition to
150  *       S_CLEAR_FLOWS.
151  *
152  * If we receive an OFPT_ERROR:
153  *
154  *     - Log an error, disable Geneve, and transition to S_CLEAR_FLOWS. */
155
156 static void
157 run_S_TLV_TABLE_REQUESTED(void)
158 {
159 }
160
161 static void
162 recv_S_TLV_TABLE_REQUESTED(const struct ofp_header *oh, enum ofptype type)
163 {
164     if (oh->xid != xid) {
165         ofctrl_recv(oh, type);
166     } else if (type == OFPTYPE_NXT_TLV_TABLE_REPLY) {
167         struct ofputil_tlv_table_reply reply;
168         enum ofperr error = ofputil_decode_tlv_table_reply(oh, &reply);
169         if (error) {
170             VLOG_ERR("failed to decode TLV table request (%s)",
171                      ofperr_to_string(error));
172             goto error;
173         }
174
175         const struct ofputil_tlv_map *map;
176         uint64_t md_free = UINT64_MAX;
177         BUILD_ASSERT(TUN_METADATA_NUM_OPTS == 64);
178
179         LIST_FOR_EACH (map, list_node, &reply.mappings) {
180             if (map->option_class == OVN_GENEVE_CLASS
181                 && map->option_type == OVN_GENEVE_TYPE
182                 && map->option_len == OVN_GENEVE_LEN) {
183                 if (map->index >= TUN_METADATA_NUM_OPTS) {
184                     VLOG_ERR("desired Geneve tunnel option 0x%"PRIx16","
185                              "%"PRIu8",%"PRIu8" already in use with "
186                              "unsupported index %"PRIu16,
187                              map->option_class, map->option_type,
188                              map->option_len, map->index);
189                     goto error;
190                 } else {
191                     mff_ovn_geneve = MFF_TUN_METADATA0 + map->index;
192                     state = S_CLEAR_FLOWS;
193                     return;
194                 }
195             }
196
197             if (map->index < TUN_METADATA_NUM_OPTS) {
198                 md_free &= ~(UINT64_C(1) << map->index);
199             }
200         }
201
202         VLOG_DBG("OVN Geneve option not found");
203         if (!md_free) {
204             VLOG_ERR("no Geneve options free for use by OVN");
205             goto error;
206         }
207
208         unsigned int index = rightmost_1bit_idx(md_free);
209         mff_ovn_geneve = MFF_TUN_METADATA0 + index;
210         struct ofputil_tlv_map tm;
211         tm.option_class = OVN_GENEVE_CLASS;
212         tm.option_type = OVN_GENEVE_TYPE;
213         tm.option_len = OVN_GENEVE_LEN;
214         tm.index = index;
215
216         struct ofputil_tlv_table_mod ttm;
217         ttm.command = NXTTMC_ADD;
218         ovs_list_init(&ttm.mappings);
219         ovs_list_push_back(&ttm.mappings, &tm.list_node);
220
221         xid = queue_msg(ofputil_encode_tlv_table_mod(OFP13_VERSION, &ttm));
222         xid2 = queue_msg(ofputil_encode_barrier_request(OFP13_VERSION));
223         state = S_TLV_TABLE_MOD_SENT;
224     } else if (type == OFPTYPE_ERROR) {
225         VLOG_ERR("switch refused to allocate Geneve option (%s)",
226                  ofperr_to_string(ofperr_decode_msg(oh, NULL)));
227         goto error;
228     } else {
229         char *s = ofp_to_string(oh, ntohs(oh->length), 1);
230         VLOG_ERR("unexpected reply to TLV table request (%s)",
231                  s);
232         free(s);
233         goto error;
234     }
235     return;
236
237 error:
238     mff_ovn_geneve = 0;
239     state = S_CLEAR_FLOWS;
240 }
241 \f
242 /* S_TLV_TABLE_MOD_SENT, when NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST
243  * have been sent and we're waiting for a reply to one or the other.
244  *
245  * If we receive an OFPT_ERROR:
246  *
247  *     - If the error is NXTTMFC_ALREADY_MAPPED or NXTTMFC_DUP_ENTRY, we
248  *       raced with some other controller.  Transition to S_NEW.
249  *
250  *     - Otherwise, log an error, disable Geneve, and transition to
251  *       S_CLEAR_FLOWS.
252  *
253  * If we receive OFPT_BARRIER_REPLY:
254  *
255  *     - Set the tunnel metadata field ID to the one that we requested.
256  *       Transition to S_CLEAR_FLOWS.
257  */
258
259 static void
260 run_S_TLV_TABLE_MOD_SENT(void)
261 {
262 }
263
264 static void
265 recv_S_TLV_TABLE_MOD_SENT(const struct ofp_header *oh, enum ofptype type)
266 {
267     if (oh->xid != xid && oh->xid != xid2) {
268         ofctrl_recv(oh, type);
269     } else if (oh->xid == xid2 && type == OFPTYPE_BARRIER_REPLY) {
270         state = S_CLEAR_FLOWS;
271     } else if (oh->xid == xid && type == OFPTYPE_ERROR) {
272         enum ofperr error = ofperr_decode_msg(oh, NULL);
273         if (error == OFPERR_NXTTMFC_ALREADY_MAPPED ||
274             error == OFPERR_NXTTMFC_DUP_ENTRY) {
275             VLOG_INFO("raced with another controller adding "
276                       "Geneve option (%s); trying again",
277                       ofperr_to_string(error));
278             state = S_NEW;
279         } else {
280             VLOG_ERR("error adding Geneve option (%s)",
281                      ofperr_to_string(error));
282             goto error;
283         }
284     } else {
285         char *s = ofp_to_string(oh, ntohs(oh->length), 1);
286         VLOG_ERR("unexpected reply to Geneve option allocation request (%s)",
287                  s);
288         free(s);
289         goto error;
290     }
291     return;
292
293 error:
294     state = S_CLEAR_FLOWS;
295 }
296 \f
297 /* S_CLEAR_FLOWS, after we've established a Geneve metadata field ID and it's
298  * time to set up some flows.
299  *
300  * Sends an OFPT_TABLE_MOD to clear all flows, then transitions to
301  * S_UPDATE_FLOWS. */
302
303 static void
304 run_S_CLEAR_FLOWS(void)
305 {
306     /* Send a flow_mod to delete all flows. */
307     struct ofputil_flow_mod fm = {
308         .match = MATCH_CATCHALL_INITIALIZER,
309         .table_id = OFPTT_ALL,
310         .command = OFPFC_DELETE,
311     };
312     queue_flow_mod(&fm);
313     VLOG_DBG("clearing all flows");
314
315     /* Clear installed_flows, to match the state of the switch. */
316     ovn_flow_table_clear(&installed_flows);
317
318     state = S_UPDATE_FLOWS;
319 }
320
321 static void
322 recv_S_CLEAR_FLOWS(const struct ofp_header *oh, enum ofptype type)
323 {
324     ofctrl_recv(oh, type);
325 }
326 \f
327 /* S_UPDATE_FLOWS, for maintaining the flow table over time.
328  *
329  * Compare the installed flows to the ones we want.  Send OFPT_FLOW_MOD as
330  * necessary.
331  *
332  * This is a terminal state.  We only transition out of it if the connection
333  * drops. */
334
335 static void
336 run_S_UPDATE_FLOWS(void)
337 {
338     /* Nothing to do here.
339      *
340      * Being in this state enables ofctrl_put() to work, however. */
341 }
342
343 static void
344 recv_S_UPDATE_FLOWS(const struct ofp_header *oh, enum ofptype type)
345 {
346     ofctrl_recv(oh, type);
347 }
348 \f
349 /* Runs the OpenFlow state machine against 'br_int', which is local to the
350  * hypervisor on which we are running.  Attempts to negotiate a Geneve option
351  * field for class OVN_GENEVE_CLASS, type OVN_GENEVE_TYPE.  If successful,
352  * returns the MFF_* field ID for the option, otherwise returns 0. */
353 enum mf_field_id
354 ofctrl_run(const struct ovsrec_bridge *br_int)
355 {
356     if (br_int) {
357         char *target;
358         target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
359         if (strcmp(target, rconn_get_target(swconn))) {
360             VLOG_INFO("%s: connecting to switch", target);
361             rconn_connect(swconn, target, target);
362         }
363         free(target);
364     } else {
365         rconn_disconnect(swconn);
366     }
367
368     rconn_run(swconn);
369
370     if (!rconn_is_connected(swconn)) {
371         return 0;
372     }
373     if (seqno != rconn_get_connection_seqno(swconn)) {
374         seqno = rconn_get_connection_seqno(swconn);
375         state = S_NEW;
376     }
377
378     enum ofctrl_state old_state;
379     do {
380         old_state = state;
381         switch (state) {
382 #define STATE(NAME) case NAME: run_##NAME(); break;
383             STATES
384 #undef STATE
385         default:
386             OVS_NOT_REACHED();
387         }
388     } while (state != old_state);
389
390     for (int i = 0; state == old_state && i < 50; i++) {
391         struct ofpbuf *msg = rconn_recv(swconn);
392         if (!msg) {
393             break;
394         }
395
396         const struct ofp_header *oh = msg->data;
397         enum ofptype type;
398         enum ofperr error;
399
400         error = ofptype_decode(&type, oh);
401         if (!error) {
402             switch (state) {
403 #define STATE(NAME) case NAME: recv_##NAME(oh, type); break;
404                 STATES
405 #undef STATE
406             default:
407                 OVS_NOT_REACHED();
408             }
409         } else {
410             char *s = ofp_to_string(oh, ntohs(oh->length), 1);
411             VLOG_WARN("could not decode OpenFlow message (%s): %s",
412                       ofperr_to_string(error), s);
413             free(s);
414         }
415
416         ofpbuf_delete(msg);
417     }
418
419     return (state == S_CLEAR_FLOWS || state == S_UPDATE_FLOWS
420             ? mff_ovn_geneve : 0);
421 }
422
423 void
424 ofctrl_wait(void)
425 {
426     rconn_run_wait(swconn);
427     rconn_recv_wait(swconn);
428 }
429
430 void
431 ofctrl_destroy(void)
432 {
433     rconn_destroy(swconn);
434     ovn_flow_table_destroy(&installed_flows);
435     rconn_packet_counter_destroy(tx_counter);
436 }
437 \f
438 static ovs_be32
439 queue_msg(struct ofpbuf *msg)
440 {
441     const struct ofp_header *oh = msg->data;
442     ovs_be32 xid = oh->xid;
443     rconn_send(swconn, msg, tx_counter);
444     return xid;
445 }
446
447 static void
448 ofctrl_recv(const struct ofp_header *oh, enum ofptype type)
449 {
450     if (type == OFPTYPE_ECHO_REQUEST) {
451         queue_msg(make_echo_reply(oh));
452     } else if (type != OFPTYPE_ECHO_REPLY &&
453                type != OFPTYPE_BARRIER_REPLY &&
454                type != OFPTYPE_PACKET_IN &&
455                type != OFPTYPE_PORT_STATUS &&
456                type != OFPTYPE_FLOW_REMOVED) {
457         if (VLOG_IS_DBG_ENABLED()) {
458             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
459
460             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
461             VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", s);
462             free(s);
463         }
464     }
465 }
466 \f
467 /* Flow table interface to the rest of ovn-controller. */
468
469 /* Adds a flow to 'desired_flows' with the specified 'match' and 'actions' to
470  * the OpenFlow table numbered 'table_id' with the given 'priority'.  The
471  * caller retains ownership of 'match' and 'actions'.
472  *
473  * This just assembles the desired flow table in memory.  Nothing is actually
474  * sent to the switch until a later call to ofctrl_run().
475  *
476  * The caller should initialize its own hmap to hold the flows. */
477 void
478 ofctrl_add_flow(struct hmap *desired_flows,
479                 uint8_t table_id, uint16_t priority,
480                 const struct match *match, const struct ofpbuf *actions)
481 {
482     struct ovn_flow *f = xmalloc(sizeof *f);
483     f->table_id = table_id;
484     f->priority = priority;
485     f->match = *match;
486     f->ofpacts = xmemdup(actions->data, actions->size);
487     f->ofpacts_len = actions->size;
488     f->hmap_node.hash = ovn_flow_hash(f);
489
490     if (ovn_flow_lookup(desired_flows, f)) {
491         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
492         if (!VLOG_DROP_INFO(&rl)) {
493             char *s = ovn_flow_to_string(f);
494             VLOG_INFO("dropping duplicate flow: %s", s);
495             free(s);
496         }
497
498         ovn_flow_destroy(f);
499         return;
500     }
501
502     hmap_insert(desired_flows, &f->hmap_node, f->hmap_node.hash);
503 }
504 \f
505 /* ovn_flow. */
506
507 /* Returns a hash of the key in 'f'. */
508 static uint32_t
509 ovn_flow_hash(const struct ovn_flow *f)
510 {
511     return hash_2words((f->table_id << 16) | f->priority,
512                        match_hash(&f->match, 0));
513
514 }
515
516 /* Finds and returns an ovn_flow in 'flow_table' whose key is identical to
517  * 'target''s key, or NULL if there is none. */
518 static struct ovn_flow *
519 ovn_flow_lookup(struct hmap *flow_table, const struct ovn_flow *target)
520 {
521     struct ovn_flow *f;
522
523     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, target->hmap_node.hash,
524                              flow_table) {
525         if (f->table_id == target->table_id
526             && f->priority == target->priority
527             && match_equal(&f->match, &target->match)) {
528             return f;
529         }
530     }
531     return NULL;
532 }
533
534 static char *
535 ovn_flow_to_string(const struct ovn_flow *f)
536 {
537     struct ds s = DS_EMPTY_INITIALIZER;
538     ds_put_format(&s, "table_id=%"PRIu8", ", f->table_id);
539     ds_put_format(&s, "priority=%"PRIu16", ", f->priority);
540     match_format(&f->match, &s, OFP_DEFAULT_PRIORITY);
541     ds_put_cstr(&s, ", actions=");
542     ofpacts_format(f->ofpacts, f->ofpacts_len, &s);
543     return ds_steal_cstr(&s);
544 }
545
546 static void
547 ovn_flow_log(const struct ovn_flow *f, const char *action)
548 {
549     if (VLOG_IS_DBG_ENABLED()) {
550         char *s = ovn_flow_to_string(f);
551         VLOG_DBG("%s flow: %s", action, s);
552         free(s);
553     }
554 }
555
556 static void
557 ovn_flow_destroy(struct ovn_flow *f)
558 {
559     if (f) {
560         free(f->ofpacts);
561         free(f);
562     }
563 }
564 \f
565 /* Flow tables of struct ovn_flow. */
566
567 static void
568 ovn_flow_table_clear(struct hmap *flow_table)
569 {
570     struct ovn_flow *f, *next;
571     HMAP_FOR_EACH_SAFE (f, next, hmap_node, flow_table) {
572         hmap_remove(flow_table, &f->hmap_node);
573         ovn_flow_destroy(f);
574     }
575 }
576
577 static void
578 ovn_flow_table_destroy(struct hmap *flow_table)
579 {
580     ovn_flow_table_clear(flow_table);
581     hmap_destroy(flow_table);
582 }
583 \f
584 /* Flow table update. */
585
586 static void
587 queue_flow_mod(struct ofputil_flow_mod *fm)
588 {
589     fm->buffer_id = UINT32_MAX;
590     fm->out_port = OFPP_ANY;
591     fm->out_group = OFPG_ANY;
592     queue_msg(ofputil_encode_flow_mod(fm, OFPUTIL_P_OF13_OXM));
593 }
594
595 /* Replaces the flow table on the switch, if possible, by the flows in
596  * 'flow_table', which should have been added with ofctrl_add_flow().
597  * Regardless of whether the flow table is updated, this deletes all of the
598  * flows from 'flow_table' and frees them.  (The hmap itself isn't
599  * destroyed.)
600  *
601  * This called be called be ofctrl_run() within the main loop. */
602 void
603 ofctrl_put(struct hmap *flow_table)
604 {
605     /* The flow table can be updated if the connection to the switch is up and
606      * in the correct state and not backlogged with existing flow_mods.  (Our
607      * criteria for being backlogged appear very conservative, but the socket
608      * between ovn-controller and OVS provides some buffering.)  Otherwise,
609      * discard the flows.  A solution to either of those problems will cause us
610      * to wake up and retry. */
611     if (state != S_UPDATE_FLOWS
612         || rconn_packet_counter_n_packets(tx_counter)) {
613         ovn_flow_table_clear(flow_table);
614         return;
615     }
616
617     /* Iterate through all of the installed flows.  If any of them are no
618      * longer desired, delete them; if any of them should have different
619      * actions, update them. */
620     struct ovn_flow *i, *next;
621     HMAP_FOR_EACH_SAFE (i, next, hmap_node, &installed_flows) {
622         struct ovn_flow *d = ovn_flow_lookup(flow_table, i);
623         if (!d) {
624             /* Installed flow is no longer desirable.  Delete it from the
625              * switch and from installed_flows. */
626             struct ofputil_flow_mod fm = {
627                 .match = i->match,
628                 .priority = i->priority,
629                 .table_id = i->table_id,
630                 .command = OFPFC_DELETE_STRICT,
631             };
632             queue_flow_mod(&fm);
633             ovn_flow_log(i, "removing");
634
635             hmap_remove(&installed_flows, &i->hmap_node);
636             ovn_flow_destroy(i);
637         } else {
638             if (!ofpacts_equal(i->ofpacts, i->ofpacts_len,
639                                d->ofpacts, d->ofpacts_len)) {
640                 /* Update actions in installed flow. */
641                 struct ofputil_flow_mod fm = {
642                     .match = i->match,
643                     .priority = i->priority,
644                     .table_id = i->table_id,
645                     .ofpacts = d->ofpacts,
646                     .ofpacts_len = d->ofpacts_len,
647                     .command = OFPFC_MODIFY_STRICT,
648                 };
649                 queue_flow_mod(&fm);
650                 ovn_flow_log(i, "updating");
651
652                 /* Replace 'i''s actions by 'd''s. */
653                 free(i->ofpacts);
654                 i->ofpacts = d->ofpacts;
655                 i->ofpacts_len = d->ofpacts_len;
656                 d->ofpacts = NULL;
657                 d->ofpacts_len = 0;
658             }
659
660             hmap_remove(flow_table, &d->hmap_node);
661             ovn_flow_destroy(d);
662         }
663     }
664
665     /* The previous loop removed from 'flow_table' all of the flows that are
666      * already installed.  Thus, any flows remaining in 'flow_table' need to
667      * be added to the flow table. */
668     struct ovn_flow *d;
669     HMAP_FOR_EACH_SAFE (d, next, hmap_node, flow_table) {
670         /* Send flow_mod to add flow. */
671         struct ofputil_flow_mod fm = {
672             .match = d->match,
673             .priority = d->priority,
674             .table_id = d->table_id,
675             .ofpacts = d->ofpacts,
676             .ofpacts_len = d->ofpacts_len,
677             .command = OFPFC_ADD,
678         };
679         queue_flow_mod(&fm);
680         ovn_flow_log(d, "adding");
681
682         /* Move 'd' from 'flow_table' to installed_flows. */
683         hmap_remove(flow_table, &d->hmap_node);
684         hmap_insert(&installed_flows, &d->hmap_node, d->hmap_node.hash);
685     }
686 }