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