]> git.proxmox.com Git - mirror_ovs.git/blob - ovn/northd/ovn-northd.c
ovn-northd: Fix parsing of load balance keys.
[mirror_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 "bitmap.h"
22 #include "command-line.h"
23 #include "daemon.h"
24 #include "dirs.h"
25 #include "openvswitch/dynamic-string.h"
26 #include "fatal-signal.h"
27 #include "hash.h"
28 #include "openvswitch/hmap.h"
29 #include "openvswitch/json.h"
30 #include "ovn/lex.h"
31 #include "ovn/lib/chassis-index.h"
32 #include "ovn/lib/logical-fields.h"
33 #include "ovn/lib/ovn-l7.h"
34 #include "ovn/lib/ovn-nb-idl.h"
35 #include "ovn/lib/ovn-sb-idl.h"
36 #include "ovn/lib/ovn-util.h"
37 #include "ovn/actions.h"
38 #include "packets.h"
39 #include "openvswitch/poll-loop.h"
40 #include "smap.h"
41 #include "sset.h"
42 #include "stream.h"
43 #include "stream-ssl.h"
44 #include "unixctl.h"
45 #include "util.h"
46 #include "uuid.h"
47 #include "openvswitch/vlog.h"
48
49 VLOG_DEFINE_THIS_MODULE(ovn_northd);
50
51 static unixctl_cb_func ovn_northd_exit;
52
53 struct northd_context {
54 struct ovsdb_idl *ovnnb_idl;
55 struct ovsdb_idl *ovnsb_idl;
56 struct ovsdb_idl_txn *ovnnb_txn;
57 struct ovsdb_idl_txn *ovnsb_txn;
58 };
59
60 static const char *ovnnb_db;
61 static const char *ovnsb_db;
62 static const char *unixctl_path;
63
64 #define MAC_ADDR_PREFIX 0x0A0000000000ULL
65 #define MAC_ADDR_SPACE 0xffffff
66
67 /* MAC address management (macam) table of "struct eth_addr"s, that holds the
68 * MAC addresses allocated by the OVN ipam module. */
69 static struct hmap macam = HMAP_INITIALIZER(&macam);
70
71 #define MAX_OVN_TAGS 4096
72 \f
73 /* Pipeline stages. */
74
75 /* The two pipelines in an OVN logical flow table. */
76 enum ovn_pipeline {
77 P_IN, /* Ingress pipeline. */
78 P_OUT /* Egress pipeline. */
79 };
80
81 /* The two purposes for which ovn-northd uses OVN logical datapaths. */
82 enum ovn_datapath_type {
83 DP_SWITCH, /* OVN logical switch. */
84 DP_ROUTER /* OVN logical router. */
85 };
86
87 /* Returns an "enum ovn_stage" built from the arguments.
88 *
89 * (It's better to use ovn_stage_build() for type-safety reasons, but inline
90 * functions can't be used in enums or switch cases.) */
91 #define OVN_STAGE_BUILD(DP_TYPE, PIPELINE, TABLE) \
92 (((DP_TYPE) << 9) | ((PIPELINE) << 8) | (TABLE))
93
94 /* A stage within an OVN logical switch or router.
95 *
96 * An "enum ovn_stage" indicates whether the stage is part of a logical switch
97 * or router, whether the stage is part of the ingress or egress pipeline, and
98 * the table within that pipeline. The first three components are combined to
99 * form the stage's full name, e.g. S_SWITCH_IN_PORT_SEC_L2,
100 * S_ROUTER_OUT_DELIVERY. */
101 enum ovn_stage {
102 #define PIPELINE_STAGES \
103 /* Logical switch ingress stages. */ \
104 PIPELINE_STAGE(SWITCH, IN, PORT_SEC_L2, 0, "ls_in_port_sec_l2") \
105 PIPELINE_STAGE(SWITCH, IN, PORT_SEC_IP, 1, "ls_in_port_sec_ip") \
106 PIPELINE_STAGE(SWITCH, IN, PORT_SEC_ND, 2, "ls_in_port_sec_nd") \
107 PIPELINE_STAGE(SWITCH, IN, PRE_ACL, 3, "ls_in_pre_acl") \
108 PIPELINE_STAGE(SWITCH, IN, PRE_LB, 4, "ls_in_pre_lb") \
109 PIPELINE_STAGE(SWITCH, IN, PRE_STATEFUL, 5, "ls_in_pre_stateful") \
110 PIPELINE_STAGE(SWITCH, IN, ACL, 6, "ls_in_acl") \
111 PIPELINE_STAGE(SWITCH, IN, QOS_MARK, 7, "ls_in_qos_mark") \
112 PIPELINE_STAGE(SWITCH, IN, QOS_METER, 8, "ls_in_qos_meter") \
113 PIPELINE_STAGE(SWITCH, IN, LB, 9, "ls_in_lb") \
114 PIPELINE_STAGE(SWITCH, IN, STATEFUL, 10, "ls_in_stateful") \
115 PIPELINE_STAGE(SWITCH, IN, ARP_ND_RSP, 11, "ls_in_arp_rsp") \
116 PIPELINE_STAGE(SWITCH, IN, DHCP_OPTIONS, 12, "ls_in_dhcp_options") \
117 PIPELINE_STAGE(SWITCH, IN, DHCP_RESPONSE, 13, "ls_in_dhcp_response") \
118 PIPELINE_STAGE(SWITCH, IN, DNS_LOOKUP, 14, "ls_in_dns_lookup") \
119 PIPELINE_STAGE(SWITCH, IN, DNS_RESPONSE, 15, "ls_in_dns_response") \
120 PIPELINE_STAGE(SWITCH, IN, L2_LKUP, 16, "ls_in_l2_lkup") \
121 \
122 /* Logical switch egress stages. */ \
123 PIPELINE_STAGE(SWITCH, OUT, PRE_LB, 0, "ls_out_pre_lb") \
124 PIPELINE_STAGE(SWITCH, OUT, PRE_ACL, 1, "ls_out_pre_acl") \
125 PIPELINE_STAGE(SWITCH, OUT, PRE_STATEFUL, 2, "ls_out_pre_stateful") \
126 PIPELINE_STAGE(SWITCH, OUT, LB, 3, "ls_out_lb") \
127 PIPELINE_STAGE(SWITCH, OUT, ACL, 4, "ls_out_acl") \
128 PIPELINE_STAGE(SWITCH, OUT, QOS_MARK, 5, "ls_out_qos_mark") \
129 PIPELINE_STAGE(SWITCH, OUT, QOS_METER, 6, "ls_out_qos_meter") \
130 PIPELINE_STAGE(SWITCH, OUT, STATEFUL, 7, "ls_out_stateful") \
131 PIPELINE_STAGE(SWITCH, OUT, PORT_SEC_IP, 8, "ls_out_port_sec_ip") \
132 PIPELINE_STAGE(SWITCH, OUT, PORT_SEC_L2, 9, "ls_out_port_sec_l2") \
133 \
134 /* Logical router ingress stages. */ \
135 PIPELINE_STAGE(ROUTER, IN, ADMISSION, 0, "lr_in_admission") \
136 PIPELINE_STAGE(ROUTER, IN, IP_INPUT, 1, "lr_in_ip_input") \
137 PIPELINE_STAGE(ROUTER, IN, DEFRAG, 2, "lr_in_defrag") \
138 PIPELINE_STAGE(ROUTER, IN, UNSNAT, 3, "lr_in_unsnat") \
139 PIPELINE_STAGE(ROUTER, IN, DNAT, 4, "lr_in_dnat") \
140 PIPELINE_STAGE(ROUTER, IN, ND_RA_OPTIONS, 5, "lr_in_nd_ra_options") \
141 PIPELINE_STAGE(ROUTER, IN, ND_RA_RESPONSE, 6, "lr_in_nd_ra_response") \
142 PIPELINE_STAGE(ROUTER, IN, IP_ROUTING, 7, "lr_in_ip_routing") \
143 PIPELINE_STAGE(ROUTER, IN, ARP_RESOLVE, 8, "lr_in_arp_resolve") \
144 PIPELINE_STAGE(ROUTER, IN, GW_REDIRECT, 9, "lr_in_gw_redirect") \
145 PIPELINE_STAGE(ROUTER, IN, ARP_REQUEST, 10, "lr_in_arp_request") \
146 \
147 /* Logical router egress stages. */ \
148 PIPELINE_STAGE(ROUTER, OUT, UNDNAT, 0, "lr_out_undnat") \
149 PIPELINE_STAGE(ROUTER, OUT, SNAT, 1, "lr_out_snat") \
150 PIPELINE_STAGE(ROUTER, OUT, EGR_LOOP, 2, "lr_out_egr_loop") \
151 PIPELINE_STAGE(ROUTER, OUT, DELIVERY, 3, "lr_out_delivery")
152
153 #define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
154 S_##DP_TYPE##_##PIPELINE##_##STAGE \
155 = OVN_STAGE_BUILD(DP_##DP_TYPE, P_##PIPELINE, TABLE),
156 PIPELINE_STAGES
157 #undef PIPELINE_STAGE
158 };
159
160 /* Due to various hard-coded priorities need to implement ACLs, the
161 * northbound database supports a smaller range of ACL priorities than
162 * are available to logical flows. This value is added to an ACL
163 * priority to determine the ACL's logical flow priority. */
164 #define OVN_ACL_PRI_OFFSET 1000
165
166 /* Register definitions specific to switches. */
167 #define REGBIT_CONNTRACK_DEFRAG "reg0[0]"
168 #define REGBIT_CONNTRACK_COMMIT "reg0[1]"
169 #define REGBIT_CONNTRACK_NAT "reg0[2]"
170 #define REGBIT_DHCP_OPTS_RESULT "reg0[3]"
171 #define REGBIT_DNS_LOOKUP_RESULT "reg0[4]"
172 #define REGBIT_ND_RA_OPTS_RESULT "reg0[5]"
173
174 /* Register definitions for switches and routers. */
175 #define REGBIT_NAT_REDIRECT "reg9[0]"
176 /* Indicate that this packet has been recirculated using egress
177 * loopback. This allows certain checks to be bypassed, such as a
178 * logical router dropping packets with source IP address equals
179 * one of the logical router's own IP addresses. */
180 #define REGBIT_EGRESS_LOOPBACK "reg9[1]"
181
182 /* Returns an "enum ovn_stage" built from the arguments. */
183 static enum ovn_stage
184 ovn_stage_build(enum ovn_datapath_type dp_type, enum ovn_pipeline pipeline,
185 uint8_t table)
186 {
187 return OVN_STAGE_BUILD(dp_type, pipeline, table);
188 }
189
190 /* Returns the pipeline to which 'stage' belongs. */
191 static enum ovn_pipeline
192 ovn_stage_get_pipeline(enum ovn_stage stage)
193 {
194 return (stage >> 8) & 1;
195 }
196
197 /* Returns the pipeline name to which 'stage' belongs. */
198 static const char *
199 ovn_stage_get_pipeline_name(enum ovn_stage stage)
200 {
201 return ovn_stage_get_pipeline(stage) == P_IN ? "ingress" : "egress";
202 }
203
204 /* Returns the table to which 'stage' belongs. */
205 static uint8_t
206 ovn_stage_get_table(enum ovn_stage stage)
207 {
208 return stage & 0xff;
209 }
210
211 /* Returns a string name for 'stage'. */
212 static const char *
213 ovn_stage_to_str(enum ovn_stage stage)
214 {
215 switch (stage) {
216 #define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
217 case S_##DP_TYPE##_##PIPELINE##_##STAGE: return NAME;
218 PIPELINE_STAGES
219 #undef PIPELINE_STAGE
220 default: return "<unknown>";
221 }
222 }
223
224 /* Returns the type of the datapath to which a flow with the given 'stage' may
225 * be added. */
226 static enum ovn_datapath_type
227 ovn_stage_to_datapath_type(enum ovn_stage stage)
228 {
229 switch (stage) {
230 #define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
231 case S_##DP_TYPE##_##PIPELINE##_##STAGE: return DP_##DP_TYPE;
232 PIPELINE_STAGES
233 #undef PIPELINE_STAGE
234 default: OVS_NOT_REACHED();
235 }
236 }
237 \f
238 static void
239 usage(void)
240 {
241 printf("\
242 %s: OVN northbound management daemon\n\
243 usage: %s [OPTIONS]\n\
244 \n\
245 Options:\n\
246 --ovnnb-db=DATABASE connect to ovn-nb database at DATABASE\n\
247 (default: %s)\n\
248 --ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\n\
249 (default: %s)\n\
250 --unixctl=SOCKET override default control socket name\n\
251 -h, --help display this help message\n\
252 -o, --options list available options\n\
253 -V, --version display version information\n\
254 ", program_name, program_name, default_nb_db(), default_sb_db());
255 daemon_usage();
256 vlog_usage();
257 stream_usage("database", true, true, false);
258 }
259 \f
260 struct tnlid_node {
261 struct hmap_node hmap_node;
262 uint32_t tnlid;
263 };
264
265 static void
266 destroy_tnlids(struct hmap *tnlids)
267 {
268 struct tnlid_node *node;
269 HMAP_FOR_EACH_POP (node, hmap_node, tnlids) {
270 free(node);
271 }
272 hmap_destroy(tnlids);
273 }
274
275 static void
276 add_tnlid(struct hmap *set, uint32_t tnlid)
277 {
278 struct tnlid_node *node = xmalloc(sizeof *node);
279 hmap_insert(set, &node->hmap_node, hash_int(tnlid, 0));
280 node->tnlid = tnlid;
281 }
282
283 static bool
284 tnlid_in_use(const struct hmap *set, uint32_t tnlid)
285 {
286 const struct tnlid_node *node;
287 HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_int(tnlid, 0), set) {
288 if (node->tnlid == tnlid) {
289 return true;
290 }
291 }
292 return false;
293 }
294
295 static uint32_t
296 next_tnlid(uint32_t tnlid, uint32_t max)
297 {
298 return tnlid + 1 <= max ? tnlid + 1 : 1;
299 }
300
301 static uint32_t
302 allocate_tnlid(struct hmap *set, const char *name, uint32_t max,
303 uint32_t *hint)
304 {
305 for (uint32_t tnlid = next_tnlid(*hint, max); tnlid != *hint;
306 tnlid = next_tnlid(tnlid, max)) {
307 if (!tnlid_in_use(set, tnlid)) {
308 add_tnlid(set, tnlid);
309 *hint = tnlid;
310 return tnlid;
311 }
312 }
313
314 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
315 VLOG_WARN_RL(&rl, "all %s tunnel ids exhausted", name);
316 return 0;
317 }
318 \f
319 struct ovn_chassis_qdisc_queues {
320 struct hmap_node key_node;
321 uint32_t queue_id;
322 struct uuid chassis_uuid;
323 };
324
325 static void
326 destroy_chassis_queues(struct hmap *set)
327 {
328 struct ovn_chassis_qdisc_queues *node;
329 HMAP_FOR_EACH_POP (node, key_node, set) {
330 free(node);
331 }
332 hmap_destroy(set);
333 }
334
335 static void
336 add_chassis_queue(struct hmap *set, struct uuid *chassis_uuid,
337 uint32_t queue_id)
338 {
339 struct ovn_chassis_qdisc_queues *node = xmalloc(sizeof *node);
340 node->queue_id = queue_id;
341 memcpy(&node->chassis_uuid, chassis_uuid, sizeof node->chassis_uuid);
342 hmap_insert(set, &node->key_node, uuid_hash(chassis_uuid));
343 }
344
345 static bool
346 chassis_queueid_in_use(const struct hmap *set, struct uuid *chassis_uuid,
347 uint32_t queue_id)
348 {
349 const struct ovn_chassis_qdisc_queues *node;
350 HMAP_FOR_EACH_WITH_HASH (node, key_node, uuid_hash(chassis_uuid), set) {
351 if (uuid_equals(chassis_uuid, &node->chassis_uuid)
352 && node->queue_id == queue_id) {
353 return true;
354 }
355 }
356 return false;
357 }
358
359 static uint32_t
360 allocate_chassis_queueid(struct hmap *set, struct sbrec_chassis *chassis)
361 {
362 for (uint32_t queue_id = QDISC_MIN_QUEUE_ID + 1;
363 queue_id <= QDISC_MAX_QUEUE_ID;
364 queue_id++) {
365 if (!chassis_queueid_in_use(set, &chassis->header_.uuid, queue_id)) {
366 add_chassis_queue(set, &chassis->header_.uuid, queue_id);
367 return queue_id;
368 }
369 }
370
371 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
372 VLOG_WARN_RL(&rl, "all %s queue ids exhausted", chassis->name);
373 return 0;
374 }
375
376 static void
377 free_chassis_queueid(struct hmap *set, struct sbrec_chassis *chassis,
378 uint32_t queue_id)
379 {
380 struct ovn_chassis_qdisc_queues *node;
381 HMAP_FOR_EACH_WITH_HASH (node, key_node,
382 uuid_hash(&chassis->header_.uuid),
383 set) {
384 if (uuid_equals(&chassis->header_.uuid, &node->chassis_uuid)
385 && node->queue_id == queue_id) {
386 hmap_remove(set, &node->key_node);
387 break;
388 }
389 }
390 }
391
392 static inline bool
393 port_has_qos_params(const struct smap *opts)
394 {
395 return (smap_get(opts, "qos_max_rate") ||
396 smap_get(opts, "qos_burst"));
397 }
398 \f
399
400 struct ipam_info {
401 uint32_t start_ipv4;
402 size_t total_ipv4s;
403 unsigned long *allocated_ipv4s; /* A bitmap of allocated IPv4s */
404 bool ipv6_prefix_set;
405 struct in6_addr ipv6_prefix;
406 };
407
408 /* The 'key' comes from nbs->header_.uuid or nbr->header_.uuid or
409 * sb->external_ids:logical-switch. */
410 struct ovn_datapath {
411 struct hmap_node key_node; /* Index on 'key'. */
412 struct uuid key; /* (nbs/nbr)->header_.uuid. */
413
414 const struct nbrec_logical_switch *nbs; /* May be NULL. */
415 const struct nbrec_logical_router *nbr; /* May be NULL. */
416 const struct sbrec_datapath_binding *sb; /* May be NULL. */
417
418 struct ovs_list list; /* In list of similar records. */
419
420 /* Logical switch data. */
421 struct ovn_port **router_ports;
422 size_t n_router_ports;
423
424 struct hmap port_tnlids;
425 uint32_t port_key_hint;
426
427 bool has_unknown;
428
429 /* IPAM data. */
430 struct ipam_info *ipam_info;
431
432 /* OVN northd only needs to know about the logical router gateway port for
433 * NAT on a distributed router. This "distributed gateway port" is
434 * populated only when there is a "redirect-chassis" specified for one of
435 * the ports on the logical router. Otherwise this will be NULL. */
436 struct ovn_port *l3dgw_port;
437 /* The "derived" OVN port representing the instance of l3dgw_port on
438 * the "redirect-chassis". */
439 struct ovn_port *l3redirect_port;
440 struct ovn_port *localnet_port;
441 };
442
443 struct macam_node {
444 struct hmap_node hmap_node;
445 struct eth_addr mac_addr; /* Allocated MAC address. */
446 };
447
448 static void
449 cleanup_macam(struct hmap *macam_)
450 {
451 struct macam_node *node;
452 HMAP_FOR_EACH_POP (node, hmap_node, macam_) {
453 free(node);
454 }
455 }
456
457 static struct ovn_datapath *
458 ovn_datapath_create(struct hmap *datapaths, const struct uuid *key,
459 const struct nbrec_logical_switch *nbs,
460 const struct nbrec_logical_router *nbr,
461 const struct sbrec_datapath_binding *sb)
462 {
463 struct ovn_datapath *od = xzalloc(sizeof *od);
464 od->key = *key;
465 od->sb = sb;
466 od->nbs = nbs;
467 od->nbr = nbr;
468 hmap_init(&od->port_tnlids);
469 od->port_key_hint = 0;
470 hmap_insert(datapaths, &od->key_node, uuid_hash(&od->key));
471 return od;
472 }
473
474 static void
475 ovn_datapath_destroy(struct hmap *datapaths, struct ovn_datapath *od)
476 {
477 if (od) {
478 /* Don't remove od->list. It is used within build_datapaths() as a
479 * private list and once we've exited that function it is not safe to
480 * use it. */
481 hmap_remove(datapaths, &od->key_node);
482 destroy_tnlids(&od->port_tnlids);
483 if (od->ipam_info) {
484 bitmap_free(od->ipam_info->allocated_ipv4s);
485 free(od->ipam_info);
486 }
487 free(od->router_ports);
488 free(od);
489 }
490 }
491
492 /* Returns 'od''s datapath type. */
493 static enum ovn_datapath_type
494 ovn_datapath_get_type(const struct ovn_datapath *od)
495 {
496 return od->nbs ? DP_SWITCH : DP_ROUTER;
497 }
498
499 static struct ovn_datapath *
500 ovn_datapath_find(struct hmap *datapaths, const struct uuid *uuid)
501 {
502 struct ovn_datapath *od;
503
504 HMAP_FOR_EACH_WITH_HASH (od, key_node, uuid_hash(uuid), datapaths) {
505 if (uuid_equals(uuid, &od->key)) {
506 return od;
507 }
508 }
509 return NULL;
510 }
511
512 static struct ovn_datapath *
513 ovn_datapath_from_sbrec(struct hmap *datapaths,
514 const struct sbrec_datapath_binding *sb)
515 {
516 struct uuid key;
517
518 if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key) &&
519 !smap_get_uuid(&sb->external_ids, "logical-router", &key)) {
520 return NULL;
521 }
522 return ovn_datapath_find(datapaths, &key);
523 }
524
525 static bool
526 lrouter_is_enabled(const struct nbrec_logical_router *lrouter)
527 {
528 return !lrouter->enabled || *lrouter->enabled;
529 }
530
531 static void
532 init_ipam_info_for_datapath(struct ovn_datapath *od)
533 {
534 if (!od->nbs) {
535 return;
536 }
537
538 const char *subnet_str = smap_get(&od->nbs->other_config, "subnet");
539 const char *ipv6_prefix = smap_get(&od->nbs->other_config, "ipv6_prefix");
540
541 if (ipv6_prefix) {
542 od->ipam_info = xzalloc(sizeof *od->ipam_info);
543 od->ipam_info->ipv6_prefix_set = ipv6_parse(
544 ipv6_prefix, &od->ipam_info->ipv6_prefix);
545 }
546
547 if (!subnet_str) {
548 return;
549 }
550
551 ovs_be32 subnet, mask;
552 char *error = ip_parse_masked(subnet_str, &subnet, &mask);
553 if (error || mask == OVS_BE32_MAX || !ip_is_cidr(mask)) {
554 static struct vlog_rate_limit rl
555 = VLOG_RATE_LIMIT_INIT(5, 1);
556 VLOG_WARN_RL(&rl, "bad 'subnet' %s", subnet_str);
557 free(error);
558 return;
559 }
560
561 if (!od->ipam_info) {
562 od->ipam_info = xzalloc(sizeof *od->ipam_info);
563 }
564 od->ipam_info->start_ipv4 = ntohl(subnet) + 1;
565 od->ipam_info->total_ipv4s = ~ntohl(mask);
566 od->ipam_info->allocated_ipv4s =
567 bitmap_allocate(od->ipam_info->total_ipv4s);
568
569 /* Mark first IP as taken */
570 bitmap_set1(od->ipam_info->allocated_ipv4s, 0);
571
572 /* Check if there are any reserver IPs (list) to be excluded from IPAM */
573 const char *exclude_ip_list = smap_get(&od->nbs->other_config,
574 "exclude_ips");
575 if (!exclude_ip_list) {
576 return;
577 }
578
579 struct lexer lexer;
580 lexer_init(&lexer, exclude_ip_list);
581 /* exclude_ip_list could be in the format -
582 * "10.0.0.4 10.0.0.10 10.0.0.20..10.0.0.50 10.0.0.100..10.0.0.110".
583 */
584 lexer_get(&lexer);
585 while (lexer.token.type != LEX_T_END) {
586 if (lexer.token.type != LEX_T_INTEGER) {
587 lexer_syntax_error(&lexer, "expecting address");
588 break;
589 }
590 uint32_t start = ntohl(lexer.token.value.ipv4);
591 lexer_get(&lexer);
592
593 uint32_t end = start + 1;
594 if (lexer_match(&lexer, LEX_T_ELLIPSIS)) {
595 if (lexer.token.type != LEX_T_INTEGER) {
596 lexer_syntax_error(&lexer, "expecting address range");
597 break;
598 }
599 end = ntohl(lexer.token.value.ipv4) + 1;
600 lexer_get(&lexer);
601 }
602
603 /* Clamp start...end to fit the subnet. */
604 start = MAX(od->ipam_info->start_ipv4, start);
605 end = MIN(od->ipam_info->start_ipv4 + od->ipam_info->total_ipv4s, end);
606 if (end > start) {
607 bitmap_set_multiple(od->ipam_info->allocated_ipv4s,
608 start - od->ipam_info->start_ipv4,
609 end - start, 1);
610 } else {
611 lexer_error(&lexer, "excluded addresses not in subnet");
612 }
613 }
614 if (lexer.error) {
615 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
616 VLOG_WARN_RL(&rl, "logical switch "UUID_FMT": bad exclude_ips (%s)",
617 UUID_ARGS(&od->key), lexer.error);
618 }
619 lexer_destroy(&lexer);
620 }
621
622 static void
623 ovn_datapath_update_external_ids(struct ovn_datapath *od)
624 {
625 /* Get the logical-switch or logical-router UUID to set in
626 * external-ids. */
627 char uuid_s[UUID_LEN + 1];
628 sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->key));
629 const char *key = od->nbs ? "logical-switch" : "logical-router";
630
631 /* Get names to set in external-ids. */
632 const char *name = od->nbs ? od->nbs->name : od->nbr->name;
633 const char *name2 = (od->nbs
634 ? smap_get(&od->nbs->external_ids,
635 "neutron:network_name")
636 : smap_get(&od->nbr->external_ids,
637 "neutron:router_name"));
638
639 /* Set external-ids. */
640 struct smap ids = SMAP_INITIALIZER(&ids);
641 smap_add(&ids, key, uuid_s);
642 smap_add(&ids, "name", name);
643 if (name2 && name2[0]) {
644 smap_add(&ids, "name2", name2);
645 }
646 sbrec_datapath_binding_set_external_ids(od->sb, &ids);
647 smap_destroy(&ids);
648 }
649
650 static void
651 join_datapaths(struct northd_context *ctx, struct hmap *datapaths,
652 struct ovs_list *sb_only, struct ovs_list *nb_only,
653 struct ovs_list *both)
654 {
655 hmap_init(datapaths);
656 ovs_list_init(sb_only);
657 ovs_list_init(nb_only);
658 ovs_list_init(both);
659
660 const struct sbrec_datapath_binding *sb, *sb_next;
661 SBREC_DATAPATH_BINDING_FOR_EACH_SAFE (sb, sb_next, ctx->ovnsb_idl) {
662 struct uuid key;
663 if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key) &&
664 !smap_get_uuid(&sb->external_ids, "logical-router", &key)) {
665 ovsdb_idl_txn_add_comment(
666 ctx->ovnsb_txn,
667 "deleting Datapath_Binding "UUID_FMT" that lacks "
668 "external-ids:logical-switch and "
669 "external-ids:logical-router",
670 UUID_ARGS(&sb->header_.uuid));
671 sbrec_datapath_binding_delete(sb);
672 continue;
673 }
674
675 if (ovn_datapath_find(datapaths, &key)) {
676 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
677 VLOG_INFO_RL(
678 &rl, "deleting Datapath_Binding "UUID_FMT" with "
679 "duplicate external-ids:logical-switch/router "UUID_FMT,
680 UUID_ARGS(&sb->header_.uuid), UUID_ARGS(&key));
681 sbrec_datapath_binding_delete(sb);
682 continue;
683 }
684
685 struct ovn_datapath *od = ovn_datapath_create(datapaths, &key,
686 NULL, NULL, sb);
687 ovs_list_push_back(sb_only, &od->list);
688 }
689
690 const struct nbrec_logical_switch *nbs;
691 NBREC_LOGICAL_SWITCH_FOR_EACH (nbs, ctx->ovnnb_idl) {
692 struct ovn_datapath *od = ovn_datapath_find(datapaths,
693 &nbs->header_.uuid);
694 if (od) {
695 od->nbs = nbs;
696 ovs_list_remove(&od->list);
697 ovs_list_push_back(both, &od->list);
698 ovn_datapath_update_external_ids(od);
699 } else {
700 od = ovn_datapath_create(datapaths, &nbs->header_.uuid,
701 nbs, NULL, NULL);
702 ovs_list_push_back(nb_only, &od->list);
703 }
704
705 init_ipam_info_for_datapath(od);
706 }
707
708 const struct nbrec_logical_router *nbr;
709 NBREC_LOGICAL_ROUTER_FOR_EACH (nbr, ctx->ovnnb_idl) {
710 if (!lrouter_is_enabled(nbr)) {
711 continue;
712 }
713
714 struct ovn_datapath *od = ovn_datapath_find(datapaths,
715 &nbr->header_.uuid);
716 if (od) {
717 if (!od->nbs) {
718 od->nbr = nbr;
719 ovs_list_remove(&od->list);
720 ovs_list_push_back(both, &od->list);
721 ovn_datapath_update_external_ids(od);
722 } else {
723 /* Can't happen! */
724 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
725 VLOG_WARN_RL(&rl,
726 "duplicate UUID "UUID_FMT" in OVN_Northbound",
727 UUID_ARGS(&nbr->header_.uuid));
728 continue;
729 }
730 } else {
731 od = ovn_datapath_create(datapaths, &nbr->header_.uuid,
732 NULL, nbr, NULL);
733 ovs_list_push_back(nb_only, &od->list);
734 }
735 }
736 }
737
738 static uint32_t
739 ovn_datapath_allocate_key(struct hmap *dp_tnlids)
740 {
741 static uint32_t hint;
742 return allocate_tnlid(dp_tnlids, "datapath", (1u << 24) - 1, &hint);
743 }
744
745 /* Updates the southbound Datapath_Binding table so that it contains the
746 * logical switches and routers specified by the northbound database.
747 *
748 * Initializes 'datapaths' to contain a "struct ovn_datapath" for every logical
749 * switch and router. */
750 static void
751 build_datapaths(struct northd_context *ctx, struct hmap *datapaths)
752 {
753 struct ovs_list sb_only, nb_only, both;
754
755 join_datapaths(ctx, datapaths, &sb_only, &nb_only, &both);
756
757 if (!ovs_list_is_empty(&nb_only)) {
758 /* First index the in-use datapath tunnel IDs. */
759 struct hmap dp_tnlids = HMAP_INITIALIZER(&dp_tnlids);
760 struct ovn_datapath *od;
761 LIST_FOR_EACH (od, list, &both) {
762 add_tnlid(&dp_tnlids, od->sb->tunnel_key);
763 }
764
765 /* Add southbound record for each unmatched northbound record. */
766 LIST_FOR_EACH (od, list, &nb_only) {
767 uint16_t tunnel_key = ovn_datapath_allocate_key(&dp_tnlids);
768 if (!tunnel_key) {
769 break;
770 }
771
772 od->sb = sbrec_datapath_binding_insert(ctx->ovnsb_txn);
773 ovn_datapath_update_external_ids(od);
774 sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key);
775 }
776 destroy_tnlids(&dp_tnlids);
777 }
778
779 /* Delete southbound records without northbound matches. */
780 struct ovn_datapath *od, *next;
781 LIST_FOR_EACH_SAFE (od, next, list, &sb_only) {
782 ovs_list_remove(&od->list);
783 sbrec_datapath_binding_delete(od->sb);
784 ovn_datapath_destroy(datapaths, od);
785 }
786 }
787 \f
788 struct ovn_port {
789 struct hmap_node key_node; /* Index on 'key'. */
790 char *key; /* nbs->name, nbr->name, sb->logical_port. */
791 char *json_key; /* 'key', quoted for use in JSON. */
792
793 const struct sbrec_port_binding *sb; /* May be NULL. */
794
795 /* Logical switch port data. */
796 const struct nbrec_logical_switch_port *nbsp; /* May be NULL. */
797
798 struct lport_addresses *lsp_addrs; /* Logical switch port addresses. */
799 unsigned int n_lsp_addrs;
800
801 struct lport_addresses *ps_addrs; /* Port security addresses. */
802 unsigned int n_ps_addrs;
803
804 /* Logical router port data. */
805 const struct nbrec_logical_router_port *nbrp; /* May be NULL. */
806
807 struct lport_addresses lrp_networks;
808
809 bool derived; /* Indicates whether this is an additional port
810 * derived from nbsp or nbrp. */
811
812 /* The port's peer:
813 *
814 * - A switch port S of type "router" has a router port R as a peer,
815 * and R in turn has S has its peer.
816 *
817 * - Two connected logical router ports have each other as peer. */
818 struct ovn_port *peer;
819
820 struct ovn_datapath *od;
821
822 struct ovs_list list; /* In list of similar records. */
823 };
824
825 static struct ovn_port *
826 ovn_port_create(struct hmap *ports, const char *key,
827 const struct nbrec_logical_switch_port *nbsp,
828 const struct nbrec_logical_router_port *nbrp,
829 const struct sbrec_port_binding *sb)
830 {
831 struct ovn_port *op = xzalloc(sizeof *op);
832
833 struct ds json_key = DS_EMPTY_INITIALIZER;
834 json_string_escape(key, &json_key);
835 op->json_key = ds_steal_cstr(&json_key);
836
837 op->key = xstrdup(key);
838 op->sb = sb;
839 op->nbsp = nbsp;
840 op->nbrp = nbrp;
841 op->derived = false;
842 hmap_insert(ports, &op->key_node, hash_string(op->key, 0));
843 return op;
844 }
845
846 static void
847 ovn_port_destroy(struct hmap *ports, struct ovn_port *port)
848 {
849 if (port) {
850 /* Don't remove port->list. It is used within build_ports() as a
851 * private list and once we've exited that function it is not safe to
852 * use it. */
853 hmap_remove(ports, &port->key_node);
854
855 for (int i = 0; i < port->n_lsp_addrs; i++) {
856 destroy_lport_addresses(&port->lsp_addrs[i]);
857 }
858 free(port->lsp_addrs);
859
860 for (int i = 0; i < port->n_ps_addrs; i++) {
861 destroy_lport_addresses(&port->ps_addrs[i]);
862 }
863 free(port->ps_addrs);
864
865 destroy_lport_addresses(&port->lrp_networks);
866 free(port->json_key);
867 free(port->key);
868 free(port);
869 }
870 }
871
872 static struct ovn_port *
873 ovn_port_find(struct hmap *ports, const char *name)
874 {
875 struct ovn_port *op;
876
877 HMAP_FOR_EACH_WITH_HASH (op, key_node, hash_string(name, 0), ports) {
878 if (!strcmp(op->key, name)) {
879 return op;
880 }
881 }
882 return NULL;
883 }
884
885 static uint32_t
886 ovn_port_allocate_key(struct ovn_datapath *od)
887 {
888 return allocate_tnlid(&od->port_tnlids, "port",
889 (1u << 15) - 1, &od->port_key_hint);
890 }
891
892 static char *
893 chassis_redirect_name(const char *port_name)
894 {
895 return xasprintf("cr-%s", port_name);
896 }
897
898 static bool
899 ipam_is_duplicate_mac(struct eth_addr *ea, uint64_t mac64, bool warn)
900 {
901 struct macam_node *macam_node;
902 HMAP_FOR_EACH_WITH_HASH (macam_node, hmap_node, hash_uint64(mac64),
903 &macam) {
904 if (eth_addr_equals(*ea, macam_node->mac_addr)) {
905 if (warn) {
906 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
907 VLOG_WARN_RL(&rl, "Duplicate MAC set: "ETH_ADDR_FMT,
908 ETH_ADDR_ARGS(macam_node->mac_addr));
909 }
910 return true;
911 }
912 }
913 return false;
914 }
915
916 static void
917 ipam_insert_mac(struct eth_addr *ea, bool check)
918 {
919 if (!ea) {
920 return;
921 }
922
923 uint64_t mac64 = eth_addr_to_uint64(*ea);
924 /* If the new MAC was not assigned by this address management system or
925 * check is true and the new MAC is a duplicate, do not insert it into the
926 * macam hmap. */
927 if (((mac64 ^ MAC_ADDR_PREFIX) >> 24)
928 || (check && ipam_is_duplicate_mac(ea, mac64, true))) {
929 return;
930 }
931
932 struct macam_node *new_macam_node = xmalloc(sizeof *new_macam_node);
933 new_macam_node->mac_addr = *ea;
934 hmap_insert(&macam, &new_macam_node->hmap_node, hash_uint64(mac64));
935 }
936
937 static void
938 ipam_insert_ip(struct ovn_datapath *od, uint32_t ip)
939 {
940 if (!od || !od->ipam_info || !od->ipam_info->allocated_ipv4s) {
941 return;
942 }
943
944 if (ip >= od->ipam_info->start_ipv4 &&
945 ip < (od->ipam_info->start_ipv4 + od->ipam_info->total_ipv4s)) {
946 bitmap_set1(od->ipam_info->allocated_ipv4s,
947 ip - od->ipam_info->start_ipv4);
948 }
949 }
950
951 static void
952 ipam_insert_lsp_addresses(struct ovn_datapath *od, struct ovn_port *op,
953 char *address)
954 {
955 if (!od || !op || !address || !strcmp(address, "unknown")
956 || !strcmp(address, "router") || is_dynamic_lsp_address(address)) {
957 return;
958 }
959
960 struct lport_addresses laddrs;
961 if (!extract_lsp_addresses(address, &laddrs)) {
962 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
963 VLOG_WARN_RL(&rl, "Extract addresses failed.");
964 return;
965 }
966 ipam_insert_mac(&laddrs.ea, true);
967
968 /* IP is only added to IPAM if the switch's subnet option
969 * is set, whereas MAC is always added to MACAM. */
970 if (!od->ipam_info || !od->ipam_info->allocated_ipv4s) {
971 destroy_lport_addresses(&laddrs);
972 return;
973 }
974
975 for (size_t j = 0; j < laddrs.n_ipv4_addrs; j++) {
976 uint32_t ip = ntohl(laddrs.ipv4_addrs[j].addr);
977 ipam_insert_ip(od, ip);
978 }
979
980 destroy_lport_addresses(&laddrs);
981 }
982
983 static void
984 ipam_add_port_addresses(struct ovn_datapath *od, struct ovn_port *op)
985 {
986 if (!od || !op) {
987 return;
988 }
989
990 if (op->nbsp) {
991 /* Add all the port's addresses to address data structures. */
992 for (size_t i = 0; i < op->nbsp->n_addresses; i++) {
993 ipam_insert_lsp_addresses(od, op, op->nbsp->addresses[i]);
994 }
995 if (op->nbsp->dynamic_addresses) {
996 ipam_insert_lsp_addresses(od, op, op->nbsp->dynamic_addresses);
997 }
998 } else if (op->nbrp) {
999 struct lport_addresses lrp_networks;
1000 if (!extract_lrp_networks(op->nbrp, &lrp_networks)) {
1001 static struct vlog_rate_limit rl
1002 = VLOG_RATE_LIMIT_INIT(1, 1);
1003 VLOG_WARN_RL(&rl, "Extract addresses failed.");
1004 return;
1005 }
1006 ipam_insert_mac(&lrp_networks.ea, true);
1007
1008 if (!op->peer || !op->peer->nbsp || !op->peer->od || !op->peer->od->nbs
1009 || !smap_get(&op->peer->od->nbs->other_config, "subnet")) {
1010 destroy_lport_addresses(&lrp_networks);
1011 return;
1012 }
1013
1014 for (size_t i = 0; i < lrp_networks.n_ipv4_addrs; i++) {
1015 uint32_t ip = ntohl(lrp_networks.ipv4_addrs[i].addr);
1016 ipam_insert_ip(op->peer->od, ip);
1017 }
1018
1019 destroy_lport_addresses(&lrp_networks);
1020 }
1021 }
1022
1023 static uint64_t
1024 ipam_get_unused_mac(void)
1025 {
1026 /* Stores the suffix of the most recently ipam-allocated MAC address. */
1027 static uint32_t last_mac;
1028
1029 uint64_t mac64;
1030 struct eth_addr mac;
1031 uint32_t mac_addr_suffix, i;
1032 for (i = 0; i < MAC_ADDR_SPACE - 1; i++) {
1033 /* The tentative MAC's suffix will be in the interval (1, 0xfffffe). */
1034 mac_addr_suffix = ((last_mac + i) % (MAC_ADDR_SPACE - 1)) + 1;
1035 mac64 = MAC_ADDR_PREFIX | mac_addr_suffix;
1036 eth_addr_from_uint64(mac64, &mac);
1037 if (!ipam_is_duplicate_mac(&mac, mac64, false)) {
1038 last_mac = mac_addr_suffix;
1039 break;
1040 }
1041 }
1042
1043 if (i == MAC_ADDR_SPACE) {
1044 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1045 VLOG_WARN_RL(&rl, "MAC address space exhausted.");
1046 mac64 = 0;
1047 }
1048
1049 return mac64;
1050 }
1051
1052 static uint32_t
1053 ipam_get_unused_ip(struct ovn_datapath *od)
1054 {
1055 if (!od || !od->ipam_info || !od->ipam_info->allocated_ipv4s) {
1056 return 0;
1057 }
1058
1059 size_t new_ip_index = bitmap_scan(od->ipam_info->allocated_ipv4s, 0, 0,
1060 od->ipam_info->total_ipv4s - 1);
1061 if (new_ip_index == od->ipam_info->total_ipv4s - 1) {
1062 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
1063 VLOG_WARN_RL( &rl, "Subnet address space has been exhausted.");
1064 return 0;
1065 }
1066
1067 return od->ipam_info->start_ipv4 + new_ip_index;
1068 }
1069
1070 static bool
1071 ipam_allocate_addresses(struct ovn_datapath *od, struct ovn_port *op,
1072 const char *addrspec)
1073 {
1074 if (!op->nbsp || !od->ipam_info) {
1075 return false;
1076 }
1077
1078 /* Get or generate MAC address. */
1079 struct eth_addr mac;
1080 bool dynamic_mac;
1081 int n = 0;
1082 if (ovs_scan(addrspec, ETH_ADDR_SCAN_FMT" dynamic%n",
1083 ETH_ADDR_SCAN_ARGS(mac), &n)
1084 && addrspec[n] == '\0') {
1085 dynamic_mac = false;
1086 } else {
1087 uint64_t mac64 = ipam_get_unused_mac();
1088 if (!mac64) {
1089 return false;
1090 }
1091 eth_addr_from_uint64(mac64, &mac);
1092 dynamic_mac = true;
1093 }
1094
1095 /* Generate IPv4 address, if desirable. */
1096 bool dynamic_ip4 = od->ipam_info->allocated_ipv4s != NULL;
1097 uint32_t ip4 = dynamic_ip4 ? ipam_get_unused_ip(od) : 0;
1098
1099 /* Generate IPv6 address, if desirable. */
1100 bool dynamic_ip6 = od->ipam_info->ipv6_prefix_set;
1101 struct in6_addr ip6;
1102 if (dynamic_ip6) {
1103 in6_generate_eui64(mac, &od->ipam_info->ipv6_prefix, &ip6);
1104 }
1105
1106 /* If we didn't generate anything, bail out. */
1107 if (!dynamic_ip4 && !dynamic_ip6) {
1108 return false;
1109 }
1110
1111 /* Save the dynamic addresses. */
1112 struct ds new_addr = DS_EMPTY_INITIALIZER;
1113 ds_put_format(&new_addr, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
1114 if (dynamic_ip4 && ip4) {
1115 ipam_insert_ip(od, ip4);
1116 ds_put_format(&new_addr, " "IP_FMT, IP_ARGS(htonl(ip4)));
1117 }
1118 if (dynamic_ip6) {
1119 char ip6_s[INET6_ADDRSTRLEN + 1];
1120 ipv6_string_mapped(ip6_s, &ip6);
1121 ds_put_format(&new_addr, " %s", ip6_s);
1122 }
1123 ipam_insert_mac(&mac, !dynamic_mac);
1124 nbrec_logical_switch_port_set_dynamic_addresses(op->nbsp,
1125 ds_cstr(&new_addr));
1126 ds_destroy(&new_addr);
1127 return true;
1128 }
1129
1130 static void
1131 build_ipam(struct hmap *datapaths, struct hmap *ports)
1132 {
1133 /* IPAM generally stands for IP address management. In non-virtualized
1134 * world, MAC addresses come with the hardware. But, with virtualized
1135 * workloads, they need to be assigned and managed. This function
1136 * does both IP address management (ipam) and MAC address management
1137 * (macam). */
1138
1139 /* If the switch's other_config:subnet is set, allocate new addresses for
1140 * ports that have the "dynamic" keyword in their addresses column. */
1141 struct ovn_datapath *od;
1142 HMAP_FOR_EACH (od, key_node, datapaths) {
1143 if (!od->nbs || !od->ipam_info) {
1144 continue;
1145 }
1146
1147 struct ovn_port *op;
1148 for (size_t i = 0; i < od->nbs->n_ports; i++) {
1149 const struct nbrec_logical_switch_port *nbsp =
1150 od->nbs->ports[i];
1151
1152 if (!nbsp) {
1153 continue;
1154 }
1155
1156 op = ovn_port_find(ports, nbsp->name);
1157 if (!op || (op->nbsp && op->peer)) {
1158 /* Do not allocate addresses for logical switch ports that
1159 * have a peer. */
1160 continue;
1161 }
1162
1163 for (size_t j = 0; j < nbsp->n_addresses; j++) {
1164 if (is_dynamic_lsp_address(nbsp->addresses[j])
1165 && !nbsp->dynamic_addresses) {
1166 if (!ipam_allocate_addresses(od, op, nbsp->addresses[j])
1167 || !extract_lsp_addresses(nbsp->dynamic_addresses,
1168 &op->lsp_addrs[op->n_lsp_addrs])) {
1169 static struct vlog_rate_limit rl
1170 = VLOG_RATE_LIMIT_INIT(1, 1);
1171 VLOG_INFO_RL(&rl, "Failed to allocate address.");
1172 } else {
1173 op->n_lsp_addrs++;
1174 }
1175 break;
1176 }
1177 }
1178
1179 if (!nbsp->n_addresses && nbsp->dynamic_addresses) {
1180 nbrec_logical_switch_port_set_dynamic_addresses(op->nbsp,
1181 NULL);
1182 }
1183 }
1184 }
1185 }
1186 \f
1187 /* Tag allocation for nested containers.
1188 *
1189 * For a logical switch port with 'parent_name' and a request to allocate tags,
1190 * keeps a track of all allocated tags. */
1191 struct tag_alloc_node {
1192 struct hmap_node hmap_node;
1193 char *parent_name;
1194 unsigned long *allocated_tags; /* A bitmap to track allocated tags. */
1195 };
1196
1197 static void
1198 tag_alloc_destroy(struct hmap *tag_alloc_table)
1199 {
1200 struct tag_alloc_node *node;
1201 HMAP_FOR_EACH_POP (node, hmap_node, tag_alloc_table) {
1202 bitmap_free(node->allocated_tags);
1203 free(node->parent_name);
1204 free(node);
1205 }
1206 hmap_destroy(tag_alloc_table);
1207 }
1208
1209 static struct tag_alloc_node *
1210 tag_alloc_get_node(struct hmap *tag_alloc_table, const char *parent_name)
1211 {
1212 /* If a node for the 'parent_name' exists, return it. */
1213 struct tag_alloc_node *tag_alloc_node;
1214 HMAP_FOR_EACH_WITH_HASH (tag_alloc_node, hmap_node,
1215 hash_string(parent_name, 0),
1216 tag_alloc_table) {
1217 if (!strcmp(tag_alloc_node->parent_name, parent_name)) {
1218 return tag_alloc_node;
1219 }
1220 }
1221
1222 /* Create a new node. */
1223 tag_alloc_node = xmalloc(sizeof *tag_alloc_node);
1224 tag_alloc_node->parent_name = xstrdup(parent_name);
1225 tag_alloc_node->allocated_tags = bitmap_allocate(MAX_OVN_TAGS);
1226 /* Tag 0 is invalid for nested containers. */
1227 bitmap_set1(tag_alloc_node->allocated_tags, 0);
1228 hmap_insert(tag_alloc_table, &tag_alloc_node->hmap_node,
1229 hash_string(parent_name, 0));
1230
1231 return tag_alloc_node;
1232 }
1233
1234 static void
1235 tag_alloc_add_existing_tags(struct hmap *tag_alloc_table,
1236 const struct nbrec_logical_switch_port *nbsp)
1237 {
1238 /* Add the tags of already existing nested containers. If there is no
1239 * 'nbsp->parent_name' or no 'nbsp->tag' set, there is nothing to do. */
1240 if (!nbsp->parent_name || !nbsp->parent_name[0] || !nbsp->tag) {
1241 return;
1242 }
1243
1244 struct tag_alloc_node *tag_alloc_node;
1245 tag_alloc_node = tag_alloc_get_node(tag_alloc_table, nbsp->parent_name);
1246 bitmap_set1(tag_alloc_node->allocated_tags, *nbsp->tag);
1247 }
1248
1249 static void
1250 tag_alloc_create_new_tag(struct hmap *tag_alloc_table,
1251 const struct nbrec_logical_switch_port *nbsp)
1252 {
1253 if (!nbsp->tag_request) {
1254 return;
1255 }
1256
1257 if (nbsp->parent_name && nbsp->parent_name[0]
1258 && *nbsp->tag_request == 0) {
1259 /* For nested containers that need allocation, do the allocation. */
1260
1261 if (nbsp->tag) {
1262 /* This has already been allocated. */
1263 return;
1264 }
1265
1266 struct tag_alloc_node *tag_alloc_node;
1267 int64_t tag;
1268 tag_alloc_node = tag_alloc_get_node(tag_alloc_table,
1269 nbsp->parent_name);
1270 tag = bitmap_scan(tag_alloc_node->allocated_tags, 0, 1, MAX_OVN_TAGS);
1271 if (tag == MAX_OVN_TAGS) {
1272 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1273 VLOG_ERR_RL(&rl, "out of vlans for logical switch ports with "
1274 "parent %s", nbsp->parent_name);
1275 return;
1276 }
1277 bitmap_set1(tag_alloc_node->allocated_tags, tag);
1278 nbrec_logical_switch_port_set_tag(nbsp, &tag, 1);
1279 } else if (*nbsp->tag_request != 0) {
1280 /* For everything else, copy the contents of 'tag_request' to 'tag'. */
1281 nbrec_logical_switch_port_set_tag(nbsp, nbsp->tag_request, 1);
1282 }
1283 }
1284 \f
1285
1286 /*
1287 * This function checks if the MAC in "address" parameter (if present) is
1288 * different from the one stored in Logical_Switch_Port.dynamic_addresses
1289 * and updates it.
1290 */
1291 static void
1292 check_and_update_mac_in_dynamic_addresses(
1293 const char *address,
1294 const struct nbrec_logical_switch_port *nbsp)
1295 {
1296 if (!nbsp->dynamic_addresses) {
1297 return;
1298 }
1299 int buf_index = 0;
1300 struct eth_addr ea;
1301 if (!ovs_scan_len(address, &buf_index,
1302 ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))) {
1303 return;
1304 }
1305
1306 struct eth_addr present_ea;
1307 buf_index = 0;
1308 if (ovs_scan_len(nbsp->dynamic_addresses, &buf_index,
1309 ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(present_ea))
1310 && !eth_addr_equals(ea, present_ea)) {
1311 /* MAC address has changed. Update it */
1312 char *new_addr = xasprintf(
1313 ETH_ADDR_FMT"%s", ETH_ADDR_ARGS(ea),
1314 &nbsp->dynamic_addresses[buf_index]);
1315 nbrec_logical_switch_port_set_dynamic_addresses(
1316 nbsp, new_addr);
1317 free(new_addr);
1318 }
1319 }
1320
1321 static void
1322 join_logical_ports(struct northd_context *ctx,
1323 struct hmap *datapaths, struct hmap *ports,
1324 struct hmap *chassis_qdisc_queues,
1325 struct hmap *tag_alloc_table, struct ovs_list *sb_only,
1326 struct ovs_list *nb_only, struct ovs_list *both)
1327 {
1328 hmap_init(ports);
1329 ovs_list_init(sb_only);
1330 ovs_list_init(nb_only);
1331 ovs_list_init(both);
1332
1333 const struct sbrec_port_binding *sb;
1334 SBREC_PORT_BINDING_FOR_EACH (sb, ctx->ovnsb_idl) {
1335 struct ovn_port *op = ovn_port_create(ports, sb->logical_port,
1336 NULL, NULL, sb);
1337 ovs_list_push_back(sb_only, &op->list);
1338 }
1339
1340 struct ovn_datapath *od;
1341 HMAP_FOR_EACH (od, key_node, datapaths) {
1342 if (od->nbs) {
1343 for (size_t i = 0; i < od->nbs->n_ports; i++) {
1344 const struct nbrec_logical_switch_port *nbsp
1345 = od->nbs->ports[i];
1346 struct ovn_port *op = ovn_port_find(ports, nbsp->name);
1347 if (op) {
1348 if (op->nbsp || op->nbrp) {
1349 static struct vlog_rate_limit rl
1350 = VLOG_RATE_LIMIT_INIT(5, 1);
1351 VLOG_WARN_RL(&rl, "duplicate logical port %s",
1352 nbsp->name);
1353 continue;
1354 }
1355 op->nbsp = nbsp;
1356 ovs_list_remove(&op->list);
1357
1358 uint32_t queue_id = smap_get_int(&op->sb->options,
1359 "qdisc_queue_id", 0);
1360 if (queue_id && op->sb->chassis) {
1361 add_chassis_queue(
1362 chassis_qdisc_queues, &op->sb->chassis->header_.uuid,
1363 queue_id);
1364 }
1365
1366 ovs_list_push_back(both, &op->list);
1367
1368 /* This port exists due to a SB binding, but should
1369 * not have been initialized fully. */
1370 ovs_assert(!op->n_lsp_addrs && !op->n_ps_addrs);
1371 } else {
1372 op = ovn_port_create(ports, nbsp->name, nbsp, NULL, NULL);
1373 ovs_list_push_back(nb_only, &op->list);
1374 }
1375
1376 if (!strcmp(nbsp->type, "localnet")) {
1377 od->localnet_port = op;
1378 }
1379
1380 op->lsp_addrs
1381 = xmalloc(sizeof *op->lsp_addrs * nbsp->n_addresses);
1382 for (size_t j = 0; j < nbsp->n_addresses; j++) {
1383 if (!strcmp(nbsp->addresses[j], "unknown")
1384 || !strcmp(nbsp->addresses[j], "router")) {
1385 continue;
1386 }
1387 if (is_dynamic_lsp_address(nbsp->addresses[j])) {
1388 if (nbsp->dynamic_addresses) {
1389 check_and_update_mac_in_dynamic_addresses(
1390 nbsp->addresses[j], nbsp);
1391 if (!extract_lsp_addresses(nbsp->dynamic_addresses,
1392 &op->lsp_addrs[op->n_lsp_addrs])) {
1393 static struct vlog_rate_limit rl
1394 = VLOG_RATE_LIMIT_INIT(1, 1);
1395 VLOG_INFO_RL(&rl, "invalid syntax '%s' in "
1396 "logical switch port "
1397 "dynamic_addresses. No "
1398 "MAC address found",
1399 op->nbsp->dynamic_addresses);
1400 continue;
1401 }
1402 } else {
1403 continue;
1404 }
1405 } else if (!extract_lsp_addresses(nbsp->addresses[j],
1406 &op->lsp_addrs[op->n_lsp_addrs])) {
1407 static struct vlog_rate_limit rl
1408 = VLOG_RATE_LIMIT_INIT(1, 1);
1409 VLOG_INFO_RL(&rl, "invalid syntax '%s' in logical "
1410 "switch port addresses. No MAC "
1411 "address found",
1412 op->nbsp->addresses[j]);
1413 continue;
1414 }
1415 op->n_lsp_addrs++;
1416 }
1417
1418 op->ps_addrs
1419 = xmalloc(sizeof *op->ps_addrs * nbsp->n_port_security);
1420 for (size_t j = 0; j < nbsp->n_port_security; j++) {
1421 if (!extract_lsp_addresses(nbsp->port_security[j],
1422 &op->ps_addrs[op->n_ps_addrs])) {
1423 static struct vlog_rate_limit rl
1424 = VLOG_RATE_LIMIT_INIT(1, 1);
1425 VLOG_INFO_RL(&rl, "invalid syntax '%s' in port "
1426 "security. No MAC address found",
1427 op->nbsp->port_security[j]);
1428 continue;
1429 }
1430 op->n_ps_addrs++;
1431 }
1432
1433 op->od = od;
1434 ipam_add_port_addresses(od, op);
1435 tag_alloc_add_existing_tags(tag_alloc_table, nbsp);
1436 }
1437 } else {
1438 for (size_t i = 0; i < od->nbr->n_ports; i++) {
1439 const struct nbrec_logical_router_port *nbrp
1440 = od->nbr->ports[i];
1441
1442 struct lport_addresses lrp_networks;
1443 if (!extract_lrp_networks(nbrp, &lrp_networks)) {
1444 static struct vlog_rate_limit rl
1445 = VLOG_RATE_LIMIT_INIT(5, 1);
1446 VLOG_WARN_RL(&rl, "bad 'mac' %s", nbrp->mac);
1447 continue;
1448 }
1449
1450 if (!lrp_networks.n_ipv4_addrs && !lrp_networks.n_ipv6_addrs) {
1451 continue;
1452 }
1453
1454 struct ovn_port *op = ovn_port_find(ports, nbrp->name);
1455 if (op) {
1456 if (op->nbsp || op->nbrp) {
1457 static struct vlog_rate_limit rl
1458 = VLOG_RATE_LIMIT_INIT(5, 1);
1459 VLOG_WARN_RL(&rl, "duplicate logical router port %s",
1460 nbrp->name);
1461 continue;
1462 }
1463 op->nbrp = nbrp;
1464 ovs_list_remove(&op->list);
1465 ovs_list_push_back(both, &op->list);
1466
1467 /* This port exists but should not have been
1468 * initialized fully. */
1469 ovs_assert(!op->lrp_networks.n_ipv4_addrs
1470 && !op->lrp_networks.n_ipv6_addrs);
1471 } else {
1472 op = ovn_port_create(ports, nbrp->name, NULL, nbrp, NULL);
1473 ovs_list_push_back(nb_only, &op->list);
1474 }
1475
1476 op->lrp_networks = lrp_networks;
1477 op->od = od;
1478 ipam_add_port_addresses(op->od, op);
1479
1480 const char *redirect_chassis = smap_get(&op->nbrp->options,
1481 "redirect-chassis");
1482 if (redirect_chassis || op->nbrp->n_gateway_chassis) {
1483 /* Additional "derived" ovn_port crp represents the
1484 * instance of op on the "redirect-chassis". */
1485 const char *gw_chassis = smap_get(&op->od->nbr->options,
1486 "chassis");
1487 if (gw_chassis) {
1488 static struct vlog_rate_limit rl
1489 = VLOG_RATE_LIMIT_INIT(1, 1);
1490 VLOG_WARN_RL(&rl, "Bad configuration: "
1491 "redirect-chassis configured on port %s "
1492 "on L3 gateway router", nbrp->name);
1493 continue;
1494 }
1495 if (od->l3dgw_port || od->l3redirect_port) {
1496 static struct vlog_rate_limit rl
1497 = VLOG_RATE_LIMIT_INIT(1, 1);
1498 VLOG_WARN_RL(&rl, "Bad configuration: multiple ports "
1499 "with redirect-chassis on same logical "
1500 "router %s", od->nbr->name);
1501 continue;
1502 }
1503
1504 char *redirect_name = chassis_redirect_name(nbrp->name);
1505 struct ovn_port *crp = ovn_port_find(ports, redirect_name);
1506 if (crp) {
1507 crp->derived = true;
1508 crp->nbrp = nbrp;
1509 ovs_list_remove(&crp->list);
1510 ovs_list_push_back(both, &crp->list);
1511 } else {
1512 crp = ovn_port_create(ports, redirect_name,
1513 NULL, nbrp, NULL);
1514 crp->derived = true;
1515 ovs_list_push_back(nb_only, &crp->list);
1516 }
1517 crp->od = od;
1518 free(redirect_name);
1519
1520 /* Set l3dgw_port and l3redirect_port in od, for later
1521 * use during flow creation. */
1522 od->l3dgw_port = op;
1523 od->l3redirect_port = crp;
1524 }
1525 }
1526 }
1527 }
1528
1529 /* Connect logical router ports, and logical switch ports of type "router",
1530 * to their peers. */
1531 struct ovn_port *op;
1532 HMAP_FOR_EACH (op, key_node, ports) {
1533 if (op->nbsp && !strcmp(op->nbsp->type, "router") && !op->derived) {
1534 const char *peer_name = smap_get(&op->nbsp->options, "router-port");
1535 if (!peer_name) {
1536 continue;
1537 }
1538
1539 struct ovn_port *peer = ovn_port_find(ports, peer_name);
1540 if (!peer || !peer->nbrp) {
1541 continue;
1542 }
1543
1544 peer->peer = op;
1545 op->peer = peer;
1546 op->od->router_ports = xrealloc(
1547 op->od->router_ports,
1548 sizeof *op->od->router_ports * (op->od->n_router_ports + 1));
1549 op->od->router_ports[op->od->n_router_ports++] = op;
1550
1551 /* Fill op->lsp_addrs for op->nbsp->addresses[] with
1552 * contents "router", which was skipped in the loop above. */
1553 for (size_t j = 0; j < op->nbsp->n_addresses; j++) {
1554 if (!strcmp(op->nbsp->addresses[j], "router")) {
1555 if (extract_lrp_networks(peer->nbrp,
1556 &op->lsp_addrs[op->n_lsp_addrs])) {
1557 op->n_lsp_addrs++;
1558 }
1559 break;
1560 }
1561 }
1562 } else if (op->nbrp && op->nbrp->peer && !op->derived) {
1563 struct ovn_port *peer = ovn_port_find(ports, op->nbrp->peer);
1564 if (peer) {
1565 if (peer->nbrp) {
1566 op->peer = peer;
1567 } else if (peer->nbsp) {
1568 /* An ovn_port for a switch port of type "router" does have
1569 * a router port as its peer (see the case above for
1570 * "router" ports), but this is set via options:router-port
1571 * in Logical_Switch_Port and does not involve the
1572 * Logical_Router_Port's 'peer' column. */
1573 static struct vlog_rate_limit rl =
1574 VLOG_RATE_LIMIT_INIT(5, 1);
1575 VLOG_WARN_RL(&rl, "Bad configuration: The peer of router "
1576 "port %s is a switch port", op->key);
1577 }
1578 }
1579 }
1580 }
1581 }
1582
1583 static void
1584 ip_address_and_port_from_lb_key(const char *key, char **ip_address,
1585 uint16_t *port, int *addr_family);
1586
1587 static void
1588 get_router_load_balancer_ips(const struct ovn_datapath *od,
1589 struct sset *all_ips, int *addr_family)
1590 {
1591 if (!od->nbr) {
1592 return;
1593 }
1594
1595 for (int i = 0; i < od->nbr->n_load_balancer; i++) {
1596 struct nbrec_load_balancer *lb = od->nbr->load_balancer[i];
1597 struct smap *vips = &lb->vips;
1598 struct smap_node *node;
1599
1600 SMAP_FOR_EACH (node, vips) {
1601 /* node->key contains IP:port or just IP. */
1602 char *ip_address = NULL;
1603 uint16_t port;
1604
1605 ip_address_and_port_from_lb_key(node->key, &ip_address, &port,
1606 addr_family);
1607 if (!ip_address) {
1608 continue;
1609 }
1610
1611 if (!sset_contains(all_ips, ip_address)) {
1612 sset_add(all_ips, ip_address);
1613 }
1614
1615 free(ip_address);
1616 }
1617 }
1618 }
1619
1620 /* Returns an array of strings, each consisting of a MAC address followed
1621 * by one or more IP addresses, and if the port is a distributed gateway
1622 * port, followed by 'is_chassis_resident("LPORT_NAME")', where the
1623 * LPORT_NAME is the name of the L3 redirect port or the name of the
1624 * logical_port specified in a NAT rule. These strings include the
1625 * external IP addresses of all NAT rules defined on that router, and all
1626 * of the IP addresses used in load balancer VIPs defined on that router.
1627 *
1628 * The caller must free each of the n returned strings with free(),
1629 * and must free the returned array when it is no longer needed. */
1630 static char **
1631 get_nat_addresses(const struct ovn_port *op, size_t *n)
1632 {
1633 size_t n_nats = 0;
1634 struct eth_addr mac;
1635 if (!op->nbrp || !op->od || !op->od->nbr
1636 || (!op->od->nbr->n_nat && !op->od->nbr->n_load_balancer)
1637 || !eth_addr_from_string(op->nbrp->mac, &mac)) {
1638 *n = n_nats;
1639 return NULL;
1640 }
1641
1642 struct ds c_addresses = DS_EMPTY_INITIALIZER;
1643 ds_put_format(&c_addresses, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
1644 bool central_ip_address = false;
1645
1646 char **addresses;
1647 addresses = xmalloc(sizeof *addresses * (op->od->nbr->n_nat + 1));
1648
1649 /* Get NAT IP addresses. */
1650 for (size_t i = 0; i < op->od->nbr->n_nat; i++) {
1651 const struct nbrec_nat *nat = op->od->nbr->nat[i];
1652 ovs_be32 ip, mask;
1653
1654 char *error = ip_parse_masked(nat->external_ip, &ip, &mask);
1655 if (error || mask != OVS_BE32_MAX) {
1656 free(error);
1657 continue;
1658 }
1659
1660 /* Determine whether this NAT rule satisfies the conditions for
1661 * distributed NAT processing. */
1662 if (op->od->l3redirect_port && !strcmp(nat->type, "dnat_and_snat")
1663 && nat->logical_port && nat->external_mac) {
1664 /* Distributed NAT rule. */
1665 if (eth_addr_from_string(nat->external_mac, &mac)) {
1666 struct ds address = DS_EMPTY_INITIALIZER;
1667 ds_put_format(&address, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
1668 ds_put_format(&address, " %s", nat->external_ip);
1669 ds_put_format(&address, " is_chassis_resident(\"%s\")",
1670 nat->logical_port);
1671 addresses[n_nats++] = ds_steal_cstr(&address);
1672 }
1673 } else {
1674 /* Centralized NAT rule, either on gateway router or distributed
1675 * router. */
1676 ds_put_format(&c_addresses, " %s", nat->external_ip);
1677 central_ip_address = true;
1678 }
1679 }
1680
1681 /* A set to hold all load-balancer vips. */
1682 struct sset all_ips = SSET_INITIALIZER(&all_ips);
1683 int addr_family;
1684 get_router_load_balancer_ips(op->od, &all_ips, &addr_family);
1685
1686 const char *ip_address;
1687 SSET_FOR_EACH (ip_address, &all_ips) {
1688 ds_put_format(&c_addresses, " %s", ip_address);
1689 central_ip_address = true;
1690 }
1691 sset_destroy(&all_ips);
1692
1693 if (central_ip_address) {
1694 /* Gratuitous ARP for centralized NAT rules on distributed gateway
1695 * ports should be restricted to the "redirect-chassis". */
1696 if (op->od->l3redirect_port) {
1697 ds_put_format(&c_addresses, " is_chassis_resident(%s)",
1698 op->od->l3redirect_port->json_key);
1699 }
1700
1701 addresses[n_nats++] = ds_steal_cstr(&c_addresses);
1702 }
1703
1704 *n = n_nats;
1705
1706 return addresses;
1707 }
1708
1709 static bool
1710 gateway_chassis_equal(const struct nbrec_gateway_chassis *nb_gwc,
1711 const struct sbrec_chassis *nb_gwc_c,
1712 const struct sbrec_gateway_chassis *sb_gwc)
1713 {
1714 bool equal = !strcmp(nb_gwc->name, sb_gwc->name)
1715 && nb_gwc->priority == sb_gwc->priority
1716 && smap_equal(&nb_gwc->options, &sb_gwc->options)
1717 && smap_equal(&nb_gwc->external_ids, &sb_gwc->external_ids);
1718
1719 if (!equal) {
1720 return false;
1721 }
1722
1723 /* If everything else matched and we were unable to find the SBDB
1724 * Chassis entry at this time, assume a match and return true.
1725 * This happens when an ovn-controller is restarting and the Chassis
1726 * entry is gone away momentarily */
1727 return !nb_gwc_c
1728 || (sb_gwc->chassis && !strcmp(nb_gwc_c->name,
1729 sb_gwc->chassis->name));
1730 }
1731
1732 static bool
1733 sbpb_gw_chassis_needs_update(
1734 const struct sbrec_port_binding *port_binding,
1735 const struct nbrec_logical_router_port *lrp,
1736 const struct chassis_index *chassis_index)
1737 {
1738 if (!lrp || !port_binding) {
1739 return false;
1740 }
1741
1742 /* These arrays are used to collect valid Gateway_Chassis and valid
1743 * Chassis records from the Logical_Router_Port Gateway_Chassis list,
1744 * we ignore the ones we can't match on the SBDB */
1745 struct nbrec_gateway_chassis **lrp_gwc = xzalloc(lrp->n_gateway_chassis *
1746 sizeof *lrp_gwc);
1747 const struct sbrec_chassis **lrp_gwc_c = xzalloc(lrp->n_gateway_chassis *
1748 sizeof *lrp_gwc_c);
1749
1750 /* Count the number of gateway chassis chassis names from the logical
1751 * router port that we are able to match on the southbound database */
1752 int lrp_n_gateway_chassis = 0;
1753 int n;
1754 for (n = 0; n < lrp->n_gateway_chassis; n++) {
1755
1756 if (!lrp->gateway_chassis[n]->chassis_name) {
1757 continue;
1758 }
1759
1760 const struct sbrec_chassis *chassis =
1761 chassis_lookup_by_name(chassis_index,
1762 lrp->gateway_chassis[n]->chassis_name);
1763
1764 lrp_gwc_c[lrp_n_gateway_chassis] = chassis;
1765 lrp_gwc[lrp_n_gateway_chassis] = lrp->gateway_chassis[n];
1766 lrp_n_gateway_chassis++;
1767 if (!chassis) {
1768 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1769 VLOG_WARN_RL(
1770 &rl, "Chassis name %s referenced in NBDB via Gateway_Chassis "
1771 "on logical router port %s does not exist in SBDB",
1772 lrp->gateway_chassis[n]->chassis_name, lrp->name);
1773 }
1774 }
1775
1776 /* Basic check, different amount of Gateway_Chassis means that we
1777 * need to update southbound database Port_Binding */
1778 if (lrp_n_gateway_chassis != port_binding->n_gateway_chassis) {
1779 free(lrp_gwc_c);
1780 free(lrp_gwc);
1781 return true;
1782 }
1783
1784 for (n = 0; n < lrp_n_gateway_chassis; n++) {
1785 int i;
1786 /* For each of the valid gw chassis on the lrp, check if there's
1787 * a match on the Port_Binding list, we assume order is not
1788 * persisted */
1789 for (i = 0; i < port_binding->n_gateway_chassis; i++) {
1790 if (gateway_chassis_equal(lrp_gwc[n],
1791 lrp_gwc_c[n],
1792 port_binding->gateway_chassis[i])) {
1793 break; /* we found a match */
1794 }
1795 }
1796
1797 /* if no Port_Binding gateway chassis matched for the entry... */
1798 if (i == port_binding->n_gateway_chassis) {
1799 free(lrp_gwc_c);
1800 free(lrp_gwc);
1801 return true; /* found no match for this gateway chassis on lrp */
1802 }
1803 }
1804
1805 /* no need for update, all ports matched */
1806 free(lrp_gwc_c);
1807 free(lrp_gwc);
1808 return false;
1809 }
1810
1811 /* This functions translates the gw chassis on the nb database
1812 * to sb database entries, the only difference is that SB database
1813 * Gateway_Chassis table references the chassis directly instead
1814 * of using the name */
1815 static void
1816 copy_gw_chassis_from_nbrp_to_sbpb(
1817 struct northd_context *ctx,
1818 const struct nbrec_logical_router_port *lrp,
1819 const struct chassis_index *chassis_index,
1820 const struct sbrec_port_binding *port_binding) {
1821
1822 if (!lrp || !port_binding || !lrp->n_gateway_chassis) {
1823 return;
1824 }
1825
1826 struct sbrec_gateway_chassis **gw_chassis = NULL;
1827 int n_gwc = 0;
1828 int n;
1829
1830 /* XXX: This can be improved. This code will generate a set of new
1831 * Gateway_Chassis and push them all in a single transaction, instead
1832 * this would be more optimal if we just add/update/remove the rows in
1833 * the southbound db that need to change. We don't expect lots of
1834 * changes to the Gateway_Chassis table, but if that proves to be wrong
1835 * we should optimize this. */
1836 for (n = 0; n < lrp->n_gateway_chassis; n++) {
1837 struct nbrec_gateway_chassis *lrp_gwc = lrp->gateway_chassis[n];
1838 if (!lrp_gwc->chassis_name) {
1839 continue;
1840 }
1841
1842 const struct sbrec_chassis *chassis =
1843 chassis_lookup_by_name(chassis_index, lrp_gwc->chassis_name);
1844
1845 gw_chassis = xrealloc(gw_chassis, (n_gwc + 1) * sizeof *gw_chassis);
1846
1847 struct sbrec_gateway_chassis *pb_gwc =
1848 sbrec_gateway_chassis_insert(ctx->ovnsb_txn);
1849
1850 sbrec_gateway_chassis_set_name(pb_gwc, lrp_gwc->name);
1851 sbrec_gateway_chassis_set_priority(pb_gwc, lrp_gwc->priority);
1852 sbrec_gateway_chassis_set_chassis(pb_gwc, chassis);
1853 sbrec_gateway_chassis_set_options(pb_gwc, &lrp_gwc->options);
1854 sbrec_gateway_chassis_set_external_ids(pb_gwc, &lrp_gwc->external_ids);
1855
1856 gw_chassis[n_gwc++] = pb_gwc;
1857 }
1858 sbrec_port_binding_set_gateway_chassis(port_binding, gw_chassis, n_gwc);
1859 free(gw_chassis);
1860 }
1861
1862 static void
1863 ovn_port_update_sbrec(struct northd_context *ctx,
1864 const struct ovn_port *op,
1865 const struct chassis_index *chassis_index,
1866 struct hmap *chassis_qdisc_queues)
1867 {
1868 sbrec_port_binding_set_datapath(op->sb, op->od->sb);
1869 if (op->nbrp) {
1870 /* If the router is for l3 gateway, it resides on a chassis
1871 * and its port type is "l3gateway". */
1872 const char *chassis_name = smap_get(&op->od->nbr->options, "chassis");
1873 if (op->derived) {
1874 sbrec_port_binding_set_type(op->sb, "chassisredirect");
1875 } else if (chassis_name) {
1876 sbrec_port_binding_set_type(op->sb, "l3gateway");
1877 } else {
1878 sbrec_port_binding_set_type(op->sb, "patch");
1879 }
1880
1881 struct smap new;
1882 smap_init(&new);
1883 if (op->derived) {
1884 const char *redirect_chassis = smap_get(&op->nbrp->options,
1885 "redirect-chassis");
1886 if (op->nbrp->n_gateway_chassis && redirect_chassis) {
1887 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1888 VLOG_WARN_RL(
1889 &rl, "logical router port %s has both options:"
1890 "redirect-chassis and gateway_chassis populated "
1891 "redirect-chassis will be ignored in favour of "
1892 "gateway chassis", op->nbrp->name);
1893 }
1894
1895 if (op->nbrp->n_gateway_chassis) {
1896 if (sbpb_gw_chassis_needs_update(op->sb, op->nbrp,
1897 chassis_index)) {
1898 copy_gw_chassis_from_nbrp_to_sbpb(ctx, op->nbrp,
1899 chassis_index, op->sb);
1900 }
1901
1902 } else if (redirect_chassis) {
1903 /* Handle ports that had redirect-chassis option attached
1904 * to them, and for backwards compatibility convert them
1905 * to a single Gateway_Chassis entry */
1906 const struct sbrec_chassis *chassis =
1907 chassis_lookup_by_name(chassis_index, redirect_chassis);
1908 if (chassis) {
1909 /* If we found the chassis, and the gw chassis on record
1910 * differs from what we expect go ahead and update */
1911 if (op->sb->n_gateway_chassis != 1
1912 || !op->sb->gateway_chassis[0]->chassis
1913 || strcmp(op->sb->gateway_chassis[0]->chassis->name,
1914 chassis->name)
1915 || op->sb->gateway_chassis[0]->priority != 0) {
1916 /* Construct a single Gateway_Chassis entry on the
1917 * Port_Binding attached to the redirect_chassis
1918 * name */
1919 struct sbrec_gateway_chassis *gw_chassis =
1920 sbrec_gateway_chassis_insert(ctx->ovnsb_txn);
1921
1922 char *gwc_name = xasprintf("%s_%s", op->nbrp->name,
1923 chassis->name);
1924
1925 /* XXX: Again, here, we could just update an existing
1926 * Gateway_Chassis, instead of creating a new one
1927 * and replacing it */
1928 sbrec_gateway_chassis_set_name(gw_chassis, gwc_name);
1929 sbrec_gateway_chassis_set_priority(gw_chassis, 0);
1930 sbrec_gateway_chassis_set_chassis(gw_chassis, chassis);
1931 sbrec_gateway_chassis_set_external_ids(gw_chassis,
1932 &op->nbrp->external_ids);
1933 sbrec_port_binding_set_gateway_chassis(op->sb,
1934 &gw_chassis, 1);
1935 free(gwc_name);
1936 }
1937 } else {
1938 VLOG_WARN("chassis name '%s' from redirect from logical "
1939 " router port '%s' redirect-chassis not found",
1940 redirect_chassis, op->nbrp->name);
1941 if (op->sb->n_gateway_chassis) {
1942 sbrec_port_binding_set_gateway_chassis(op->sb, NULL,
1943 0);
1944 }
1945 }
1946 }
1947 smap_add(&new, "distributed-port", op->nbrp->name);
1948 } else {
1949 if (op->peer) {
1950 smap_add(&new, "peer", op->peer->key);
1951 }
1952 if (chassis_name) {
1953 smap_add(&new, "l3gateway-chassis", chassis_name);
1954 }
1955 }
1956 sbrec_port_binding_set_options(op->sb, &new);
1957 smap_destroy(&new);
1958
1959 sbrec_port_binding_set_parent_port(op->sb, NULL);
1960 sbrec_port_binding_set_tag(op->sb, NULL, 0);
1961
1962 struct ds s = DS_EMPTY_INITIALIZER;
1963 ds_put_cstr(&s, op->nbrp->mac);
1964 for (int i = 0; i < op->nbrp->n_networks; ++i) {
1965 ds_put_format(&s, " %s", op->nbrp->networks[i]);
1966 }
1967 const char *addresses = ds_cstr(&s);
1968 sbrec_port_binding_set_mac(op->sb, &addresses, 1);
1969 ds_destroy(&s);
1970
1971 struct smap ids = SMAP_INITIALIZER(&ids);
1972 sbrec_port_binding_set_external_ids(op->sb, &ids);
1973 } else {
1974 if (strcmp(op->nbsp->type, "router")) {
1975 uint32_t queue_id = smap_get_int(
1976 &op->sb->options, "qdisc_queue_id", 0);
1977 bool has_qos = port_has_qos_params(&op->nbsp->options);
1978 struct smap options;
1979
1980 if (op->sb->chassis && has_qos && !queue_id) {
1981 queue_id = allocate_chassis_queueid(chassis_qdisc_queues,
1982 op->sb->chassis);
1983 } else if (!has_qos && queue_id) {
1984 free_chassis_queueid(chassis_qdisc_queues,
1985 op->sb->chassis,
1986 queue_id);
1987 queue_id = 0;
1988 }
1989
1990 smap_clone(&options, &op->nbsp->options);
1991 if (queue_id) {
1992 smap_add_format(&options,
1993 "qdisc_queue_id", "%d", queue_id);
1994 }
1995 sbrec_port_binding_set_options(op->sb, &options);
1996 smap_destroy(&options);
1997 if (ovn_is_known_nb_lsp_type(op->nbsp->type)) {
1998 sbrec_port_binding_set_type(op->sb, op->nbsp->type);
1999 } else {
2000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2001 VLOG_WARN_RL(
2002 &rl, "Unknown port type '%s' set on logical switch '%s'.",
2003 op->nbsp->type, op->nbsp->name);
2004 }
2005 } else {
2006 const char *chassis = NULL;
2007 if (op->peer && op->peer->od && op->peer->od->nbr) {
2008 chassis = smap_get(&op->peer->od->nbr->options, "chassis");
2009 }
2010
2011 /* A switch port connected to a gateway router is also of
2012 * type "l3gateway". */
2013 if (chassis) {
2014 sbrec_port_binding_set_type(op->sb, "l3gateway");
2015 } else {
2016 sbrec_port_binding_set_type(op->sb, "patch");
2017 }
2018
2019 const char *router_port = smap_get(&op->nbsp->options,
2020 "router-port");
2021 if (router_port || chassis) {
2022 struct smap new;
2023 smap_init(&new);
2024 if (router_port) {
2025 smap_add(&new, "peer", router_port);
2026 }
2027 if (chassis) {
2028 smap_add(&new, "l3gateway-chassis", chassis);
2029 }
2030 sbrec_port_binding_set_options(op->sb, &new);
2031 smap_destroy(&new);
2032 }
2033
2034 const char *nat_addresses = smap_get(&op->nbsp->options,
2035 "nat-addresses");
2036 if (nat_addresses && !strcmp(nat_addresses, "router")) {
2037 if (op->peer && op->peer->od
2038 && (chassis || op->peer->od->l3redirect_port)) {
2039 size_t n_nats;
2040 char **nats = get_nat_addresses(op->peer, &n_nats);
2041 if (n_nats) {
2042 sbrec_port_binding_set_nat_addresses(op->sb,
2043 (const char **) nats, n_nats);
2044 for (size_t i = 0; i < n_nats; i++) {
2045 free(nats[i]);
2046 }
2047 free(nats);
2048 } else {
2049 sbrec_port_binding_set_nat_addresses(op->sb, NULL, 0);
2050 }
2051 } else {
2052 sbrec_port_binding_set_nat_addresses(op->sb, NULL, 0);
2053 }
2054 /* Only accept manual specification of ethernet address
2055 * followed by IPv4 addresses on type "l3gateway" ports. */
2056 } else if (nat_addresses && chassis) {
2057 struct lport_addresses laddrs;
2058 if (!extract_lsp_addresses(nat_addresses, &laddrs)) {
2059 static struct vlog_rate_limit rl =
2060 VLOG_RATE_LIMIT_INIT(1, 1);
2061 VLOG_WARN_RL(&rl, "Error extracting nat-addresses.");
2062 sbrec_port_binding_set_nat_addresses(op->sb, NULL, 0);
2063 } else {
2064 sbrec_port_binding_set_nat_addresses(op->sb,
2065 &nat_addresses, 1);
2066 destroy_lport_addresses(&laddrs);
2067 }
2068 } else {
2069 sbrec_port_binding_set_nat_addresses(op->sb, NULL, 0);
2070 }
2071 }
2072 sbrec_port_binding_set_parent_port(op->sb, op->nbsp->parent_name);
2073 sbrec_port_binding_set_tag(op->sb, op->nbsp->tag, op->nbsp->n_tag);
2074 sbrec_port_binding_set_mac(op->sb, (const char **) op->nbsp->addresses,
2075 op->nbsp->n_addresses);
2076
2077 struct smap ids = SMAP_INITIALIZER(&ids);
2078 smap_clone(&ids, &op->nbsp->external_ids);
2079 const char *name = smap_get(&ids, "neutron:port_name");
2080 if (name && name[0]) {
2081 smap_add(&ids, "name", name);
2082 }
2083 sbrec_port_binding_set_external_ids(op->sb, &ids);
2084 smap_destroy(&ids);
2085 }
2086 }
2087
2088 /* Remove mac_binding entries that refer to logical_ports which are
2089 * deleted. */
2090 static void
2091 cleanup_mac_bindings(struct northd_context *ctx, struct hmap *ports)
2092 {
2093 const struct sbrec_mac_binding *b, *n;
2094 SBREC_MAC_BINDING_FOR_EACH_SAFE (b, n, ctx->ovnsb_idl) {
2095 if (!ovn_port_find(ports, b->logical_port)) {
2096 sbrec_mac_binding_delete(b);
2097 }
2098 }
2099 }
2100
2101 /* Updates the southbound Port_Binding table so that it contains the logical
2102 * switch ports specified by the northbound database.
2103 *
2104 * Initializes 'ports' to contain a "struct ovn_port" for every logical port,
2105 * using the "struct ovn_datapath"s in 'datapaths' to look up logical
2106 * datapaths. */
2107 static void
2108 build_ports(struct northd_context *ctx, struct hmap *datapaths,
2109 const struct chassis_index *chassis_index, struct hmap *ports)
2110 {
2111 struct ovs_list sb_only, nb_only, both;
2112 struct hmap tag_alloc_table = HMAP_INITIALIZER(&tag_alloc_table);
2113 struct hmap chassis_qdisc_queues = HMAP_INITIALIZER(&chassis_qdisc_queues);
2114
2115 join_logical_ports(ctx, datapaths, ports, &chassis_qdisc_queues,
2116 &tag_alloc_table, &sb_only, &nb_only, &both);
2117
2118 struct ovn_port *op, *next;
2119 /* For logical ports that are in both databases, update the southbound
2120 * record based on northbound data. Also index the in-use tunnel_keys.
2121 * For logical ports that are in NB database, do any tag allocation
2122 * needed. */
2123 LIST_FOR_EACH_SAFE (op, next, list, &both) {
2124 if (op->nbsp) {
2125 tag_alloc_create_new_tag(&tag_alloc_table, op->nbsp);
2126 }
2127 ovn_port_update_sbrec(ctx, op, chassis_index, &chassis_qdisc_queues);
2128
2129 add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key);
2130 if (op->sb->tunnel_key > op->od->port_key_hint) {
2131 op->od->port_key_hint = op->sb->tunnel_key;
2132 }
2133 }
2134
2135 /* Add southbound record for each unmatched northbound record. */
2136 LIST_FOR_EACH_SAFE (op, next, list, &nb_only) {
2137 uint16_t tunnel_key = ovn_port_allocate_key(op->od);
2138 if (!tunnel_key) {
2139 continue;
2140 }
2141
2142 op->sb = sbrec_port_binding_insert(ctx->ovnsb_txn);
2143 ovn_port_update_sbrec(ctx, op, chassis_index, &chassis_qdisc_queues);
2144
2145 sbrec_port_binding_set_logical_port(op->sb, op->key);
2146 sbrec_port_binding_set_tunnel_key(op->sb, tunnel_key);
2147 }
2148
2149 bool remove_mac_bindings = false;
2150 if (!ovs_list_is_empty(&sb_only)) {
2151 remove_mac_bindings = true;
2152 }
2153
2154 /* Delete southbound records without northbound matches. */
2155 LIST_FOR_EACH_SAFE(op, next, list, &sb_only) {
2156 ovs_list_remove(&op->list);
2157 sbrec_port_binding_delete(op->sb);
2158 ovn_port_destroy(ports, op);
2159 }
2160 if (remove_mac_bindings) {
2161 cleanup_mac_bindings(ctx, ports);
2162 }
2163
2164 tag_alloc_destroy(&tag_alloc_table);
2165 destroy_chassis_queues(&chassis_qdisc_queues);
2166 }
2167 \f
2168 #define OVN_MIN_MULTICAST 32768
2169 #define OVN_MAX_MULTICAST 65535
2170
2171 struct multicast_group {
2172 const char *name;
2173 uint16_t key; /* OVN_MIN_MULTICAST...OVN_MAX_MULTICAST. */
2174 };
2175
2176 #define MC_FLOOD "_MC_flood"
2177 static const struct multicast_group mc_flood = { MC_FLOOD, 65535 };
2178
2179 #define MC_UNKNOWN "_MC_unknown"
2180 static const struct multicast_group mc_unknown = { MC_UNKNOWN, 65534 };
2181
2182 static bool
2183 multicast_group_equal(const struct multicast_group *a,
2184 const struct multicast_group *b)
2185 {
2186 return !strcmp(a->name, b->name) && a->key == b->key;
2187 }
2188
2189 /* Multicast group entry. */
2190 struct ovn_multicast {
2191 struct hmap_node hmap_node; /* Index on 'datapath' and 'key'. */
2192 struct ovn_datapath *datapath;
2193 const struct multicast_group *group;
2194
2195 struct ovn_port **ports;
2196 size_t n_ports, allocated_ports;
2197 };
2198
2199 static uint32_t
2200 ovn_multicast_hash(const struct ovn_datapath *datapath,
2201 const struct multicast_group *group)
2202 {
2203 return hash_pointer(datapath, group->key);
2204 }
2205
2206 static struct ovn_multicast *
2207 ovn_multicast_find(struct hmap *mcgroups, struct ovn_datapath *datapath,
2208 const struct multicast_group *group)
2209 {
2210 struct ovn_multicast *mc;
2211
2212 HMAP_FOR_EACH_WITH_HASH (mc, hmap_node,
2213 ovn_multicast_hash(datapath, group), mcgroups) {
2214 if (mc->datapath == datapath
2215 && multicast_group_equal(mc->group, group)) {
2216 return mc;
2217 }
2218 }
2219 return NULL;
2220 }
2221
2222 static void
2223 ovn_multicast_add(struct hmap *mcgroups, const struct multicast_group *group,
2224 struct ovn_port *port)
2225 {
2226 struct ovn_datapath *od = port->od;
2227 struct ovn_multicast *mc = ovn_multicast_find(mcgroups, od, group);
2228 if (!mc) {
2229 mc = xmalloc(sizeof *mc);
2230 hmap_insert(mcgroups, &mc->hmap_node, ovn_multicast_hash(od, group));
2231 mc->datapath = od;
2232 mc->group = group;
2233 mc->n_ports = 0;
2234 mc->allocated_ports = 4;
2235 mc->ports = xmalloc(mc->allocated_ports * sizeof *mc->ports);
2236 }
2237 if (mc->n_ports >= mc->allocated_ports) {
2238 mc->ports = x2nrealloc(mc->ports, &mc->allocated_ports,
2239 sizeof *mc->ports);
2240 }
2241 mc->ports[mc->n_ports++] = port;
2242 }
2243
2244 static void
2245 ovn_multicast_destroy(struct hmap *mcgroups, struct ovn_multicast *mc)
2246 {
2247 if (mc) {
2248 hmap_remove(mcgroups, &mc->hmap_node);
2249 free(mc->ports);
2250 free(mc);
2251 }
2252 }
2253
2254 static void
2255 ovn_multicast_update_sbrec(const struct ovn_multicast *mc,
2256 const struct sbrec_multicast_group *sb)
2257 {
2258 struct sbrec_port_binding **ports = xmalloc(mc->n_ports * sizeof *ports);
2259 for (size_t i = 0; i < mc->n_ports; i++) {
2260 ports[i] = CONST_CAST(struct sbrec_port_binding *, mc->ports[i]->sb);
2261 }
2262 sbrec_multicast_group_set_ports(sb, ports, mc->n_ports);
2263 free(ports);
2264 }
2265 \f
2266 /* Logical flow generation.
2267 *
2268 * This code generates the Logical_Flow table in the southbound database, as a
2269 * function of most of the northbound database.
2270 */
2271
2272 struct ovn_lflow {
2273 struct hmap_node hmap_node;
2274
2275 struct ovn_datapath *od;
2276 enum ovn_stage stage;
2277 uint16_t priority;
2278 char *match;
2279 char *actions;
2280 char *stage_hint;
2281 const char *where;
2282 };
2283
2284 static size_t
2285 ovn_lflow_hash(const struct ovn_lflow *lflow)
2286 {
2287 return ovn_logical_flow_hash(&lflow->od->sb->header_.uuid,
2288 ovn_stage_get_table(lflow->stage),
2289 ovn_stage_get_pipeline_name(lflow->stage),
2290 lflow->priority, lflow->match,
2291 lflow->actions);
2292 }
2293
2294 static bool
2295 ovn_lflow_equal(const struct ovn_lflow *a, const struct ovn_lflow *b)
2296 {
2297 return (a->od == b->od
2298 && a->stage == b->stage
2299 && a->priority == b->priority
2300 && !strcmp(a->match, b->match)
2301 && !strcmp(a->actions, b->actions));
2302 }
2303
2304 static void
2305 ovn_lflow_init(struct ovn_lflow *lflow, struct ovn_datapath *od,
2306 enum ovn_stage stage, uint16_t priority,
2307 char *match, char *actions, char *stage_hint,
2308 const char *where)
2309 {
2310 lflow->od = od;
2311 lflow->stage = stage;
2312 lflow->priority = priority;
2313 lflow->match = match;
2314 lflow->actions = actions;
2315 lflow->stage_hint = stage_hint;
2316 lflow->where = where;
2317 }
2318
2319 /* Adds a row with the specified contents to the Logical_Flow table. */
2320 static void
2321 ovn_lflow_add_at(struct hmap *lflow_map, struct ovn_datapath *od,
2322 enum ovn_stage stage, uint16_t priority,
2323 const char *match, const char *actions,
2324 const char *stage_hint, const char *where)
2325 {
2326 ovs_assert(ovn_stage_to_datapath_type(stage) == ovn_datapath_get_type(od));
2327
2328 struct ovn_lflow *lflow = xmalloc(sizeof *lflow);
2329 ovn_lflow_init(lflow, od, stage, priority,
2330 xstrdup(match), xstrdup(actions),
2331 nullable_xstrdup(stage_hint), where);
2332 hmap_insert(lflow_map, &lflow->hmap_node, ovn_lflow_hash(lflow));
2333 }
2334
2335 /* Adds a row with the specified contents to the Logical_Flow table. */
2336 #define ovn_lflow_add_with_hint(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, \
2337 ACTIONS, STAGE_HINT) \
2338 ovn_lflow_add_at(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, ACTIONS, \
2339 STAGE_HINT, OVS_SOURCE_LOCATOR)
2340
2341 #define ovn_lflow_add(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, ACTIONS) \
2342 ovn_lflow_add_with_hint(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, \
2343 ACTIONS, NULL)
2344
2345 static struct ovn_lflow *
2346 ovn_lflow_find(struct hmap *lflows, struct ovn_datapath *od,
2347 enum ovn_stage stage, uint16_t priority,
2348 const char *match, const char *actions, uint32_t hash)
2349 {
2350 struct ovn_lflow target;
2351 ovn_lflow_init(&target, od, stage, priority,
2352 CONST_CAST(char *, match), CONST_CAST(char *, actions),
2353 NULL, NULL);
2354
2355 struct ovn_lflow *lflow;
2356 HMAP_FOR_EACH_WITH_HASH (lflow, hmap_node, hash, lflows) {
2357 if (ovn_lflow_equal(lflow, &target)) {
2358 return lflow;
2359 }
2360 }
2361 return NULL;
2362 }
2363
2364 static void
2365 ovn_lflow_destroy(struct hmap *lflows, struct ovn_lflow *lflow)
2366 {
2367 if (lflow) {
2368 hmap_remove(lflows, &lflow->hmap_node);
2369 free(lflow->match);
2370 free(lflow->actions);
2371 free(lflow->stage_hint);
2372 free(lflow);
2373 }
2374 }
2375
2376 /* Appends port security constraints on L2 address field 'eth_addr_field'
2377 * (e.g. "eth.src" or "eth.dst") to 'match'. 'ps_addrs', with 'n_ps_addrs'
2378 * elements, is the collection of port_security constraints from an
2379 * OVN_NB Logical_Switch_Port row generated by extract_lsp_addresses(). */
2380 static void
2381 build_port_security_l2(const char *eth_addr_field,
2382 struct lport_addresses *ps_addrs,
2383 unsigned int n_ps_addrs,
2384 struct ds *match)
2385 {
2386 if (!n_ps_addrs) {
2387 return;
2388 }
2389
2390 ds_put_format(match, " && %s == {", eth_addr_field);
2391
2392 for (size_t i = 0; i < n_ps_addrs; i++) {
2393 ds_put_format(match, "%s ", ps_addrs[i].ea_s);
2394 }
2395 ds_chomp(match, ' ');
2396 ds_put_cstr(match, "}");
2397 }
2398
2399 static void
2400 build_port_security_ipv6_nd_flow(
2401 struct ds *match, struct eth_addr ea, struct ipv6_netaddr *ipv6_addrs,
2402 int n_ipv6_addrs)
2403 {
2404 ds_put_format(match, " && ip6 && nd && ((nd.sll == "ETH_ADDR_FMT" || "
2405 "nd.sll == "ETH_ADDR_FMT") || ((nd.tll == "ETH_ADDR_FMT" || "
2406 "nd.tll == "ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth_addr_zero),
2407 ETH_ADDR_ARGS(ea), ETH_ADDR_ARGS(eth_addr_zero),
2408 ETH_ADDR_ARGS(ea));
2409 if (!n_ipv6_addrs) {
2410 ds_put_cstr(match, "))");
2411 return;
2412 }
2413
2414 char ip6_str[INET6_ADDRSTRLEN + 1];
2415 struct in6_addr lla;
2416 in6_generate_lla(ea, &lla);
2417 memset(ip6_str, 0, sizeof(ip6_str));
2418 ipv6_string_mapped(ip6_str, &lla);
2419 ds_put_format(match, " && (nd.target == %s", ip6_str);
2420
2421 for(int i = 0; i < n_ipv6_addrs; i++) {
2422 memset(ip6_str, 0, sizeof(ip6_str));
2423 ipv6_string_mapped(ip6_str, &ipv6_addrs[i].addr);
2424 ds_put_format(match, " || nd.target == %s", ip6_str);
2425 }
2426
2427 ds_put_format(match, ")))");
2428 }
2429
2430 static void
2431 build_port_security_ipv6_flow(
2432 enum ovn_pipeline pipeline, struct ds *match, struct eth_addr ea,
2433 struct ipv6_netaddr *ipv6_addrs, int n_ipv6_addrs)
2434 {
2435 char ip6_str[INET6_ADDRSTRLEN + 1];
2436
2437 ds_put_format(match, " && %s == {",
2438 pipeline == P_IN ? "ip6.src" : "ip6.dst");
2439
2440 /* Allow link-local address. */
2441 struct in6_addr lla;
2442 in6_generate_lla(ea, &lla);
2443 ipv6_string_mapped(ip6_str, &lla);
2444 ds_put_format(match, "%s, ", ip6_str);
2445
2446 /* Allow ip6.dst=ff00::/8 for multicast packets */
2447 if (pipeline == P_OUT) {
2448 ds_put_cstr(match, "ff00::/8, ");
2449 }
2450 for(int i = 0; i < n_ipv6_addrs; i++) {
2451 ipv6_string_mapped(ip6_str, &ipv6_addrs[i].addr);
2452 ds_put_format(match, "%s, ", ip6_str);
2453 }
2454 /* Replace ", " by "}". */
2455 ds_chomp(match, ' ');
2456 ds_chomp(match, ',');
2457 ds_put_cstr(match, "}");
2458 }
2459
2460 /**
2461 * Build port security constraints on ARP and IPv6 ND fields
2462 * and add logical flows to S_SWITCH_IN_PORT_SEC_ND stage.
2463 *
2464 * For each port security of the logical port, following
2465 * logical flows are added
2466 * - If the port security has no IP (both IPv4 and IPv6) or
2467 * if it has IPv4 address(es)
2468 * - Priority 90 flow to allow ARP packets for known MAC addresses
2469 * in the eth.src and arp.spa fields. If the port security
2470 * has IPv4 addresses, allow known IPv4 addresses in the arp.tpa field.
2471 *
2472 * - If the port security has no IP (both IPv4 and IPv6) or
2473 * if it has IPv6 address(es)
2474 * - Priority 90 flow to allow IPv6 ND packets for known MAC addresses
2475 * in the eth.src and nd.sll/nd.tll fields. If the port security
2476 * has IPv6 addresses, allow known IPv6 addresses in the nd.target field
2477 * for IPv6 Neighbor Advertisement packet.
2478 *
2479 * - Priority 80 flow to drop ARP and IPv6 ND packets.
2480 */
2481 static void
2482 build_port_security_nd(struct ovn_port *op, struct hmap *lflows)
2483 {
2484 struct ds match = DS_EMPTY_INITIALIZER;
2485
2486 for (size_t i = 0; i < op->n_ps_addrs; i++) {
2487 struct lport_addresses *ps = &op->ps_addrs[i];
2488
2489 bool no_ip = !(ps->n_ipv4_addrs || ps->n_ipv6_addrs);
2490
2491 ds_clear(&match);
2492 if (ps->n_ipv4_addrs || no_ip) {
2493 ds_put_format(&match,
2494 "inport == %s && eth.src == %s && arp.sha == %s",
2495 op->json_key, ps->ea_s, ps->ea_s);
2496
2497 if (ps->n_ipv4_addrs) {
2498 ds_put_cstr(&match, " && arp.spa == {");
2499 for (size_t j = 0; j < ps->n_ipv4_addrs; j++) {
2500 /* When the netmask is applied, if the host portion is
2501 * non-zero, the host can only use the specified
2502 * address in the arp.spa. If zero, the host is allowed
2503 * to use any address in the subnet. */
2504 if (ps->ipv4_addrs[j].plen == 32
2505 || ps->ipv4_addrs[j].addr & ~ps->ipv4_addrs[j].mask) {
2506 ds_put_cstr(&match, ps->ipv4_addrs[j].addr_s);
2507 } else {
2508 ds_put_format(&match, "%s/%d",
2509 ps->ipv4_addrs[j].network_s,
2510 ps->ipv4_addrs[j].plen);
2511 }
2512 ds_put_cstr(&match, ", ");
2513 }
2514 ds_chomp(&match, ' ');
2515 ds_chomp(&match, ',');
2516 ds_put_cstr(&match, "}");
2517 }
2518 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_PORT_SEC_ND, 90,
2519 ds_cstr(&match), "next;");
2520 }
2521
2522 if (ps->n_ipv6_addrs || no_ip) {
2523 ds_clear(&match);
2524 ds_put_format(&match, "inport == %s && eth.src == %s",
2525 op->json_key, ps->ea_s);
2526 build_port_security_ipv6_nd_flow(&match, ps->ea, ps->ipv6_addrs,
2527 ps->n_ipv6_addrs);
2528 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_PORT_SEC_ND, 90,
2529 ds_cstr(&match), "next;");
2530 }
2531 }
2532
2533 ds_clear(&match);
2534 ds_put_format(&match, "inport == %s && (arp || nd)", op->json_key);
2535 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_PORT_SEC_ND, 80,
2536 ds_cstr(&match), "drop;");
2537 ds_destroy(&match);
2538 }
2539
2540 /**
2541 * Build port security constraints on IPv4 and IPv6 src and dst fields
2542 * and add logical flows to S_SWITCH_(IN/OUT)_PORT_SEC_IP stage.
2543 *
2544 * For each port security of the logical port, following
2545 * logical flows are added
2546 * - If the port security has IPv4 addresses,
2547 * - Priority 90 flow to allow IPv4 packets for known IPv4 addresses
2548 *
2549 * - If the port security has IPv6 addresses,
2550 * - Priority 90 flow to allow IPv6 packets for known IPv6 addresses
2551 *
2552 * - If the port security has IPv4 addresses or IPv6 addresses or both
2553 * - Priority 80 flow to drop all IPv4 and IPv6 traffic
2554 */
2555 static void
2556 build_port_security_ip(enum ovn_pipeline pipeline, struct ovn_port *op,
2557 struct hmap *lflows)
2558 {
2559 char *port_direction;
2560 enum ovn_stage stage;
2561 if (pipeline == P_IN) {
2562 port_direction = "inport";
2563 stage = S_SWITCH_IN_PORT_SEC_IP;
2564 } else {
2565 port_direction = "outport";
2566 stage = S_SWITCH_OUT_PORT_SEC_IP;
2567 }
2568
2569 for (size_t i = 0; i < op->n_ps_addrs; i++) {
2570 struct lport_addresses *ps = &op->ps_addrs[i];
2571
2572 if (!(ps->n_ipv4_addrs || ps->n_ipv6_addrs)) {
2573 continue;
2574 }
2575
2576 if (ps->n_ipv4_addrs) {
2577 struct ds match = DS_EMPTY_INITIALIZER;
2578 if (pipeline == P_IN) {
2579 /* Permit use of the unspecified address for DHCP discovery */
2580 struct ds dhcp_match = DS_EMPTY_INITIALIZER;
2581 ds_put_format(&dhcp_match, "inport == %s"
2582 " && eth.src == %s"
2583 " && ip4.src == 0.0.0.0"
2584 " && ip4.dst == 255.255.255.255"
2585 " && udp.src == 68 && udp.dst == 67",
2586 op->json_key, ps->ea_s);
2587 ovn_lflow_add(lflows, op->od, stage, 90,
2588 ds_cstr(&dhcp_match), "next;");
2589 ds_destroy(&dhcp_match);
2590 ds_put_format(&match, "inport == %s && eth.src == %s"
2591 " && ip4.src == {", op->json_key,
2592 ps->ea_s);
2593 } else {
2594 ds_put_format(&match, "outport == %s && eth.dst == %s"
2595 " && ip4.dst == {255.255.255.255, 224.0.0.0/4, ",
2596 op->json_key, ps->ea_s);
2597 }
2598
2599 for (int j = 0; j < ps->n_ipv4_addrs; j++) {
2600 ovs_be32 mask = ps->ipv4_addrs[j].mask;
2601 /* When the netmask is applied, if the host portion is
2602 * non-zero, the host can only use the specified
2603 * address. If zero, the host is allowed to use any
2604 * address in the subnet.
2605 */
2606 if (ps->ipv4_addrs[j].plen == 32
2607 || ps->ipv4_addrs[j].addr & ~mask) {
2608 ds_put_format(&match, "%s", ps->ipv4_addrs[j].addr_s);
2609 if (pipeline == P_OUT && ps->ipv4_addrs[j].plen != 32) {
2610 /* Host is also allowed to receive packets to the
2611 * broadcast address in the specified subnet. */
2612 ds_put_format(&match, ", %s",
2613 ps->ipv4_addrs[j].bcast_s);
2614 }
2615 } else {
2616 /* host portion is zero */
2617 ds_put_format(&match, "%s/%d", ps->ipv4_addrs[j].network_s,
2618 ps->ipv4_addrs[j].plen);
2619 }
2620 ds_put_cstr(&match, ", ");
2621 }
2622
2623 /* Replace ", " by "}". */
2624 ds_chomp(&match, ' ');
2625 ds_chomp(&match, ',');
2626 ds_put_cstr(&match, "}");
2627 ovn_lflow_add(lflows, op->od, stage, 90, ds_cstr(&match), "next;");
2628 ds_destroy(&match);
2629 }
2630
2631 if (ps->n_ipv6_addrs) {
2632 struct ds match = DS_EMPTY_INITIALIZER;
2633 if (pipeline == P_IN) {
2634 /* Permit use of unspecified address for duplicate address
2635 * detection */
2636 struct ds dad_match = DS_EMPTY_INITIALIZER;
2637 ds_put_format(&dad_match, "inport == %s"
2638 " && eth.src == %s"
2639 " && ip6.src == ::"
2640 " && ip6.dst == ff02::/16"
2641 " && icmp6.type == {131, 135, 143}", op->json_key,
2642 ps->ea_s);
2643 ovn_lflow_add(lflows, op->od, stage, 90,
2644 ds_cstr(&dad_match), "next;");
2645 ds_destroy(&dad_match);
2646 }
2647 ds_put_format(&match, "%s == %s && %s == %s",
2648 port_direction, op->json_key,
2649 pipeline == P_IN ? "eth.src" : "eth.dst", ps->ea_s);
2650 build_port_security_ipv6_flow(pipeline, &match, ps->ea,
2651 ps->ipv6_addrs, ps->n_ipv6_addrs);
2652 ovn_lflow_add(lflows, op->od, stage, 90,
2653 ds_cstr(&match), "next;");
2654 ds_destroy(&match);
2655 }
2656
2657 char *match = xasprintf("%s == %s && %s == %s && ip",
2658 port_direction, op->json_key,
2659 pipeline == P_IN ? "eth.src" : "eth.dst",
2660 ps->ea_s);
2661 ovn_lflow_add(lflows, op->od, stage, 80, match, "drop;");
2662 free(match);
2663 }
2664
2665 }
2666
2667 static bool
2668 lsp_is_enabled(const struct nbrec_logical_switch_port *lsp)
2669 {
2670 return !lsp->enabled || *lsp->enabled;
2671 }
2672
2673 static bool
2674 lsp_is_up(const struct nbrec_logical_switch_port *lsp)
2675 {
2676 return !lsp->up || *lsp->up;
2677 }
2678
2679 static bool
2680 build_dhcpv4_action(struct ovn_port *op, ovs_be32 offer_ip,
2681 struct ds *options_action, struct ds *response_action,
2682 struct ds *ipv4_addr_match)
2683 {
2684 if (!op->nbsp->dhcpv4_options) {
2685 /* CMS has disabled native DHCPv4 for this lport. */
2686 return false;
2687 }
2688
2689 ovs_be32 host_ip, mask;
2690 char *error = ip_parse_masked(op->nbsp->dhcpv4_options->cidr, &host_ip,
2691 &mask);
2692 if (error || ((offer_ip ^ host_ip) & mask)) {
2693 /* Either
2694 * - cidr defined is invalid or
2695 * - the offer ip of the logical port doesn't belong to the cidr
2696 * defined in the DHCPv4 options.
2697 * */
2698 free(error);
2699 return false;
2700 }
2701
2702 const char *server_ip = smap_get(
2703 &op->nbsp->dhcpv4_options->options, "server_id");
2704 const char *server_mac = smap_get(
2705 &op->nbsp->dhcpv4_options->options, "server_mac");
2706 const char *lease_time = smap_get(
2707 &op->nbsp->dhcpv4_options->options, "lease_time");
2708
2709 if (!(server_ip && server_mac && lease_time)) {
2710 /* "server_id", "server_mac" and "lease_time" should be
2711 * present in the dhcp_options. */
2712 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2713 VLOG_WARN_RL(&rl, "Required DHCPv4 options not defined for lport - %s",
2714 op->json_key);
2715 return false;
2716 }
2717
2718 struct smap dhcpv4_options = SMAP_INITIALIZER(&dhcpv4_options);
2719 smap_clone(&dhcpv4_options, &op->nbsp->dhcpv4_options->options);
2720
2721 /* server_mac is not DHCPv4 option, delete it from the smap. */
2722 smap_remove(&dhcpv4_options, "server_mac");
2723 char *netmask = xasprintf(IP_FMT, IP_ARGS(mask));
2724 smap_add(&dhcpv4_options, "netmask", netmask);
2725 free(netmask);
2726
2727 ds_put_format(options_action,
2728 REGBIT_DHCP_OPTS_RESULT" = put_dhcp_opts(offerip = "
2729 IP_FMT", ", IP_ARGS(offer_ip));
2730
2731 /* We're not using SMAP_FOR_EACH because we want a consistent order of the
2732 * options on different architectures (big or little endian, SSE4.2) */
2733 const struct smap_node **sorted_opts = smap_sort(&dhcpv4_options);
2734 for (size_t i = 0; i < smap_count(&dhcpv4_options); i++) {
2735 const struct smap_node *node = sorted_opts[i];
2736 ds_put_format(options_action, "%s = %s, ", node->key, node->value);
2737 }
2738 free(sorted_opts);
2739
2740 ds_chomp(options_action, ' ');
2741 ds_chomp(options_action, ',');
2742 ds_put_cstr(options_action, "); next;");
2743
2744 ds_put_format(response_action, "eth.dst = eth.src; eth.src = %s; "
2745 "ip4.dst = "IP_FMT"; ip4.src = %s; udp.src = 67; "
2746 "udp.dst = 68; outport = inport; flags.loopback = 1; "
2747 "output;",
2748 server_mac, IP_ARGS(offer_ip), server_ip);
2749
2750 ds_put_format(ipv4_addr_match,
2751 "ip4.src == "IP_FMT" && ip4.dst == {%s, 255.255.255.255}",
2752 IP_ARGS(offer_ip), server_ip);
2753 smap_destroy(&dhcpv4_options);
2754 return true;
2755 }
2756
2757 static bool
2758 build_dhcpv6_action(struct ovn_port *op, struct in6_addr *offer_ip,
2759 struct ds *options_action, struct ds *response_action)
2760 {
2761 if (!op->nbsp->dhcpv6_options) {
2762 /* CMS has disabled native DHCPv6 for this lport. */
2763 return false;
2764 }
2765
2766 struct in6_addr host_ip, mask;
2767
2768 char *error = ipv6_parse_masked(op->nbsp->dhcpv6_options->cidr, &host_ip,
2769 &mask);
2770 if (error) {
2771 free(error);
2772 return false;
2773 }
2774 struct in6_addr ip6_mask = ipv6_addr_bitxor(offer_ip, &host_ip);
2775 ip6_mask = ipv6_addr_bitand(&ip6_mask, &mask);
2776 if (!ipv6_mask_is_any(&ip6_mask)) {
2777 /* offer_ip doesn't belongs to the cidr defined in lport's DHCPv6
2778 * options.*/
2779 return false;
2780 }
2781
2782 const struct smap *options_map = &op->nbsp->dhcpv6_options->options;
2783 /* "server_id" should be the MAC address. */
2784 const char *server_mac = smap_get(options_map, "server_id");
2785 struct eth_addr ea;
2786 if (!server_mac || !eth_addr_from_string(server_mac, &ea)) {
2787 /* "server_id" should be present in the dhcpv6_options. */
2788 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
2789 VLOG_WARN_RL(&rl, "server_id not present in the DHCPv6 options"
2790 " for lport %s", op->json_key);
2791 return false;
2792 }
2793
2794 /* Get the link local IP of the DHCPv6 server from the server MAC. */
2795 struct in6_addr lla;
2796 in6_generate_lla(ea, &lla);
2797
2798 char server_ip[INET6_ADDRSTRLEN + 1];
2799 ipv6_string_mapped(server_ip, &lla);
2800
2801 char ia_addr[INET6_ADDRSTRLEN + 1];
2802 ipv6_string_mapped(ia_addr, offer_ip);
2803
2804 ds_put_format(options_action,
2805 REGBIT_DHCP_OPTS_RESULT" = put_dhcpv6_opts(");
2806
2807 /* Check whether the dhcpv6 options should be configured as stateful.
2808 * Only reply with ia_addr option for dhcpv6 stateful address mode. */
2809 if (!smap_get_bool(options_map, "dhcpv6_stateless", false)) {
2810 ipv6_string_mapped(ia_addr, offer_ip);
2811 ds_put_format(options_action, "ia_addr = %s, ", ia_addr);
2812 }
2813
2814 /* We're not using SMAP_FOR_EACH because we want a consistent order of the
2815 * options on different architectures (big or little endian, SSE4.2) */
2816 const struct smap_node **sorted_opts = smap_sort(options_map);
2817 for (size_t i = 0; i < smap_count(options_map); i++) {
2818 const struct smap_node *node = sorted_opts[i];
2819 if (strcmp(node->key, "dhcpv6_stateless")) {
2820 ds_put_format(options_action, "%s = %s, ", node->key, node->value);
2821 }
2822 }
2823 free(sorted_opts);
2824
2825 ds_chomp(options_action, ' ');
2826 ds_chomp(options_action, ',');
2827 ds_put_cstr(options_action, "); next;");
2828
2829 ds_put_format(response_action, "eth.dst = eth.src; eth.src = %s; "
2830 "ip6.dst = ip6.src; ip6.src = %s; udp.src = 547; "
2831 "udp.dst = 546; outport = inport; flags.loopback = 1; "
2832 "output;",
2833 server_mac, server_ip);
2834
2835 return true;
2836 }
2837
2838 static bool
2839 has_stateful_acl(struct ovn_datapath *od)
2840 {
2841 for (size_t i = 0; i < od->nbs->n_acls; i++) {
2842 struct nbrec_acl *acl = od->nbs->acls[i];
2843 if (!strcmp(acl->action, "allow-related")) {
2844 return true;
2845 }
2846 }
2847
2848 return false;
2849 }
2850
2851 static void
2852 build_pre_acls(struct ovn_datapath *od, struct hmap *lflows)
2853 {
2854 bool has_stateful = has_stateful_acl(od);
2855
2856 /* Ingress and Egress Pre-ACL Table (Priority 0): Packets are
2857 * allowed by default. */
2858 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_ACL, 0, "1", "next;");
2859 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_ACL, 0, "1", "next;");
2860
2861 /* If there are any stateful ACL rules in this datapath, we must
2862 * send all IP packets through the conntrack action, which handles
2863 * defragmentation, in order to match L4 headers. */
2864 if (has_stateful) {
2865 for (size_t i = 0; i < od->n_router_ports; i++) {
2866 struct ovn_port *op = od->router_ports[i];
2867 /* Can't use ct() for router ports. Consider the
2868 * following configuration: lp1(10.0.0.2) on
2869 * hostA--ls1--lr0--ls2--lp2(10.0.1.2) on hostB, For a
2870 * ping from lp1 to lp2, First, the response will go
2871 * through ct() with a zone for lp2 in the ls2 ingress
2872 * pipeline on hostB. That ct zone knows about this
2873 * connection. Next, it goes through ct() with the zone
2874 * for the router port in the egress pipeline of ls2 on
2875 * hostB. This zone does not know about the connection,
2876 * as the icmp request went through the logical router
2877 * on hostA, not hostB. This would only work with
2878 * distributed conntrack state across all chassis. */
2879 struct ds match_in = DS_EMPTY_INITIALIZER;
2880 struct ds match_out = DS_EMPTY_INITIALIZER;
2881
2882 ds_put_format(&match_in, "ip && inport == %s", op->json_key);
2883 ds_put_format(&match_out, "ip && outport == %s", op->json_key);
2884 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_ACL, 110,
2885 ds_cstr(&match_in), "next;");
2886 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_ACL, 110,
2887 ds_cstr(&match_out), "next;");
2888
2889 ds_destroy(&match_in);
2890 ds_destroy(&match_out);
2891 }
2892 if (od->localnet_port) {
2893 struct ds match_in = DS_EMPTY_INITIALIZER;
2894 struct ds match_out = DS_EMPTY_INITIALIZER;
2895
2896 ds_put_format(&match_in, "ip && inport == %s",
2897 od->localnet_port->json_key);
2898 ds_put_format(&match_out, "ip && outport == %s",
2899 od->localnet_port->json_key);
2900 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_ACL, 110,
2901 ds_cstr(&match_in), "next;");
2902 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_ACL, 110,
2903 ds_cstr(&match_out), "next;");
2904
2905 ds_destroy(&match_in);
2906 ds_destroy(&match_out);
2907 }
2908
2909 /* Ingress and Egress Pre-ACL Table (Priority 110).
2910 *
2911 * Not to do conntrack on ND and ICMP destination
2912 * unreachable packets. */
2913 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_ACL, 110,
2914 "nd || nd_rs || nd_ra || icmp4.type == 3 || "
2915 "icmp6.type == 1 || (tcp && tcp.flags == 4)",
2916 "next;");
2917 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_ACL, 110,
2918 "nd || nd_rs || nd_ra || icmp4.type == 3 || "
2919 "icmp6.type == 1 || (tcp && tcp.flags == 4)",
2920 "next;");
2921
2922 /* Ingress and Egress Pre-ACL Table (Priority 100).
2923 *
2924 * Regardless of whether the ACL is "from-lport" or "to-lport",
2925 * we need rules in both the ingress and egress table, because
2926 * the return traffic needs to be followed.
2927 *
2928 * 'REGBIT_CONNTRACK_DEFRAG' is set to let the pre-stateful table send
2929 * it to conntrack for tracking and defragmentation. */
2930 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_ACL, 100, "ip",
2931 REGBIT_CONNTRACK_DEFRAG" = 1; next;");
2932 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_ACL, 100, "ip",
2933 REGBIT_CONNTRACK_DEFRAG" = 1; next;");
2934 }
2935 }
2936
2937 /* For a 'key' of the form "IP:port" or just "IP", sets 'port' and
2938 * 'ip_address'. The caller must free() the memory allocated for
2939 * 'ip_address'. */
2940 static void
2941 ip_address_and_port_from_lb_key(const char *key, char **ip_address,
2942 uint16_t *port, int *addr_family)
2943 {
2944 struct sockaddr_storage ss;
2945 if (!inet_parse_active(key, 0, &ss)) {
2946 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
2947 VLOG_WARN_RL(&rl, "bad ip address or port for load balancer key %s",
2948 key);
2949 return;
2950 }
2951
2952 struct ds s = DS_EMPTY_INITIALIZER;
2953 ss_format_address_nobracks(&ss, &s);
2954 *ip_address = ds_steal_cstr(&s);
2955
2956 *port = ss_get_port(&ss);
2957
2958 *addr_family = ss.ss_family;
2959 }
2960
2961 /*
2962 * Returns true if logical switch is configured with DNS records, false
2963 * otherwise.
2964 */
2965 static bool
2966 ls_has_dns_records(const struct nbrec_logical_switch *nbs)
2967 {
2968 for (size_t i = 0; i < nbs->n_dns_records; i++) {
2969 if (!smap_is_empty(&nbs->dns_records[i]->records)) {
2970 return true;
2971 }
2972 }
2973
2974 return false;
2975 }
2976
2977 static void
2978 build_pre_lb(struct ovn_datapath *od, struct hmap *lflows)
2979 {
2980 /* Allow all packets to go to next tables by default. */
2981 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_LB, 0, "1", "next;");
2982 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_LB, 0, "1", "next;");
2983
2984 struct sset all_ips = SSET_INITIALIZER(&all_ips);
2985 bool vip_configured = false;
2986 int addr_family = AF_INET;
2987 for (int i = 0; i < od->nbs->n_load_balancer; i++) {
2988 struct nbrec_load_balancer *lb = od->nbs->load_balancer[i];
2989 struct smap *vips = &lb->vips;
2990 struct smap_node *node;
2991
2992 SMAP_FOR_EACH (node, vips) {
2993 vip_configured = true;
2994
2995 /* node->key contains IP:port or just IP. */
2996 char *ip_address = NULL;
2997 uint16_t port;
2998 ip_address_and_port_from_lb_key(node->key, &ip_address, &port,
2999 &addr_family);
3000 if (!ip_address) {
3001 continue;
3002 }
3003
3004 if (!sset_contains(&all_ips, ip_address)) {
3005 sset_add(&all_ips, ip_address);
3006 }
3007
3008 free(ip_address);
3009
3010 /* Ignore L4 port information in the key because fragmented packets
3011 * may not have L4 information. The pre-stateful table will send
3012 * the packet through ct() action to de-fragment. In stateful
3013 * table, we will eventually look at L4 information. */
3014 }
3015 }
3016
3017 /* 'REGBIT_CONNTRACK_DEFRAG' is set to let the pre-stateful table send
3018 * packet to conntrack for defragmentation. */
3019 const char *ip_address;
3020 SSET_FOR_EACH(ip_address, &all_ips) {
3021 char *match;
3022
3023 if (addr_family == AF_INET) {
3024 match = xasprintf("ip && ip4.dst == %s", ip_address);
3025 } else {
3026 match = xasprintf("ip && ip6.dst == %s", ip_address);
3027 }
3028 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_LB,
3029 100, match, REGBIT_CONNTRACK_DEFRAG" = 1; next;");
3030 free(match);
3031 }
3032
3033 sset_destroy(&all_ips);
3034
3035 if (vip_configured) {
3036 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_LB,
3037 100, "ip", REGBIT_CONNTRACK_DEFRAG" = 1; next;");
3038 }
3039 }
3040
3041 static void
3042 build_pre_stateful(struct ovn_datapath *od, struct hmap *lflows)
3043 {
3044 /* Ingress and Egress pre-stateful Table (Priority 0): Packets are
3045 * allowed by default. */
3046 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_STATEFUL, 0, "1", "next;");
3047 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_STATEFUL, 0, "1", "next;");
3048
3049 /* If REGBIT_CONNTRACK_DEFRAG is set as 1, then the packets should be
3050 * sent to conntrack for tracking and defragmentation. */
3051 ovn_lflow_add(lflows, od, S_SWITCH_IN_PRE_STATEFUL, 100,
3052 REGBIT_CONNTRACK_DEFRAG" == 1", "ct_next;");
3053 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PRE_STATEFUL, 100,
3054 REGBIT_CONNTRACK_DEFRAG" == 1", "ct_next;");
3055 }
3056
3057 static void
3058 build_acl_log(struct ds *actions, const struct nbrec_acl *acl)
3059 {
3060 if (!acl->log) {
3061 return;
3062 }
3063
3064 ds_put_cstr(actions, "log(");
3065
3066 if (acl->name) {
3067 ds_put_format(actions, "name=\"%s\", ", acl->name);
3068 }
3069
3070 /* If a severity level isn't specified, default to "info". */
3071 if (acl->severity) {
3072 ds_put_format(actions, "severity=%s, ", acl->severity);
3073 } else {
3074 ds_put_format(actions, "severity=info, ");
3075 }
3076
3077 if (!strcmp(acl->action, "drop")) {
3078 ds_put_cstr(actions, "verdict=drop, ");
3079 } else if (!strcmp(acl->action, "reject")) {
3080 ds_put_cstr(actions, "verdict=reject, ");
3081 } else if (!strcmp(acl->action, "allow")
3082 || !strcmp(acl->action, "allow-related")) {
3083 ds_put_cstr(actions, "verdict=allow, ");
3084 }
3085
3086 ds_chomp(actions, ' ');
3087 ds_chomp(actions, ',');
3088 ds_put_cstr(actions, "); ");
3089 }
3090
3091 static void
3092 build_reject_acl_rules(struct ovn_datapath *od, struct hmap *lflows,
3093 enum ovn_stage stage, struct nbrec_acl *acl,
3094 struct ds *extra_match, struct ds *extra_actions)
3095 {
3096 struct ds match = DS_EMPTY_INITIALIZER;
3097 struct ds actions = DS_EMPTY_INITIALIZER;
3098 bool ingress = (stage == S_SWITCH_IN_ACL);
3099
3100 /* TCP */
3101 build_acl_log(&actions, acl);
3102 if (extra_match->length > 0) {
3103 ds_put_format(&match, "(%s) && ", extra_match->string);
3104 }
3105 ds_put_format(&match, "ip4 && tcp && (%s)", acl->match);
3106 ds_put_format(&actions, "reg0 = 0; "
3107 "eth.dst <-> eth.src; ip4.dst <-> ip4.src; "
3108 "tcp_reset { outport <-> inport; %s };",
3109 ingress ? "output;" : "next(pipeline=ingress,table=0);");
3110 ovn_lflow_add(lflows, od, stage, acl->priority + OVN_ACL_PRI_OFFSET + 10,
3111 ds_cstr(&match), ds_cstr(&actions));
3112 ds_clear(&match);
3113 ds_clear(&actions);
3114 build_acl_log(&actions, acl);
3115 if (extra_match->length > 0) {
3116 ds_put_format(&match, "(%s) && ", extra_match->string);
3117 }
3118 ds_put_format(&match, "ip6 && tcp && (%s)", acl->match);
3119 ds_put_format(&actions, "reg0 = 0; "
3120 "eth.dst <-> eth.src; ip6.dst <-> ip6.src; "
3121 "tcp_reset { outport <-> inport; %s };",
3122 ingress ? "output;" : "next(pipeline=ingress,table=0);");
3123 ovn_lflow_add(lflows, od, stage, acl->priority + OVN_ACL_PRI_OFFSET + 10,
3124 ds_cstr(&match), ds_cstr(&actions));
3125
3126 /* IP traffic */
3127 ds_clear(&match);
3128 ds_clear(&actions);
3129 build_acl_log(&actions, acl);
3130 if (extra_match->length > 0) {
3131 ds_put_format(&match, "(%s) && ", extra_match->string);
3132 }
3133 ds_put_format(&match, "ip4 && (%s)", acl->match);
3134 if (extra_actions->length > 0) {
3135 ds_put_format(&actions, "%s ", extra_actions->string);
3136 }
3137 ds_put_format(&actions, "reg0 = 0; "
3138 "eth.dst <-> eth.src; ip4.dst <-> ip4.src; "
3139 "icmp4 { outport <-> inport; %s };",
3140 ingress ? "output;" : "next(pipeline=ingress,table=0);");
3141 ovn_lflow_add(lflows, od, stage, acl->priority + OVN_ACL_PRI_OFFSET,
3142 ds_cstr(&match), ds_cstr(&actions));
3143 ds_clear(&match);
3144 ds_clear(&actions);
3145 build_acl_log(&actions, acl);
3146 if (extra_match->length > 0) {
3147 ds_put_format(&match, "(%s) && ", extra_match->string);
3148 }
3149 ds_put_format(&match, "ip6 && (%s)", acl->match);
3150 if (extra_actions->length > 0) {
3151 ds_put_format(&actions, "%s ", extra_actions->string);
3152 }
3153 ds_put_format(&actions, "reg0 = 0; icmp6 { "
3154 "eth.dst <-> eth.src; ip6.dst <-> ip6.src; "
3155 "outport <-> inport; %s };",
3156 ingress ? "output;" : "next(pipeline=ingress,table=0);");
3157 ovn_lflow_add(lflows, od, stage, acl->priority + OVN_ACL_PRI_OFFSET,
3158 ds_cstr(&match), ds_cstr(&actions));
3159
3160 ds_destroy(&match);
3161 ds_destroy(&actions);
3162 }
3163
3164 static void
3165 consider_acl(struct hmap *lflows, struct ovn_datapath *od,
3166 struct nbrec_acl *acl, bool has_stateful)
3167 {
3168 bool ingress = !strcmp(acl->direction, "from-lport") ? true :false;
3169 enum ovn_stage stage = ingress ? S_SWITCH_IN_ACL : S_SWITCH_OUT_ACL;
3170
3171 char *stage_hint = xasprintf("%08x", acl->header_.uuid.parts[0]);
3172 if (!strcmp(acl->action, "allow")
3173 || !strcmp(acl->action, "allow-related")) {
3174 /* If there are any stateful flows, we must even commit "allow"
3175 * actions. This is because, while the initiater's
3176 * direction may not have any stateful rules, the server's
3177 * may and then its return traffic would not have an
3178 * associated conntrack entry and would return "+invalid". */
3179 if (!has_stateful) {
3180 struct ds actions = DS_EMPTY_INITIALIZER;
3181 build_acl_log(&actions, acl);
3182 ds_put_cstr(&actions, "next;");
3183 ovn_lflow_add_with_hint(lflows, od, stage,
3184 acl->priority + OVN_ACL_PRI_OFFSET,
3185 acl->match, ds_cstr(&actions),
3186 stage_hint);
3187 ds_destroy(&actions);
3188 } else {
3189 struct ds match = DS_EMPTY_INITIALIZER;
3190 struct ds actions = DS_EMPTY_INITIALIZER;
3191
3192 /* Commit the connection tracking entry if it's a new
3193 * connection that matches this ACL. After this commit,
3194 * the reply traffic is allowed by a flow we create at
3195 * priority 65535, defined earlier.
3196 *
3197 * It's also possible that a known connection was marked for
3198 * deletion after a policy was deleted, but the policy was
3199 * re-added while that connection is still known. We catch
3200 * that case here and un-set ct_label.blocked (which will be done
3201 * by ct_commit in the "stateful" stage) to indicate that the
3202 * connection should be allowed to resume.
3203 */
3204 ds_put_format(&match, "((ct.new && !ct.est)"
3205 " || (!ct.new && ct.est && !ct.rpl "
3206 "&& ct_label.blocked == 1)) "
3207 "&& (%s)", acl->match);
3208 ds_put_cstr(&actions, REGBIT_CONNTRACK_COMMIT" = 1; ");
3209 build_acl_log(&actions, acl);
3210 ds_put_cstr(&actions, "next;");
3211 ovn_lflow_add_with_hint(lflows, od, stage,
3212 acl->priority + OVN_ACL_PRI_OFFSET,
3213 ds_cstr(&match),
3214 ds_cstr(&actions),
3215 stage_hint);
3216
3217 /* Match on traffic in the request direction for an established
3218 * connection tracking entry that has not been marked for
3219 * deletion. There is no need to commit here, so we can just
3220 * proceed to the next table. We use this to ensure that this
3221 * connection is still allowed by the currently defined
3222 * policy. */
3223 ds_clear(&match);
3224 ds_clear(&actions);
3225 ds_put_format(&match,
3226 "!ct.new && ct.est && !ct.rpl"
3227 " && ct_label.blocked == 0 && (%s)",
3228 acl->match);
3229
3230 build_acl_log(&actions, acl);
3231 ds_put_cstr(&actions, "next;");
3232 ovn_lflow_add_with_hint(lflows, od, stage,
3233 acl->priority + OVN_ACL_PRI_OFFSET,
3234 ds_cstr(&match), ds_cstr(&actions),
3235 stage_hint);
3236
3237 ds_destroy(&match);
3238 ds_destroy(&actions);
3239 }
3240 } else if (!strcmp(acl->action, "drop")
3241 || !strcmp(acl->action, "reject")) {
3242 struct ds match = DS_EMPTY_INITIALIZER;
3243 struct ds actions = DS_EMPTY_INITIALIZER;
3244
3245 /* The implementation of "drop" differs if stateful ACLs are in
3246 * use for this datapath. In that case, the actions differ
3247 * depending on whether the connection was previously committed
3248 * to the connection tracker with ct_commit. */
3249 if (has_stateful) {
3250 /* If the packet is not part of an established connection, then
3251 * we can simply reject/drop it. */
3252 ds_put_cstr(&match,
3253 "(!ct.est || (ct.est && ct_label.blocked == 1))");
3254 if (!strcmp(acl->action, "reject")) {
3255 build_reject_acl_rules(od, lflows, stage, acl, &match,
3256 &actions);
3257 } else {
3258 ds_put_format(&match, " && (%s)", acl->match);
3259 build_acl_log(&actions, acl);
3260 ds_put_cstr(&actions, "/* drop */");
3261 ovn_lflow_add(lflows, od, stage,
3262 acl->priority + OVN_ACL_PRI_OFFSET,
3263 ds_cstr(&match), ds_cstr(&actions));
3264 }
3265 /* For an existing connection without ct_label set, we've
3266 * encountered a policy change. ACLs previously allowed
3267 * this connection and we committed the connection tracking
3268 * entry. Current policy says that we should drop this
3269 * connection. First, we set bit 0 of ct_label to indicate
3270 * that this connection is set for deletion. By not
3271 * specifying "next;", we implicitly drop the packet after
3272 * updating conntrack state. We would normally defer
3273 * ct_commit() to the "stateful" stage, but since we're
3274 * rejecting/dropping the packet, we go ahead and do it here.
3275 */
3276 ds_clear(&match);
3277 ds_clear(&actions);
3278 ds_put_cstr(&match, "ct.est && ct_label.blocked == 0");
3279 ds_put_cstr(&actions, "ct_commit(ct_label=1/1); ");
3280 if (!strcmp(acl->action, "reject")) {
3281 build_reject_acl_rules(od, lflows, stage, acl, &match,
3282 &actions);
3283 } else {
3284 ds_put_format(&match, " && (%s)", acl->match);
3285 build_acl_log(&actions, acl);
3286 ds_put_cstr(&actions, "/* drop */");
3287 ovn_lflow_add(lflows, od, stage,
3288 acl->priority + OVN_ACL_PRI_OFFSET,
3289 ds_cstr(&match), ds_cstr(&actions));
3290 }
3291 } else {
3292 /* There are no stateful ACLs in use on this datapath,
3293 * so a "reject/drop" ACL is simply the "reject/drop"
3294 * logical flow action in all cases. */
3295 if (!strcmp(acl->action, "reject")) {
3296 build_reject_acl_rules(od, lflows, stage, acl, &match,
3297 &actions);
3298 } else {
3299 build_acl_log(&actions, acl);
3300 ds_put_cstr(&actions, "/* drop */");
3301 ovn_lflow_add(lflows, od, stage,
3302 acl->priority + OVN_ACL_PRI_OFFSET,
3303 acl->match, ds_cstr(&actions));
3304 }
3305 }
3306 ds_destroy(&match);
3307 ds_destroy(&actions);
3308 }
3309 free(stage_hint);
3310 }
3311
3312 struct ovn_port_group_ls {
3313 struct hmap_node key_node; /* Index on 'key'. */
3314 struct uuid key; /* nb_ls->header_.uuid. */
3315 const struct nbrec_logical_switch *nb_ls;
3316 };
3317
3318 struct ovn_port_group {
3319 struct hmap_node key_node; /* Index on 'key'. */
3320 struct uuid key; /* nb_pg->header_.uuid. */
3321 const struct nbrec_port_group *nb_pg;
3322 struct hmap nb_lswitches; /* NB lswitches related to the port group */
3323 size_t n_acls; /* Number of ACLs applied to the port group */
3324 struct nbrec_acl **acls; /* ACLs applied to the port group */
3325 };
3326
3327 static struct ovn_port_group *
3328 ovn_port_group_create(struct hmap *pgs,
3329 const struct nbrec_port_group *nb_pg)
3330 {
3331 struct ovn_port_group *pg = xzalloc(sizeof *pg);
3332 pg->key = nb_pg->header_.uuid;
3333 pg->nb_pg = nb_pg;
3334 pg->n_acls = nb_pg->n_acls;
3335 pg->acls = nb_pg->acls;
3336 hmap_init(&pg->nb_lswitches);
3337 hmap_insert(pgs, &pg->key_node, uuid_hash(&pg->key));
3338 return pg;
3339 }
3340
3341 static void
3342 ovn_port_group_ls_add(struct ovn_port_group *pg,
3343 const struct nbrec_logical_switch *nb_ls)
3344 {
3345 struct ovn_port_group_ls *pg_ls = xzalloc(sizeof *pg_ls);
3346 pg_ls->key = nb_ls->header_.uuid;
3347 pg_ls->nb_ls = nb_ls;
3348 hmap_insert(&pg->nb_lswitches, &pg_ls->key_node, uuid_hash(&pg_ls->key));
3349 }
3350
3351 static struct ovn_port_group_ls *
3352 ovn_port_group_ls_find(struct ovn_port_group *pg, const struct uuid *ls_uuid)
3353 {
3354 struct ovn_port_group_ls *pg_ls;
3355
3356 HMAP_FOR_EACH_WITH_HASH (pg_ls, key_node, uuid_hash(ls_uuid),
3357 &pg->nb_lswitches) {
3358 if (uuid_equals(ls_uuid, &pg_ls->key)) {
3359 return pg_ls;
3360 }
3361 }
3362 return NULL;
3363 }
3364
3365 static void
3366 ovn_port_group_destroy(struct hmap *pgs, struct ovn_port_group *pg)
3367 {
3368 if (pg) {
3369 hmap_remove(pgs, &pg->key_node);
3370 struct ovn_port_group_ls *ls;
3371 HMAP_FOR_EACH_POP (ls, key_node, &pg->nb_lswitches) {
3372 free(ls);
3373 }
3374 hmap_destroy(&pg->nb_lswitches);
3375 free(pg);
3376 }
3377 }
3378
3379 static void
3380 build_port_group_lswitches(struct northd_context *ctx, struct hmap *pgs,
3381 struct hmap *ports)
3382 {
3383 hmap_init(pgs);
3384
3385 const struct nbrec_port_group *nb_pg;
3386 NBREC_PORT_GROUP_FOR_EACH (nb_pg, ctx->ovnnb_idl) {
3387 struct ovn_port_group *pg = ovn_port_group_create(pgs, nb_pg);
3388 for (size_t i = 0; i < nb_pg->n_ports; i++) {
3389 struct ovn_port *op = ovn_port_find(ports, nb_pg->ports[i]->name);
3390 if (!op) {
3391 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3392 VLOG_ERR_RL(&rl, "lport %s in port group %s not found.",
3393 nb_pg->ports[i]->name,
3394 nb_pg->name);
3395 continue;
3396 }
3397
3398 if (!op->od->nbs) {
3399 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3400 VLOG_WARN_RL(&rl, "lport %s in port group %s has no lswitch.",
3401 nb_pg->ports[i]->name,
3402 nb_pg->name);
3403 continue;
3404 }
3405
3406 struct ovn_port_group_ls *pg_ls =
3407 ovn_port_group_ls_find(pg, &op->od->nbs->header_.uuid);
3408 if (!pg_ls) {
3409 ovn_port_group_ls_add(pg, op->od->nbs);
3410 }
3411 }
3412 }
3413 }
3414
3415 static void
3416 build_acls(struct ovn_datapath *od, struct hmap *lflows,
3417 struct hmap *port_groups)
3418 {
3419 bool has_stateful = has_stateful_acl(od);
3420
3421 /* Ingress and Egress ACL Table (Priority 0): Packets are allowed by
3422 * default. A related rule at priority 1 is added below if there
3423 * are any stateful ACLs in this datapath. */
3424 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, 0, "1", "next;");
3425 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, 0, "1", "next;");
3426
3427 if (has_stateful) {
3428 /* Ingress and Egress ACL Table (Priority 1).
3429 *
3430 * By default, traffic is allowed. This is partially handled by
3431 * the Priority 0 ACL flows added earlier, but we also need to
3432 * commit IP flows. This is because, while the initiater's
3433 * direction may not have any stateful rules, the server's may
3434 * and then its return traffic would not have an associated
3435 * conntrack entry and would return "+invalid".
3436 *
3437 * We use "ct_commit" for a connection that is not already known
3438 * by the connection tracker. Once a connection is committed,
3439 * subsequent packets will hit the flow at priority 0 that just
3440 * uses "next;"
3441 *
3442 * We also check for established connections that have ct_label.blocked
3443 * set on them. That's a connection that was disallowed, but is
3444 * now allowed by policy again since it hit this default-allow flow.
3445 * We need to set ct_label.blocked=0 to let the connection continue,
3446 * which will be done by ct_commit() in the "stateful" stage.
3447 * Subsequent packets will hit the flow at priority 0 that just
3448 * uses "next;". */
3449 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, 1,
3450 "ip && (!ct.est || (ct.est && ct_label.blocked == 1))",
3451 REGBIT_CONNTRACK_COMMIT" = 1; next;");
3452 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, 1,
3453 "ip && (!ct.est || (ct.est && ct_label.blocked == 1))",
3454 REGBIT_CONNTRACK_COMMIT" = 1; next;");
3455
3456 /* Ingress and Egress ACL Table (Priority 65535).
3457 *
3458 * Always drop traffic that's in an invalid state. Also drop
3459 * reply direction packets for connections that have been marked
3460 * for deletion (bit 0 of ct_label is set).
3461 *
3462 * This is enforced at a higher priority than ACLs can be defined. */
3463 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, UINT16_MAX,
3464 "ct.inv || (ct.est && ct.rpl && ct_label.blocked == 1)",
3465 "drop;");
3466 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, UINT16_MAX,
3467 "ct.inv || (ct.est && ct.rpl && ct_label.blocked == 1)",
3468 "drop;");
3469
3470 /* Ingress and Egress ACL Table (Priority 65535).
3471 *
3472 * Allow reply traffic that is part of an established
3473 * conntrack entry that has not been marked for deletion
3474 * (bit 0 of ct_label). We only match traffic in the
3475 * reply direction because we want traffic in the request
3476 * direction to hit the currently defined policy from ACLs.
3477 *
3478 * This is enforced at a higher priority than ACLs can be defined. */
3479 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, UINT16_MAX,
3480 "ct.est && !ct.rel && !ct.new && !ct.inv "
3481 "&& ct.rpl && ct_label.blocked == 0",
3482 "next;");
3483 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, UINT16_MAX,
3484 "ct.est && !ct.rel && !ct.new && !ct.inv "
3485 "&& ct.rpl && ct_label.blocked == 0",
3486 "next;");
3487
3488 /* Ingress and Egress ACL Table (Priority 65535).
3489 *
3490 * Allow traffic that is related to an existing conntrack entry that
3491 * has not been marked for deletion (bit 0 of ct_label).
3492 *
3493 * This is enforced at a higher priority than ACLs can be defined.
3494 *
3495 * NOTE: This does not support related data sessions (eg,
3496 * a dynamically negotiated FTP data channel), but will allow
3497 * related traffic such as an ICMP Port Unreachable through
3498 * that's generated from a non-listening UDP port. */
3499 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, UINT16_MAX,
3500 "!ct.est && ct.rel && !ct.new && !ct.inv "
3501 "&& ct_label.blocked == 0",
3502 "next;");
3503 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, UINT16_MAX,
3504 "!ct.est && ct.rel && !ct.new && !ct.inv "
3505 "&& ct_label.blocked == 0",
3506 "next;");
3507
3508 /* Ingress and Egress ACL Table (Priority 65535).
3509 *
3510 * Not to do conntrack on ND packets. */
3511 ovn_lflow_add(lflows, od, S_SWITCH_IN_ACL, UINT16_MAX, "nd", "next;");
3512 ovn_lflow_add(lflows, od, S_SWITCH_OUT_ACL, UINT16_MAX, "nd", "next;");
3513 }
3514
3515 /* Ingress or Egress ACL Table (Various priorities). */
3516 for (size_t i = 0; i < od->nbs->n_acls; i++) {
3517 struct nbrec_acl *acl = od->nbs->acls[i];
3518 consider_acl(lflows, od, acl, has_stateful);
3519 }
3520 struct ovn_port_group *pg;
3521 HMAP_FOR_EACH (pg, key_node, port_groups) {
3522 if (ovn_port_group_ls_find(pg, &od->nbs->header_.uuid)) {
3523 for (size_t i = 0; i < pg->n_acls; i++) {
3524 consider_acl(lflows, od, pg->acls[i], has_stateful);
3525 }
3526 }
3527 }
3528
3529 /* Add 34000 priority flow to allow DHCP reply from ovn-controller to all
3530 * logical ports of the datapath if the CMS has configured DHCPv4 options.
3531 * */
3532 for (size_t i = 0; i < od->nbs->n_ports; i++) {
3533 if (od->nbs->ports[i]->dhcpv4_options) {
3534 const char *server_id = smap_get(
3535 &od->nbs->ports[i]->dhcpv4_options->options, "server_id");
3536 const char *server_mac = smap_get(
3537 &od->nbs->ports[i]->dhcpv4_options->options, "server_mac");
3538 const char *lease_time = smap_get(
3539 &od->nbs->ports[i]->dhcpv4_options->options, "lease_time");
3540 if (server_id && server_mac && lease_time) {
3541 struct ds match = DS_EMPTY_INITIALIZER;
3542 const char *actions =
3543 has_stateful ? "ct_commit; next;" : "next;";
3544 ds_put_format(&match, "outport == \"%s\" && eth.src == %s "
3545 "&& ip4.src == %s && udp && udp.src == 67 "
3546 "&& udp.dst == 68", od->nbs->ports[i]->name,
3547 server_mac, server_id);
3548 ovn_lflow_add(
3549 lflows, od, S_SWITCH_OUT_ACL, 34000, ds_cstr(&match),
3550 actions);
3551 ds_destroy(&match);
3552 }
3553 }
3554
3555 if (od->nbs->ports[i]->dhcpv6_options) {
3556 const char *server_mac = smap_get(
3557 &od->nbs->ports[i]->dhcpv6_options->options, "server_id");
3558 struct eth_addr ea;
3559 if (server_mac && eth_addr_from_string(server_mac, &ea)) {
3560 /* Get the link local IP of the DHCPv6 server from the
3561 * server MAC. */
3562 struct in6_addr lla;
3563 in6_generate_lla(ea, &lla);
3564
3565 char server_ip[INET6_ADDRSTRLEN + 1];
3566 ipv6_string_mapped(server_ip, &lla);
3567
3568 struct ds match = DS_EMPTY_INITIALIZER;
3569 const char *actions = has_stateful ? "ct_commit; next;" :
3570 "next;";
3571 ds_put_format(&match, "outport == \"%s\" && eth.src == %s "
3572 "&& ip6.src == %s && udp && udp.src == 547 "
3573 "&& udp.dst == 546", od->nbs->ports[i]->name,
3574 server_mac, server_ip);
3575 ovn_lflow_add(
3576 lflows, od, S_SWITCH_OUT_ACL, 34000, ds_cstr(&match),
3577 actions);
3578 ds_destroy(&match);
3579 }
3580 }
3581 }
3582
3583 /* Add a 34000 priority flow to advance the DNS reply from ovn-controller,
3584 * if the CMS has configured DNS records for the datapath.
3585 */
3586 if (ls_has_dns_records(od->nbs)) {
3587 const char *actions = has_stateful ? "ct_commit; next;" : "next;";
3588 ovn_lflow_add(
3589 lflows, od, S_SWITCH_OUT_ACL, 34000, "udp.src == 53",
3590 actions);
3591 }
3592 }
3593
3594 static void
3595 build_qos(struct ovn_datapath *od, struct hmap *lflows) {
3596 ovn_lflow_add(lflows, od, S_SWITCH_IN_QOS_MARK, 0, "1", "next;");
3597 ovn_lflow_add(lflows, od, S_SWITCH_OUT_QOS_MARK, 0, "1", "next;");
3598 ovn_lflow_add(lflows, od, S_SWITCH_IN_QOS_METER, 0, "1", "next;");
3599 ovn_lflow_add(lflows, od, S_SWITCH_OUT_QOS_METER, 0, "1", "next;");
3600
3601 for (size_t i = 0; i < od->nbs->n_qos_rules; i++) {
3602 struct nbrec_qos *qos = od->nbs->qos_rules[i];
3603 bool ingress = !strcmp(qos->direction, "from-lport") ? true :false;
3604 enum ovn_stage stage = ingress ? S_SWITCH_IN_QOS_MARK : S_SWITCH_OUT_QOS_MARK;
3605 int64_t rate = 0;
3606 int64_t burst = 0;
3607
3608 for (size_t j = 0; j < qos->n_action; j++) {
3609 if (!strcmp(qos->key_action[j], "dscp")) {
3610 struct ds dscp_action = DS_EMPTY_INITIALIZER;
3611
3612 ds_put_format(&dscp_action, "ip.dscp = %"PRId64"; next;",
3613 qos->value_action[j]);
3614 ovn_lflow_add(lflows, od, stage,
3615 qos->priority,
3616 qos->match, ds_cstr(&dscp_action));
3617 ds_destroy(&dscp_action);
3618 }
3619 }
3620
3621 for (size_t n = 0; n < qos->n_bandwidth; n++) {
3622 if (!strcmp(qos->key_bandwidth[n], "rate")) {
3623 rate = qos->value_bandwidth[n];
3624 } else if (!strcmp(qos->key_bandwidth[n], "burst")) {
3625 burst = qos->value_bandwidth[n];
3626 }
3627 }
3628 if (rate) {
3629 struct ds meter_action = DS_EMPTY_INITIALIZER;
3630 stage = ingress ? S_SWITCH_IN_QOS_METER : S_SWITCH_OUT_QOS_METER;
3631 if (burst) {
3632 ds_put_format(&meter_action,
3633 "set_meter(%"PRId64", %"PRId64"); next;",
3634 rate, burst);
3635 } else {
3636 ds_put_format(&meter_action,
3637 "set_meter(%"PRId64"); next;",
3638 rate);
3639 }
3640
3641 /* Ingress and Egress QoS Meter Table.
3642 *
3643 * We limit the bandwidth of this flow by adding a meter table.
3644 */
3645 ovn_lflow_add(lflows, od, stage,
3646 qos->priority,
3647 qos->match, ds_cstr(&meter_action));
3648 ds_destroy(&meter_action);
3649 }
3650 }
3651 }
3652
3653 static void
3654 build_lb(struct ovn_datapath *od, struct hmap *lflows)
3655 {
3656 /* Ingress and Egress LB Table (Priority 0): Packets are allowed by
3657 * default. */
3658 ovn_lflow_add(lflows, od, S_SWITCH_IN_LB, 0, "1", "next;");
3659 ovn_lflow_add(lflows, od, S_SWITCH_OUT_LB, 0, "1", "next;");
3660
3661 if (od->nbs->load_balancer) {
3662 /* Ingress and Egress LB Table (Priority 65535).
3663 *
3664 * Send established traffic through conntrack for just NAT. */
3665 ovn_lflow_add(lflows, od, S_SWITCH_IN_LB, UINT16_MAX,
3666 "ct.est && !ct.rel && !ct.new && !ct.inv",
3667 REGBIT_CONNTRACK_NAT" = 1; next;");
3668 ovn_lflow_add(lflows, od, S_SWITCH_OUT_LB, UINT16_MAX,
3669 "ct.est && !ct.rel && !ct.new && !ct.inv",
3670 REGBIT_CONNTRACK_NAT" = 1; next;");
3671 }
3672 }
3673
3674 static void
3675 build_stateful(struct ovn_datapath *od, struct hmap *lflows)
3676 {
3677 /* Ingress and Egress stateful Table (Priority 0): Packets are
3678 * allowed by default. */
3679 ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL, 0, "1", "next;");
3680 ovn_lflow_add(lflows, od, S_SWITCH_OUT_STATEFUL, 0, "1", "next;");
3681
3682 /* If REGBIT_CONNTRACK_COMMIT is set as 1, then the packets should be
3683 * committed to conntrack. We always set ct_label.blocked to 0 here as
3684 * any packet that makes it this far is part of a connection we
3685 * want to allow to continue. */
3686 ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL, 100,
3687 REGBIT_CONNTRACK_COMMIT" == 1", "ct_commit(ct_label=0/1); next;");
3688 ovn_lflow_add(lflows, od, S_SWITCH_OUT_STATEFUL, 100,
3689 REGBIT_CONNTRACK_COMMIT" == 1", "ct_commit(ct_label=0/1); next;");
3690
3691 /* If REGBIT_CONNTRACK_NAT is set as 1, then packets should just be sent
3692 * through nat (without committing).
3693 *
3694 * REGBIT_CONNTRACK_COMMIT is set for new connections and
3695 * REGBIT_CONNTRACK_NAT is set for established connections. So they
3696 * don't overlap.
3697 */
3698 ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL, 100,
3699 REGBIT_CONNTRACK_NAT" == 1", "ct_lb;");
3700 ovn_lflow_add(lflows, od, S_SWITCH_OUT_STATEFUL, 100,
3701 REGBIT_CONNTRACK_NAT" == 1", "ct_lb;");
3702
3703 /* Load balancing rules for new connections get committed to conntrack
3704 * table. So even if REGBIT_CONNTRACK_COMMIT is set in a previous table
3705 * a higher priority rule for load balancing below also commits the
3706 * connection, so it is okay if we do not hit the above match on
3707 * REGBIT_CONNTRACK_COMMIT. */
3708 for (int i = 0; i < od->nbs->n_load_balancer; i++) {
3709 struct nbrec_load_balancer *lb = od->nbs->load_balancer[i];
3710 struct smap *vips = &lb->vips;
3711 struct smap_node *node;
3712
3713 SMAP_FOR_EACH (node, vips) {
3714 uint16_t port = 0;
3715 int addr_family;
3716
3717 /* node->key contains IP:port or just IP. */
3718 char *ip_address = NULL;
3719 ip_address_and_port_from_lb_key(node->key, &ip_address, &port,
3720 &addr_family);
3721 if (!ip_address) {
3722 continue;
3723 }
3724
3725 /* New connections in Ingress table. */
3726 char *action = xasprintf("ct_lb(%s);", node->value);
3727 struct ds match = DS_EMPTY_INITIALIZER;
3728 if (addr_family == AF_INET) {
3729 ds_put_format(&match, "ct.new && ip4.dst == %s", ip_address);
3730 } else {
3731 ds_put_format(&match, "ct.new && ip6.dst == %s", ip_address);
3732 }
3733 if (port) {
3734 if (lb->protocol && !strcmp(lb->protocol, "udp")) {
3735 ds_put_format(&match, " && udp.dst == %d", port);
3736 } else {
3737 ds_put_format(&match, " && tcp.dst == %d", port);
3738 }
3739 ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL,
3740 120, ds_cstr(&match), action);
3741 } else {
3742 ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL,
3743 110, ds_cstr(&match), action);
3744 }
3745
3746 free(ip_address);
3747 ds_destroy(&match);
3748 free(action);
3749 }
3750 }
3751 }
3752
3753 static void
3754 build_lswitch_flows(struct hmap *datapaths, struct hmap *ports,
3755 struct hmap *port_groups, struct hmap *lflows,
3756 struct hmap *mcgroups)
3757 {
3758 /* This flow table structure is documented in ovn-northd(8), so please
3759 * update ovn-northd.8.xml if you change anything. */
3760
3761 struct ds match = DS_EMPTY_INITIALIZER;
3762 struct ds actions = DS_EMPTY_INITIALIZER;
3763
3764 /* Build pre-ACL and ACL tables for both ingress and egress.
3765 * Ingress tables 3 through 10. Egress tables 0 through 7. */
3766 struct ovn_datapath *od;
3767 HMAP_FOR_EACH (od, key_node, datapaths) {
3768 if (!od->nbs) {
3769 continue;
3770 }
3771
3772 build_pre_acls(od, lflows);
3773 build_pre_lb(od, lflows);
3774 build_pre_stateful(od, lflows);
3775 build_acls(od, lflows, port_groups);
3776 build_qos(od, lflows);
3777 build_lb(od, lflows);
3778 build_stateful(od, lflows);
3779 }
3780
3781 /* Logical switch ingress table 0: Admission control framework (priority
3782 * 100). */
3783 HMAP_FOR_EACH (od, key_node, datapaths) {
3784 if (!od->nbs) {
3785 continue;
3786 }
3787
3788 /* Logical VLANs not supported. */
3789 ovn_lflow_add(lflows, od, S_SWITCH_IN_PORT_SEC_L2, 100, "vlan.present",
3790 "drop;");
3791
3792 /* Broadcast/multicast source address is invalid. */
3793 ovn_lflow_add(lflows, od, S_SWITCH_IN_PORT_SEC_L2, 100, "eth.src[40]",
3794 "drop;");
3795
3796 /* Port security flows have priority 50 (see below) and will continue
3797 * to the next table if packet source is acceptable. */
3798 }
3799
3800 /* Logical switch ingress table 0: Ingress port security - L2
3801 * (priority 50).
3802 * Ingress table 1: Ingress port security - IP (priority 90 and 80)
3803 * Ingress table 2: Ingress port security - ND (priority 90 and 80)
3804 */
3805 struct ovn_port *op;
3806 HMAP_FOR_EACH (op, key_node, ports) {
3807 if (!op->nbsp) {
3808 continue;
3809 }
3810
3811 if (!lsp_is_enabled(op->nbsp)) {
3812 /* Drop packets from disabled logical ports (since logical flow
3813 * tables are default-drop). */
3814 continue;
3815 }
3816
3817 ds_clear(&match);
3818 ds_clear(&actions);
3819 ds_put_format(&match, "inport == %s", op->json_key);
3820 build_port_security_l2("eth.src", op->ps_addrs, op->n_ps_addrs,
3821 &match);
3822
3823 const char *queue_id = smap_get(&op->sb->options, "qdisc_queue_id");
3824 if (queue_id) {
3825 ds_put_format(&actions, "set_queue(%s); ", queue_id);
3826 }
3827 ds_put_cstr(&actions, "next;");
3828 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_PORT_SEC_L2, 50,
3829 ds_cstr(&match), ds_cstr(&actions));
3830
3831 if (op->nbsp->n_port_security) {
3832 build_port_security_ip(P_IN, op, lflows);
3833 build_port_security_nd(op, lflows);
3834 }
3835 }
3836
3837 /* Ingress table 1 and 2: Port security - IP and ND, by default goto next.
3838 * (priority 0)*/
3839 HMAP_FOR_EACH (od, key_node, datapaths) {
3840 if (!od->nbs) {
3841 continue;
3842 }
3843
3844 ovn_lflow_add(lflows, od, S_SWITCH_IN_PORT_SEC_ND, 0, "1", "next;");
3845 ovn_lflow_add(lflows, od, S_SWITCH_IN_PORT_SEC_IP, 0, "1", "next;");
3846 }
3847
3848 /* Ingress table 11: ARP/ND responder, skip requests coming from localnet
3849 * and vtep ports. (priority 100); see ovn-northd.8.xml for the
3850 * rationale. */
3851 HMAP_FOR_EACH (op, key_node, ports) {
3852 if (!op->nbsp) {
3853 continue;
3854 }
3855
3856 if ((!strcmp(op->nbsp->type, "localnet")) ||
3857 (!strcmp(op->nbsp->type, "vtep"))) {
3858 ds_clear(&match);
3859 ds_put_format(&match, "inport == %s", op->json_key);
3860 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_ARP_ND_RSP, 100,
3861 ds_cstr(&match), "next;");
3862 }
3863 }
3864
3865 /* Ingress table 11: ARP/ND responder, reply for known IPs.
3866 * (priority 50). */
3867 HMAP_FOR_EACH (op, key_node, ports) {
3868 if (!op->nbsp) {
3869 continue;
3870 }
3871
3872 /*
3873 * Add ARP/ND reply flows if either the
3874 * - port is up or
3875 * - port type is router or
3876 * - port type is localport
3877 */
3878 if (!lsp_is_up(op->nbsp) && strcmp(op->nbsp->type, "router") &&
3879 strcmp(op->nbsp->type, "localport")) {
3880 continue;
3881 }
3882
3883 for (size_t i = 0; i < op->n_lsp_addrs; i++) {
3884 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv4_addrs; j++) {
3885 ds_clear(&match);
3886 ds_put_format(&match, "arp.tpa == %s && arp.op == 1",
3887 op->lsp_addrs[i].ipv4_addrs[j].addr_s);
3888 ds_clear(&actions);
3889 ds_put_format(&actions,
3890 "eth.dst = eth.src; "
3891 "eth.src = %s; "
3892 "arp.op = 2; /* ARP reply */ "
3893 "arp.tha = arp.sha; "
3894 "arp.sha = %s; "
3895 "arp.tpa = arp.spa; "
3896 "arp.spa = %s; "
3897 "outport = inport; "
3898 "flags.loopback = 1; "
3899 "output;",
3900 op->lsp_addrs[i].ea_s, op->lsp_addrs[i].ea_s,
3901 op->lsp_addrs[i].ipv4_addrs[j].addr_s);
3902 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_ARP_ND_RSP, 50,
3903 ds_cstr(&match), ds_cstr(&actions));
3904
3905 /* Do not reply to an ARP request from the port that owns the
3906 * address (otherwise a DHCP client that ARPs to check for a
3907 * duplicate address will fail). Instead, forward it the usual
3908 * way.
3909 *
3910 * (Another alternative would be to simply drop the packet. If
3911 * everything is working as it is configured, then this would
3912 * produce equivalent results, since no one should reply to the
3913 * request. But ARPing for one's own IP address is intended to
3914 * detect situations where the network is not working as
3915 * configured, so dropping the request would frustrate that
3916 * intent.) */
3917 ds_put_format(&match, " && inport == %s", op->json_key);
3918 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_ARP_ND_RSP, 100,
3919 ds_cstr(&match), "next;");
3920 }
3921
3922 /* For ND solicitations, we need to listen for both the
3923 * unicast IPv6 address and its all-nodes multicast address,
3924 * but always respond with the unicast IPv6 address. */
3925 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv6_addrs; j++) {
3926 ds_clear(&match);
3927 ds_put_format(&match,
3928 "nd_ns && ip6.dst == {%s, %s} && nd.target == %s",
3929 op->lsp_addrs[i].ipv6_addrs[j].addr_s,
3930 op->lsp_addrs[i].ipv6_addrs[j].sn_addr_s,
3931 op->lsp_addrs[i].ipv6_addrs[j].addr_s);
3932
3933 ds_clear(&actions);
3934 ds_put_format(&actions,
3935 "nd_na { "
3936 "eth.src = %s; "
3937 "ip6.src = %s; "
3938 "nd.target = %s; "
3939 "nd.tll = %s; "
3940 "outport = inport; "
3941 "flags.loopback = 1; "
3942 "output; "
3943 "};",
3944 op->lsp_addrs[i].ea_s,
3945 op->lsp_addrs[i].ipv6_addrs[j].addr_s,
3946 op->lsp_addrs[i].ipv6_addrs[j].addr_s,
3947 op->lsp_addrs[i].ea_s);
3948 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_ARP_ND_RSP, 50,
3949 ds_cstr(&match), ds_cstr(&actions));
3950
3951 /* Do not reply to a solicitation from the port that owns the
3952 * address (otherwise DAD detection will fail). */
3953 ds_put_format(&match, " && inport == %s", op->json_key);
3954 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_ARP_ND_RSP, 100,
3955 ds_cstr(&match), "next;");
3956 }
3957 }
3958 }
3959
3960 /* Ingress table 11: ARP/ND responder, by default goto next.
3961 * (priority 0)*/
3962 HMAP_FOR_EACH (od, key_node, datapaths) {
3963 if (!od->nbs) {
3964 continue;
3965 }
3966
3967 ovn_lflow_add(lflows, od, S_SWITCH_IN_ARP_ND_RSP, 0, "1", "next;");
3968 }
3969
3970 /* Logical switch ingress table 12 and 13: DHCP options and response
3971 * priority 100 flows. */
3972 HMAP_FOR_EACH (op, key_node, ports) {
3973 if (!op->nbsp) {
3974 continue;
3975 }
3976
3977 if (!lsp_is_enabled(op->nbsp) || !strcmp(op->nbsp->type, "router")) {
3978 /* Don't add the DHCP flows if the port is not enabled or if the
3979 * port is a router port. */
3980 continue;
3981 }
3982
3983 if (!op->nbsp->dhcpv4_options && !op->nbsp->dhcpv6_options) {
3984 /* CMS has disabled both native DHCPv4 and DHCPv6 for this lport.
3985 */
3986 continue;
3987 }
3988
3989 for (size_t i = 0; i < op->n_lsp_addrs; i++) {
3990 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv4_addrs; j++) {
3991 struct ds options_action = DS_EMPTY_INITIALIZER;
3992 struct ds response_action = DS_EMPTY_INITIALIZER;
3993 struct ds ipv4_addr_match = DS_EMPTY_INITIALIZER;
3994 if (build_dhcpv4_action(
3995 op, op->lsp_addrs[i].ipv4_addrs[j].addr,
3996 &options_action, &response_action, &ipv4_addr_match)) {
3997 ds_clear(&match);
3998 ds_put_format(
3999 &match, "inport == %s && eth.src == %s && "
4000 "ip4.src == 0.0.0.0 && ip4.dst == 255.255.255.255 && "
4001 "udp.src == 68 && udp.dst == 67", op->json_key,
4002 op->lsp_addrs[i].ea_s);
4003
4004 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_DHCP_OPTIONS,
4005 100, ds_cstr(&match),
4006 ds_cstr(&options_action));
4007 ds_clear(&match);
4008 /* Allow ip4.src = OFFER_IP and
4009 * ip4.dst = {SERVER_IP, 255.255.255.255} for the below
4010 * cases
4011 * - When the client wants to renew the IP by sending
4012 * the DHCPREQUEST to the server ip.
4013 * - When the client wants to renew the IP by
4014 * broadcasting the DHCPREQUEST.
4015 */
4016 ds_put_format(
4017 &match, "inport == %s && eth.src == %s && "
4018 "%s && udp.src == 68 && udp.dst == 67", op->json_key,
4019 op->lsp_addrs[i].ea_s, ds_cstr(&ipv4_addr_match));
4020
4021 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_DHCP_OPTIONS,
4022 100, ds_cstr(&match),
4023 ds_cstr(&options_action));
4024 ds_clear(&match);
4025
4026 /* If REGBIT_DHCP_OPTS_RESULT is set, it means the
4027 * put_dhcp_opts action is successful. */
4028 ds_put_format(
4029 &match, "inport == %s && eth.src == %s && "
4030 "ip4 && udp.src == 68 && udp.dst == 67"
4031 " && "REGBIT_DHCP_OPTS_RESULT, op->json_key,
4032 op->lsp_addrs[i].ea_s);
4033 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_DHCP_RESPONSE,
4034 100, ds_cstr(&match),
4035 ds_cstr(&response_action));
4036 ds_destroy(&options_action);
4037 ds_destroy(&response_action);
4038 ds_destroy(&ipv4_addr_match);
4039 break;
4040 }
4041 }
4042
4043 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv6_addrs; j++) {
4044 struct ds options_action = DS_EMPTY_INITIALIZER;
4045 struct ds response_action = DS_EMPTY_INITIALIZER;
4046 if (build_dhcpv6_action(
4047 op, &op->lsp_addrs[i].ipv6_addrs[j].addr,
4048 &options_action, &response_action)) {
4049 ds_clear(&match);
4050 ds_put_format(
4051 &match, "inport == %s && eth.src == %s"
4052 " && ip6.dst == ff02::1:2 && udp.src == 546 &&"
4053 " udp.dst == 547", op->json_key,
4054 op->lsp_addrs[i].ea_s);
4055
4056 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_DHCP_OPTIONS, 100,
4057 ds_cstr(&match), ds_cstr(&options_action));
4058
4059 /* If REGBIT_DHCP_OPTS_RESULT is set to 1, it means the
4060 * put_dhcpv6_opts action is successful */
4061 ds_put_cstr(&match, " && "REGBIT_DHCP_OPTS_RESULT);
4062 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_DHCP_RESPONSE, 100,
4063 ds_cstr(&match), ds_cstr(&response_action));
4064 ds_destroy(&options_action);
4065 ds_destroy(&response_action);
4066 break;
4067 }
4068 }
4069 }
4070 }
4071
4072 /* Logical switch ingress table 14 and 15: DNS lookup and response
4073 * priority 100 flows.
4074 */
4075 HMAP_FOR_EACH (od, key_node, datapaths) {
4076 if (!od->nbs || !ls_has_dns_records(od->nbs)) {
4077 continue;
4078 }
4079
4080 struct ds action = DS_EMPTY_INITIALIZER;
4081
4082 ds_clear(&match);
4083 ds_put_cstr(&match, "udp.dst == 53");
4084 ds_put_format(&action,
4085 REGBIT_DNS_LOOKUP_RESULT" = dns_lookup(); next;");
4086 ovn_lflow_add(lflows, od, S_SWITCH_IN_DNS_LOOKUP, 100,
4087 ds_cstr(&match), ds_cstr(&action));
4088 ds_clear(&action);
4089 ds_put_cstr(&match, " && "REGBIT_DNS_LOOKUP_RESULT);
4090 ds_put_format(&action, "eth.dst <-> eth.src; ip4.src <-> ip4.dst; "
4091 "udp.dst = udp.src; udp.src = 53; outport = inport; "
4092 "flags.loopback = 1; output;");
4093 ovn_lflow_add(lflows, od, S_SWITCH_IN_DNS_RESPONSE, 100,
4094 ds_cstr(&match), ds_cstr(&action));
4095 ds_clear(&action);
4096 ds_put_format(&action, "eth.dst <-> eth.src; ip6.src <-> ip6.dst; "
4097 "udp.dst = udp.src; udp.src = 53; outport = inport; "
4098 "flags.loopback = 1; output;");
4099 ovn_lflow_add(lflows, od, S_SWITCH_IN_DNS_RESPONSE, 100,
4100 ds_cstr(&match), ds_cstr(&action));
4101 ds_destroy(&action);
4102 }
4103
4104 /* Ingress table 12 and 13: DHCP options and response, by default goto
4105 * next. (priority 0).
4106 * Ingress table 14 and 15: DNS lookup and response, by default goto next.
4107 * (priority 0).*/
4108
4109 HMAP_FOR_EACH (od, key_node, datapaths) {
4110 if (!od->nbs) {
4111 continue;
4112 }
4113
4114 ovn_lflow_add(lflows, od, S_SWITCH_IN_DHCP_OPTIONS, 0, "1", "next;");
4115 ovn_lflow_add(lflows, od, S_SWITCH_IN_DHCP_RESPONSE, 0, "1", "next;");
4116 ovn_lflow_add(lflows, od, S_SWITCH_IN_DNS_LOOKUP, 0, "1", "next;");
4117 ovn_lflow_add(lflows, od, S_SWITCH_IN_DNS_RESPONSE, 0, "1", "next;");
4118 }
4119
4120 /* Ingress table 16: Destination lookup, broadcast and multicast handling
4121 * (priority 100). */
4122 HMAP_FOR_EACH (op, key_node, ports) {
4123 if (!op->nbsp) {
4124 continue;
4125 }
4126
4127 if (lsp_is_enabled(op->nbsp)) {
4128 ovn_multicast_add(mcgroups, &mc_flood, op);
4129 }
4130 }
4131 HMAP_FOR_EACH (od, key_node, datapaths) {
4132 if (!od->nbs) {
4133 continue;
4134 }
4135
4136 ovn_lflow_add(lflows, od, S_SWITCH_IN_L2_LKUP, 100, "eth.mcast",
4137 "outport = \""MC_FLOOD"\"; output;");
4138 }
4139
4140 /* Ingress table 16: Destination lookup, unicast handling (priority 50), */
4141 HMAP_FOR_EACH (op, key_node, ports) {
4142 if (!op->nbsp) {
4143 continue;
4144 }
4145
4146 for (size_t i = 0; i < op->nbsp->n_addresses; i++) {
4147 /* Addresses are owned by the logical port.
4148 * Ethernet address followed by zero or more IPv4
4149 * or IPv6 addresses (or both). */
4150 struct eth_addr mac;
4151 if (ovs_scan(op->nbsp->addresses[i],
4152 ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
4153 ds_clear(&match);
4154 ds_put_format(&match, "eth.dst == "ETH_ADDR_FMT,
4155 ETH_ADDR_ARGS(mac));
4156
4157 ds_clear(&actions);
4158 ds_put_format(&actions, "outport = %s; output;", op->json_key);
4159 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_L2_LKUP, 50,
4160 ds_cstr(&match), ds_cstr(&actions));
4161 } else if (!strcmp(op->nbsp->addresses[i], "unknown")) {
4162 if (lsp_is_enabled(op->nbsp)) {
4163 ovn_multicast_add(mcgroups, &mc_unknown, op);
4164 op->od->has_unknown = true;
4165 }
4166 } else if (is_dynamic_lsp_address(op->nbsp->addresses[i])) {
4167 if (!op->nbsp->dynamic_addresses
4168 || !ovs_scan(op->nbsp->dynamic_addresses,
4169 ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
4170 continue;
4171 }
4172 ds_clear(&match);
4173 ds_put_format(&match, "eth.dst == "ETH_ADDR_FMT,
4174 ETH_ADDR_ARGS(mac));
4175
4176 ds_clear(&actions);
4177 ds_put_format(&actions, "outport = %s; output;", op->json_key);
4178 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_L2_LKUP, 50,
4179 ds_cstr(&match), ds_cstr(&actions));
4180 } else if (!strcmp(op->nbsp->addresses[i], "router")) {
4181 if (!op->peer || !op->peer->nbrp
4182 || !ovs_scan(op->peer->nbrp->mac,
4183 ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
4184 continue;
4185 }
4186 ds_clear(&match);
4187 ds_put_format(&match, "eth.dst == "ETH_ADDR_FMT,
4188 ETH_ADDR_ARGS(mac));
4189 if (op->peer->od->l3dgw_port
4190 && op->peer == op->peer->od->l3dgw_port
4191 && op->peer->od->l3redirect_port) {
4192 /* The destination lookup flow for the router's
4193 * distributed gateway port MAC address should only be
4194 * programmed on the "redirect-chassis". */
4195 ds_put_format(&match, " && is_chassis_resident(%s)",
4196 op->peer->od->l3redirect_port->json_key);
4197 }
4198
4199 ds_clear(&actions);
4200 ds_put_format(&actions, "outport = %s; output;", op->json_key);
4201 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_L2_LKUP, 50,
4202 ds_cstr(&match), ds_cstr(&actions));
4203
4204 /* Add ethernet addresses specified in NAT rules on
4205 * distributed logical routers. */
4206 if (op->peer->od->l3dgw_port
4207 && op->peer == op->peer->od->l3dgw_port) {
4208 for (int j = 0; j < op->peer->od->nbr->n_nat; j++) {
4209 const struct nbrec_nat *nat
4210 = op->peer->od->nbr->nat[j];
4211 if (!strcmp(nat->type, "dnat_and_snat")
4212 && nat->logical_port && nat->external_mac
4213 && eth_addr_from_string(nat->external_mac, &mac)) {
4214
4215 ds_clear(&match);
4216 ds_put_format(&match, "eth.dst == "ETH_ADDR_FMT
4217 " && is_chassis_resident(\"%s\")",
4218 ETH_ADDR_ARGS(mac),
4219 nat->logical_port);
4220
4221 ds_clear(&actions);
4222 ds_put_format(&actions, "outport = %s; output;",
4223 op->json_key);
4224 ovn_lflow_add(lflows, op->od, S_SWITCH_IN_L2_LKUP,
4225 50, ds_cstr(&match),
4226 ds_cstr(&actions));
4227 }
4228 }
4229 }
4230 } else {
4231 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4232
4233 VLOG_INFO_RL(&rl,
4234 "%s: invalid syntax '%s' in addresses column",
4235 op->nbsp->name, op->nbsp->addresses[i]);
4236 }
4237 }
4238 }
4239
4240 /* Ingress table 16: Destination lookup for unknown MACs (priority 0). */
4241 HMAP_FOR_EACH (od, key_node, datapaths) {
4242 if (!od->nbs) {
4243 continue;
4244 }
4245
4246 if (od->has_unknown) {
4247 ovn_lflow_add(lflows, od, S_SWITCH_IN_L2_LKUP, 0, "1",
4248 "outport = \""MC_UNKNOWN"\"; output;");
4249 }
4250 }
4251
4252 /* Egress tables 8: Egress port security - IP (priority 0)
4253 * Egress table 9: Egress port security L2 - multicast/broadcast
4254 * (priority 100). */
4255 HMAP_FOR_EACH (od, key_node, datapaths) {
4256 if (!od->nbs) {
4257 continue;
4258 }
4259
4260 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PORT_SEC_IP, 0, "1", "next;");
4261 ovn_lflow_add(lflows, od, S_SWITCH_OUT_PORT_SEC_L2, 100, "eth.mcast",
4262 "output;");
4263 }
4264
4265 /* Egress table 8: Egress port security - IP (priorities 90 and 80)
4266 * if port security enabled.
4267 *
4268 * Egress table 9: Egress port security - L2 (priorities 50 and 150).
4269 *
4270 * Priority 50 rules implement port security for enabled logical port.
4271 *
4272 * Priority 150 rules drop packets to disabled logical ports, so that they
4273 * don't even receive multicast or broadcast packets. */
4274 HMAP_FOR_EACH (op, key_node, ports) {
4275 if (!op->nbsp) {
4276 continue;
4277 }
4278
4279 ds_clear(&match);
4280 ds_put_format(&match, "outport == %s", op->json_key);
4281 if (lsp_is_enabled(op->nbsp)) {
4282 build_port_security_l2("eth.dst", op->ps_addrs, op->n_ps_addrs,
4283 &match);
4284 ovn_lflow_add(lflows, op->od, S_SWITCH_OUT_PORT_SEC_L2, 50,
4285 ds_cstr(&match), "output;");
4286 } else {
4287 ovn_lflow_add(lflows, op->od, S_SWITCH_OUT_PORT_SEC_L2, 150,
4288 ds_cstr(&match), "drop;");
4289 }
4290
4291 if (op->nbsp->n_port_security) {
4292 build_port_security_ip(P_OUT, op, lflows);
4293 }
4294 }
4295
4296 ds_destroy(&match);
4297 ds_destroy(&actions);
4298 }
4299
4300 static bool
4301 lrport_is_enabled(const struct nbrec_logical_router_port *lrport)
4302 {
4303 return !lrport->enabled || *lrport->enabled;
4304 }
4305
4306 /* Returns a string of the IP address of the router port 'op' that
4307 * overlaps with 'ip_s". If one is not found, returns NULL.
4308 *
4309 * The caller must not free the returned string. */
4310 static const char *
4311 find_lrp_member_ip(const struct ovn_port *op, const char *ip_s)
4312 {
4313 bool is_ipv4 = strchr(ip_s, '.') ? true : false;
4314
4315 if (is_ipv4) {
4316 ovs_be32 ip;
4317
4318 if (!ip_parse(ip_s, &ip)) {
4319 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4320 VLOG_WARN_RL(&rl, "bad ip address %s", ip_s);
4321 return NULL;
4322 }
4323
4324 for (int i = 0; i < op->lrp_networks.n_ipv4_addrs; i++) {
4325 const struct ipv4_netaddr *na = &op->lrp_networks.ipv4_addrs[i];
4326
4327 if (!((na->network ^ ip) & na->mask)) {
4328 /* There should be only 1 interface that matches the
4329 * supplied IP. Otherwise, it's a configuration error,
4330 * because subnets of a router's interfaces should NOT
4331 * overlap. */
4332 return na->addr_s;
4333 }
4334 }
4335 } else {
4336 struct in6_addr ip6;
4337
4338 if (!ipv6_parse(ip_s, &ip6)) {
4339 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4340 VLOG_WARN_RL(&rl, "bad ipv6 address %s", ip_s);
4341 return NULL;
4342 }
4343
4344 for (int i = 0; i < op->lrp_networks.n_ipv6_addrs; i++) {
4345 const struct ipv6_netaddr *na = &op->lrp_networks.ipv6_addrs[i];
4346 struct in6_addr xor_addr = ipv6_addr_bitxor(&na->network, &ip6);
4347 struct in6_addr and_addr = ipv6_addr_bitand(&xor_addr, &na->mask);
4348
4349 if (ipv6_is_zero(&and_addr)) {
4350 /* There should be only 1 interface that matches the
4351 * supplied IP. Otherwise, it's a configuration error,
4352 * because subnets of a router's interfaces should NOT
4353 * overlap. */
4354 return na->addr_s;
4355 }
4356 }
4357 }
4358
4359 return NULL;
4360 }
4361
4362 static void
4363 add_route(struct hmap *lflows, const struct ovn_port *op,
4364 const char *lrp_addr_s, const char *network_s, int plen,
4365 const char *gateway, const char *policy)
4366 {
4367 bool is_ipv4 = strchr(network_s, '.') ? true : false;
4368 struct ds match = DS_EMPTY_INITIALIZER;
4369 const char *dir;
4370 uint16_t priority;
4371
4372 if (policy && !strcmp(policy, "src-ip")) {
4373 dir = "src";
4374 priority = plen * 2;
4375 } else {
4376 dir = "dst";
4377 priority = (plen * 2) + 1;
4378 }
4379
4380 /* IPv6 link-local addresses must be scoped to the local router port. */
4381 if (!is_ipv4) {
4382 struct in6_addr network;
4383 ovs_assert(ipv6_parse(network_s, &network));
4384 if (in6_is_lla(&network)) {
4385 ds_put_format(&match, "inport == %s && ", op->json_key);
4386 }
4387 }
4388 ds_put_format(&match, "ip%s.%s == %s/%d", is_ipv4 ? "4" : "6", dir,
4389 network_s, plen);
4390
4391 struct ds actions = DS_EMPTY_INITIALIZER;
4392 ds_put_format(&actions, "ip.ttl--; %sreg0 = ", is_ipv4 ? "" : "xx");
4393
4394 if (gateway) {
4395 ds_put_cstr(&actions, gateway);
4396 } else {
4397 ds_put_format(&actions, "ip%s.dst", is_ipv4 ? "4" : "6");
4398 }
4399 ds_put_format(&actions, "; "
4400 "%sreg1 = %s; "
4401 "eth.src = %s; "
4402 "outport = %s; "
4403 "flags.loopback = 1; "
4404 "next;",
4405 is_ipv4 ? "" : "xx",
4406 lrp_addr_s,
4407 op->lrp_networks.ea_s,
4408 op->json_key);
4409
4410 /* The priority here is calculated to implement longest-prefix-match
4411 * routing. */
4412 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_ROUTING, priority,
4413 ds_cstr(&match), ds_cstr(&actions));
4414 ds_destroy(&match);
4415 ds_destroy(&actions);
4416 }
4417
4418 static void
4419 build_static_route_flow(struct hmap *lflows, struct ovn_datapath *od,
4420 struct hmap *ports,
4421 const struct nbrec_logical_router_static_route *route)
4422 {
4423 ovs_be32 nexthop;
4424 const char *lrp_addr_s = NULL;
4425 unsigned int plen;
4426 bool is_ipv4;
4427
4428 /* Verify that the next hop is an IP address with an all-ones mask. */
4429 char *error = ip_parse_cidr(route->nexthop, &nexthop, &plen);
4430 if (!error) {
4431 if (plen != 32) {
4432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4433 VLOG_WARN_RL(&rl, "bad next hop mask %s", route->nexthop);
4434 return;
4435 }
4436 is_ipv4 = true;
4437 } else {
4438 free(error);
4439
4440 struct in6_addr ip6;
4441 error = ipv6_parse_cidr(route->nexthop, &ip6, &plen);
4442 if (!error) {
4443 if (plen != 128) {
4444 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4445 VLOG_WARN_RL(&rl, "bad next hop mask %s", route->nexthop);
4446 return;
4447 }
4448 is_ipv4 = false;
4449 } else {
4450 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4451 VLOG_WARN_RL(&rl, "bad next hop ip address %s", route->nexthop);
4452 free(error);
4453 return;
4454 }
4455 }
4456
4457 char *prefix_s;
4458 if (is_ipv4) {
4459 ovs_be32 prefix;
4460 /* Verify that ip prefix is a valid IPv4 address. */
4461 error = ip_parse_cidr(route->ip_prefix, &prefix, &plen);
4462 if (error) {
4463 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4464 VLOG_WARN_RL(&rl, "bad 'ip_prefix' in static routes %s",
4465 route->ip_prefix);
4466 free(error);
4467 return;
4468 }
4469 prefix_s = xasprintf(IP_FMT, IP_ARGS(prefix & be32_prefix_mask(plen)));
4470 } else {
4471 /* Verify that ip prefix is a valid IPv6 address. */
4472 struct in6_addr prefix;
4473 error = ipv6_parse_cidr(route->ip_prefix, &prefix, &plen);
4474 if (error) {
4475 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4476 VLOG_WARN_RL(&rl, "bad 'ip_prefix' in static routes %s",
4477 route->ip_prefix);
4478 free(error);
4479 return;
4480 }
4481 struct in6_addr mask = ipv6_create_mask(plen);
4482 struct in6_addr network = ipv6_addr_bitand(&prefix, &mask);
4483 prefix_s = xmalloc(INET6_ADDRSTRLEN);
4484 inet_ntop(AF_INET6, &network, prefix_s, INET6_ADDRSTRLEN);
4485 }
4486
4487 /* Find the outgoing port. */
4488 struct ovn_port *out_port = NULL;
4489 if (route->output_port) {
4490 out_port = ovn_port_find(ports, route->output_port);
4491 if (!out_port) {
4492 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4493 VLOG_WARN_RL(&rl, "Bad out port %s for static route %s",
4494 route->output_port, route->ip_prefix);
4495 goto free_prefix_s;
4496 }
4497 lrp_addr_s = find_lrp_member_ip(out_port, route->nexthop);
4498 if (!lrp_addr_s) {
4499 /* There are no IP networks configured on the router's port via
4500 * which 'route->nexthop' is theoretically reachable. But since
4501 * 'out_port' has been specified, we honor it by trying to reach
4502 * 'route->nexthop' via the first IP address of 'out_port'.
4503 * (There are cases, e.g in GCE, where each VM gets a /32 IP
4504 * address and the default gateway is still reachable from it.) */
4505 if (is_ipv4) {
4506 if (out_port->lrp_networks.n_ipv4_addrs) {
4507 lrp_addr_s = out_port->lrp_networks.ipv4_addrs[0].addr_s;
4508 }
4509 } else {
4510 if (out_port->lrp_networks.n_ipv6_addrs) {
4511 lrp_addr_s = out_port->lrp_networks.ipv6_addrs[0].addr_s;
4512 }
4513 }
4514 }
4515 } else {
4516 /* output_port is not specified, find the
4517 * router port matching the next hop. */
4518 int i;
4519 for (i = 0; i < od->nbr->n_ports; i++) {
4520 struct nbrec_logical_router_port *lrp = od->nbr->ports[i];
4521 out_port = ovn_port_find(ports, lrp->name);
4522 if (!out_port) {
4523 /* This should not happen. */
4524 continue;
4525 }
4526
4527 lrp_addr_s = find_lrp_member_ip(out_port, route->nexthop);
4528 if (lrp_addr_s) {
4529 break;
4530 }
4531 }
4532 }
4533
4534 if (!out_port || !lrp_addr_s) {
4535 /* There is no matched out port. */
4536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4537 VLOG_WARN_RL(&rl, "No path for static route %s; next hop %s",
4538 route->ip_prefix, route->nexthop);
4539 goto free_prefix_s;
4540 }
4541
4542 char *policy = route->policy ? route->policy : "dst-ip";
4543 add_route(lflows, out_port, lrp_addr_s, prefix_s, plen, route->nexthop,
4544 policy);
4545
4546 free_prefix_s:
4547 free(prefix_s);
4548 }
4549
4550 static void
4551 op_put_v4_networks(struct ds *ds, const struct ovn_port *op, bool add_bcast)
4552 {
4553 if (!add_bcast && op->lrp_networks.n_ipv4_addrs == 1) {
4554 ds_put_format(ds, "%s", op->lrp_networks.ipv4_addrs[0].addr_s);
4555 return;
4556 }
4557
4558 ds_put_cstr(ds, "{");
4559 for (int i = 0; i < op->lrp_networks.n_ipv4_addrs; i++) {
4560 ds_put_format(ds, "%s, ", op->lrp_networks.ipv4_addrs[i].addr_s);
4561 if (add_bcast) {
4562 ds_put_format(ds, "%s, ", op->lrp_networks.ipv4_addrs[i].bcast_s);
4563 }
4564 }
4565 ds_chomp(ds, ' ');
4566 ds_chomp(ds, ',');
4567 ds_put_cstr(ds, "}");
4568 }
4569
4570 static void
4571 op_put_v6_networks(struct ds *ds, const struct ovn_port *op)
4572 {
4573 if (op->lrp_networks.n_ipv6_addrs == 1) {
4574 ds_put_format(ds, "%s", op->lrp_networks.ipv6_addrs[0].addr_s);
4575 return;
4576 }
4577
4578 ds_put_cstr(ds, "{");
4579 for (int i = 0; i < op->lrp_networks.n_ipv6_addrs; i++) {
4580 ds_put_format(ds, "%s, ", op->lrp_networks.ipv6_addrs[i].addr_s);
4581 }
4582 ds_chomp(ds, ' ');
4583 ds_chomp(ds, ',');
4584 ds_put_cstr(ds, "}");
4585 }
4586
4587 static const char *
4588 get_force_snat_ip(struct ovn_datapath *od, const char *key_type, ovs_be32 *ip)
4589 {
4590 char *key = xasprintf("%s_force_snat_ip", key_type);
4591 const char *ip_address = smap_get(&od->nbr->options, key);
4592 free(key);
4593
4594 if (ip_address) {
4595 ovs_be32 mask;
4596 char *error = ip_parse_masked(ip_address, ip, &mask);
4597 if (error || mask != OVS_BE32_MAX) {
4598 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4599 VLOG_WARN_RL(&rl, "bad ip %s in options of router "UUID_FMT"",
4600 ip_address, UUID_ARGS(&od->key));
4601 free(error);
4602 *ip = 0;
4603 return NULL;
4604 }
4605 return ip_address;
4606 }
4607
4608 *ip = 0;
4609 return NULL;
4610 }
4611
4612 static void
4613 add_router_lb_flow(struct hmap *lflows, struct ovn_datapath *od,
4614 struct ds *match, struct ds *actions, int priority,
4615 const char *lb_force_snat_ip, char *backend_ips,
4616 bool is_udp, int addr_family)
4617 {
4618 /* A match and actions for new connections. */
4619 char *new_match = xasprintf("ct.new && %s", ds_cstr(match));
4620 if (lb_force_snat_ip) {
4621 char *new_actions = xasprintf("flags.force_snat_for_lb = 1; %s",
4622 ds_cstr(actions));
4623 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, priority, new_match,
4624 new_actions);
4625 free(new_actions);
4626 } else {
4627 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, priority, new_match,
4628 ds_cstr(actions));
4629 }
4630
4631 /* A match and actions for established connections. */
4632 char *est_match = xasprintf("ct.est && %s", ds_cstr(match));
4633 if (lb_force_snat_ip) {
4634 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, priority, est_match,
4635 "flags.force_snat_for_lb = 1; ct_dnat;");
4636 } else {
4637 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, priority, est_match,
4638 "ct_dnat;");
4639 }
4640
4641 free(new_match);
4642 free(est_match);
4643
4644 if (!od->l3dgw_port || !od->l3redirect_port || !backend_ips
4645 || addr_family != AF_INET) {
4646 return;
4647 }
4648
4649 /* Add logical flows to UNDNAT the load balanced reverse traffic in
4650 * the router egress pipleine stage - S_ROUTER_OUT_UNDNAT if the logical
4651 * router has a gateway router port associated.
4652 */
4653 struct ds undnat_match = DS_EMPTY_INITIALIZER;
4654 ds_put_cstr(&undnat_match, "ip4 && (");
4655 char *start, *next, *ip_str;
4656 start = next = xstrdup(backend_ips);
4657 ip_str = strsep(&next, ",");
4658 bool backend_ips_found = false;
4659 while (ip_str && ip_str[0]) {
4660 char *ip_address = NULL;
4661 uint16_t port = 0;
4662 int addr_family_;
4663 ip_address_and_port_from_lb_key(ip_str, &ip_address, &port,
4664 &addr_family_);
4665 if (!ip_address) {
4666 break;
4667 }
4668
4669 ds_put_format(&undnat_match, "(ip4.src == %s", ip_address);
4670 free(ip_address);
4671 if (port) {
4672 ds_put_format(&undnat_match, " && %s.src == %d) || ",
4673 is_udp ? "udp" : "tcp", port);
4674 } else {
4675 ds_put_cstr(&undnat_match, ") || ");
4676 }
4677 ip_str = strsep(&next, ",");
4678 backend_ips_found = true;
4679 }
4680
4681 free(start);
4682 if (!backend_ips_found) {
4683 ds_destroy(&undnat_match);
4684 return;
4685 }
4686 ds_chomp(&undnat_match, ' ');
4687 ds_chomp(&undnat_match, '|');
4688 ds_chomp(&undnat_match, '|');
4689 ds_chomp(&undnat_match, ' ');
4690 ds_put_format(&undnat_match, ") && outport == %s && "
4691 "is_chassis_resident(%s)", od->l3dgw_port->json_key,
4692 od->l3redirect_port->json_key);
4693 if (lb_force_snat_ip) {
4694 ovn_lflow_add(lflows, od, S_ROUTER_OUT_UNDNAT, 120,
4695 ds_cstr(&undnat_match),
4696 "flags.force_snat_for_lb = 1; ct_dnat;");
4697 } else {
4698 ovn_lflow_add(lflows, od, S_ROUTER_OUT_UNDNAT, 120,
4699 ds_cstr(&undnat_match), "ct_dnat;");
4700 }
4701
4702 ds_destroy(&undnat_match);
4703 }
4704
4705 #define ND_RA_MAX_INTERVAL_MAX 1800
4706 #define ND_RA_MAX_INTERVAL_MIN 4
4707
4708 #define ND_RA_MIN_INTERVAL_MAX(max) ((max) * 3 / 4)
4709 #define ND_RA_MIN_INTERVAL_MIN 3
4710
4711 static void
4712 copy_ra_to_sb(struct ovn_port *op, const char *address_mode)
4713 {
4714 struct smap options;
4715 smap_clone(&options, &op->sb->options);
4716
4717 smap_add(&options, "ipv6_ra_send_periodic", "true");
4718 smap_add(&options, "ipv6_ra_address_mode", address_mode);
4719
4720 int max_interval = smap_get_int(&op->nbrp->ipv6_ra_configs,
4721 "max_interval", ND_RA_MAX_INTERVAL_DEFAULT);
4722 if (max_interval > ND_RA_MAX_INTERVAL_MAX) {
4723 max_interval = ND_RA_MAX_INTERVAL_MAX;
4724 }
4725 if (max_interval < ND_RA_MAX_INTERVAL_MIN) {
4726 max_interval = ND_RA_MAX_INTERVAL_MIN;
4727 }
4728 smap_add_format(&options, "ipv6_ra_max_interval", "%d", max_interval);
4729
4730 int min_interval = smap_get_int(&op->nbrp->ipv6_ra_configs,
4731 "min_interval", nd_ra_min_interval_default(max_interval));
4732 if (min_interval > ND_RA_MIN_INTERVAL_MAX(max_interval)) {
4733 min_interval = ND_RA_MIN_INTERVAL_MAX(max_interval);
4734 }
4735 if (min_interval < ND_RA_MIN_INTERVAL_MIN) {
4736 min_interval = ND_RA_MIN_INTERVAL_MIN;
4737 }
4738 smap_add_format(&options, "ipv6_ra_min_interval", "%d", min_interval);
4739
4740 int mtu = smap_get_int(&op->nbrp->ipv6_ra_configs, "mtu", ND_MTU_DEFAULT);
4741 /* RFC 2460 requires the MTU for IPv6 to be at least 1280 */
4742 if (mtu && mtu >= 1280) {
4743 smap_add_format(&options, "ipv6_ra_mtu", "%d", mtu);
4744 }
4745
4746 struct ds s = DS_EMPTY_INITIALIZER;
4747 for (int i = 0; i < op->lrp_networks.n_ipv6_addrs; ++i) {
4748 struct ipv6_netaddr *addrs = &op->lrp_networks.ipv6_addrs[i];
4749 if (in6_is_lla(&addrs->network)) {
4750 smap_add(&options, "ipv6_ra_src_addr", addrs->addr_s);
4751 continue;
4752 }
4753 ds_put_format(&s, "%s/%u ", addrs->network_s, addrs->plen);
4754 }
4755 /* Remove trailing space */
4756 ds_chomp(&s, ' ');
4757 smap_add(&options, "ipv6_ra_prefixes", ds_cstr(&s));
4758 ds_destroy(&s);
4759
4760 smap_add(&options, "ipv6_ra_src_eth", op->lrp_networks.ea_s);
4761
4762 sbrec_port_binding_set_options(op->sb, &options);
4763 smap_destroy(&options);
4764 }
4765
4766 static void
4767 build_lrouter_flows(struct hmap *datapaths, struct hmap *ports,
4768 struct hmap *lflows)
4769 {
4770 /* This flow table structure is documented in ovn-northd(8), so please
4771 * update ovn-northd.8.xml if you change anything. */
4772
4773 struct ds match = DS_EMPTY_INITIALIZER;
4774 struct ds actions = DS_EMPTY_INITIALIZER;
4775
4776 /* Logical router ingress table 0: Admission control framework. */
4777 struct ovn_datapath *od;
4778 HMAP_FOR_EACH (od, key_node, datapaths) {
4779 if (!od->nbr) {
4780 continue;
4781 }
4782
4783 /* Logical VLANs not supported.
4784 * Broadcast/multicast source address is invalid. */
4785 ovn_lflow_add(lflows, od, S_ROUTER_IN_ADMISSION, 100,
4786 "vlan.present || eth.src[40]", "drop;");
4787 }
4788
4789 /* Logical router ingress table 0: match (priority 50). */
4790 struct ovn_port *op;
4791 HMAP_FOR_EACH (op, key_node, ports) {
4792 if (!op->nbrp) {
4793 continue;
4794 }
4795
4796 if (!lrport_is_enabled(op->nbrp)) {
4797 /* Drop packets from disabled logical ports (since logical flow
4798 * tables are default-drop). */
4799 continue;
4800 }
4801
4802 if (op->derived) {
4803 /* No ingress packets should be received on a chassisredirect
4804 * port. */
4805 continue;
4806 }
4807
4808 ds_clear(&match);
4809 ds_put_format(&match, "eth.mcast && inport == %s", op->json_key);
4810 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_ADMISSION, 50,
4811 ds_cstr(&match), "next;");
4812
4813 ds_clear(&match);
4814 ds_put_format(&match, "eth.dst == %s && inport == %s",
4815 op->lrp_networks.ea_s, op->json_key);
4816 if (op->od->l3dgw_port && op == op->od->l3dgw_port
4817 && op->od->l3redirect_port) {
4818 /* Traffic with eth.dst = l3dgw_port->lrp_networks.ea_s
4819 * should only be received on the "redirect-chassis". */
4820 ds_put_format(&match, " && is_chassis_resident(%s)",
4821 op->od->l3redirect_port->json_key);
4822 }
4823 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_ADMISSION, 50,
4824 ds_cstr(&match), "next;");
4825 }
4826
4827 /* Logical router ingress table 1: IP Input. */
4828 HMAP_FOR_EACH (od, key_node, datapaths) {
4829 if (!od->nbr) {
4830 continue;
4831 }
4832
4833 /* L3 admission control: drop multicast and broadcast source, localhost
4834 * source or destination, and zero network source or destination
4835 * (priority 100). */
4836 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 100,
4837 "ip4.mcast || "
4838 "ip4.src == 255.255.255.255 || "
4839 "ip4.src == 127.0.0.0/8 || "
4840 "ip4.dst == 127.0.0.0/8 || "
4841 "ip4.src == 0.0.0.0/8 || "
4842 "ip4.dst == 0.0.0.0/8",
4843 "drop;");
4844
4845 /* ARP reply handling. Use ARP replies to populate the logical
4846 * router's ARP table. */
4847 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 90, "arp.op == 2",
4848 "put_arp(inport, arp.spa, arp.sha);");
4849
4850 /* Drop Ethernet local broadcast. By definition this traffic should
4851 * not be forwarded.*/
4852 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 50,
4853 "eth.bcast", "drop;");
4854
4855 /* TTL discard.
4856 *
4857 * XXX Need to send ICMP time exceeded if !ip.later_frag. */
4858 ds_clear(&match);
4859 ds_put_cstr(&match, "ip4 && ip.ttl == {0, 1}");
4860 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 30,
4861 ds_cstr(&match), "drop;");
4862
4863 /* ND advertisement handling. Use advertisements to populate
4864 * the logical router's ARP/ND table. */
4865 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 90, "nd_na",
4866 "put_nd(inport, nd.target, nd.tll);");
4867
4868 /* Lean from neighbor solicitations that were not directed at
4869 * us. (A priority-90 flow will respond to requests to us and
4870 * learn the sender's mac address. */
4871 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 80, "nd_ns",
4872 "put_nd(inport, ip6.src, nd.sll);");
4873
4874 /* Pass other traffic not already handled to the next table for
4875 * routing. */
4876 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_INPUT, 0, "1", "next;");
4877 }
4878
4879 /* Logical router ingress table 1: IP Input for IPv4. */
4880 HMAP_FOR_EACH (op, key_node, ports) {
4881 if (!op->nbrp) {
4882 continue;
4883 }
4884
4885 if (op->derived) {
4886 /* No ingress packets are accepted on a chassisredirect
4887 * port, so no need to program flows for that port. */
4888 continue;
4889 }
4890
4891 if (op->lrp_networks.n_ipv4_addrs) {
4892 /* L3 admission control: drop packets that originate from an
4893 * IPv4 address owned by the router or a broadcast address
4894 * known to the router (priority 100). */
4895 ds_clear(&match);
4896 ds_put_cstr(&match, "ip4.src == ");
4897 op_put_v4_networks(&match, op, true);
4898 ds_put_cstr(&match, " && "REGBIT_EGRESS_LOOPBACK" == 0");
4899 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 100,
4900 ds_cstr(&match), "drop;");
4901
4902 /* ICMP echo reply. These flows reply to ICMP echo requests
4903 * received for the router's IP address. Since packets only
4904 * get here as part of the logical router datapath, the inport
4905 * (i.e. the incoming locally attached net) does not matter.
4906 * The ip.ttl also does not matter (RFC1812 section 4.2.2.9) */
4907 ds_clear(&match);
4908 ds_put_cstr(&match, "ip4.dst == ");
4909 op_put_v4_networks(&match, op, false);
4910 ds_put_cstr(&match, " && icmp4.type == 8 && icmp4.code == 0");
4911
4912 ds_clear(&actions);
4913 ds_put_format(&actions,
4914 "ip4.dst <-> ip4.src; "
4915 "ip.ttl = 255; "
4916 "icmp4.type = 0; "
4917 "flags.loopback = 1; "
4918 "next; ");
4919 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
4920 ds_cstr(&match), ds_cstr(&actions));
4921 }
4922
4923 /* ARP reply. These flows reply to ARP requests for the router's own
4924 * IP address. */
4925 for (int i = 0; i < op->lrp_networks.n_ipv4_addrs; i++) {
4926 ds_clear(&match);
4927 ds_put_format(&match,
4928 "inport == %s && arp.tpa == %s && arp.op == 1",
4929 op->json_key, op->lrp_networks.ipv4_addrs[i].addr_s);
4930 if (op->od->l3dgw_port && op == op->od->l3dgw_port
4931 && op->od->l3redirect_port) {
4932 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
4933 * should only be sent from the "redirect-chassis", so that
4934 * upstream MAC learning points to the "redirect-chassis".
4935 * Also need to avoid generation of multiple ARP responses
4936 * from different chassis. */
4937 ds_put_format(&match, " && is_chassis_resident(%s)",
4938 op->od->l3redirect_port->json_key);
4939 }
4940
4941 ds_clear(&actions);
4942 ds_put_format(&actions,
4943 "eth.dst = eth.src; "
4944 "eth.src = %s; "
4945 "arp.op = 2; /* ARP reply */ "
4946 "arp.tha = arp.sha; "
4947 "arp.sha = %s; "
4948 "arp.tpa = arp.spa; "
4949 "arp.spa = %s; "
4950 "outport = %s; "
4951 "flags.loopback = 1; "
4952 "output;",
4953 op->lrp_networks.ea_s,
4954 op->lrp_networks.ea_s,
4955 op->lrp_networks.ipv4_addrs[i].addr_s,
4956 op->json_key);
4957 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
4958 ds_cstr(&match), ds_cstr(&actions));
4959 }
4960
4961 /* A set to hold all load-balancer vips that need ARP responses. */
4962 struct sset all_ips = SSET_INITIALIZER(&all_ips);
4963 int addr_family;
4964 get_router_load_balancer_ips(op->od, &all_ips, &addr_family);
4965
4966 const char *ip_address;
4967 SSET_FOR_EACH(ip_address, &all_ips) {
4968 ds_clear(&match);
4969 if (addr_family == AF_INET) {
4970 ds_put_format(&match,
4971 "inport == %s && arp.tpa == %s && arp.op == 1",
4972 op->json_key, ip_address);
4973 } else {
4974 ds_put_format(&match,
4975 "inport == %s && nd_ns && nd.target == %s",
4976 op->json_key, ip_address);
4977 }
4978
4979 ds_clear(&actions);
4980 if (addr_family == AF_INET) {
4981 ds_put_format(&actions,
4982 "eth.dst = eth.src; "
4983 "eth.src = %s; "
4984 "arp.op = 2; /* ARP reply */ "
4985 "arp.tha = arp.sha; "
4986 "arp.sha = %s; "
4987 "arp.tpa = arp.spa; "
4988 "arp.spa = %s; "
4989 "outport = %s; "
4990 "flags.loopback = 1; "
4991 "output;",
4992 op->lrp_networks.ea_s,
4993 op->lrp_networks.ea_s,
4994 ip_address,
4995 op->json_key);
4996 } else {
4997 ds_put_format(&actions,
4998 "nd_na { "
4999 "eth.src = %s; "
5000 "ip6.src = %s; "
5001 "nd.target = %s; "
5002 "nd.tll = %s; "
5003 "outport = inport; "
5004 "flags.loopback = 1; "
5005 "output; "
5006 "};",
5007 op->lrp_networks.ea_s,
5008 ip_address,
5009 ip_address,
5010 op->lrp_networks.ea_s);
5011 }
5012 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
5013 ds_cstr(&match), ds_cstr(&actions));
5014 }
5015
5016 sset_destroy(&all_ips);
5017
5018 /* A gateway router can have 2 SNAT IP addresses to force DNATed and
5019 * LBed traffic respectively to be SNATed. In addition, there can be
5020 * a number of SNAT rules in the NAT table. */
5021 ovs_be32 *snat_ips = xmalloc(sizeof *snat_ips *
5022 (op->od->nbr->n_nat + 2));
5023 size_t n_snat_ips = 0;
5024
5025 ovs_be32 snat_ip;
5026 const char *dnat_force_snat_ip = get_force_snat_ip(op->od, "dnat",
5027 &snat_ip);
5028 if (dnat_force_snat_ip) {
5029 snat_ips[n_snat_ips++] = snat_ip;
5030 }
5031
5032 const char *lb_force_snat_ip = get_force_snat_ip(op->od, "lb",
5033 &snat_ip);
5034 if (lb_force_snat_ip) {
5035 snat_ips[n_snat_ips++] = snat_ip;
5036 }
5037
5038 for (int i = 0; i < op->od->nbr->n_nat; i++) {
5039 const struct nbrec_nat *nat;
5040
5041 nat = op->od->nbr->nat[i];
5042
5043 ovs_be32 ip;
5044 if (!ip_parse(nat->external_ip, &ip) || !ip) {
5045 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
5046 VLOG_WARN_RL(&rl, "bad ip address %s in nat configuration "
5047 "for router %s", nat->external_ip, op->key);
5048 continue;
5049 }
5050
5051 if (!strcmp(nat->type, "snat")) {
5052 snat_ips[n_snat_ips++] = ip;
5053 continue;
5054 }
5055
5056 /* ARP handling for external IP addresses.
5057 *
5058 * DNAT IP addresses are external IP addresses that need ARP
5059 * handling. */
5060 ds_clear(&match);
5061 ds_put_format(&match,
5062 "inport == %s && arp.tpa == "IP_FMT" && arp.op == 1",
5063 op->json_key, IP_ARGS(ip));
5064
5065 ds_clear(&actions);
5066 ds_put_format(&actions,
5067 "eth.dst = eth.src; "
5068 "arp.op = 2; /* ARP reply */ "
5069 "arp.tha = arp.sha; ");
5070
5071 if (op->od->l3dgw_port && op == op->od->l3dgw_port) {
5072 struct eth_addr mac;
5073 if (nat->external_mac &&
5074 eth_addr_from_string(nat->external_mac, &mac)
5075 && nat->logical_port) {
5076 /* distributed NAT case, use nat->external_mac */
5077 ds_put_format(&actions,
5078 "eth.src = "ETH_ADDR_FMT"; "
5079 "arp.sha = "ETH_ADDR_FMT"; ",
5080 ETH_ADDR_ARGS(mac),
5081 ETH_ADDR_ARGS(mac));
5082 /* Traffic with eth.src = nat->external_mac should only be
5083 * sent from the chassis where nat->logical_port is
5084 * resident, so that upstream MAC learning points to the
5085 * correct chassis. Also need to avoid generation of
5086 * multiple ARP responses from different chassis. */
5087 ds_put_format(&match, " && is_chassis_resident(\"%s\")",
5088 nat->logical_port);
5089 } else {
5090 ds_put_format(&actions,
5091 "eth.src = %s; "
5092 "arp.sha = %s; ",
5093 op->lrp_networks.ea_s,
5094 op->lrp_networks.ea_s);
5095 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
5096 * should only be sent from the "redirect-chassis", so that
5097 * upstream MAC learning points to the "redirect-chassis".
5098 * Also need to avoid generation of multiple ARP responses
5099 * from different chassis. */
5100 if (op->od->l3redirect_port) {
5101 ds_put_format(&match, " && is_chassis_resident(%s)",
5102 op->od->l3redirect_port->json_key);
5103 }
5104 }
5105 } else {
5106 ds_put_format(&actions,
5107 "eth.src = %s; "
5108 "arp.sha = %s; ",
5109 op->lrp_networks.ea_s,
5110 op->lrp_networks.ea_s);
5111 }
5112 ds_put_format(&actions,
5113 "arp.tpa = arp.spa; "
5114 "arp.spa = "IP_FMT"; "
5115 "outport = %s; "
5116 "flags.loopback = 1; "
5117 "output;",
5118 IP_ARGS(ip),
5119 op->json_key);
5120 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
5121 ds_cstr(&match), ds_cstr(&actions));
5122 }
5123
5124 ds_clear(&match);
5125 ds_put_cstr(&match, "ip4.dst == {");
5126 bool has_drop_ips = false;
5127 for (int i = 0; i < op->lrp_networks.n_ipv4_addrs; i++) {
5128 bool snat_ip_is_router_ip = false;
5129 for (int j = 0; j < n_snat_ips; j++) {
5130 /* Packets to SNAT IPs should not be dropped. */
5131 if (op->lrp_networks.ipv4_addrs[i].addr == snat_ips[j]) {
5132 snat_ip_is_router_ip = true;
5133 break;
5134 }
5135 }
5136 if (snat_ip_is_router_ip) {
5137 continue;
5138 }
5139 ds_put_format(&match, "%s, ",
5140 op->lrp_networks.ipv4_addrs[i].addr_s);
5141 has_drop_ips = true;
5142 }
5143 ds_chomp(&match, ' ');
5144 ds_chomp(&match, ',');
5145 ds_put_cstr(&match, "}");
5146
5147 if (has_drop_ips) {
5148 /* Drop IP traffic to this router. */
5149 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 60,
5150 ds_cstr(&match), "drop;");
5151 }
5152
5153 free(snat_ips);
5154 }
5155
5156 /* Logical router ingress table 1: IP Input for IPv6. */
5157 HMAP_FOR_EACH (op, key_node, ports) {
5158 if (!op->nbrp) {
5159 continue;
5160 }
5161
5162 if (op->derived) {
5163 /* No ingress packets are accepted on a chassisredirect
5164 * port, so no need to program flows for that port. */
5165 continue;
5166 }
5167
5168 if (op->lrp_networks.n_ipv6_addrs) {
5169 /* L3 admission control: drop packets that originate from an
5170 * IPv6 address owned by the router (priority 100). */
5171 ds_clear(&match);
5172 ds_put_cstr(&match, "ip6.src == ");
5173 op_put_v6_networks(&match, op);
5174 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 100,
5175 ds_cstr(&match), "drop;");
5176
5177 /* ICMPv6 echo reply. These flows reply to echo requests
5178 * received for the router's IP address. */
5179 ds_clear(&match);
5180 ds_put_cstr(&match, "ip6.dst == ");
5181 op_put_v6_networks(&match, op);
5182 ds_put_cstr(&match, " && icmp6.type == 128 && icmp6.code == 0");
5183
5184 ds_clear(&actions);
5185 ds_put_cstr(&actions,
5186 "ip6.dst <-> ip6.src; "
5187 "ip.ttl = 255; "
5188 "icmp6.type = 129; "
5189 "flags.loopback = 1; "
5190 "next; ");
5191 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
5192 ds_cstr(&match), ds_cstr(&actions));
5193
5194 /* Drop IPv6 traffic to this router. */
5195 ds_clear(&match);
5196 ds_put_cstr(&match, "ip6.dst == ");
5197 op_put_v6_networks(&match, op);
5198 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 60,
5199 ds_cstr(&match), "drop;");
5200 }
5201
5202 /* ND reply. These flows reply to ND solicitations for the
5203 * router's own IP address. */
5204 for (int i = 0; i < op->lrp_networks.n_ipv6_addrs; i++) {
5205 ds_clear(&match);
5206 ds_put_format(&match,
5207 "inport == %s && nd_ns && ip6.dst == {%s, %s} "
5208 "&& nd.target == %s",
5209 op->json_key,
5210 op->lrp_networks.ipv6_addrs[i].addr_s,
5211 op->lrp_networks.ipv6_addrs[i].sn_addr_s,
5212 op->lrp_networks.ipv6_addrs[i].addr_s);
5213 if (op->od->l3dgw_port && op == op->od->l3dgw_port
5214 && op->od->l3redirect_port) {
5215 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
5216 * should only be sent from the "redirect-chassis", so that
5217 * upstream MAC learning points to the "redirect-chassis".
5218 * Also need to avoid generation of multiple ND replies
5219 * from different chassis. */
5220 ds_put_format(&match, " && is_chassis_resident(%s)",
5221 op->od->l3redirect_port->json_key);
5222 }
5223
5224 ds_clear(&actions);
5225 ds_put_format(&actions,
5226 "put_nd(inport, ip6.src, nd.sll); "
5227 "nd_na { "
5228 "eth.src = %s; "
5229 "ip6.src = %s; "
5230 "nd.target = %s; "
5231 "nd.tll = %s; "
5232 "outport = inport; "
5233 "flags.loopback = 1; "
5234 "output; "
5235 "};",
5236 op->lrp_networks.ea_s,
5237 op->lrp_networks.ipv6_addrs[i].addr_s,
5238 op->lrp_networks.ipv6_addrs[i].addr_s,
5239 op->lrp_networks.ea_s);
5240 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_IP_INPUT, 90,
5241 ds_cstr(&match), ds_cstr(&actions));
5242 }
5243 }
5244
5245 /* NAT, Defrag and load balancing. */
5246 HMAP_FOR_EACH (od, key_node, datapaths) {
5247 if (!od->nbr) {
5248 continue;
5249 }
5250
5251 /* Packets are allowed by default. */
5252 ovn_lflow_add(lflows, od, S_ROUTER_IN_DEFRAG, 0, "1", "next;");
5253 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 0, "1", "next;");
5254 ovn_lflow_add(lflows, od, S_ROUTER_OUT_SNAT, 0, "1", "next;");
5255 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, 0, "1", "next;");
5256 ovn_lflow_add(lflows, od, S_ROUTER_OUT_UNDNAT, 0, "1", "next;");
5257 ovn_lflow_add(lflows, od, S_ROUTER_OUT_EGR_LOOP, 0, "1", "next;");
5258
5259 /* NAT rules are only valid on Gateway routers and routers with
5260 * l3dgw_port (router has a port with "redirect-chassis"
5261 * specified). */
5262 if (!smap_get(&od->nbr->options, "chassis") && !od->l3dgw_port) {
5263 continue;
5264 }
5265
5266 ovs_be32 snat_ip;
5267 const char *dnat_force_snat_ip = get_force_snat_ip(od, "dnat",
5268 &snat_ip);
5269 const char *lb_force_snat_ip = get_force_snat_ip(od, "lb",
5270 &snat_ip);
5271
5272 for (int i = 0; i < od->nbr->n_nat; i++) {
5273 const struct nbrec_nat *nat;
5274
5275 nat = od->nbr->nat[i];
5276
5277 ovs_be32 ip, mask;
5278
5279 char *error = ip_parse_masked(nat->external_ip, &ip, &mask);
5280 if (error || mask != OVS_BE32_MAX) {
5281 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
5282 VLOG_WARN_RL(&rl, "bad external ip %s for nat",
5283 nat->external_ip);
5284 free(error);
5285 continue;
5286 }
5287
5288 /* Check the validity of nat->logical_ip. 'logical_ip' can
5289 * be a subnet when the type is "snat". */
5290 error = ip_parse_masked(nat->logical_ip, &ip, &mask);
5291 if (!strcmp(nat->type, "snat")) {
5292 if (error) {
5293 static struct vlog_rate_limit rl =
5294 VLOG_RATE_LIMIT_INIT(5, 1);
5295 VLOG_WARN_RL(&rl, "bad ip network or ip %s for snat "
5296 "in router "UUID_FMT"",
5297 nat->logical_ip, UUID_ARGS(&od->key));
5298 free(error);
5299 continue;
5300 }
5301 } else {
5302 if (error || mask != OVS_BE32_MAX) {
5303 static struct vlog_rate_limit rl =
5304 VLOG_RATE_LIMIT_INIT(5, 1);
5305 VLOG_WARN_RL(&rl, "bad ip %s for dnat in router "
5306 ""UUID_FMT"", nat->logical_ip, UUID_ARGS(&od->key));
5307 free(error);
5308 continue;
5309 }
5310 }
5311
5312 /* For distributed router NAT, determine whether this NAT rule
5313 * satisfies the conditions for distributed NAT processing. */
5314 bool distributed = false;
5315 struct eth_addr mac;
5316 if (od->l3dgw_port && !strcmp(nat->type, "dnat_and_snat") &&
5317 nat->logical_port && nat->external_mac) {
5318 if (eth_addr_from_string(nat->external_mac, &mac)) {
5319 distributed = true;
5320 } else {
5321 static struct vlog_rate_limit rl =
5322 VLOG_RATE_LIMIT_INIT(5, 1);
5323 VLOG_WARN_RL(&rl, "bad mac %s for dnat in router "
5324 ""UUID_FMT"", nat->external_mac, UUID_ARGS(&od->key));
5325 continue;
5326 }
5327 }
5328
5329 /* Ingress UNSNAT table: It is for already established connections'
5330 * reverse traffic. i.e., SNAT has already been done in egress
5331 * pipeline and now the packet has entered the ingress pipeline as
5332 * part of a reply. We undo the SNAT here.
5333 *
5334 * Undoing SNAT has to happen before DNAT processing. This is
5335 * because when the packet was DNATed in ingress pipeline, it did
5336 * not know about the possibility of eventual additional SNAT in
5337 * egress pipeline. */
5338 if (!strcmp(nat->type, "snat")
5339 || !strcmp(nat->type, "dnat_and_snat")) {
5340 if (!od->l3dgw_port) {
5341 /* Gateway router. */
5342 ds_clear(&match);
5343 ds_put_format(&match, "ip && ip4.dst == %s",
5344 nat->external_ip);
5345 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 90,
5346 ds_cstr(&match), "ct_snat;");
5347 } else {
5348 /* Distributed router. */
5349
5350 /* Traffic received on l3dgw_port is subject to NAT. */
5351 ds_clear(&match);
5352 ds_put_format(&match, "ip && ip4.dst == %s"
5353 " && inport == %s",
5354 nat->external_ip,
5355 od->l3dgw_port->json_key);
5356 if (!distributed && od->l3redirect_port) {
5357 /* Flows for NAT rules that are centralized are only
5358 * programmed on the "redirect-chassis". */
5359 ds_put_format(&match, " && is_chassis_resident(%s)",
5360 od->l3redirect_port->json_key);
5361 }
5362 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 100,
5363 ds_cstr(&match), "ct_snat;");
5364
5365 /* Traffic received on other router ports must be
5366 * redirected to the central instance of the l3dgw_port
5367 * for NAT processing. */
5368 ds_clear(&match);
5369 ds_put_format(&match, "ip && ip4.dst == %s",
5370 nat->external_ip);
5371 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 50,
5372 ds_cstr(&match),
5373 REGBIT_NAT_REDIRECT" = 1; next;");
5374 }
5375 }
5376
5377 /* Ingress DNAT table: Packets enter the pipeline with destination
5378 * IP address that needs to be DNATted from a external IP address
5379 * to a logical IP address. */
5380 if (!strcmp(nat->type, "dnat")
5381 || !strcmp(nat->type, "dnat_and_snat")) {
5382 if (!od->l3dgw_port) {
5383 /* Gateway router. */
5384 /* Packet when it goes from the initiator to destination.
5385 * We need to set flags.loopback because the router can
5386 * send the packet back through the same interface. */
5387 ds_clear(&match);
5388 ds_put_format(&match, "ip && ip4.dst == %s",
5389 nat->external_ip);
5390 ds_clear(&actions);
5391 if (dnat_force_snat_ip) {
5392 /* Indicate to the future tables that a DNAT has taken
5393 * place and a force SNAT needs to be done in the
5394 * Egress SNAT table. */
5395 ds_put_format(&actions,
5396 "flags.force_snat_for_dnat = 1; ");
5397 }
5398 ds_put_format(&actions, "flags.loopback = 1; ct_dnat(%s);",
5399 nat->logical_ip);
5400 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, 100,
5401 ds_cstr(&match), ds_cstr(&actions));
5402 } else {
5403 /* Distributed router. */
5404
5405 /* Traffic received on l3dgw_port is subject to NAT. */
5406 ds_clear(&match);
5407 ds_put_format(&match, "ip && ip4.dst == %s"
5408 " && inport == %s",
5409 nat->external_ip,
5410 od->l3dgw_port->json_key);
5411 if (!distributed && od->l3redirect_port) {
5412 /* Flows for NAT rules that are centralized are only
5413 * programmed on the "redirect-chassis". */
5414 ds_put_format(&match, " && is_chassis_resident(%s)",
5415 od->l3redirect_port->json_key);
5416 }
5417 ds_clear(&actions);
5418 ds_put_format(&actions, "ct_dnat(%s);",
5419 nat->logical_ip);
5420 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, 100,
5421 ds_cstr(&match), ds_cstr(&actions));
5422
5423 /* Traffic received on other router ports must be
5424 * redirected to the central instance of the l3dgw_port
5425 * for NAT processing. */
5426 ds_clear(&match);
5427 ds_put_format(&match, "ip && ip4.dst == %s",
5428 nat->external_ip);
5429 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, 50,
5430 ds_cstr(&match),
5431 REGBIT_NAT_REDIRECT" = 1; next;");
5432 }
5433 }
5434
5435 /* Egress UNDNAT table: It is for already established connections'
5436 * reverse traffic. i.e., DNAT has already been done in ingress
5437 * pipeline and now the packet has entered the egress pipeline as
5438 * part of a reply. We undo the DNAT here.
5439 *
5440 * Note that this only applies for NAT on a distributed router.
5441 * Undo DNAT on a gateway router is done in the ingress DNAT
5442 * pipeline stage. */
5443 if (od->l3dgw_port && (!strcmp(nat->type, "dnat")
5444 || !strcmp(nat->type, "dnat_and_snat"))) {
5445 ds_clear(&match);
5446 ds_put_format(&match, "ip && ip4.src == %s"
5447 " && outport == %s",
5448 nat->logical_ip,
5449 od->l3dgw_port->json_key);
5450 if (!distributed && od->l3redirect_port) {
5451 /* Flows for NAT rules that are centralized are only
5452 * programmed on the "redirect-chassis". */
5453 ds_put_format(&match, " && is_chassis_resident(%s)",
5454 od->l3redirect_port->json_key);
5455 }
5456 ds_clear(&actions);
5457 if (distributed) {
5458 ds_put_format(&actions, "eth.src = "ETH_ADDR_FMT"; ",
5459 ETH_ADDR_ARGS(mac));
5460 }
5461 ds_put_format(&actions, "ct_dnat;");
5462 ovn_lflow_add(lflows, od, S_ROUTER_OUT_UNDNAT, 100,
5463 ds_cstr(&match), ds_cstr(&actions));
5464 }
5465
5466 /* Egress SNAT table: Packets enter the egress pipeline with
5467 * source ip address that needs to be SNATted to a external ip
5468 * address. */
5469 if (!strcmp(nat->type, "snat")
5470 || !strcmp(nat->type, "dnat_and_snat")) {
5471 if (!od->l3dgw_port) {
5472 /* Gateway router. */
5473 ds_clear(&match);
5474 ds_put_format(&match, "ip && ip4.src == %s",
5475 nat->logical_ip);
5476 ds_clear(&actions);
5477 ds_put_format(&actions, "ct_snat(%s);", nat->external_ip);
5478
5479 /* The priority here is calculated such that the
5480 * nat->logical_ip with the longest mask gets a higher
5481 * priority. */
5482 ovn_lflow_add(lflows, od, S_ROUTER_OUT_SNAT,
5483 count_1bits(ntohl(mask)) + 1,
5484 ds_cstr(&match), ds_cstr(&actions));
5485 } else {
5486 /* Distributed router. */
5487 ds_clear(&match);
5488 ds_put_format(&match, "ip && ip4.src == %s"
5489 " && outport == %s",
5490 nat->logical_ip,
5491 od->l3dgw_port->json_key);
5492 if (!distributed && od->l3redirect_port) {
5493 /* Flows for NAT rules that are centralized are only
5494 * programmed on the "redirect-chassis". */
5495 ds_put_format(&match, " && is_chassis_resident(%s)",
5496 od->l3redirect_port->json_key);
5497 }
5498 ds_clear(&actions);
5499 if (distributed) {
5500 ds_put_format(&actions, "eth.src = "ETH_ADDR_FMT"; ",
5501 ETH_ADDR_ARGS(mac));
5502 }
5503 ds_put_format(&actions, "ct_snat(%s);", nat->external_ip);
5504
5505 /* The priority here is calculated such that the
5506 * nat->logical_ip with the longest mask gets a higher
5507 * priority. */
5508 ovn_lflow_add(lflows, od, S_ROUTER_OUT_SNAT,
5509 count_1bits(ntohl(mask)) + 1,
5510 ds_cstr(&match), ds_cstr(&actions));
5511 }
5512 }
5513
5514 /* Logical router ingress table 0:
5515 * For NAT on a distributed router, add rules allowing
5516 * ingress traffic with eth.dst matching nat->external_mac
5517 * on the l3dgw_port instance where nat->logical_port is
5518 * resident. */
5519 if (distributed) {
5520 ds_clear(&match);
5521 ds_put_format(&match,
5522 "eth.dst == "ETH_ADDR_FMT" && inport == %s"
5523 " && is_chassis_resident(\"%s\")",
5524 ETH_ADDR_ARGS(mac),
5525 od->l3dgw_port->json_key,
5526 nat->logical_port);
5527 ovn_lflow_add(lflows, od, S_ROUTER_IN_ADMISSION, 50,
5528 ds_cstr(&match), "next;");
5529 }
5530
5531 /* Ingress Gateway Redirect Table: For NAT on a distributed
5532 * router, add flows that are specific to a NAT rule. These
5533 * flows indicate the presence of an applicable NAT rule that
5534 * can be applied in a distributed manner. */
5535 if (distributed) {
5536 ds_clear(&match);
5537 ds_put_format(&match, "ip4.src == %s && outport == %s",
5538 nat->logical_ip,
5539 od->l3dgw_port->json_key);
5540 ovn_lflow_add(lflows, od, S_ROUTER_IN_GW_REDIRECT, 100,
5541 ds_cstr(&match), "next;");
5542 }
5543
5544 /* Egress Loopback table: For NAT on a distributed router.
5545 * If packets in the egress pipeline on the distributed
5546 * gateway port have ip.dst matching a NAT external IP, then
5547 * loop a clone of the packet back to the beginning of the
5548 * ingress pipeline with inport = outport. */
5549 if (od->l3dgw_port) {
5550 /* Distributed router. */
5551 ds_clear(&match);
5552 ds_put_format(&match, "ip4.dst == %s && outport == %s",
5553 nat->external_ip,
5554 od->l3dgw_port->json_key);
5555 ds_clear(&actions);
5556 ds_put_format(&actions,
5557 "clone { ct_clear; "
5558 "inport = outport; outport = \"\"; "
5559 "flags = 0; flags.loopback = 1; ");
5560 for (int j = 0; j < MFF_N_LOG_REGS; j++) {
5561 ds_put_format(&actions, "reg%d = 0; ", j);
5562 }
5563 ds_put_format(&actions, REGBIT_EGRESS_LOOPBACK" = 1; "
5564 "next(pipeline=ingress, table=0); };");
5565 ovn_lflow_add(lflows, od, S_ROUTER_OUT_EGR_LOOP, 100,
5566 ds_cstr(&match), ds_cstr(&actions));
5567 }
5568 }
5569
5570 /* Handle force SNAT options set in the gateway router. */
5571 if (dnat_force_snat_ip && !od->l3dgw_port) {
5572 /* If a packet with destination IP address as that of the
5573 * gateway router (as set in options:dnat_force_snat_ip) is seen,
5574 * UNSNAT it. */
5575 ds_clear(&match);
5576 ds_put_format(&match, "ip && ip4.dst == %s", dnat_force_snat_ip);
5577 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 110,
5578 ds_cstr(&match), "ct_snat;");
5579
5580 /* Higher priority rules to force SNAT with the IP addresses
5581 * configured in the Gateway router. This only takes effect
5582 * when the packet has already been DNATed once. */
5583 ds_clear(&match);
5584 ds_put_format(&match, "flags.force_snat_for_dnat == 1 && ip");
5585 ds_clear(&actions);
5586 ds_put_format(&actions, "ct_snat(%s);", dnat_force_snat_ip);
5587 ovn_lflow_add(lflows, od, S_ROUTER_OUT_SNAT, 100,
5588 ds_cstr(&match), ds_cstr(&actions));
5589 }
5590 if (lb_force_snat_ip && !od->l3dgw_port) {
5591 /* If a packet with destination IP address as that of the
5592 * gateway router (as set in options:lb_force_snat_ip) is seen,
5593 * UNSNAT it. */
5594 ds_clear(&match);
5595 ds_put_format(&match, "ip && ip4.dst == %s", lb_force_snat_ip);
5596 ovn_lflow_add(lflows, od, S_ROUTER_IN_UNSNAT, 100,
5597 ds_cstr(&match), "ct_snat;");
5598
5599 /* Load balanced traffic will have flags.force_snat_for_lb set.
5600 * Force SNAT it. */
5601 ds_clear(&match);
5602 ds_put_format(&match, "flags.force_snat_for_lb == 1 && ip");
5603 ds_clear(&actions);
5604 ds_put_format(&actions, "ct_snat(%s);", lb_force_snat_ip);
5605 ovn_lflow_add(lflows, od, S_ROUTER_OUT_SNAT, 100,
5606 ds_cstr(&match), ds_cstr(&actions));
5607 }
5608
5609 if (!od->l3dgw_port) {
5610 /* For gateway router, re-circulate every packet through
5611 * the DNAT zone. This helps with the following.
5612 *
5613 * Any packet that needs to be unDNATed in the reverse
5614 * direction gets unDNATed. Ideally this could be done in
5615 * the egress pipeline. But since the gateway router
5616 * does not have any feature that depends on the source
5617 * ip address being external IP address for IP routing,
5618 * we can do it here, saving a future re-circulation. */
5619 ovn_lflow_add(lflows, od, S_ROUTER_IN_DNAT, 50,
5620 "ip", "flags.loopback = 1; ct_dnat;");
5621 } else {
5622 /* For NAT on a distributed router, add flows to Ingress
5623 * IP Routing table, Ingress ARP Resolution table, and
5624 * Ingress Gateway Redirect Table that are not specific to a
5625 * NAT rule. */
5626
5627 /* The highest priority IN_IP_ROUTING rule matches packets
5628 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages),
5629 * with action "ip.ttl--; next;". The IN_GW_REDIRECT table
5630 * will take care of setting the outport. */
5631 ovn_lflow_add(lflows, od, S_ROUTER_IN_IP_ROUTING, 300,
5632 REGBIT_NAT_REDIRECT" == 1", "ip.ttl--; next;");
5633
5634 /* The highest priority IN_ARP_RESOLVE rule matches packets
5635 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages),
5636 * then sets eth.dst to the distributed gateway port's
5637 * ethernet address. */
5638 ds_clear(&actions);
5639 ds_put_format(&actions, "eth.dst = %s; next;",
5640 od->l3dgw_port->lrp_networks.ea_s);
5641 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_RESOLVE, 200,
5642 REGBIT_NAT_REDIRECT" == 1", ds_cstr(&actions));
5643
5644 /* The highest priority IN_GW_REDIRECT rule redirects packets
5645 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages) to
5646 * the central instance of the l3dgw_port for NAT processing. */
5647 ds_clear(&actions);
5648 ds_put_format(&actions, "outport = %s; next;",
5649 od->l3redirect_port->json_key);
5650 ovn_lflow_add(lflows, od, S_ROUTER_IN_GW_REDIRECT, 200,
5651 REGBIT_NAT_REDIRECT" == 1", ds_cstr(&actions));
5652 }
5653
5654 /* Load balancing and packet defrag are only valid on
5655 * Gateway routers or router with gateway port. */
5656 if (!smap_get(&od->nbr->options, "chassis") && !od->l3dgw_port) {
5657 continue;
5658 }
5659
5660 /* A set to hold all ips that need defragmentation and tracking. */
5661 struct sset all_ips = SSET_INITIALIZER(&all_ips);
5662
5663 for (int i = 0; i < od->nbr->n_load_balancer; i++) {
5664 struct nbrec_load_balancer *lb = od->nbr->load_balancer[i];
5665 struct smap *vips = &lb->vips;
5666 struct smap_node *node;
5667
5668 SMAP_FOR_EACH (node, vips) {
5669 uint16_t port = 0;
5670 int addr_family;
5671
5672 /* node->key contains IP:port or just IP. */
5673 char *ip_address = NULL;
5674 ip_address_and_port_from_lb_key(node->key, &ip_address, &port,
5675 &addr_family);
5676 if (!ip_address) {
5677 continue;
5678 }
5679
5680 if (!sset_contains(&all_ips, ip_address)) {
5681 sset_add(&all_ips, ip_address);
5682 /* If there are any load balancing rules, we should send
5683 * the packet to conntrack for defragmentation and
5684 * tracking. This helps with two things.
5685 *
5686 * 1. With tracking, we can send only new connections to
5687 * pick a DNAT ip address from a group.
5688 * 2. If there are L4 ports in load balancing rules, we
5689 * need the defragmentation to match on L4 ports. */
5690 ds_clear(&match);
5691 if (addr_family == AF_INET) {
5692 ds_put_format(&match, "ip && ip4.dst == %s",
5693 ip_address);
5694 } else {
5695 ds_put_format(&match, "ip && ip6.dst == %s",
5696 ip_address);
5697 }
5698 ovn_lflow_add(lflows, od, S_ROUTER_IN_DEFRAG,
5699 100, ds_cstr(&match), "ct_next;");
5700 }
5701
5702 /* Higher priority rules are added for load-balancing in DNAT
5703 * table. For every match (on a VIP[:port]), we add two flows
5704 * via add_router_lb_flow(). One flow is for specific matching
5705 * on ct.new with an action of "ct_lb($targets);". The other
5706 * flow is for ct.est with an action of "ct_dnat;". */
5707 ds_clear(&actions);
5708 ds_put_format(&actions, "ct_lb(%s);", node->value);
5709
5710 ds_clear(&match);
5711 if (addr_family == AF_INET) {
5712 ds_put_format(&match, "ip && ip4.dst == %s",
5713 ip_address);
5714 } else {
5715 ds_put_format(&match, "ip && ip6.dst == %s",
5716 ip_address);
5717 }
5718 free(ip_address);
5719
5720 int prio = 110;
5721 bool is_udp = lb->protocol && !strcmp(lb->protocol, "udp") ?
5722 true : false;
5723 if (port) {
5724 if (is_udp) {
5725 ds_put_format(&match, " && udp && udp.dst == %d",
5726 port);
5727 } else {
5728 ds_put_format(&match, " && tcp && tcp.dst == %d",
5729 port);
5730 }
5731 prio = 120;
5732 }
5733
5734 if (od->l3redirect_port) {
5735 ds_put_format(&match, " && is_chassis_resident(%s)",
5736 od->l3redirect_port->json_key);
5737 }
5738 add_router_lb_flow(lflows, od, &match, &actions, prio,
5739 lb_force_snat_ip, node->value, is_udp,
5740 addr_family);
5741 }
5742 }
5743 sset_destroy(&all_ips);
5744 }
5745
5746 /* Logical router ingress table 5 and 6: IPv6 Router Adv (RA) options and
5747 * response. */
5748 HMAP_FOR_EACH (op, key_node, ports) {
5749 if (!op->nbrp || op->nbrp->peer || !op->peer) {
5750 continue;
5751 }
5752
5753 if (!op->lrp_networks.n_ipv6_addrs) {
5754 continue;
5755 }
5756
5757 const char *address_mode = smap_get(
5758 &op->nbrp->ipv6_ra_configs, "address_mode");
5759
5760 if (!address_mode) {
5761 continue;
5762 }
5763 if (strcmp(address_mode, "slaac") &&
5764 strcmp(address_mode, "dhcpv6_stateful") &&
5765 strcmp(address_mode, "dhcpv6_stateless")) {
5766 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5767 VLOG_WARN_RL(&rl, "Invalid address mode [%s] defined",
5768 address_mode);
5769 continue;
5770 }
5771
5772 if (smap_get_bool(&op->nbrp->ipv6_ra_configs, "send_periodic",
5773 false)) {
5774 copy_ra_to_sb(op, address_mode);
5775 }
5776
5777 ds_clear(&match);
5778 ds_put_format(&match, "inport == %s && ip6.dst == ff02::2 && nd_rs",
5779 op->json_key);
5780 ds_clear(&actions);
5781
5782 const char *mtu_s = smap_get(
5783 &op->nbrp->ipv6_ra_configs, "mtu");
5784
5785 /* As per RFC 2460, 1280 is minimum IPv6 MTU. */
5786 uint32_t mtu = (mtu_s && atoi(mtu_s) >= 1280) ? atoi(mtu_s) : 0;
5787
5788 ds_put_format(&actions, REGBIT_ND_RA_OPTS_RESULT" = put_nd_ra_opts("
5789 "addr_mode = \"%s\", slla = %s",
5790 address_mode, op->lrp_networks.ea_s);
5791 if (mtu > 0) {
5792 ds_put_format(&actions, ", mtu = %u", mtu);
5793 }
5794
5795 bool add_rs_response_flow = false;
5796
5797 for (size_t i = 0; i < op->lrp_networks.n_ipv6_addrs; i++) {
5798 if (in6_is_lla(&op->lrp_networks.ipv6_addrs[i].network)) {
5799 continue;
5800 }
5801
5802 /* Add the prefix option if the address mode is slaac or
5803 * dhcpv6_stateless. */
5804 if (strcmp(address_mode, "dhcpv6_stateful")) {
5805 ds_put_format(&actions, ", prefix = %s/%u",
5806 op->lrp_networks.ipv6_addrs[i].network_s,
5807 op->lrp_networks.ipv6_addrs[i].plen);
5808 }
5809 add_rs_response_flow = true;
5810 }
5811
5812 if (add_rs_response_flow) {
5813 ds_put_cstr(&actions, "); next;");
5814 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_ND_RA_OPTIONS, 50,
5815 ds_cstr(&match), ds_cstr(&actions));
5816 ds_clear(&actions);
5817 ds_clear(&match);
5818 ds_put_format(&match, "inport == %s && ip6.dst == ff02::2 && "
5819 "nd_ra && "REGBIT_ND_RA_OPTS_RESULT, op->json_key);
5820
5821 char ip6_str[INET6_ADDRSTRLEN + 1];
5822 struct in6_addr lla;
5823 in6_generate_lla(op->lrp_networks.ea, &lla);
5824 memset(ip6_str, 0, sizeof(ip6_str));
5825 ipv6_string_mapped(ip6_str, &lla);
5826 ds_put_format(&actions, "eth.dst = eth.src; eth.src = %s; "
5827 "ip6.dst = ip6.src; ip6.src = %s; "
5828 "outport = inport; flags.loopback = 1; "
5829 "output;",
5830 op->lrp_networks.ea_s, ip6_str);
5831 ovn_lflow_add(lflows, op->od, S_ROUTER_IN_ND_RA_RESPONSE, 50,
5832 ds_cstr(&match), ds_cstr(&actions));
5833 }
5834 }
5835
5836 /* Logical router ingress table 5, 6: RS responder, by default goto next.
5837 * (priority 0)*/
5838 HMAP_FOR_EACH (od, key_node, datapaths) {
5839 if (!od->nbr) {
5840 continue;
5841 }
5842
5843 ovn_lflow_add(lflows, od, S_ROUTER_IN_ND_RA_OPTIONS, 0, "1", "next;");
5844 ovn_lflow_add(lflows, od, S_ROUTER_IN_ND_RA_RESPONSE, 0, "1", "next;");
5845 }
5846
5847 /* Logical router ingress table 7: IP Routing.
5848 *
5849 * A packet that arrives at this table is an IP packet that should be
5850 * routed to the address in 'ip[46].dst'. This table sets outport to
5851 * the correct output port, eth.src to the output port's MAC
5852 * address, and '[xx]reg0' to the next-hop IP address (leaving
5853 * 'ip[46].dst', the packet’s final destination, unchanged), and
5854 * advances to the next table for ARP/ND resolution. */
5855 HMAP_FOR_EACH (op, key_node, ports) {
5856 if (!op->nbrp) {
5857 continue;
5858 }
5859
5860 for (int i = 0; i < op->lrp_networks.n_ipv4_addrs; i++) {
5861 add_route(lflows, op, op->lrp_networks.ipv4_addrs[i].addr_s,
5862 op->lrp_networks.ipv4_addrs[i].network_s,
5863 op->lrp_networks.ipv4_addrs[i].plen, NULL, NULL);
5864 }
5865
5866 for (int i = 0; i < op->lrp_networks.n_ipv6_addrs; i++) {
5867 add_route(lflows, op, op->lrp_networks.ipv6_addrs[i].addr_s,
5868 op->lrp_networks.ipv6_addrs[i].network_s,
5869 op->lrp_networks.ipv6_addrs[i].plen, NULL, NULL);
5870 }
5871 }
5872
5873 /* Convert the static routes to flows. */
5874 HMAP_FOR_EACH (od, key_node, datapaths) {
5875 if (!od->nbr) {
5876 continue;
5877 }
5878
5879 for (int i = 0; i < od->nbr->n_static_routes; i++) {
5880 const struct nbrec_logical_router_static_route *route;
5881
5882 route = od->nbr->static_routes[i];
5883 build_static_route_flow(lflows, od, ports, route);
5884 }
5885 }
5886
5887 /* XXX destination unreachable */
5888
5889 /* Local router ingress table 8: ARP Resolution.
5890 *
5891 * Any packet that reaches this table is an IP packet whose next-hop IP
5892 * address is in reg0. (ip4.dst is the final destination.) This table
5893 * resolves the IP address in reg0 into an output port in outport and an
5894 * Ethernet address in eth.dst. */
5895 HMAP_FOR_EACH (op, key_node, ports) {
5896 if (op->nbsp && !lsp_is_enabled(op->nbsp)) {
5897 continue;
5898 }
5899
5900 if (op->nbrp) {
5901 /* This is a logical router port. If next-hop IP address in
5902 * '[xx]reg0' matches IP address of this router port, then
5903 * the packet is intended to eventually be sent to this
5904 * logical port. Set the destination mac address using this
5905 * port's mac address.
5906 *
5907 * The packet is still in peer's logical pipeline. So the match
5908 * should be on peer's outport. */
5909 if (op->peer && op->nbrp->peer) {
5910 if (op->lrp_networks.n_ipv4_addrs) {
5911 ds_clear(&match);
5912 ds_put_format(&match, "outport == %s && reg0 == ",
5913 op->peer->json_key);
5914 op_put_v4_networks(&match, op, false);
5915
5916 ds_clear(&actions);
5917 ds_put_format(&actions, "eth.dst = %s; next;",
5918 op->lrp_networks.ea_s);
5919 ovn_lflow_add(lflows, op->peer->od, S_ROUTER_IN_ARP_RESOLVE,
5920 100, ds_cstr(&match), ds_cstr(&actions));
5921 }
5922
5923 if (op->lrp_networks.n_ipv6_addrs) {
5924 ds_clear(&match);
5925 ds_put_format(&match, "outport == %s && xxreg0 == ",
5926 op->peer->json_key);
5927 op_put_v6_networks(&match, op);
5928
5929 ds_clear(&actions);
5930 ds_put_format(&actions, "eth.dst = %s; next;",
5931 op->lrp_networks.ea_s);
5932 ovn_lflow_add(lflows, op->peer->od, S_ROUTER_IN_ARP_RESOLVE,
5933 100, ds_cstr(&match), ds_cstr(&actions));
5934 }
5935 }
5936 } else if (op->od->n_router_ports && strcmp(op->nbsp->type, "router")) {
5937 /* This is a logical switch port that backs a VM or a container.
5938 * Extract its addresses. For each of the address, go through all
5939 * the router ports attached to the switch (to which this port
5940 * connects) and if the address in question is reachable from the
5941 * router port, add an ARP/ND entry in that router's pipeline. */
5942
5943 for (size_t i = 0; i < op->n_lsp_addrs; i++) {
5944 const char *ea_s = op->lsp_addrs[i].ea_s;
5945 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv4_addrs; j++) {
5946 const char *ip_s = op->lsp_addrs[i].ipv4_addrs[j].addr_s;
5947 for (size_t k = 0; k < op->od->n_router_ports; k++) {
5948 /* Get the Logical_Router_Port that the
5949 * Logical_Switch_Port is connected to, as
5950 * 'peer'. */
5951 const char *peer_name = smap_get(
5952 &op->od->router_ports[k]->nbsp->options,
5953 "router-port");
5954 if (!peer_name) {
5955 continue;
5956 }
5957
5958 struct ovn_port *peer = ovn_port_find(ports, peer_name);
5959 if (!peer || !peer->nbrp) {
5960 continue;
5961 }
5962
5963 if (!find_lrp_member_ip(peer, ip_s)) {
5964 continue;
5965 }
5966
5967 ds_clear(&match);
5968 ds_put_format(&match, "outport == %s && reg0 == %s",
5969 peer->json_key, ip_s);
5970
5971 ds_clear(&actions);
5972 ds_put_format(&actions, "eth.dst = %s; next;", ea_s);
5973 ovn_lflow_add(lflows, peer->od,
5974 S_ROUTER_IN_ARP_RESOLVE, 100,
5975 ds_cstr(&match), ds_cstr(&actions));
5976 }
5977 }
5978
5979 for (size_t j = 0; j < op->lsp_addrs[i].n_ipv6_addrs; j++) {
5980 const char *ip_s = op->lsp_addrs[i].ipv6_addrs[j].addr_s;
5981 for (size_t k = 0; k < op->od->n_router_ports; k++) {
5982 /* Get the Logical_Router_Port that the
5983 * Logical_Switch_Port is connected to, as
5984 * 'peer'. */
5985 const char *peer_name = smap_get(
5986 &op->od->router_ports[k]->nbsp->options,
5987 "router-port");
5988 if (!peer_name) {
5989 continue;
5990 }
5991
5992 struct ovn_port *peer = ovn_port_find(ports, peer_name);
5993 if (!peer || !peer->nbrp) {
5994 continue;
5995 }
5996
5997 if (!find_lrp_member_ip(peer, ip_s)) {
5998 continue;
5999 }
6000
6001 ds_clear(&match);
6002 ds_put_format(&match, "outport == %s && xxreg0 == %s",
6003 peer->json_key, ip_s);
6004
6005 ds_clear(&actions);
6006 ds_put_format(&actions, "eth.dst = %s; next;", ea_s);
6007 ovn_lflow_add(lflows, peer->od,
6008 S_ROUTER_IN_ARP_RESOLVE, 100,
6009 ds_cstr(&match), ds_cstr(&actions));
6010 }
6011 }
6012 }
6013 } else if (!strcmp(op->nbsp->type, "router")) {
6014 /* This is a logical switch port that connects to a router. */
6015
6016 /* The peer of this switch port is the router port for which
6017 * we need to add logical flows such that it can resolve
6018 * ARP entries for all the other router ports connected to
6019 * the switch in question. */
6020
6021 const char *peer_name = smap_get(&op->nbsp->options,
6022 "router-port");
6023 if (!peer_name) {
6024 continue;
6025 }
6026
6027 struct ovn_port *peer = ovn_port_find(ports, peer_name);
6028 if (!peer || !peer->nbrp) {
6029 continue;
6030 }
6031
6032 for (size_t i = 0; i < op->od->n_router_ports; i++) {
6033 const char *router_port_name = smap_get(
6034 &op->od->router_ports[i]->nbsp->options,
6035 "router-port");
6036 struct ovn_port *router_port = ovn_port_find(ports,
6037 router_port_name);
6038 if (!router_port || !router_port->nbrp) {
6039 continue;
6040 }
6041
6042 /* Skip the router port under consideration. */
6043 if (router_port == peer) {
6044 continue;
6045 }
6046
6047 if (router_port->lrp_networks.n_ipv4_addrs) {
6048 ds_clear(&match);
6049 ds_put_format(&match, "outport == %s && reg0 == ",
6050 peer->json_key);
6051 op_put_v4_networks(&match, router_port, false);
6052
6053 ds_clear(&actions);
6054 ds_put_format(&actions, "eth.dst = %s; next;",
6055 router_port->lrp_networks.ea_s);
6056 ovn_lflow_add(lflows, peer->od, S_ROUTER_IN_ARP_RESOLVE,
6057 100, ds_cstr(&match), ds_cstr(&actions));
6058 }
6059
6060 if (router_port->lrp_networks.n_ipv6_addrs) {
6061 ds_clear(&match);
6062 ds_put_format(&match, "outport == %s && xxreg0 == ",
6063 peer->json_key);
6064 op_put_v6_networks(&match, router_port);
6065
6066 ds_clear(&actions);
6067 ds_put_format(&actions, "eth.dst = %s; next;",
6068 router_port->lrp_networks.ea_s);
6069 ovn_lflow_add(lflows, peer->od, S_ROUTER_IN_ARP_RESOLVE,
6070 100, ds_cstr(&match), ds_cstr(&actions));
6071 }
6072 }
6073 }
6074 }
6075
6076 HMAP_FOR_EACH (od, key_node, datapaths) {
6077 if (!od->nbr) {
6078 continue;
6079 }
6080
6081 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_RESOLVE, 0, "ip4",
6082 "get_arp(outport, reg0); next;");
6083
6084 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_RESOLVE, 0, "ip6",
6085 "get_nd(outport, xxreg0); next;");
6086 }
6087
6088 /* Logical router ingress table 9: Gateway redirect.
6089 *
6090 * For traffic with outport equal to the l3dgw_port
6091 * on a distributed router, this table redirects a subset
6092 * of the traffic to the l3redirect_port which represents
6093 * the central instance of the l3dgw_port.
6094 */
6095 HMAP_FOR_EACH (od, key_node, datapaths) {
6096 if (!od->nbr) {
6097 continue;
6098 }
6099 if (od->l3dgw_port && od->l3redirect_port) {
6100 /* For traffic with outport == l3dgw_port, if the
6101 * packet did not match any higher priority redirect
6102 * rule, then the traffic is redirected to the central
6103 * instance of the l3dgw_port. */
6104 ds_clear(&match);
6105 ds_put_format(&match, "outport == %s",
6106 od->l3dgw_port->json_key);
6107 ds_clear(&actions);
6108 ds_put_format(&actions, "outport = %s; next;",
6109 od->l3redirect_port->json_key);
6110 ovn_lflow_add(lflows, od, S_ROUTER_IN_GW_REDIRECT, 50,
6111 ds_cstr(&match), ds_cstr(&actions));
6112
6113 /* If the Ethernet destination has not been resolved,
6114 * redirect to the central instance of the l3dgw_port.
6115 * Such traffic will be replaced by an ARP request or ND
6116 * Neighbor Solicitation in the ARP request ingress
6117 * table, before being redirected to the central instance.
6118 */
6119 ds_put_format(&match, " && eth.dst == 00:00:00:00:00:00");
6120 ovn_lflow_add(lflows, od, S_ROUTER_IN_GW_REDIRECT, 150,
6121 ds_cstr(&match), ds_cstr(&actions));
6122 }
6123
6124 /* Packets are allowed by default. */
6125 ovn_lflow_add(lflows, od, S_ROUTER_IN_GW_REDIRECT, 0, "1", "next;");
6126 }
6127
6128 /* Local router ingress table 10: ARP request.
6129 *
6130 * In the common case where the Ethernet destination has been resolved,
6131 * this table outputs the packet (priority 0). Otherwise, it composes
6132 * and sends an ARP/IPv6 NA request (priority 100). */
6133 HMAP_FOR_EACH (od, key_node, datapaths) {
6134 if (!od->nbr) {
6135 continue;
6136 }
6137
6138 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_REQUEST, 100,
6139 "eth.dst == 00:00:00:00:00:00",
6140 "arp { "
6141 "eth.dst = ff:ff:ff:ff:ff:ff; "
6142 "arp.spa = reg1; "
6143 "arp.tpa = reg0; "
6144 "arp.op = 1; " /* ARP request */
6145 "output; "
6146 "};");
6147 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_REQUEST, 100,
6148 "eth.dst == 00:00:00:00:00:00",
6149 "nd_ns { "
6150 "nd.target = xxreg0; "
6151 "output; "
6152 "};");
6153 ovn_lflow_add(lflows, od, S_ROUTER_IN_ARP_REQUEST, 0, "1", "output;");
6154 }
6155
6156 /* Logical router egress table 1: Delivery (priority 100).
6157 *
6158 * Priority 100 rules deliver packets to enabled logical ports. */
6159 HMAP_FOR_EACH (op, key_node, ports) {
6160 if (!op->nbrp) {
6161 continue;
6162 }
6163
6164 if (!lrport_is_enabled(op->nbrp)) {
6165 /* Drop packets to disabled logical ports (since logical flow
6166 * tables are default-drop). */
6167 continue;
6168 }
6169
6170 if (op->derived) {
6171 /* No egress packets should be processed in the context of
6172 * a chassisredirect port. The chassisredirect port should
6173 * be replaced by the l3dgw port in the local output
6174 * pipeline stage before egress processing. */
6175 continue;
6176 }
6177
6178 ds_clear(&match);
6179 ds_put_format(&match, "outport == %s", op->json_key);
6180 ovn_lflow_add(lflows, op->od, S_ROUTER_OUT_DELIVERY, 100,
6181 ds_cstr(&match), "output;");
6182 }
6183
6184 ds_destroy(&match);
6185 ds_destroy(&actions);
6186 }
6187
6188 /* Updates the Logical_Flow and Multicast_Group tables in the OVN_SB database,
6189 * constructing their contents based on the OVN_NB database. */
6190 static void
6191 build_lflows(struct northd_context *ctx, struct hmap *datapaths,
6192 struct hmap *ports, struct hmap *port_groups)
6193 {
6194 struct hmap lflows = HMAP_INITIALIZER(&lflows);
6195 struct hmap mcgroups = HMAP_INITIALIZER(&mcgroups);
6196
6197 build_lswitch_flows(datapaths, ports, port_groups, &lflows, &mcgroups);
6198 build_lrouter_flows(datapaths, ports, &lflows);
6199
6200 /* Push changes to the Logical_Flow table to database. */
6201 const struct sbrec_logical_flow *sbflow, *next_sbflow;
6202 SBREC_LOGICAL_FLOW_FOR_EACH_SAFE (sbflow, next_sbflow, ctx->ovnsb_idl) {
6203 struct ovn_datapath *od
6204 = ovn_datapath_from_sbrec(datapaths, sbflow->logical_datapath);
6205 if (!od) {
6206 sbrec_logical_flow_delete(sbflow);
6207 continue;
6208 }
6209
6210 enum ovn_datapath_type dp_type = od->nbs ? DP_SWITCH : DP_ROUTER;
6211 enum ovn_pipeline pipeline
6212 = !strcmp(sbflow->pipeline, "ingress") ? P_IN : P_OUT;
6213 struct ovn_lflow *lflow = ovn_lflow_find(
6214 &lflows, od, ovn_stage_build(dp_type, pipeline, sbflow->table_id),
6215 sbflow->priority, sbflow->match, sbflow->actions, sbflow->hash);
6216 if (lflow) {
6217 ovn_lflow_destroy(&lflows, lflow);
6218 } else {
6219 sbrec_logical_flow_delete(sbflow);
6220 }
6221 }
6222 struct ovn_lflow *lflow, *next_lflow;
6223 HMAP_FOR_EACH_SAFE (lflow, next_lflow, hmap_node, &lflows) {
6224 const char *pipeline = ovn_stage_get_pipeline_name(lflow->stage);
6225 uint8_t table = ovn_stage_get_table(lflow->stage);
6226
6227 sbflow = sbrec_logical_flow_insert(ctx->ovnsb_txn);
6228 sbrec_logical_flow_set_logical_datapath(sbflow, lflow->od->sb);
6229 sbrec_logical_flow_set_pipeline(sbflow, pipeline);
6230 sbrec_logical_flow_set_table_id(sbflow, table);
6231 sbrec_logical_flow_set_priority(sbflow, lflow->priority);
6232 sbrec_logical_flow_set_match(sbflow, lflow->match);
6233 sbrec_logical_flow_set_actions(sbflow, lflow->actions);
6234
6235 /* Trim the source locator lflow->where, which looks something like
6236 * "ovn/northd/ovn-northd.c:1234", down to just the part following the
6237 * last slash, e.g. "ovn-northd.c:1234". */
6238 const char *slash = strrchr(lflow->where, '/');
6239 #if _WIN32
6240 const char *backslash = strrchr(lflow->where, '\\');
6241 if (!slash || backslash > slash) {
6242 slash = backslash;
6243 }
6244 #endif
6245 const char *where = slash ? slash + 1 : lflow->where;
6246
6247 struct smap ids = SMAP_INITIALIZER(&ids);
6248 smap_add(&ids, "stage-name", ovn_stage_to_str(lflow->stage));
6249 smap_add(&ids, "source", where);
6250 if (lflow->stage_hint) {
6251 smap_add(&ids, "stage-hint", lflow->stage_hint);
6252 }
6253 sbrec_logical_flow_set_external_ids(sbflow, &ids);
6254 smap_destroy(&ids);
6255
6256 ovn_lflow_destroy(&lflows, lflow);
6257 }
6258 hmap_destroy(&lflows);
6259
6260 /* Push changes to the Multicast_Group table to database. */
6261 const struct sbrec_multicast_group *sbmc, *next_sbmc;
6262 SBREC_MULTICAST_GROUP_FOR_EACH_SAFE (sbmc, next_sbmc, ctx->ovnsb_idl) {
6263 struct ovn_datapath *od = ovn_datapath_from_sbrec(datapaths,
6264 sbmc->datapath);
6265 if (!od) {
6266 sbrec_multicast_group_delete(sbmc);
6267 continue;
6268 }
6269
6270 struct multicast_group group = { .name = sbmc->name,
6271 .key = sbmc->tunnel_key };
6272 struct ovn_multicast *mc = ovn_multicast_find(&mcgroups, od, &group);
6273 if (mc) {
6274 ovn_multicast_update_sbrec(mc, sbmc);
6275 ovn_multicast_destroy(&mcgroups, mc);
6276 } else {
6277 sbrec_multicast_group_delete(sbmc);
6278 }
6279 }
6280 struct ovn_multicast *mc, *next_mc;
6281 HMAP_FOR_EACH_SAFE (mc, next_mc, hmap_node, &mcgroups) {
6282 sbmc = sbrec_multicast_group_insert(ctx->ovnsb_txn);
6283 sbrec_multicast_group_set_datapath(sbmc, mc->datapath->sb);
6284 sbrec_multicast_group_set_name(sbmc, mc->group->name);
6285 sbrec_multicast_group_set_tunnel_key(sbmc, mc->group->key);
6286 ovn_multicast_update_sbrec(mc, sbmc);
6287 ovn_multicast_destroy(&mcgroups, mc);
6288 }
6289 hmap_destroy(&mcgroups);
6290 }
6291
6292 static void
6293 sync_address_set(struct northd_context *ctx, const char *name,
6294 const char **addrs, size_t n_addrs,
6295 struct shash *sb_address_sets)
6296 {
6297 const struct sbrec_address_set *sb_address_set;
6298 sb_address_set = shash_find_and_delete(sb_address_sets,
6299 name);
6300 if (!sb_address_set) {
6301 sb_address_set = sbrec_address_set_insert(ctx->ovnsb_txn);
6302 sbrec_address_set_set_name(sb_address_set, name);
6303 }
6304
6305 sbrec_address_set_set_addresses(sb_address_set,
6306 addrs, n_addrs);
6307 }
6308
6309 /* OVN_Southbound Address_Set table contains same records as in north
6310 * bound, plus the records generated from Port_Group table in north bound.
6311 *
6312 * There are 2 records generated from each port group, one for IPv4, and
6313 * one for IPv6, named in the format: <port group name>_ip4 and
6314 * <port group name>_ip6 respectively. MAC addresses are ignored.
6315 *
6316 * We always update OVN_Southbound to match the Address_Set and Port_Group
6317 * in OVN_Northbound, so that the address sets used in Logical_Flows in
6318 * OVN_Southbound is checked against the proper set.*/
6319 static void
6320 sync_address_sets(struct northd_context *ctx)
6321 {
6322 struct shash sb_address_sets = SHASH_INITIALIZER(&sb_address_sets);
6323
6324 const struct sbrec_address_set *sb_address_set;
6325 SBREC_ADDRESS_SET_FOR_EACH (sb_address_set, ctx->ovnsb_idl) {
6326 shash_add(&sb_address_sets, sb_address_set->name, sb_address_set);
6327 }
6328
6329 /* sync port group generated address sets first */
6330 const struct nbrec_port_group *nb_port_group;
6331 NBREC_PORT_GROUP_FOR_EACH (nb_port_group, ctx->ovnnb_idl) {
6332 char **ipv4_addrs = xcalloc(1, sizeof *ipv4_addrs);
6333 size_t n_ipv4_addrs = 0;
6334 size_t n_ipv4_addrs_buf = 1;
6335 char **ipv6_addrs = xcalloc(1, sizeof *ipv6_addrs);
6336 size_t n_ipv6_addrs = 0;
6337 size_t n_ipv6_addrs_buf = 1;
6338 for (size_t i = 0; i < nb_port_group->n_ports; i++) {
6339 for (size_t j = 0; j < nb_port_group->ports[i]->n_addresses; j++) {
6340 struct lport_addresses laddrs;
6341 extract_lsp_addresses(nb_port_group->ports[i]->addresses[j],
6342 &laddrs);
6343 while (n_ipv4_addrs_buf < n_ipv4_addrs + laddrs.n_ipv4_addrs) {
6344 n_ipv4_addrs_buf *= 2;
6345 ipv4_addrs = xrealloc(ipv4_addrs,
6346 n_ipv4_addrs_buf * sizeof *ipv4_addrs);
6347 }
6348 for (size_t k = 0; k < laddrs.n_ipv4_addrs; k++) {
6349 ipv4_addrs[n_ipv4_addrs++] =
6350 xstrdup(laddrs.ipv4_addrs[k].addr_s);
6351 }
6352 while (n_ipv6_addrs_buf < n_ipv6_addrs + laddrs.n_ipv6_addrs) {
6353 n_ipv6_addrs_buf *= 2;
6354 ipv6_addrs = xrealloc(ipv6_addrs,
6355 n_ipv6_addrs_buf * sizeof *ipv6_addrs);
6356 }
6357 for (size_t k = 0; k < laddrs.n_ipv6_addrs; k++) {
6358 ipv6_addrs[n_ipv6_addrs++] =
6359 xstrdup(laddrs.ipv6_addrs[k].addr_s);
6360 }
6361 destroy_lport_addresses(&laddrs);
6362 }
6363 }
6364 char *ipv4_addrs_name = xasprintf("%s_ip4", nb_port_group->name);
6365 char *ipv6_addrs_name = xasprintf("%s_ip6", nb_port_group->name);
6366 sync_address_set(ctx, ipv4_addrs_name, (const char **)ipv4_addrs,
6367 n_ipv4_addrs, &sb_address_sets);
6368 sync_address_set(ctx, ipv6_addrs_name, (const char **)ipv6_addrs,
6369 n_ipv6_addrs, &sb_address_sets);
6370 free(ipv4_addrs_name);
6371 free(ipv6_addrs_name);
6372 for (size_t i = 0; i < n_ipv4_addrs; i++) {
6373 free(ipv4_addrs[i]);
6374 }
6375 free(ipv4_addrs);
6376 for (size_t i = 0; i < n_ipv6_addrs; i++) {
6377 free(ipv6_addrs[i]);
6378 }
6379 free(ipv6_addrs);
6380 }
6381
6382 /* sync user defined address sets, which may overwrite port group
6383 * generated address sets if same name is used */
6384 const struct nbrec_address_set *nb_address_set;
6385 NBREC_ADDRESS_SET_FOR_EACH (nb_address_set, ctx->ovnnb_idl) {
6386 sync_address_set(ctx, nb_address_set->name,
6387 /* "char **" is not compatible with "const char **" */
6388 (const char **)nb_address_set->addresses,
6389 nb_address_set->n_addresses, &sb_address_sets);
6390 }
6391
6392 struct shash_node *node, *next;
6393 SHASH_FOR_EACH_SAFE (node, next, &sb_address_sets) {
6394 sbrec_address_set_delete(node->data);
6395 shash_delete(&sb_address_sets, node);
6396 }
6397 shash_destroy(&sb_address_sets);
6398 }
6399
6400 /* Each port group in Port_Group table in OVN_Northbound has a corresponding
6401 * entry in Port_Group table in OVN_Southbound. In OVN_Northbound the entries
6402 * contains lport uuids, while in OVN_Southbound we store the lport names.
6403 */
6404 static void
6405 sync_port_groups(struct northd_context *ctx)
6406 {
6407 struct shash sb_port_groups = SHASH_INITIALIZER(&sb_port_groups);
6408
6409 const struct sbrec_port_group *sb_port_group;
6410 SBREC_PORT_GROUP_FOR_EACH (sb_port_group, ctx->ovnsb_idl) {
6411 shash_add(&sb_port_groups, sb_port_group->name, sb_port_group);
6412 }
6413
6414 const struct nbrec_port_group *nb_port_group;
6415 NBREC_PORT_GROUP_FOR_EACH (nb_port_group, ctx->ovnnb_idl) {
6416 sb_port_group = shash_find_and_delete(&sb_port_groups,
6417 nb_port_group->name);
6418 if (!sb_port_group) {
6419 sb_port_group = sbrec_port_group_insert(ctx->ovnsb_txn);
6420 sbrec_port_group_set_name(sb_port_group, nb_port_group->name);
6421 }
6422
6423 const char **nb_port_names = xcalloc(nb_port_group->n_ports,
6424 sizeof *nb_port_names);
6425 int i;
6426 for (i = 0; i < nb_port_group->n_ports; i++) {
6427 nb_port_names[i] = nb_port_group->ports[i]->name;
6428 }
6429 sbrec_port_group_set_ports(sb_port_group,
6430 nb_port_names,
6431 nb_port_group->n_ports);
6432 free(nb_port_names);
6433 }
6434
6435 struct shash_node *node, *next;
6436 SHASH_FOR_EACH_SAFE (node, next, &sb_port_groups) {
6437 sbrec_port_group_delete(node->data);
6438 shash_delete(&sb_port_groups, node);
6439 }
6440 shash_destroy(&sb_port_groups);
6441 }
6442
6443 /*
6444 * struct 'dns_info' is used to sync the DNS records between OVN Northbound db
6445 * and Southbound db.
6446 */
6447 struct dns_info {
6448 struct hmap_node hmap_node;
6449 const struct nbrec_dns *nb_dns; /* DNS record in the Northbound db. */
6450 const struct sbrec_dns *sb_dns; /* DNS record in the Soutbound db. */
6451
6452 /* Datapaths to which the DNS entry is associated with it. */
6453 const struct sbrec_datapath_binding **sbs;
6454 size_t n_sbs;
6455 };
6456
6457 static inline struct dns_info *
6458 get_dns_info_from_hmap(struct hmap *dns_map, struct uuid *uuid)
6459 {
6460 struct dns_info *dns_info;
6461 size_t hash = uuid_hash(uuid);
6462 HMAP_FOR_EACH_WITH_HASH (dns_info, hmap_node, hash, dns_map) {
6463 if (uuid_equals(&dns_info->nb_dns->header_.uuid, uuid)) {
6464 return dns_info;
6465 }
6466 }
6467
6468 return NULL;
6469 }
6470
6471 static void
6472 sync_dns_entries(struct northd_context *ctx, struct hmap *datapaths)
6473 {
6474 struct hmap dns_map = HMAP_INITIALIZER(&dns_map);
6475 struct ovn_datapath *od;
6476 HMAP_FOR_EACH (od, key_node, datapaths) {
6477 if (!od->nbs || !od->nbs->n_dns_records) {
6478 continue;
6479 }
6480
6481 for (size_t i = 0; i < od->nbs->n_dns_records; i++) {
6482 struct dns_info *dns_info = get_dns_info_from_hmap(
6483 &dns_map, &od->nbs->dns_records[i]->header_.uuid);
6484 if (!dns_info) {
6485 size_t hash = uuid_hash(
6486 &od->nbs->dns_records[i]->header_.uuid);
6487 dns_info = xzalloc(sizeof *dns_info);;
6488 dns_info->nb_dns = od->nbs->dns_records[i];
6489 hmap_insert(&dns_map, &dns_info->hmap_node, hash);
6490 }
6491
6492 dns_info->n_sbs++;
6493 dns_info->sbs = xrealloc(dns_info->sbs,
6494 dns_info->n_sbs * sizeof *dns_info->sbs);
6495 dns_info->sbs[dns_info->n_sbs - 1] = od->sb;
6496 }
6497 }
6498
6499 const struct sbrec_dns *sbrec_dns, *next;
6500 SBREC_DNS_FOR_EACH_SAFE (sbrec_dns, next, ctx->ovnsb_idl) {
6501 const char *nb_dns_uuid = smap_get(&sbrec_dns->external_ids, "dns_id");
6502 struct uuid dns_uuid;
6503 if (!nb_dns_uuid || !uuid_from_string(&dns_uuid, nb_dns_uuid)) {
6504 sbrec_dns_delete(sbrec_dns);
6505 continue;
6506 }
6507
6508 struct dns_info *dns_info =
6509 get_dns_info_from_hmap(&dns_map, &dns_uuid);
6510 if (dns_info) {
6511 dns_info->sb_dns = sbrec_dns;
6512 } else {
6513 sbrec_dns_delete(sbrec_dns);
6514 }
6515 }
6516
6517 struct dns_info *dns_info;
6518 HMAP_FOR_EACH_POP (dns_info, hmap_node, &dns_map) {
6519 if (!dns_info->sb_dns) {
6520 sbrec_dns = sbrec_dns_insert(ctx->ovnsb_txn);
6521 dns_info->sb_dns = sbrec_dns;
6522 char *dns_id = xasprintf(
6523 UUID_FMT, UUID_ARGS(&dns_info->nb_dns->header_.uuid));
6524 const struct smap external_ids =
6525 SMAP_CONST1(&external_ids, "dns_id", dns_id);
6526 sbrec_dns_set_external_ids(sbrec_dns, &external_ids);
6527 free(dns_id);
6528 }
6529
6530 /* Set the datapaths and records. If nothing has changed, then
6531 * this will be a no-op.
6532 */
6533 sbrec_dns_set_datapaths(
6534 dns_info->sb_dns,
6535 (struct sbrec_datapath_binding **)dns_info->sbs,
6536 dns_info->n_sbs);
6537 sbrec_dns_set_records(dns_info->sb_dns, &dns_info->nb_dns->records);
6538 free(dns_info->sbs);
6539 free(dns_info);
6540 }
6541 hmap_destroy(&dns_map);
6542 }
6543
6544
6545 \f
6546 static void
6547 ovnnb_db_run(struct northd_context *ctx, struct chassis_index *chassis_index,
6548 struct ovsdb_idl_loop *sb_loop)
6549 {
6550 if (!ctx->ovnsb_txn || !ctx->ovnnb_txn) {
6551 return;
6552 }
6553 struct hmap datapaths, ports, port_groups;
6554 build_datapaths(ctx, &datapaths);
6555 build_ports(ctx, &datapaths, chassis_index, &ports);
6556 build_ipam(&datapaths, &ports);
6557 build_port_group_lswitches(ctx, &port_groups, &ports);
6558 build_lflows(ctx, &datapaths, &ports, &port_groups);
6559
6560 sync_address_sets(ctx);
6561 sync_port_groups(ctx);
6562 sync_dns_entries(ctx, &datapaths);
6563
6564 struct ovn_port_group *pg, *next_pg;
6565 HMAP_FOR_EACH_SAFE (pg, next_pg, key_node, &port_groups) {
6566 ovn_port_group_destroy(&port_groups, pg);
6567 }
6568 hmap_destroy(&port_groups);
6569
6570 struct ovn_datapath *dp, *next_dp;
6571 HMAP_FOR_EACH_SAFE (dp, next_dp, key_node, &datapaths) {
6572 ovn_datapath_destroy(&datapaths, dp);
6573 }
6574 hmap_destroy(&datapaths);
6575
6576 struct ovn_port *port, *next_port;
6577 HMAP_FOR_EACH_SAFE (port, next_port, key_node, &ports) {
6578 ovn_port_destroy(&ports, port);
6579 }
6580 hmap_destroy(&ports);
6581
6582 /* Copy nb_cfg from northbound to southbound database.
6583 *
6584 * Also set up to update sb_cfg once our southbound transaction commits. */
6585 const struct nbrec_nb_global *nb = nbrec_nb_global_first(ctx->ovnnb_idl);
6586 if (!nb) {
6587 nb = nbrec_nb_global_insert(ctx->ovnnb_txn);
6588 }
6589 const struct sbrec_sb_global *sb = sbrec_sb_global_first(ctx->ovnsb_idl);
6590 if (!sb) {
6591 sb = sbrec_sb_global_insert(ctx->ovnsb_txn);
6592 }
6593 sbrec_sb_global_set_nb_cfg(sb, nb->nb_cfg);
6594 sb_loop->next_cfg = nb->nb_cfg;
6595
6596 cleanup_macam(&macam);
6597 }
6598
6599 /* Handle changes to the 'chassis' column of the 'Port_Binding' table. When
6600 * this column is not empty, it means we need to set the corresponding logical
6601 * port as 'up' in the northbound DB. */
6602 static void
6603 update_logical_port_status(struct northd_context *ctx)
6604 {
6605 struct hmap lports_hmap;
6606 const struct sbrec_port_binding *sb;
6607 const struct nbrec_logical_switch_port *nbsp;
6608
6609 struct lport_hash_node {
6610 struct hmap_node node;
6611 const struct nbrec_logical_switch_port *nbsp;
6612 } *hash_node;
6613
6614 hmap_init(&lports_hmap);
6615
6616 NBREC_LOGICAL_SWITCH_PORT_FOR_EACH(nbsp, ctx->ovnnb_idl) {
6617 hash_node = xzalloc(sizeof *hash_node);
6618 hash_node->nbsp = nbsp;
6619 hmap_insert(&lports_hmap, &hash_node->node, hash_string(nbsp->name, 0));
6620 }
6621
6622 SBREC_PORT_BINDING_FOR_EACH(sb, ctx->ovnsb_idl) {
6623 nbsp = NULL;
6624 HMAP_FOR_EACH_WITH_HASH(hash_node, node,
6625 hash_string(sb->logical_port, 0),
6626 &lports_hmap) {
6627 if (!strcmp(sb->logical_port, hash_node->nbsp->name)) {
6628 nbsp = hash_node->nbsp;
6629 break;
6630 }
6631 }
6632
6633 if (!nbsp) {
6634 /* The logical port doesn't exist for this port binding. This can
6635 * happen under normal circumstances when ovn-northd hasn't gotten
6636 * around to pruning the Port_Binding yet. */
6637 continue;
6638 }
6639
6640 bool up = (sb->chassis || !strcmp(nbsp->type, "router"));
6641 if (!nbsp->up || *nbsp->up != up) {
6642 nbrec_logical_switch_port_set_up(nbsp, &up, 1);
6643 }
6644 }
6645
6646 HMAP_FOR_EACH_POP(hash_node, node, &lports_hmap) {
6647 free(hash_node);
6648 }
6649 hmap_destroy(&lports_hmap);
6650 }
6651
6652 static struct gen_opts_map supported_dhcp_opts[] = {
6653 OFFERIP,
6654 DHCP_OPT_NETMASK,
6655 DHCP_OPT_ROUTER,
6656 DHCP_OPT_DNS_SERVER,
6657 DHCP_OPT_LOG_SERVER,
6658 DHCP_OPT_LPR_SERVER,
6659 DHCP_OPT_SWAP_SERVER,
6660 DHCP_OPT_POLICY_FILTER,
6661 DHCP_OPT_ROUTER_SOLICITATION,
6662 DHCP_OPT_NIS_SERVER,
6663 DHCP_OPT_NTP_SERVER,
6664 DHCP_OPT_SERVER_ID,
6665 DHCP_OPT_TFTP_SERVER,
6666 DHCP_OPT_CLASSLESS_STATIC_ROUTE,
6667 DHCP_OPT_MS_CLASSLESS_STATIC_ROUTE,
6668 DHCP_OPT_IP_FORWARD_ENABLE,
6669 DHCP_OPT_ROUTER_DISCOVERY,
6670 DHCP_OPT_ETHERNET_ENCAP,
6671 DHCP_OPT_DEFAULT_TTL,
6672 DHCP_OPT_TCP_TTL,
6673 DHCP_OPT_MTU,
6674 DHCP_OPT_LEASE_TIME,
6675 DHCP_OPT_T1,
6676 DHCP_OPT_T2
6677 };
6678
6679 static struct gen_opts_map supported_dhcpv6_opts[] = {
6680 DHCPV6_OPT_IA_ADDR,
6681 DHCPV6_OPT_SERVER_ID,
6682 DHCPV6_OPT_DOMAIN_SEARCH,
6683 DHCPV6_OPT_DNS_SERVER
6684 };
6685
6686 static void
6687 check_and_add_supported_dhcp_opts_to_sb_db(struct northd_context *ctx)
6688 {
6689 struct hmap dhcp_opts_to_add = HMAP_INITIALIZER(&dhcp_opts_to_add);
6690 for (size_t i = 0; (i < sizeof(supported_dhcp_opts) /
6691 sizeof(supported_dhcp_opts[0])); i++) {
6692 hmap_insert(&dhcp_opts_to_add, &supported_dhcp_opts[i].hmap_node,
6693 dhcp_opt_hash(supported_dhcp_opts[i].name));
6694 }
6695
6696 const struct sbrec_dhcp_options *opt_row, *opt_row_next;
6697 SBREC_DHCP_OPTIONS_FOR_EACH_SAFE(opt_row, opt_row_next, ctx->ovnsb_idl) {
6698 struct gen_opts_map *dhcp_opt =
6699 dhcp_opts_find(&dhcp_opts_to_add, opt_row->name);
6700 if (dhcp_opt) {
6701 hmap_remove(&dhcp_opts_to_add, &dhcp_opt->hmap_node);
6702 } else {
6703 sbrec_dhcp_options_delete(opt_row);
6704 }
6705 }
6706
6707 struct gen_opts_map *opt;
6708 HMAP_FOR_EACH (opt, hmap_node, &dhcp_opts_to_add) {
6709 struct sbrec_dhcp_options *sbrec_dhcp_option =
6710 sbrec_dhcp_options_insert(ctx->ovnsb_txn);
6711 sbrec_dhcp_options_set_name(sbrec_dhcp_option, opt->name);
6712 sbrec_dhcp_options_set_code(sbrec_dhcp_option, opt->code);
6713 sbrec_dhcp_options_set_type(sbrec_dhcp_option, opt->type);
6714 }
6715
6716 hmap_destroy(&dhcp_opts_to_add);
6717 }
6718
6719 static void
6720 check_and_add_supported_dhcpv6_opts_to_sb_db(struct northd_context *ctx)
6721 {
6722 struct hmap dhcpv6_opts_to_add = HMAP_INITIALIZER(&dhcpv6_opts_to_add);
6723 for (size_t i = 0; (i < sizeof(supported_dhcpv6_opts) /
6724 sizeof(supported_dhcpv6_opts[0])); i++) {
6725 hmap_insert(&dhcpv6_opts_to_add, &supported_dhcpv6_opts[i].hmap_node,
6726 dhcp_opt_hash(supported_dhcpv6_opts[i].name));
6727 }
6728
6729 const struct sbrec_dhcpv6_options *opt_row, *opt_row_next;
6730 SBREC_DHCPV6_OPTIONS_FOR_EACH_SAFE(opt_row, opt_row_next, ctx->ovnsb_idl) {
6731 struct gen_opts_map *dhcp_opt =
6732 dhcp_opts_find(&dhcpv6_opts_to_add, opt_row->name);
6733 if (dhcp_opt) {
6734 hmap_remove(&dhcpv6_opts_to_add, &dhcp_opt->hmap_node);
6735 } else {
6736 sbrec_dhcpv6_options_delete(opt_row);
6737 }
6738 }
6739
6740 struct gen_opts_map *opt;
6741 HMAP_FOR_EACH(opt, hmap_node, &dhcpv6_opts_to_add) {
6742 struct sbrec_dhcpv6_options *sbrec_dhcpv6_option =
6743 sbrec_dhcpv6_options_insert(ctx->ovnsb_txn);
6744 sbrec_dhcpv6_options_set_name(sbrec_dhcpv6_option, opt->name);
6745 sbrec_dhcpv6_options_set_code(sbrec_dhcpv6_option, opt->code);
6746 sbrec_dhcpv6_options_set_type(sbrec_dhcpv6_option, opt->type);
6747 }
6748
6749 hmap_destroy(&dhcpv6_opts_to_add);
6750 }
6751
6752 static const char *rbac_chassis_auth[] =
6753 {"name"};
6754 static const char *rbac_chassis_update[] =
6755 {"nb_cfg", "external_ids", "encaps", "vtep_logical_switches"};
6756
6757 static const char *rbac_encap_auth[] =
6758 {"chassis_name"};
6759 static const char *rbac_encap_update[] =
6760 {"type", "options", "ip"};
6761
6762 static const char *rbac_port_binding_auth[] =
6763 {""};
6764 static const char *rbac_port_binding_update[] =
6765 {"chassis"};
6766
6767 static const char *rbac_mac_binding_auth[] =
6768 {""};
6769 static const char *rbac_mac_binding_update[] =
6770 {"logical_port", "ip", "mac", "datapath"};
6771
6772 static struct rbac_perm_cfg {
6773 const char *table;
6774 const char **auth;
6775 int n_auth;
6776 bool insdel;
6777 const char **update;
6778 int n_update;
6779 const struct sbrec_rbac_permission *row;
6780 } rbac_perm_cfg[] = {
6781 {
6782 .table = "Chassis",
6783 .auth = rbac_chassis_auth,
6784 .n_auth = ARRAY_SIZE(rbac_chassis_auth),
6785 .insdel = true,
6786 .update = rbac_chassis_update,
6787 .n_update = ARRAY_SIZE(rbac_chassis_update),
6788 .row = NULL
6789 },{
6790 .table = "Encap",
6791 .auth = rbac_encap_auth,
6792 .n_auth = ARRAY_SIZE(rbac_encap_auth),
6793 .insdel = true,
6794 .update = rbac_encap_update,
6795 .n_update = ARRAY_SIZE(rbac_encap_update),
6796 .row = NULL
6797 },{
6798 .table = "Port_Binding",
6799 .auth = rbac_port_binding_auth,
6800 .n_auth = ARRAY_SIZE(rbac_port_binding_auth),
6801 .insdel = false,
6802 .update = rbac_port_binding_update,
6803 .n_update = ARRAY_SIZE(rbac_port_binding_update),
6804 .row = NULL
6805 },{
6806 .table = "MAC_Binding",
6807 .auth = rbac_mac_binding_auth,
6808 .n_auth = ARRAY_SIZE(rbac_mac_binding_auth),
6809 .insdel = true,
6810 .update = rbac_mac_binding_update,
6811 .n_update = ARRAY_SIZE(rbac_mac_binding_update),
6812 .row = NULL
6813 },{
6814 .table = NULL,
6815 .auth = NULL,
6816 .n_auth = 0,
6817 .insdel = false,
6818 .update = NULL,
6819 .n_update = 0,
6820 .row = NULL
6821 }
6822 };
6823
6824 static bool
6825 ovn_rbac_validate_perm(const struct sbrec_rbac_permission *perm)
6826 {
6827 struct rbac_perm_cfg *pcfg;
6828 int i, j, n_found;
6829
6830 for (pcfg = rbac_perm_cfg; pcfg->table; pcfg++) {
6831 if (!strcmp(perm->table, pcfg->table)) {
6832 break;
6833 }
6834 }
6835 if (!pcfg->table) {
6836 return false;
6837 }
6838 if (perm->n_authorization != pcfg->n_auth ||
6839 perm->n_update != pcfg->n_update) {
6840 return false;
6841 }
6842 if (perm->insert_delete != pcfg->insdel) {
6843 return false;
6844 }
6845 /* verify perm->authorization vs. pcfg->auth */
6846 n_found = 0;
6847 for (i = 0; i < pcfg->n_auth; i++) {
6848 for (j = 0; j < perm->n_authorization; j++) {
6849 if (!strcmp(pcfg->auth[i], perm->authorization[j])) {
6850 n_found++;
6851 break;
6852 }
6853 }
6854 }
6855 if (n_found != pcfg->n_auth) {
6856 return false;
6857 }
6858
6859 /* verify perm->update vs. pcfg->update */
6860 n_found = 0;
6861 for (i = 0; i < pcfg->n_update; i++) {
6862 for (j = 0; j < perm->n_update; j++) {
6863 if (!strcmp(pcfg->update[i], perm->update[j])) {
6864 n_found++;
6865 break;
6866 }
6867 }
6868 }
6869 if (n_found != pcfg->n_update) {
6870 return false;
6871 }
6872
6873 /* Success, db state matches expected state */
6874 pcfg->row = perm;
6875 return true;
6876 }
6877
6878 static void
6879 ovn_rbac_create_perm(struct rbac_perm_cfg *pcfg,
6880 struct northd_context *ctx,
6881 const struct sbrec_rbac_role *rbac_role)
6882 {
6883 struct sbrec_rbac_permission *rbac_perm;
6884
6885 rbac_perm = sbrec_rbac_permission_insert(ctx->ovnsb_txn);
6886 sbrec_rbac_permission_set_table(rbac_perm, pcfg->table);
6887 sbrec_rbac_permission_set_authorization(rbac_perm,
6888 pcfg->auth,
6889 pcfg->n_auth);
6890 sbrec_rbac_permission_set_insert_delete(rbac_perm, pcfg->insdel);
6891 sbrec_rbac_permission_set_update(rbac_perm,
6892 pcfg->update,
6893 pcfg->n_update);
6894 sbrec_rbac_role_update_permissions_setkey(rbac_role, pcfg->table,
6895 rbac_perm);
6896 }
6897
6898 static void
6899 check_and_update_rbac(struct northd_context *ctx)
6900 {
6901 const struct sbrec_rbac_role *rbac_role = NULL;
6902 const struct sbrec_rbac_permission *perm_row, *perm_next;
6903 const struct sbrec_rbac_role *role_row, *role_row_next;
6904 struct rbac_perm_cfg *pcfg;
6905
6906 for (pcfg = rbac_perm_cfg; pcfg->table; pcfg++) {
6907 pcfg->row = NULL;
6908 }
6909
6910 SBREC_RBAC_PERMISSION_FOR_EACH_SAFE (perm_row, perm_next, ctx->ovnsb_idl) {
6911 if (!ovn_rbac_validate_perm(perm_row)) {
6912 sbrec_rbac_permission_delete(perm_row);
6913 }
6914 }
6915 SBREC_RBAC_ROLE_FOR_EACH_SAFE (role_row, role_row_next, ctx->ovnsb_idl) {
6916 if (strcmp(role_row->name, "ovn-controller")) {
6917 sbrec_rbac_role_delete(role_row);
6918 } else {
6919 rbac_role = role_row;
6920 }
6921 }
6922
6923 if (!rbac_role) {
6924 rbac_role = sbrec_rbac_role_insert(ctx->ovnsb_txn);
6925 sbrec_rbac_role_set_name(rbac_role, "ovn-controller");
6926 }
6927
6928 for (pcfg = rbac_perm_cfg; pcfg->table; pcfg++) {
6929 if (!pcfg->row) {
6930 ovn_rbac_create_perm(pcfg, ctx, rbac_role);
6931 }
6932 }
6933 }
6934
6935 /* Updates the sb_cfg and hv_cfg columns in the northbound NB_Global table. */
6936 static void
6937 update_northbound_cfg(struct northd_context *ctx,
6938 struct ovsdb_idl_loop *sb_loop)
6939 {
6940 /* Update northbound sb_cfg if appropriate. */
6941 const struct nbrec_nb_global *nbg = nbrec_nb_global_first(ctx->ovnnb_idl);
6942 int64_t sb_cfg = sb_loop->cur_cfg;
6943 if (nbg && sb_cfg && nbg->sb_cfg != sb_cfg) {
6944 nbrec_nb_global_set_sb_cfg(nbg, sb_cfg);
6945 }
6946
6947 /* Update northbound hv_cfg if appropriate. */
6948 if (nbg) {
6949 /* Find minimum nb_cfg among all chassis. */
6950 const struct sbrec_chassis *chassis;
6951 int64_t hv_cfg = nbg->nb_cfg;
6952 SBREC_CHASSIS_FOR_EACH (chassis, ctx->ovnsb_idl) {
6953 if (chassis->nb_cfg < hv_cfg) {
6954 hv_cfg = chassis->nb_cfg;
6955 }
6956 }
6957
6958 /* Update hv_cfg. */
6959 if (nbg->hv_cfg != hv_cfg) {
6960 nbrec_nb_global_set_hv_cfg(nbg, hv_cfg);
6961 }
6962 }
6963 }
6964
6965 /* Handle a fairly small set of changes in the southbound database. */
6966 static void
6967 ovnsb_db_run(struct northd_context *ctx, struct ovsdb_idl_loop *sb_loop)
6968 {
6969 if (!ctx->ovnnb_txn || !ovsdb_idl_has_ever_connected(ctx->ovnsb_idl)) {
6970 return;
6971 }
6972
6973 update_logical_port_status(ctx);
6974 update_northbound_cfg(ctx, sb_loop);
6975 }
6976 \f
6977 static void
6978 parse_options(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
6979 {
6980 enum {
6981 DAEMON_OPTION_ENUMS,
6982 VLOG_OPTION_ENUMS,
6983 SSL_OPTION_ENUMS,
6984 };
6985 static const struct option long_options[] = {
6986 {"ovnsb-db", required_argument, NULL, 'd'},
6987 {"ovnnb-db", required_argument, NULL, 'D'},
6988 {"unixctl", required_argument, NULL, 'u'},
6989 {"help", no_argument, NULL, 'h'},
6990 {"options", no_argument, NULL, 'o'},
6991 {"version", no_argument, NULL, 'V'},
6992 DAEMON_LONG_OPTIONS,
6993 VLOG_LONG_OPTIONS,
6994 STREAM_SSL_LONG_OPTIONS,
6995 {NULL, 0, NULL, 0},
6996 };
6997 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
6998
6999 for (;;) {
7000 int c;
7001
7002 c = getopt_long(argc, argv, short_options, long_options, NULL);
7003 if (c == -1) {
7004 break;
7005 }
7006
7007 switch (c) {
7008 DAEMON_OPTION_HANDLERS;
7009 VLOG_OPTION_HANDLERS;
7010 STREAM_SSL_OPTION_HANDLERS;
7011
7012 case 'd':
7013 ovnsb_db = optarg;
7014 break;
7015
7016 case 'D':
7017 ovnnb_db = optarg;
7018 break;
7019
7020 case 'u':
7021 unixctl_path = optarg;
7022 break;
7023
7024 case 'h':
7025 usage();
7026 exit(EXIT_SUCCESS);
7027
7028 case 'o':
7029 ovs_cmdl_print_options(long_options);
7030 exit(EXIT_SUCCESS);
7031
7032 case 'V':
7033 ovs_print_version(0, 0);
7034 exit(EXIT_SUCCESS);
7035
7036 default:
7037 break;
7038 }
7039 }
7040
7041 if (!ovnsb_db) {
7042 ovnsb_db = default_sb_db();
7043 }
7044
7045 if (!ovnnb_db) {
7046 ovnnb_db = default_nb_db();
7047 }
7048
7049 free(short_options);
7050 }
7051
7052 static void
7053 add_column_noalert(struct ovsdb_idl *idl,
7054 const struct ovsdb_idl_column *column)
7055 {
7056 ovsdb_idl_add_column(idl, column);
7057 ovsdb_idl_omit_alert(idl, column);
7058 }
7059
7060 int
7061 main(int argc, char *argv[])
7062 {
7063 int res = EXIT_SUCCESS;
7064 struct unixctl_server *unixctl;
7065 int retval;
7066 bool exiting;
7067
7068 fatal_ignore_sigpipe();
7069 ovs_cmdl_proctitle_init(argc, argv);
7070 set_program_name(argv[0]);
7071 service_start(&argc, &argv);
7072 parse_options(argc, argv);
7073
7074 daemonize_start(false);
7075
7076 retval = unixctl_server_create(unixctl_path, &unixctl);
7077 if (retval) {
7078 exit(EXIT_FAILURE);
7079 }
7080 unixctl_command_register("exit", "", 0, 0, ovn_northd_exit, &exiting);
7081
7082 daemonize_complete();
7083
7084 /* We want to detect (almost) all changes to the ovn-nb db. */
7085 struct ovsdb_idl_loop ovnnb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
7086 ovsdb_idl_create(ovnnb_db, &nbrec_idl_class, true, true));
7087 ovsdb_idl_omit_alert(ovnnb_idl_loop.idl, &nbrec_nb_global_col_sb_cfg);
7088 ovsdb_idl_omit_alert(ovnnb_idl_loop.idl, &nbrec_nb_global_col_hv_cfg);
7089
7090 /* We want to detect only selected changes to the ovn-sb db. */
7091 struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
7092 ovsdb_idl_create(ovnsb_db, &sbrec_idl_class, false, true));
7093
7094 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_sb_global);
7095 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_sb_global_col_nb_cfg);
7096
7097 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_logical_flow);
7098 add_column_noalert(ovnsb_idl_loop.idl,
7099 &sbrec_logical_flow_col_logical_datapath);
7100 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_logical_flow_col_pipeline);
7101 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_logical_flow_col_table_id);
7102 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_logical_flow_col_priority);
7103 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_logical_flow_col_match);
7104 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_logical_flow_col_actions);
7105
7106 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_multicast_group);
7107 add_column_noalert(ovnsb_idl_loop.idl,
7108 &sbrec_multicast_group_col_datapath);
7109 add_column_noalert(ovnsb_idl_loop.idl,
7110 &sbrec_multicast_group_col_tunnel_key);
7111 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_multicast_group_col_name);
7112 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_multicast_group_col_ports);
7113
7114 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_datapath_binding);
7115 add_column_noalert(ovnsb_idl_loop.idl,
7116 &sbrec_datapath_binding_col_tunnel_key);
7117 add_column_noalert(ovnsb_idl_loop.idl,
7118 &sbrec_datapath_binding_col_external_ids);
7119
7120 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_port_binding);
7121 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_binding_col_datapath);
7122 add_column_noalert(ovnsb_idl_loop.idl,
7123 &sbrec_port_binding_col_logical_port);
7124 add_column_noalert(ovnsb_idl_loop.idl,
7125 &sbrec_port_binding_col_tunnel_key);
7126 add_column_noalert(ovnsb_idl_loop.idl,
7127 &sbrec_port_binding_col_parent_port);
7128 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_binding_col_tag);
7129 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_binding_col_type);
7130 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_binding_col_options);
7131 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_binding_col_mac);
7132 add_column_noalert(ovnsb_idl_loop.idl,
7133 &sbrec_port_binding_col_nat_addresses);
7134 ovsdb_idl_add_column(ovnsb_idl_loop.idl, &sbrec_port_binding_col_chassis);
7135 ovsdb_idl_add_column(ovnsb_idl_loop.idl,
7136 &sbrec_port_binding_col_gateway_chassis);
7137 ovsdb_idl_add_column(ovnsb_idl_loop.idl,
7138 &sbrec_gateway_chassis_col_chassis);
7139 ovsdb_idl_add_column(ovnsb_idl_loop.idl, &sbrec_gateway_chassis_col_name);
7140 ovsdb_idl_add_column(ovnsb_idl_loop.idl,
7141 &sbrec_gateway_chassis_col_priority);
7142 ovsdb_idl_add_column(ovnsb_idl_loop.idl,
7143 &sbrec_gateway_chassis_col_external_ids);
7144 ovsdb_idl_add_column(ovnsb_idl_loop.idl,
7145 &sbrec_gateway_chassis_col_options);
7146 add_column_noalert(ovnsb_idl_loop.idl,
7147 &sbrec_port_binding_col_external_ids);
7148 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_mac_binding);
7149 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_mac_binding_col_datapath);
7150 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_mac_binding_col_ip);
7151 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_mac_binding_col_mac);
7152 add_column_noalert(ovnsb_idl_loop.idl,
7153 &sbrec_mac_binding_col_logical_port);
7154 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_dhcp_options);
7155 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcp_options_col_code);
7156 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcp_options_col_type);
7157 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcp_options_col_name);
7158 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_dhcpv6_options);
7159 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcpv6_options_col_code);
7160 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcpv6_options_col_type);
7161 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dhcpv6_options_col_name);
7162 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_address_set);
7163 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_address_set_col_name);
7164 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_address_set_col_addresses);
7165 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_port_group);
7166 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_group_col_name);
7167 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_port_group_col_ports);
7168
7169 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_dns);
7170 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dns_col_datapaths);
7171 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dns_col_records);
7172 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_dns_col_external_ids);
7173
7174 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_rbac_role);
7175 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_rbac_role_col_name);
7176 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_rbac_role_col_permissions);
7177
7178 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_rbac_permission);
7179 add_column_noalert(ovnsb_idl_loop.idl,
7180 &sbrec_rbac_permission_col_table);
7181 add_column_noalert(ovnsb_idl_loop.idl,
7182 &sbrec_rbac_permission_col_authorization);
7183 add_column_noalert(ovnsb_idl_loop.idl,
7184 &sbrec_rbac_permission_col_insert_delete);
7185 add_column_noalert(ovnsb_idl_loop.idl, &sbrec_rbac_permission_col_update);
7186
7187 ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_chassis);
7188 ovsdb_idl_add_column(ovnsb_idl_loop.idl, &sbrec_chassis_col_nb_cfg);
7189 ovsdb_idl_add_column(ovnsb_idl_loop.idl, &sbrec_chassis_col_name);
7190
7191 /* Ensure that only a single ovn-northd is active in the deployment by
7192 * acquiring a lock called "ovn_northd" on the southbound database
7193 * and then only performing DB transactions if the lock is held. */
7194 ovsdb_idl_set_lock(ovnsb_idl_loop.idl, "ovn_northd");
7195 bool had_lock = false;
7196
7197 /* Main loop. */
7198 exiting = false;
7199 while (!exiting) {
7200 struct northd_context ctx = {
7201 .ovnnb_idl = ovnnb_idl_loop.idl,
7202 .ovnnb_txn = ovsdb_idl_loop_run(&ovnnb_idl_loop),
7203 .ovnsb_idl = ovnsb_idl_loop.idl,
7204 .ovnsb_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
7205 };
7206
7207 if (!had_lock && ovsdb_idl_has_lock(ovnsb_idl_loop.idl)) {
7208 VLOG_INFO("ovn-northd lock acquired. "
7209 "This ovn-northd instance is now active.");
7210 had_lock = true;
7211 } else if (had_lock && !ovsdb_idl_has_lock(ovnsb_idl_loop.idl)) {
7212 VLOG_INFO("ovn-northd lock lost. "
7213 "This ovn-northd instance is now on standby.");
7214 had_lock = false;
7215 }
7216
7217 struct chassis_index chassis_index;
7218 bool destroy_chassis_index = false;
7219 if (ovsdb_idl_has_lock(ovnsb_idl_loop.idl)) {
7220 chassis_index_init(&chassis_index, ctx.ovnsb_idl);
7221 destroy_chassis_index = true;
7222
7223 ovnnb_db_run(&ctx, &chassis_index, &ovnsb_idl_loop);
7224 ovnsb_db_run(&ctx, &ovnsb_idl_loop);
7225 if (ctx.ovnsb_txn) {
7226 check_and_add_supported_dhcp_opts_to_sb_db(&ctx);
7227 check_and_add_supported_dhcpv6_opts_to_sb_db(&ctx);
7228 check_and_update_rbac(&ctx);
7229 }
7230 }
7231
7232 unixctl_server_run(unixctl);
7233 unixctl_server_wait(unixctl);
7234 if (exiting) {
7235 poll_immediate_wake();
7236 }
7237 ovsdb_idl_loop_commit_and_wait(&ovnnb_idl_loop);
7238 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
7239
7240 poll_block();
7241 if (should_service_stop()) {
7242 exiting = true;
7243 }
7244
7245 if (destroy_chassis_index) {
7246 chassis_index_destroy(&chassis_index);
7247 }
7248 }
7249
7250 unixctl_server_destroy(unixctl);
7251 ovsdb_idl_loop_destroy(&ovnnb_idl_loop);
7252 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
7253 service_stop();
7254
7255 exit(res);
7256 }
7257
7258 static void
7259 ovn_northd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
7260 const char *argv[] OVS_UNUSED, void *exiting_)
7261 {
7262 bool *exiting = exiting_;
7263 *exiting = true;
7264
7265 unixctl_command_reply(conn, NULL);
7266 }