b451453c3ed4eac909a0d6f11ce5b38eff638cc8
[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 log_openflow_rl(struct vlog_rate_limit *rl, enum vlog_level level,
474                 const struct ofp_header *oh, const char *title)
475 {
476     if (!vlog_should_drop(&this_module, level, rl)) {
477         char *s = ofp_to_string(oh, ntohs(oh->length), 2);
478         vlog(&this_module, level, "%s: %s", title, s);
479         free(s);
480     }
481 }
482
483 static void
484 ofctrl_recv(const struct ofp_header *oh, enum ofptype type)
485 {
486     if (type == OFPTYPE_ECHO_REQUEST) {
487         queue_msg(make_echo_reply(oh));
488     } else if (type == OFPTYPE_ERROR) {
489         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
490         log_openflow_rl(&rl, VLL_INFO, oh, "OpenFlow error");
491     } else if (type != OFPTYPE_ECHO_REPLY &&
492                type != OFPTYPE_BARRIER_REPLY &&
493                type != OFPTYPE_PACKET_IN &&
494                type != OFPTYPE_PORT_STATUS &&
495                type != OFPTYPE_FLOW_REMOVED) {
496         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
497         log_openflow_rl(&rl, VLL_DBG, oh, "OpenFlow packet ignored");
498     }
499 }
500 \f
501 /* Flow table interface to the rest of ovn-controller. */
502
503 /* Adds a flow to 'desired_flows' with the specified 'match' and 'actions' to
504  * the OpenFlow table numbered 'table_id' with the given 'priority'.  The
505  * caller retains ownership of 'match' and 'actions'.
506  *
507  * This just assembles the desired flow table in memory.  Nothing is actually
508  * sent to the switch until a later call to ofctrl_run().
509  *
510  * The caller should initialize its own hmap to hold the flows. */
511 void
512 ofctrl_add_flow(struct hmap *desired_flows,
513                 uint8_t table_id, uint16_t priority,
514                 const struct match *match, const struct ofpbuf *actions)
515 {
516     struct ovn_flow *f = xmalloc(sizeof *f);
517     f->table_id = table_id;
518     f->priority = priority;
519     f->match = *match;
520     f->ofpacts = xmemdup(actions->data, actions->size);
521     f->ofpacts_len = actions->size;
522     f->hmap_node.hash = ovn_flow_hash(f);
523
524     if (ovn_flow_lookup(desired_flows, f)) {
525         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
526         if (!VLOG_DROP_INFO(&rl)) {
527             char *s = ovn_flow_to_string(f);
528             VLOG_INFO("dropping duplicate flow: %s", s);
529             free(s);
530         }
531
532         ovn_flow_destroy(f);
533         return;
534     }
535
536     hmap_insert(desired_flows, &f->hmap_node, f->hmap_node.hash);
537 }
538 \f
539 /* ovn_flow. */
540
541 /* Returns a hash of the key in 'f'. */
542 static uint32_t
543 ovn_flow_hash(const struct ovn_flow *f)
544 {
545     return hash_2words((f->table_id << 16) | f->priority,
546                        match_hash(&f->match, 0));
547
548 }
549
550 /* Finds and returns an ovn_flow in 'flow_table' whose key is identical to
551  * 'target''s key, or NULL if there is none. */
552 static struct ovn_flow *
553 ovn_flow_lookup(struct hmap *flow_table, const struct ovn_flow *target)
554 {
555     struct ovn_flow *f;
556
557     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, target->hmap_node.hash,
558                              flow_table) {
559         if (f->table_id == target->table_id
560             && f->priority == target->priority
561             && match_equal(&f->match, &target->match)) {
562             return f;
563         }
564     }
565     return NULL;
566 }
567
568 static char *
569 ovn_flow_to_string(const struct ovn_flow *f)
570 {
571     struct ds s = DS_EMPTY_INITIALIZER;
572     ds_put_format(&s, "table_id=%"PRIu8", ", f->table_id);
573     ds_put_format(&s, "priority=%"PRIu16", ", f->priority);
574     match_format(&f->match, &s, OFP_DEFAULT_PRIORITY);
575     ds_put_cstr(&s, ", actions=");
576     ofpacts_format(f->ofpacts, f->ofpacts_len, &s);
577     return ds_steal_cstr(&s);
578 }
579
580 static void
581 ovn_flow_log(const struct ovn_flow *f, const char *action)
582 {
583     if (VLOG_IS_DBG_ENABLED()) {
584         char *s = ovn_flow_to_string(f);
585         VLOG_DBG("%s flow: %s", action, s);
586         free(s);
587     }
588 }
589
590 static void
591 ovn_flow_destroy(struct ovn_flow *f)
592 {
593     if (f) {
594         free(f->ofpacts);
595         free(f);
596     }
597 }
598 \f
599 /* Flow tables of struct ovn_flow. */
600
601 static void
602 ovn_flow_table_clear(struct hmap *flow_table)
603 {
604     struct ovn_flow *f;
605     HMAP_FOR_EACH_POP (f, hmap_node, flow_table) {
606         ovn_flow_destroy(f);
607     }
608 }
609
610 static void
611 ovn_flow_table_destroy(struct hmap *flow_table)
612 {
613     ovn_flow_table_clear(flow_table);
614     hmap_destroy(flow_table);
615 }
616 \f
617 /* Flow table update. */
618
619 static void
620 queue_flow_mod(struct ofputil_flow_mod *fm)
621 {
622     fm->buffer_id = UINT32_MAX;
623     fm->out_port = OFPP_ANY;
624     fm->out_group = OFPG_ANY;
625     queue_msg(ofputil_encode_flow_mod(fm, OFPUTIL_P_OF13_OXM));
626 }
627
628 \f
629 /* group_table. */
630
631 /* Finds and returns a group_info in 'existing_groups' whose key is identical
632  * to 'target''s key, or NULL if there is none. */
633 static struct group_info *
634 ovn_group_lookup(struct hmap *exisiting_groups,
635                  const struct group_info *target)
636 {
637     struct group_info *e;
638
639     HMAP_FOR_EACH_WITH_HASH(e, hmap_node, target->hmap_node.hash,
640                             exisiting_groups) {
641         if (e->group_id == target->group_id) {
642             return e;
643         }
644    }
645     return NULL;
646 }
647
648 /* Clear either desired_groups or existing_groups in group_table. */
649 static void
650 ovn_group_table_clear(struct group_table *group_table, bool existing)
651 {
652     struct group_info *g, *next;
653     struct hmap *target_group = existing
654                                 ? &group_table->existing_groups
655                                 : &group_table->desired_groups;
656
657     HMAP_FOR_EACH_SAFE (g, next, hmap_node, target_group) {
658         hmap_remove(target_group, &g->hmap_node);
659         bitmap_set0(group_table->group_ids, g->group_id);
660         ds_destroy(&g->group);
661         free(g);
662     }
663 }
664
665 static void
666 queue_group_mod(struct ofputil_group_mod *gm)
667 {
668     queue_msg(ofputil_encode_group_mod(OFP13_VERSION, gm));
669 }
670 \f
671
672 /* Replaces the flow table on the switch, if possible, by the flows in
673  * 'flow_table', which should have been added with ofctrl_add_flow().
674  * Regardless of whether the flow table is updated, this deletes all of the
675  * flows from 'flow_table' and frees them.  (The hmap itself isn't
676  * destroyed.)
677  *
678  * Replaces the group table on the switch, if possible, by the groups in
679  * 'group_table->desired_groups'. Regardless of whether the group table
680  * is updated, this deletes all the groups from the
681  * 'group_table->desired_groups' and frees them. (The hmap itself isn't
682  * destroyed.)
683  *
684  * This should be called after ofctrl_run() within the main loop. */
685 void
686 ofctrl_put(struct hmap *flow_table, struct group_table *group_table)
687 {
688     if (!groups) {
689         groups = group_table;
690     }
691
692     /* The flow table can be updated if the connection to the switch is up and
693      * in the correct state and not backlogged with existing flow_mods.  (Our
694      * criteria for being backlogged appear very conservative, but the socket
695      * between ovn-controller and OVS provides some buffering.)  Otherwise,
696      * discard the flows.  A solution to either of those problems will cause us
697      * to wake up and retry. */
698     if (state != S_UPDATE_FLOWS
699         || rconn_packet_counter_n_packets(tx_counter)) {
700         ovn_flow_table_clear(flow_table);
701         ovn_group_table_clear(group_table, false);
702         return;
703     }
704
705     /* Iterate through all the desired groups. If there are new ones,
706      * add them to the switch. */
707     struct group_info *desired;
708     HMAP_FOR_EACH(desired, hmap_node, &group_table->desired_groups) {
709         if (!ovn_group_lookup(&group_table->existing_groups, desired)) {
710             /* Create and install new group. */
711             struct ofputil_group_mod gm;
712             enum ofputil_protocol usable_protocols;
713             char *error;
714             struct ds group_string = DS_EMPTY_INITIALIZER;
715             ds_put_format(&group_string, "group_id=%u,%s",
716                           desired->group_id, ds_cstr(&desired->group));
717
718             error = parse_ofp_group_mod_str(&gm, OFPGC11_ADD,
719                                             ds_cstr(&group_string),
720                                             &usable_protocols);
721             if (!error) {
722                 queue_group_mod(&gm);
723             } else {
724                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
725                 VLOG_ERR_RL(&rl, "new group %s %s", error,
726                          ds_cstr(&group_string));
727                 free(error);
728             }
729             ds_destroy(&group_string);
730             ofputil_bucket_list_destroy(&gm.buckets);
731         }
732     }
733
734     /* Iterate through all of the installed flows.  If any of them are no
735      * longer desired, delete them; if any of them should have different
736      * actions, update them. */
737     struct ovn_flow *i, *next;
738     HMAP_FOR_EACH_SAFE (i, next, hmap_node, &installed_flows) {
739         struct ovn_flow *d = ovn_flow_lookup(flow_table, i);
740         if (!d) {
741             /* Installed flow is no longer desirable.  Delete it from the
742              * switch and from installed_flows. */
743             struct ofputil_flow_mod fm = {
744                 .match = i->match,
745                 .priority = i->priority,
746                 .table_id = i->table_id,
747                 .command = OFPFC_DELETE_STRICT,
748             };
749             queue_flow_mod(&fm);
750             ovn_flow_log(i, "removing");
751
752             hmap_remove(&installed_flows, &i->hmap_node);
753             ovn_flow_destroy(i);
754         } else {
755             if (!ofpacts_equal(i->ofpacts, i->ofpacts_len,
756                                d->ofpacts, d->ofpacts_len)) {
757                 /* Update actions in installed flow. */
758                 struct ofputil_flow_mod fm = {
759                     .match = i->match,
760                     .priority = i->priority,
761                     .table_id = i->table_id,
762                     .ofpacts = d->ofpacts,
763                     .ofpacts_len = d->ofpacts_len,
764                     .command = OFPFC_MODIFY_STRICT,
765                 };
766                 queue_flow_mod(&fm);
767                 ovn_flow_log(i, "updating");
768
769                 /* Replace 'i''s actions by 'd''s. */
770                 free(i->ofpacts);
771                 i->ofpacts = d->ofpacts;
772                 i->ofpacts_len = d->ofpacts_len;
773                 d->ofpacts = NULL;
774                 d->ofpacts_len = 0;
775             }
776
777             hmap_remove(flow_table, &d->hmap_node);
778             ovn_flow_destroy(d);
779         }
780     }
781
782     /* The previous loop removed from 'flow_table' all of the flows that are
783      * already installed.  Thus, any flows remaining in 'flow_table' need to
784      * be added to the flow table. */
785     struct ovn_flow *d;
786     HMAP_FOR_EACH_SAFE (d, next, hmap_node, flow_table) {
787         /* Send flow_mod to add flow. */
788         struct ofputil_flow_mod fm = {
789             .match = d->match,
790             .priority = d->priority,
791             .table_id = d->table_id,
792             .ofpacts = d->ofpacts,
793             .ofpacts_len = d->ofpacts_len,
794             .command = OFPFC_ADD,
795         };
796         queue_flow_mod(&fm);
797         ovn_flow_log(d, "adding");
798
799         /* Move 'd' from 'flow_table' to installed_flows. */
800         hmap_remove(flow_table, &d->hmap_node);
801         hmap_insert(&installed_flows, &d->hmap_node, d->hmap_node.hash);
802     }
803
804     /* Iterate through the installed groups from previous runs. If they
805      * are not needed delete them. */
806     struct group_info *installed, *next_group;
807     HMAP_FOR_EACH_SAFE(installed, next_group, hmap_node,
808                        &group_table->existing_groups) {
809         if (!ovn_group_lookup(&group_table->desired_groups, installed)) {
810             /* Delete the group. */
811             struct ofputil_group_mod gm;
812             enum ofputil_protocol usable_protocols;
813             char *error;
814             struct ds group_string = DS_EMPTY_INITIALIZER;
815             ds_put_format(&group_string, "group_id=%u", installed->group_id);
816
817             error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
818                                             ds_cstr(&group_string),
819                                             &usable_protocols);
820             if (!error) {
821                 queue_group_mod(&gm);
822             } else {
823                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
824                 VLOG_ERR_RL(&rl, "Error deleting group %d: %s",
825                          installed->group_id, error);
826                 free(error);
827             }
828             ds_destroy(&group_string);
829             ofputil_bucket_list_destroy(&gm.buckets);
830
831             /* Remove 'installed' from 'group_table->existing_groups' */
832             hmap_remove(&group_table->existing_groups, &installed->hmap_node);
833             ds_destroy(&installed->group);
834
835             /* Dealloc group_id. */
836             bitmap_set0(group_table->group_ids, installed->group_id);
837             free(installed);
838         }
839     }
840
841     /* Move the contents of desired_groups to existing_groups. */
842     HMAP_FOR_EACH_SAFE(desired, next_group, hmap_node,
843                        &group_table->desired_groups) {
844         hmap_remove(&group_table->desired_groups, &desired->hmap_node);
845         if (!ovn_group_lookup(&group_table->existing_groups, desired)) {
846             hmap_insert(&group_table->existing_groups, &desired->hmap_node,
847                         desired->hmap_node.hash);
848         } else {
849             ds_destroy(&desired->group);
850             free(desired);
851         }
852     }
853 }