smap: New macro SMAP_CONST1 for initializing immutable 1-member smaps.
[cascardo/ovs.git] / ovn / northd / ovn-northd.c
1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at:
5  *
6  *     http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14
15 #include <config.h>
16
17 #include <getopt.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include "command-line.h"
22 #include "daemon.h"
23 #include "dirs.h"
24 #include "dynamic-string.h"
25 #include "fatal-signal.h"
26 #include "hash.h"
27 #include "hmap.h"
28 #include "json.h"
29 #include "ovn/lib/lex.h"
30 #include "ovn/lib/ovn-nb-idl.h"
31 #include "ovn/lib/ovn-sb-idl.h"
32 #include "poll-loop.h"
33 #include "smap.h"
34 #include "stream.h"
35 #include "stream-ssl.h"
36 #include "unixctl.h"
37 #include "util.h"
38 #include "uuid.h"
39 #include "openvswitch/vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(ovn_northd);
42
43 static unixctl_cb_func ovn_northd_exit;
44
45 struct northd_context {
46     struct ovsdb_idl *ovnnb_idl;
47     struct ovsdb_idl *ovnsb_idl;
48     struct ovsdb_idl_txn *ovnnb_txn;
49     struct ovsdb_idl_txn *ovnsb_txn;
50 };
51
52 static const char *ovnnb_db;
53 static const char *ovnsb_db;
54
55 static const char *default_db(void);
56
57
58 /* Ingress pipeline stages.
59  *
60  * These must be listed in the order that the stages will be executed. */
61 #define INGRESS_STAGES                         \
62     INGRESS_STAGE(PORT_SEC, port_sec)          \
63     INGRESS_STAGE(L2_LKUP, l2_lkup)
64
65 enum ingress_stage {
66 #define INGRESS_STAGE(NAME, STR) S_IN_##NAME,
67     INGRESS_STAGES
68 #undef INGRESS_STAGE
69     INGRESS_N_STAGES
70 };
71
72 /* Egress pipeline stages.
73  *
74  * These must be listed in the order that the stages will be executed. */
75 #define EGRESS_STAGES                         \
76     EGRESS_STAGE(ACL, acl)                    \
77     EGRESS_STAGE(PORT_SEC, port_sec)
78
79 enum egress_stage {
80 #define EGRESS_STAGE(NAME, STR) S_OUT_##NAME,
81     EGRESS_STAGES
82 #undef EGRESS_STAGE
83     EGRESS_N_STAGES
84 };
85
86 static void
87 usage(void)
88 {
89     printf("\
90 %s: OVN northbound management daemon\n\
91 usage: %s [OPTIONS]\n\
92 \n\
93 Options:\n\
94   --ovnnb-db=DATABASE       connect to ovn-nb database at DATABASE\n\
95                             (default: %s)\n\
96   --ovnsb-db=DATABASE       connect to ovn-sb database at DATABASE\n\
97                             (default: %s)\n\
98   -h, --help                display this help message\n\
99   -o, --options             list available options\n\
100   -V, --version             display version information\n\
101 ", program_name, program_name, default_db(), default_db());
102     daemon_usage();
103     vlog_usage();
104     stream_usage("database", true, true, false);
105 }
106 \f
107 struct tnlid_node {
108     struct hmap_node hmap_node;
109     uint32_t tnlid;
110 };
111
112 static void
113 destroy_tnlids(struct hmap *tnlids)
114 {
115     struct tnlid_node *node, *next;
116     HMAP_FOR_EACH_SAFE (node, next, hmap_node, tnlids) {
117         hmap_remove(tnlids, &node->hmap_node);
118         free(node);
119     }
120     hmap_destroy(tnlids);
121 }
122
123 static void
124 add_tnlid(struct hmap *set, uint32_t tnlid)
125 {
126     struct tnlid_node *node = xmalloc(sizeof *node);
127     hmap_insert(set, &node->hmap_node, hash_int(tnlid, 0));
128     node->tnlid = tnlid;
129 }
130
131 static bool
132 tnlid_in_use(const struct hmap *set, uint32_t tnlid)
133 {
134     const struct tnlid_node *node;
135     HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_int(tnlid, 0), set) {
136         if (node->tnlid == tnlid) {
137             return true;
138         }
139     }
140     return false;
141 }
142
143 static uint32_t
144 allocate_tnlid(struct hmap *set, const char *name, uint32_t max,
145                uint32_t *hint)
146 {
147     for (uint32_t tnlid = *hint + 1; tnlid != *hint;
148          tnlid = tnlid + 1 <= max ? tnlid + 1 : 1) {
149         if (!tnlid_in_use(set, tnlid)) {
150             add_tnlid(set, tnlid);
151             *hint = tnlid;
152             return tnlid;
153         }
154     }
155
156     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
157     VLOG_WARN_RL(&rl, "all %s tunnel ids exhausted", name);
158     return 0;
159 }
160 \f
161 /* The 'key' comes from nb->header_.uuid or sb->external_ids:logical-switch. */
162 struct ovn_datapath {
163     struct hmap_node key_node;  /* Index on 'key'. */
164     struct uuid key;            /* nb->header_.uuid. */
165
166     const struct nbrec_logical_switch *nb;   /* May be NULL. */
167     const struct sbrec_datapath_binding *sb; /* May be NULL. */
168
169     struct ovs_list list;       /* In list of similar records. */
170
171     struct hmap port_tnlids;
172     uint32_t port_key_hint;
173
174     bool has_unknown;
175 };
176
177 static struct ovn_datapath *
178 ovn_datapath_create(struct hmap *datapaths, const struct uuid *key,
179                     const struct nbrec_logical_switch *nb,
180                     const struct sbrec_datapath_binding *sb)
181 {
182     struct ovn_datapath *od = xzalloc(sizeof *od);
183     od->key = *key;
184     od->sb = sb;
185     od->nb = nb;
186     hmap_init(&od->port_tnlids);
187     od->port_key_hint = 0;
188     hmap_insert(datapaths, &od->key_node, uuid_hash(&od->key));
189     return od;
190 }
191
192 static void
193 ovn_datapath_destroy(struct hmap *datapaths, struct ovn_datapath *od)
194 {
195     if (od) {
196         /* Don't remove od->list.  It is used within build_datapaths() as a
197          * private list and once we've exited that function it is not safe to
198          * use it. */
199         hmap_remove(datapaths, &od->key_node);
200         destroy_tnlids(&od->port_tnlids);
201         free(od);
202     }
203 }
204
205 static struct ovn_datapath *
206 ovn_datapath_find(struct hmap *datapaths, const struct uuid *uuid)
207 {
208     struct ovn_datapath *od;
209
210     HMAP_FOR_EACH_WITH_HASH (od, key_node, uuid_hash(uuid), datapaths) {
211         if (uuid_equals(uuid, &od->key)) {
212             return od;
213         }
214     }
215     return NULL;
216 }
217
218 static struct ovn_datapath *
219 ovn_datapath_from_sbrec(struct hmap *datapaths,
220                         const struct sbrec_datapath_binding *sb)
221 {
222     struct uuid key;
223
224     if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key)) {
225         return NULL;
226     }
227     return ovn_datapath_find(datapaths, &key);
228 }
229
230 static void
231 join_datapaths(struct northd_context *ctx, struct hmap *datapaths,
232                struct ovs_list *sb_only, struct ovs_list *nb_only,
233                struct ovs_list *both)
234 {
235     hmap_init(datapaths);
236     list_init(sb_only);
237     list_init(nb_only);
238     list_init(both);
239
240     const struct sbrec_datapath_binding *sb, *sb_next;
241     SBREC_DATAPATH_BINDING_FOR_EACH_SAFE (sb, sb_next, ctx->ovnsb_idl) {
242         struct uuid key;
243         if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key)) {
244             ovsdb_idl_txn_add_comment(ctx->ovnsb_txn,
245                                       "deleting Datapath_Binding "UUID_FMT" that "
246                                       "lacks external-ids:logical-switch",
247                          UUID_ARGS(&sb->header_.uuid));
248             sbrec_datapath_binding_delete(sb);
249             continue;
250         }
251
252         if (ovn_datapath_find(datapaths, &key)) {
253             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
254             VLOG_INFO_RL(&rl, "deleting Datapath_Binding "UUID_FMT" with "
255                          "duplicate external-ids:logical-switch "UUID_FMT,
256                          UUID_ARGS(&sb->header_.uuid), UUID_ARGS(&key));
257             sbrec_datapath_binding_delete(sb);
258             continue;
259         }
260
261         struct ovn_datapath *od = ovn_datapath_create(datapaths, &key,
262                                                       NULL, sb);
263         list_push_back(sb_only, &od->list);
264     }
265
266     const struct nbrec_logical_switch *nb;
267     NBREC_LOGICAL_SWITCH_FOR_EACH (nb, ctx->ovnnb_idl) {
268         struct ovn_datapath *od = ovn_datapath_find(datapaths,
269                                                     &nb->header_.uuid);
270         if (od) {
271             od->nb = nb;
272             list_remove(&od->list);
273             list_push_back(both, &od->list);
274         } else {
275             od = ovn_datapath_create(datapaths, &nb->header_.uuid, nb, NULL);
276             list_push_back(nb_only, &od->list);
277         }
278     }
279 }
280
281 static uint32_t
282 ovn_datapath_allocate_key(struct hmap *dp_tnlids)
283 {
284     static uint32_t hint;
285     return allocate_tnlid(dp_tnlids, "datapath", (1u << 24) - 1, &hint);
286 }
287
288 static void
289 build_datapaths(struct northd_context *ctx, struct hmap *datapaths)
290 {
291     struct ovs_list sb_only, nb_only, both;
292
293     join_datapaths(ctx, datapaths, &sb_only, &nb_only, &both);
294
295     if (!list_is_empty(&nb_only)) {
296         /* First index the in-use datapath tunnel IDs. */
297         struct hmap dp_tnlids = HMAP_INITIALIZER(&dp_tnlids);
298         struct ovn_datapath *od;
299         LIST_FOR_EACH (od, list, &both) {
300             add_tnlid(&dp_tnlids, od->sb->tunnel_key);
301         }
302
303         /* Add southbound record for each unmatched northbound record. */
304         LIST_FOR_EACH (od, list, &nb_only) {
305             uint16_t tunnel_key = ovn_datapath_allocate_key(&dp_tnlids);
306             if (!tunnel_key) {
307                 break;
308             }
309
310             od->sb = sbrec_datapath_binding_insert(ctx->ovnsb_txn);
311
312             char uuid_s[UUID_LEN + 1];
313             sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->nb->header_.uuid));
314             const struct smap id = SMAP_CONST1(&id, "logical-switch", uuid_s);
315             sbrec_datapath_binding_set_external_ids(od->sb, &id);
316
317             sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key);
318         }
319         destroy_tnlids(&dp_tnlids);
320     }
321
322     /* Delete southbound records without northbound matches. */
323     struct ovn_datapath *od, *next;
324     LIST_FOR_EACH_SAFE (od, next, list, &sb_only) {
325         list_remove(&od->list);
326         sbrec_datapath_binding_delete(od->sb);
327         ovn_datapath_destroy(datapaths, od);
328     }
329 }
330 \f
331 struct ovn_port {
332     struct hmap_node key_node;  /* Index on 'key'. */
333     const char *key;            /* nb->name and sb->logical_port */
334
335     const struct nbrec_logical_port *nb; /* May be NULL. */
336     const struct sbrec_port_binding *sb; /* May be NULL. */
337
338     struct ovn_datapath *od;
339
340     struct ovs_list list;       /* In list of similar records. */
341 };
342
343 static struct ovn_port *
344 ovn_port_create(struct hmap *ports, const char *key,
345                 const struct nbrec_logical_port *nb,
346                 const struct sbrec_port_binding *sb)
347 {
348     struct ovn_port *op = xzalloc(sizeof *op);
349     op->key = key;
350     op->sb = sb;
351     op->nb = nb;
352     hmap_insert(ports, &op->key_node, hash_string(op->key, 0));
353     return op;
354 }
355
356 static void
357 ovn_port_destroy(struct hmap *ports, struct ovn_port *port)
358 {
359     if (port) {
360         /* Don't remove port->list.  It is used within build_ports() as a
361          * private list and once we've exited that function it is not safe to
362          * use it. */
363         hmap_remove(ports, &port->key_node);
364         free(port);
365     }
366 }
367
368 static struct ovn_port *
369 ovn_port_find(struct hmap *ports, const char *name)
370 {
371     struct ovn_port *op;
372
373     HMAP_FOR_EACH_WITH_HASH (op, key_node, hash_string(name, 0), ports) {
374         if (!strcmp(op->key, name)) {
375             return op;
376         }
377     }
378     return NULL;
379 }
380
381 static uint32_t
382 ovn_port_allocate_key(struct ovn_datapath *od)
383 {
384     return allocate_tnlid(&od->port_tnlids, "port",
385                           (1u << 15) - 1, &od->port_key_hint);
386 }
387
388 static void
389 join_logical_ports(struct northd_context *ctx,
390                    struct hmap *datapaths, struct hmap *ports,
391                    struct ovs_list *sb_only, struct ovs_list *nb_only,
392                    struct ovs_list *both)
393 {
394     hmap_init(ports);
395     list_init(sb_only);
396     list_init(nb_only);
397     list_init(both);
398
399     const struct sbrec_port_binding *sb;
400     SBREC_PORT_BINDING_FOR_EACH (sb, ctx->ovnsb_idl) {
401         struct ovn_port *op = ovn_port_create(ports, sb->logical_port,
402                                               NULL, sb);
403         list_push_back(sb_only, &op->list);
404     }
405
406     struct ovn_datapath *od;
407     HMAP_FOR_EACH (od, key_node, datapaths) {
408         for (size_t i = 0; i < od->nb->n_ports; i++) {
409             const struct nbrec_logical_port *nb = od->nb->ports[i];
410             struct ovn_port *op = ovn_port_find(ports, nb->name);
411             if (op) {
412                 op->nb = nb;
413                 list_remove(&op->list);
414                 list_push_back(both, &op->list);
415             } else {
416                 op = ovn_port_create(ports, nb->name, nb, NULL);
417                 list_push_back(nb_only, &op->list);
418             }
419             op->od = od;
420         }
421     }
422 }
423
424 static void
425 ovn_port_update_sbrec(const struct ovn_port *op)
426 {
427     sbrec_port_binding_set_type(op->sb, op->nb->type);
428     sbrec_port_binding_set_options(op->sb, &op->nb->options);
429     sbrec_port_binding_set_datapath(op->sb, op->od->sb);
430     sbrec_port_binding_set_parent_port(op->sb, op->nb->parent_name);
431     sbrec_port_binding_set_tag(op->sb, op->nb->tag, op->nb->n_tag);
432     sbrec_port_binding_set_mac(op->sb, (const char **) op->nb->macs,
433                                op->nb->n_macs);
434 }
435
436 static void
437 build_ports(struct northd_context *ctx, struct hmap *datapaths,
438             struct hmap *ports)
439 {
440     struct ovs_list sb_only, nb_only, both;
441
442     join_logical_ports(ctx, datapaths, ports, &sb_only, &nb_only, &both);
443
444     /* For logical ports that are in both databases, update the southbound
445      * record based on northbound data.  Also index the in-use tunnel_keys. */
446     struct ovn_port *op, *next;
447     LIST_FOR_EACH_SAFE (op, next, list, &both) {
448         ovn_port_update_sbrec(op);
449
450         add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key);
451         if (op->sb->tunnel_key > op->od->port_key_hint) {
452             op->od->port_key_hint = op->sb->tunnel_key;
453         }
454     }
455
456     /* Add southbound record for each unmatched northbound record. */
457     LIST_FOR_EACH_SAFE (op, next, list, &nb_only) {
458         uint16_t tunnel_key = ovn_port_allocate_key(op->od);
459         if (!tunnel_key) {
460             continue;
461         }
462
463         op->sb = sbrec_port_binding_insert(ctx->ovnsb_txn);
464         ovn_port_update_sbrec(op);
465
466         sbrec_port_binding_set_logical_port(op->sb, op->key);
467         sbrec_port_binding_set_tunnel_key(op->sb, tunnel_key);
468     }
469
470     /* Delete southbound records without northbound matches. */
471     LIST_FOR_EACH_SAFE(op, next, list, &sb_only) {
472         list_remove(&op->list);
473         sbrec_port_binding_delete(op->sb);
474         ovn_port_destroy(ports, op);
475     }
476 }
477 \f
478 #define OVN_MIN_MULTICAST 32768
479 #define OVN_MAX_MULTICAST 65535
480
481 struct multicast_group {
482     const char *name;
483     uint16_t key;               /* OVN_MIN_MULTICAST...OVN_MAX_MULTICAST. */
484 };
485
486 #define MC_FLOOD "_MC_flood"
487 static const struct multicast_group mc_flood = { MC_FLOOD, 65535 };
488
489 #define MC_UNKNOWN "_MC_unknown"
490 static const struct multicast_group mc_unknown = { MC_UNKNOWN, 65534 };
491
492 static bool
493 multicast_group_equal(const struct multicast_group *a,
494                       const struct multicast_group *b)
495 {
496     return !strcmp(a->name, b->name) && a->key == b->key;
497 }
498
499 /* Multicast group entry. */
500 struct ovn_multicast {
501     struct hmap_node hmap_node; /* Index on 'datapath' and 'key'. */
502     struct ovn_datapath *datapath;
503     const struct multicast_group *group;
504
505     struct ovn_port **ports;
506     size_t n_ports, allocated_ports;
507 };
508
509 static uint32_t
510 ovn_multicast_hash(const struct ovn_datapath *datapath,
511                    const struct multicast_group *group)
512 {
513     return hash_pointer(datapath, group->key);
514 }
515
516 static struct ovn_multicast *
517 ovn_multicast_find(struct hmap *mcgroups, struct ovn_datapath *datapath,
518                    const struct multicast_group *group)
519 {
520     struct ovn_multicast *mc;
521
522     HMAP_FOR_EACH_WITH_HASH (mc, hmap_node,
523                              ovn_multicast_hash(datapath, group), mcgroups) {
524         if (mc->datapath == datapath
525             && multicast_group_equal(mc->group, group)) {
526             return mc;
527         }
528     }
529     return NULL;
530 }
531
532 static void
533 ovn_multicast_add(struct hmap *mcgroups, const struct multicast_group *group,
534                   struct ovn_port *port)
535 {
536     struct ovn_datapath *od = port->od;
537     struct ovn_multicast *mc = ovn_multicast_find(mcgroups, od, group);
538     if (!mc) {
539         mc = xmalloc(sizeof *mc);
540         hmap_insert(mcgroups, &mc->hmap_node, ovn_multicast_hash(od, group));
541         mc->datapath = od;
542         mc->group = group;
543         mc->n_ports = 0;
544         mc->allocated_ports = 4;
545         mc->ports = xmalloc(mc->allocated_ports * sizeof *mc->ports);
546     }
547     if (mc->n_ports >= mc->allocated_ports) {
548         mc->ports = x2nrealloc(mc->ports, &mc->allocated_ports,
549                                sizeof *mc->ports);
550     }
551     mc->ports[mc->n_ports++] = port;
552 }
553
554 static void
555 ovn_multicast_destroy(struct hmap *mcgroups, struct ovn_multicast *mc)
556 {
557     if (mc) {
558         hmap_remove(mcgroups, &mc->hmap_node);
559         free(mc->ports);
560         free(mc);
561     }
562 }
563
564 static void
565 ovn_multicast_update_sbrec(const struct ovn_multicast *mc,
566                            const struct sbrec_multicast_group *sb)
567 {
568     struct sbrec_port_binding **ports = xmalloc(mc->n_ports * sizeof *ports);
569     for (size_t i = 0; i < mc->n_ports; i++) {
570         ports[i] = CONST_CAST(struct sbrec_port_binding *, mc->ports[i]->sb);
571     }
572     sbrec_multicast_group_set_ports(sb, ports, mc->n_ports);
573     free(ports);
574 }
575 \f
576 /* Logical flow generation.
577  *
578  * This code generates the Logical_Flow table in the southbound database, as a
579  * function of most of the northbound database.
580  */
581
582 struct ovn_lflow {
583     struct hmap_node hmap_node;
584
585     struct ovn_datapath *od;
586     enum ovn_pipeline { P_IN, P_OUT } pipeline;
587     uint8_t table_id;
588     uint16_t priority;
589     char *match;
590     char *actions;
591 };
592
593 static size_t
594 ovn_lflow_hash(const struct ovn_lflow *lflow)
595 {
596     size_t hash = uuid_hash(&lflow->od->key);
597     hash = hash_2words((lflow->table_id << 16) | lflow->priority, hash);
598     hash = hash_string(lflow->match, hash);
599     return hash_string(lflow->actions, hash);
600 }
601
602 static bool
603 ovn_lflow_equal(const struct ovn_lflow *a, const struct ovn_lflow *b)
604 {
605     return (a->od == b->od
606             && a->pipeline == b->pipeline
607             && a->table_id == b->table_id
608             && a->priority == b->priority
609             && !strcmp(a->match, b->match)
610             && !strcmp(a->actions, b->actions));
611 }
612
613 static void
614 ovn_lflow_init(struct ovn_lflow *lflow, struct ovn_datapath *od,
615               enum ovn_pipeline pipeline, uint8_t table_id, uint16_t priority,
616               char *match, char *actions)
617 {
618     lflow->od = od;
619     lflow->pipeline = pipeline;
620     lflow->table_id = table_id;
621     lflow->priority = priority;
622     lflow->match = match;
623     lflow->actions = actions;
624 }
625
626 static const char *
627 ingress_stage_to_str(int stage) {
628     switch (stage) {
629 #define INGRESS_STAGE(NAME, STR) case S_IN_##NAME: return #STR;
630     INGRESS_STAGES
631 #undef INGRESS_STAGE
632         default: return "<unknown>";
633     }
634 }
635
636 static const char *
637 egress_stage_to_str(int stage) {
638     switch (stage) {
639 #define EGRESS_STAGE(NAME, STR) case S_OUT_##NAME: return #STR;
640     EGRESS_STAGES
641 #undef EGRESS_STAGE
642         default: return "<unknown>";
643     }
644 }
645
646 /* Adds a row with the specified contents to the Logical_Flow table. */
647 static void
648 ovn_lflow_add(struct hmap *lflow_map, struct ovn_datapath *od,
649               enum ovn_pipeline pipeline, uint8_t table_id, uint16_t priority,
650               const char *match, const char *actions)
651 {
652     struct ovn_lflow *lflow = xmalloc(sizeof *lflow);
653     ovn_lflow_init(lflow, od, pipeline, table_id, priority,
654                    xstrdup(match), xstrdup(actions));
655     hmap_insert(lflow_map, &lflow->hmap_node, ovn_lflow_hash(lflow));
656 }
657
658 static struct ovn_lflow *
659 ovn_lflow_find(struct hmap *lflows, struct ovn_datapath *od,
660                enum ovn_pipeline pipeline, uint8_t table_id, uint16_t priority,
661                const char *match, const char *actions)
662 {
663     struct ovn_lflow target;
664     ovn_lflow_init(&target, od, pipeline, table_id, priority,
665                    CONST_CAST(char *, match), CONST_CAST(char *, actions));
666
667     struct ovn_lflow *lflow;
668     HMAP_FOR_EACH_WITH_HASH (lflow, hmap_node, ovn_lflow_hash(&target),
669                              lflows) {
670         if (ovn_lflow_equal(lflow, &target)) {
671             return lflow;
672         }
673     }
674     return NULL;
675 }
676
677 static void
678 ovn_lflow_destroy(struct hmap *lflows, struct ovn_lflow *lflow)
679 {
680     if (lflow) {
681         hmap_remove(lflows, &lflow->hmap_node);
682         free(lflow->match);
683         free(lflow->actions);
684         free(lflow);
685     }
686 }
687
688 /* Appends port security constraints on L2 address field 'eth_addr_field'
689  * (e.g. "eth.src" or "eth.dst") to 'match'.  'port_security', with
690  * 'n_port_security' elements, is the collection of port_security constraints
691  * from an OVN_NB Logical_Port row. */
692 static void
693 build_port_security(const char *eth_addr_field,
694                     char **port_security, size_t n_port_security,
695                     struct ds *match)
696 {
697     size_t base_len = match->length;
698     ds_put_format(match, " && %s == {", eth_addr_field);
699
700     size_t n = 0;
701     for (size_t i = 0; i < n_port_security; i++) {
702         struct eth_addr ea;
703
704         if (eth_addr_from_string(port_security[i], &ea)) {
705             ds_put_format(match, ETH_ADDR_FMT, ETH_ADDR_ARGS(ea));
706             ds_put_char(match, ' ');
707             n++;
708         }
709     }
710     ds_chomp(match, ' ');
711     ds_put_cstr(match, "}");
712
713     if (!n) {
714         match->length = base_len;
715     }
716 }
717
718 static bool
719 lport_is_enabled(const struct nbrec_logical_port *lport)
720 {
721     return !lport->enabled || *lport->enabled;
722 }
723
724 /* Updates the Logical_Flow and Multicast_Group tables in the OVN_SB database,
725  * constructing their contents based on the OVN_NB database. */
726 static void
727 build_lflows(struct northd_context *ctx, struct hmap *datapaths,
728              struct hmap *ports)
729 {
730     struct hmap lflows = HMAP_INITIALIZER(&lflows);
731     struct hmap mcgroups = HMAP_INITIALIZER(&mcgroups);
732
733     /* Ingress table 0: Admission control framework (priorities 0 and 100). */
734     struct ovn_datapath *od;
735     HMAP_FOR_EACH (od, key_node, datapaths) {
736         /* Logical VLANs not supported. */
737         ovn_lflow_add(&lflows, od, P_IN, S_IN_PORT_SEC, 100, "vlan.present",
738                       "drop;");
739
740         /* Broadcast/multicast source address is invalid. */
741         ovn_lflow_add(&lflows, od, P_IN, S_IN_PORT_SEC, 100, "eth.src[40]",
742                       "drop;");
743
744         /* Port security flows have priority 50 (see below) and will continue
745          * to the next table if packet source is acceptable. */
746
747         /* Otherwise drop the packet. */
748         ovn_lflow_add(&lflows, od, P_IN, S_IN_PORT_SEC, 0, "1", "drop;");
749     }
750
751     /* Ingress table 0: Ingress port security (priority 50). */
752     struct ovn_port *op;
753     HMAP_FOR_EACH (op, key_node, ports) {
754         struct ds match = DS_EMPTY_INITIALIZER;
755         ds_put_cstr(&match, "inport == ");
756         json_string_escape(op->key, &match);
757         build_port_security("eth.src",
758                             op->nb->port_security, op->nb->n_port_security,
759                             &match);
760         ovn_lflow_add(&lflows, op->od, P_IN, S_IN_PORT_SEC, 50, ds_cstr(&match),
761                       lport_is_enabled(op->nb) ? "next;" : "drop;");
762         ds_destroy(&match);
763     }
764
765     /* Ingress table 1: Destination lookup, broadcast and multicast handling
766      * (priority 100). */
767     HMAP_FOR_EACH (op, key_node, ports) {
768         if (lport_is_enabled(op->nb)) {
769             ovn_multicast_add(&mcgroups, &mc_flood, op);
770         }
771     }
772     HMAP_FOR_EACH (od, key_node, datapaths) {
773         ovn_lflow_add(&lflows, od, P_IN, S_IN_L2_LKUP, 100, "eth.dst[40]",
774                       "outport = \""MC_FLOOD"\"; output;");
775     }
776
777     /* Ingress table 1: Destination lookup, unicast handling (priority 50), */
778     HMAP_FOR_EACH (op, key_node, ports) {
779         for (size_t i = 0; i < op->nb->n_macs; i++) {
780             struct eth_addr mac;
781
782             if (eth_addr_from_string(op->nb->macs[i], &mac)) {
783                 struct ds match, actions;
784
785                 ds_init(&match);
786                 ds_put_format(&match, "eth.dst == %s", op->nb->macs[i]);
787
788                 ds_init(&actions);
789                 ds_put_cstr(&actions, "outport = ");
790                 json_string_escape(op->nb->name, &actions);
791                 ds_put_cstr(&actions, "; output;");
792                 ovn_lflow_add(&lflows, op->od, P_IN, S_IN_L2_LKUP, 50,
793                               ds_cstr(&match), ds_cstr(&actions));
794                 ds_destroy(&actions);
795                 ds_destroy(&match);
796             } else if (!strcmp(op->nb->macs[i], "unknown")) {
797                 ovn_multicast_add(&mcgroups, &mc_unknown, op);
798                 op->od->has_unknown = true;
799             } else {
800                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
801
802                 VLOG_INFO_RL(&rl, "%s: invalid syntax '%s' in macs column",
803                              op->nb->name, op->nb->macs[i]);
804             }
805         }
806     }
807
808     /* Ingress table 1: Destination lookup for unknown MACs (priority 0). */
809     HMAP_FOR_EACH (od, key_node, datapaths) {
810         if (od->has_unknown) {
811             ovn_lflow_add(&lflows, od, P_IN, S_IN_L2_LKUP, 0, "1",
812                           "outport = \""MC_UNKNOWN"\"; output;");
813         }
814     }
815
816     /* Egress table 0: ACLs (any priority). */
817     HMAP_FOR_EACH (od, key_node, datapaths) {
818         for (size_t i = 0; i < od->nb->n_acls; i++) {
819             const struct nbrec_acl *acl = od->nb->acls[i];
820             const char *action;
821
822             action = (!strcmp(acl->action, "allow") ||
823                       !strcmp(acl->action, "allow-related"))
824                 ? "next;" : "drop;";
825             ovn_lflow_add(&lflows, od, P_OUT, S_OUT_ACL, acl->priority,
826                           acl->match, action);
827         }
828     }
829     HMAP_FOR_EACH (od, key_node, datapaths) {
830         ovn_lflow_add(&lflows, od, P_OUT, S_OUT_ACL, 0, "1", "next;");
831     }
832
833     /* Egress table 1: Egress port security multicast/broadcast (priority
834      * 100). */
835     HMAP_FOR_EACH (od, key_node, datapaths) {
836         ovn_lflow_add(&lflows, od, P_OUT, S_OUT_PORT_SEC, 100, "eth.dst[40]",
837                       "output;");
838     }
839
840     /* Egress table 1: Egress port security (priority 50). */
841     HMAP_FOR_EACH (op, key_node, ports) {
842         struct ds match;
843
844         ds_init(&match);
845         ds_put_cstr(&match, "outport == ");
846         json_string_escape(op->key, &match);
847         build_port_security("eth.dst",
848                             op->nb->port_security, op->nb->n_port_security,
849                             &match);
850
851         ovn_lflow_add(&lflows, op->od, P_OUT, S_OUT_PORT_SEC, 50,
852                       ds_cstr(&match),
853                       lport_is_enabled(op->nb) ? "output;" : "drop;");
854
855         ds_destroy(&match);
856     }
857
858     /* Push changes to the Logical_Flow table to database. */
859     const struct sbrec_logical_flow *sbflow, *next_sbflow;
860     SBREC_LOGICAL_FLOW_FOR_EACH_SAFE (sbflow, next_sbflow, ctx->ovnsb_idl) {
861         struct ovn_datapath *od
862             = ovn_datapath_from_sbrec(datapaths, sbflow->logical_datapath);
863         if (!od) {
864             sbrec_logical_flow_delete(sbflow);
865             continue;
866         }
867
868         struct ovn_lflow *lflow = ovn_lflow_find(
869             &lflows, od, (!strcmp(sbflow->pipeline, "ingress") ? P_IN : P_OUT),
870             sbflow->table_id, sbflow->priority,
871             sbflow->match, sbflow->actions);
872         if (lflow) {
873             ovn_lflow_destroy(&lflows, lflow);
874         } else {
875             sbrec_logical_flow_delete(sbflow);
876         }
877     }
878     struct ovn_lflow *lflow, *next_lflow;
879     HMAP_FOR_EACH_SAFE (lflow, next_lflow, hmap_node, &lflows) {
880         sbflow = sbrec_logical_flow_insert(ctx->ovnsb_txn);
881         sbrec_logical_flow_set_logical_datapath(sbflow, lflow->od->sb);
882         sbrec_logical_flow_set_pipeline(
883             sbflow, lflow->pipeline == P_IN ? "ingress" : "egress");
884         sbrec_logical_flow_set_table_id(sbflow, lflow->table_id);
885         sbrec_logical_flow_set_priority(sbflow, lflow->priority);
886         sbrec_logical_flow_set_match(sbflow, lflow->match);
887         sbrec_logical_flow_set_actions(sbflow, lflow->actions);
888
889         const struct smap ids = SMAP_CONST1(
890             &ids, "stage-name",
891             (lflow->pipeline == P_IN
892              ? ingress_stage_to_str(lflow->table_id)
893              : egress_stage_to_str(lflow->table_id)));
894         sbrec_logical_flow_set_external_ids(sbflow, &ids);
895
896         ovn_lflow_destroy(&lflows, lflow);
897     }
898     hmap_destroy(&lflows);
899
900     /* Push changes to the Multicast_Group table to database. */
901     const struct sbrec_multicast_group *sbmc, *next_sbmc;
902     SBREC_MULTICAST_GROUP_FOR_EACH_SAFE (sbmc, next_sbmc, ctx->ovnsb_idl) {
903         struct ovn_datapath *od = ovn_datapath_from_sbrec(datapaths,
904                                                           sbmc->datapath);
905         if (!od) {
906             sbrec_multicast_group_delete(sbmc);
907             continue;
908         }
909
910         struct multicast_group group = { .name = sbmc->name,
911                                          .key = sbmc->tunnel_key };
912         struct ovn_multicast *mc = ovn_multicast_find(&mcgroups, od, &group);
913         if (mc) {
914             ovn_multicast_update_sbrec(mc, sbmc);
915             ovn_multicast_destroy(&mcgroups, mc);
916         } else {
917             sbrec_multicast_group_delete(sbmc);
918         }
919     }
920     struct ovn_multicast *mc, *next_mc;
921     HMAP_FOR_EACH_SAFE (mc, next_mc, hmap_node, &mcgroups) {
922         sbmc = sbrec_multicast_group_insert(ctx->ovnsb_txn);
923         sbrec_multicast_group_set_datapath(sbmc, mc->datapath->sb);
924         sbrec_multicast_group_set_name(sbmc, mc->group->name);
925         sbrec_multicast_group_set_tunnel_key(sbmc, mc->group->key);
926         ovn_multicast_update_sbrec(mc, sbmc);
927         ovn_multicast_destroy(&mcgroups, mc);
928     }
929     hmap_destroy(&mcgroups);
930 }
931 \f
932 static void
933 ovnnb_db_changed(struct northd_context *ctx)
934 {
935     VLOG_DBG("ovn-nb db contents have changed.");
936
937     struct hmap datapaths, ports;
938     build_datapaths(ctx, &datapaths);
939     build_ports(ctx, &datapaths, &ports);
940     build_lflows(ctx, &datapaths, &ports);
941
942     struct ovn_datapath *dp, *next_dp;
943     HMAP_FOR_EACH_SAFE (dp, next_dp, key_node, &datapaths) {
944         ovn_datapath_destroy(&datapaths, dp);
945     }
946     hmap_destroy(&datapaths);
947
948     struct ovn_port *port, *next_port;
949     HMAP_FOR_EACH_SAFE (port, next_port, key_node, &ports) {
950         ovn_port_destroy(&ports, port);
951     }
952     hmap_destroy(&ports);
953 }
954
955 /*
956  * The only change we get notified about is if the 'chassis' column of the
957  * 'Port_Binding' table changes.  When this column is not empty, it means we
958  * need to set the corresponding logical port as 'up' in the northbound DB.
959  */
960 static void
961 ovnsb_db_changed(struct northd_context *ctx)
962 {
963     struct hmap lports_hmap;
964     const struct sbrec_port_binding *sb;
965     const struct nbrec_logical_port *nb;
966
967     struct lport_hash_node {
968         struct hmap_node node;
969         const struct nbrec_logical_port *nb;
970     } *hash_node, *hash_node_next;
971
972     VLOG_DBG("Recalculating port up states for ovn-nb db.");
973
974     hmap_init(&lports_hmap);
975
976     NBREC_LOGICAL_PORT_FOR_EACH(nb, ctx->ovnnb_idl) {
977         hash_node = xzalloc(sizeof *hash_node);
978         hash_node->nb = nb;
979         hmap_insert(&lports_hmap, &hash_node->node, hash_string(nb->name, 0));
980     }
981
982     SBREC_PORT_BINDING_FOR_EACH(sb, ctx->ovnsb_idl) {
983         nb = NULL;
984         HMAP_FOR_EACH_WITH_HASH(hash_node, node,
985                                 hash_string(sb->logical_port, 0),
986                                 &lports_hmap) {
987             if (!strcmp(sb->logical_port, hash_node->nb->name)) {
988                 nb = hash_node->nb;
989                 break;
990             }
991         }
992
993         if (!nb) {
994             /* The logical port doesn't exist for this port binding.  This can
995              * happen under normal circumstances when ovn-northd hasn't gotten
996              * around to pruning the Port_Binding yet. */
997             continue;
998         }
999
1000         if (sb->chassis && (!nb->up || !*nb->up)) {
1001             bool up = true;
1002             nbrec_logical_port_set_up(nb, &up, 1);
1003         } else if (!sb->chassis && (!nb->up || *nb->up)) {
1004             bool up = false;
1005             nbrec_logical_port_set_up(nb, &up, 1);
1006         }
1007     }
1008
1009     HMAP_FOR_EACH_SAFE(hash_node, hash_node_next, node, &lports_hmap) {
1010         hmap_remove(&lports_hmap, &hash_node->node);
1011         free(hash_node);
1012     }
1013     hmap_destroy(&lports_hmap);
1014 }
1015 \f
1016
1017 static char *default_db_;
1018
1019 static const char *
1020 default_db(void)
1021 {
1022     if (!default_db_) {
1023         default_db_ = xasprintf("unix:%s/db.sock", ovs_rundir());
1024     }
1025     return default_db_;
1026 }
1027
1028 static void
1029 parse_options(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1030 {
1031     enum {
1032         DAEMON_OPTION_ENUMS,
1033         VLOG_OPTION_ENUMS,
1034     };
1035     static const struct option long_options[] = {
1036         {"ovnsb-db", required_argument, NULL, 'd'},
1037         {"ovnnb-db", required_argument, NULL, 'D'},
1038         {"help", no_argument, NULL, 'h'},
1039         {"options", no_argument, NULL, 'o'},
1040         {"version", no_argument, NULL, 'V'},
1041         DAEMON_LONG_OPTIONS,
1042         VLOG_LONG_OPTIONS,
1043         STREAM_SSL_LONG_OPTIONS,
1044         {NULL, 0, NULL, 0},
1045     };
1046     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1047
1048     for (;;) {
1049         int c;
1050
1051         c = getopt_long(argc, argv, short_options, long_options, NULL);
1052         if (c == -1) {
1053             break;
1054         }
1055
1056         switch (c) {
1057         DAEMON_OPTION_HANDLERS;
1058         VLOG_OPTION_HANDLERS;
1059         STREAM_SSL_OPTION_HANDLERS;
1060
1061         case 'd':
1062             ovnsb_db = optarg;
1063             break;
1064
1065         case 'D':
1066             ovnnb_db = optarg;
1067             break;
1068
1069         case 'h':
1070             usage();
1071             exit(EXIT_SUCCESS);
1072
1073         case 'o':
1074             ovs_cmdl_print_options(long_options);
1075             exit(EXIT_SUCCESS);
1076
1077         case 'V':
1078             ovs_print_version(0, 0);
1079             exit(EXIT_SUCCESS);
1080
1081         default:
1082             break;
1083         }
1084     }
1085
1086     if (!ovnsb_db) {
1087         ovnsb_db = default_db();
1088     }
1089
1090     if (!ovnnb_db) {
1091         ovnnb_db = default_db();
1092     }
1093
1094     free(short_options);
1095 }
1096
1097 static void
1098 add_column_noalert(struct ovsdb_idl *idl,
1099                    const struct ovsdb_idl_column *column)
1100 {
1101     ovsdb_idl_add_column(idl, column);
1102     ovsdb_idl_omit_alert(idl, column);
1103 }
1104
1105 int
1106 main(int argc, char *argv[])
1107 {
1108     extern struct vlog_module VLM_reconnect;
1109     struct ovsdb_idl *ovnnb_idl, *ovnsb_idl;
1110     unsigned int ovnnb_seqno, ovn_seqno;
1111     int res = EXIT_SUCCESS;
1112     struct northd_context ctx = {
1113         .ovnsb_txn = NULL,
1114     };
1115     bool ovnnb_changes_pending = false;
1116     bool ovn_changes_pending = false;
1117     struct unixctl_server *unixctl;
1118     int retval;
1119     bool exiting;
1120
1121     fatal_ignore_sigpipe();
1122     set_program_name(argv[0]);
1123     service_start(&argc, &argv);
1124     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
1125     vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
1126     parse_options(argc, argv);
1127
1128     daemonize_start();
1129
1130     retval = unixctl_server_create(NULL, &unixctl);
1131     if (retval) {
1132         exit(EXIT_FAILURE);
1133     }
1134     unixctl_command_register("exit", "", 0, 0, ovn_northd_exit, &exiting);
1135
1136     daemonize_complete();
1137
1138     nbrec_init();
1139     sbrec_init();
1140
1141     /* We want to detect all changes to the ovn-nb db. */
1142     ctx.ovnnb_idl = ovnnb_idl = ovsdb_idl_create(ovnnb_db,
1143             &nbrec_idl_class, true, true);
1144
1145     ctx.ovnsb_idl = ovnsb_idl = ovsdb_idl_create(ovnsb_db,
1146             &sbrec_idl_class, false, true);
1147
1148     ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_logical_flow);
1149     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_logical_datapath);
1150     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_pipeline);
1151     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_table_id);
1152     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_priority);
1153     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_match);
1154     add_column_noalert(ovnsb_idl, &sbrec_logical_flow_col_actions);
1155
1156     ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_multicast_group);
1157     add_column_noalert(ovnsb_idl, &sbrec_multicast_group_col_datapath);
1158     add_column_noalert(ovnsb_idl, &sbrec_multicast_group_col_tunnel_key);
1159     add_column_noalert(ovnsb_idl, &sbrec_multicast_group_col_name);
1160     add_column_noalert(ovnsb_idl, &sbrec_multicast_group_col_ports);
1161
1162     ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_datapath_binding);
1163     add_column_noalert(ovnsb_idl, &sbrec_datapath_binding_col_tunnel_key);
1164     add_column_noalert(ovnsb_idl, &sbrec_datapath_binding_col_external_ids);
1165
1166     ovsdb_idl_add_table(ovnsb_idl, &sbrec_table_port_binding);
1167     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_datapath);
1168     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_logical_port);
1169     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_tunnel_key);
1170     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_parent_port);
1171     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_tag);
1172     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_type);
1173     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_options);
1174     add_column_noalert(ovnsb_idl, &sbrec_port_binding_col_mac);
1175     ovsdb_idl_add_column(ovnsb_idl, &sbrec_port_binding_col_chassis);
1176
1177     /*
1178      * The loop here just runs the IDL in a loop waiting for the seqno to
1179      * change, which indicates that the contents of the db have changed.
1180      *
1181      * If the contents of the ovn-nb db change, the mappings to the ovn-sb
1182      * db must be recalculated.
1183      *
1184      * If the contents of the ovn-sb db change, it means the 'up' state of
1185      * a port may have changed, as that's the only type of change ovn-northd is
1186      * watching for.
1187      */
1188
1189     ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl);
1190     ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl);
1191     exiting = false;
1192     while (!exiting) {
1193         ovsdb_idl_run(ovnnb_idl);
1194         ovsdb_idl_run(ovnsb_idl);
1195         unixctl_server_run(unixctl);
1196
1197         if (!ovsdb_idl_is_alive(ovnnb_idl)) {
1198             int retval = ovsdb_idl_get_last_error(ovnnb_idl);
1199             VLOG_ERR("%s: database connection failed (%s)",
1200                     ovnnb_db, ovs_retval_to_string(retval));
1201             res = EXIT_FAILURE;
1202             break;
1203         }
1204
1205         if (!ovsdb_idl_is_alive(ovnsb_idl)) {
1206             int retval = ovsdb_idl_get_last_error(ovnsb_idl);
1207             VLOG_ERR("%s: database connection failed (%s)",
1208                     ovnsb_db, ovs_retval_to_string(retval));
1209             res = EXIT_FAILURE;
1210             break;
1211         }
1212
1213         if (ovnnb_seqno != ovsdb_idl_get_seqno(ovnnb_idl)) {
1214             ovnnb_seqno = ovsdb_idl_get_seqno(ovnnb_idl);
1215             ovnnb_changes_pending = true;
1216         }
1217
1218         if (ovn_seqno != ovsdb_idl_get_seqno(ovnsb_idl)) {
1219             ovn_seqno = ovsdb_idl_get_seqno(ovnsb_idl);
1220             ovn_changes_pending = true;
1221         }
1222
1223         /*
1224          * If there are any pending changes, we delay recalculating the
1225          * necessary updates until after an existing transaction finishes.
1226          * This avoids the possibility of rapid updates causing ovn-northd to
1227          * never be able to successfully make the corresponding updates to the
1228          * other db.  Instead, pending changes are batched up until the next
1229          * time we get a chance to calculate the new state and apply it.
1230          */
1231
1232         if (ovnnb_changes_pending && !ctx.ovnsb_txn) {
1233             /*
1234              * The OVN-nb db contents have changed, so create a transaction for
1235              * updating the OVN-sb DB.
1236              */
1237             ctx.ovnsb_txn = ovsdb_idl_txn_create(ctx.ovnsb_idl);
1238             ovsdb_idl_txn_add_comment(ctx.ovnsb_txn,
1239                                       "ovn-northd: northbound db changed");
1240             ovnnb_db_changed(&ctx);
1241             ovnnb_changes_pending = false;
1242         }
1243
1244         if (ovn_changes_pending && !ctx.ovnnb_txn) {
1245             /*
1246              * The OVN-sb db contents have changed, so create a transaction for
1247              * updating the northbound DB.
1248              */
1249             ctx.ovnnb_txn = ovsdb_idl_txn_create(ctx.ovnnb_idl);
1250             ovsdb_idl_txn_add_comment(ctx.ovnnb_txn,
1251                                       "ovn-northd: southbound db changed");
1252             ovnsb_db_changed(&ctx);
1253             ovn_changes_pending = false;
1254         }
1255
1256         if (ctx.ovnnb_txn) {
1257             enum ovsdb_idl_txn_status txn_status;
1258             txn_status = ovsdb_idl_txn_commit(ctx.ovnnb_txn);
1259             switch (txn_status) {
1260             case TXN_UNCOMMITTED:
1261             case TXN_INCOMPLETE:
1262                 /* Come back around and try to commit this transaction again */
1263                 break;
1264             case TXN_ABORTED:
1265             case TXN_TRY_AGAIN:
1266             case TXN_NOT_LOCKED:
1267             case TXN_ERROR:
1268                 /* Something went wrong, so try creating a new transaction. */
1269                 ovn_changes_pending = true;
1270             case TXN_UNCHANGED:
1271             case TXN_SUCCESS:
1272                 ovsdb_idl_txn_destroy(ctx.ovnnb_txn);
1273                 ctx.ovnnb_txn = NULL;
1274             }
1275         }
1276
1277         if (ctx.ovnsb_txn) {
1278             enum ovsdb_idl_txn_status txn_status;
1279             txn_status = ovsdb_idl_txn_commit(ctx.ovnsb_txn);
1280             switch (txn_status) {
1281             case TXN_UNCOMMITTED:
1282             case TXN_INCOMPLETE:
1283                 /* Come back around and try to commit this transaction again */
1284                 break;
1285             case TXN_ABORTED:
1286             case TXN_TRY_AGAIN:
1287             case TXN_NOT_LOCKED:
1288             case TXN_ERROR:
1289                 /* Something went wrong, so try creating a new transaction. */
1290                 ovnnb_changes_pending = true;
1291             case TXN_UNCHANGED:
1292             case TXN_SUCCESS:
1293                 ovsdb_idl_txn_destroy(ctx.ovnsb_txn);
1294                 ctx.ovnsb_txn = NULL;
1295             }
1296         }
1297
1298         if (ovnnb_seqno == ovsdb_idl_get_seqno(ovnnb_idl) &&
1299                 ovn_seqno == ovsdb_idl_get_seqno(ovnsb_idl)) {
1300             ovsdb_idl_wait(ovnnb_idl);
1301             ovsdb_idl_wait(ovnsb_idl);
1302             if (ctx.ovnnb_txn) {
1303                 ovsdb_idl_txn_wait(ctx.ovnnb_txn);
1304             }
1305             if (ctx.ovnsb_txn) {
1306                 ovsdb_idl_txn_wait(ctx.ovnsb_txn);
1307             }
1308             unixctl_server_wait(unixctl);
1309             if (exiting) {
1310                 poll_immediate_wake();
1311             }
1312             poll_block();
1313         }
1314         if (should_service_stop()) {
1315             exiting = true;
1316         }
1317     }
1318
1319     unixctl_server_destroy(unixctl);
1320     ovsdb_idl_destroy(ovnsb_idl);
1321     ovsdb_idl_destroy(ovnnb_idl);
1322     service_stop();
1323
1324     free(default_db_);
1325
1326     exit(res);
1327 }
1328
1329 static void
1330 ovn_northd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1331                 const char *argv[] OVS_UNUSED, void *exiting_)
1332 {
1333     bool *exiting = exiting_;
1334     *exiting = true;
1335
1336     unixctl_command_reply(conn, NULL);
1337 }