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:
6 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "command-line.h"
25 #include "openvswitch/dynamic-string.h"
26 #include "fatal-signal.h"
28 #include "openvswitch/hmap.h"
29 #include "openvswitch/json.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"
39 #include "openvswitch/poll-loop.h"
43 #include "stream-ssl.h"
47 #include "openvswitch/vlog.h"
49 VLOG_DEFINE_THIS_MODULE(ovn_northd
);
51 static unixctl_cb_func ovn_northd_exit
;
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
;
60 static const char *ovnnb_db
;
61 static const char *ovnsb_db
;
62 static const char *unixctl_path
;
64 #define MAC_ADDR_PREFIX 0x0A0000000000ULL
65 #define MAC_ADDR_SPACE 0xffffff
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
);
71 #define MAX_OVN_TAGS 4096
73 /* Pipeline stages. */
75 /* The two pipelines in an OVN logical flow table. */
77 P_IN
, /* Ingress pipeline. */
78 P_OUT
/* Egress pipeline. */
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. */
87 /* Returns an "enum ovn_stage" built from the arguments.
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))
94 /* A stage within an OVN logical switch or router.
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. */
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") \
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") \
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") \
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")
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),
157 #undef PIPELINE_STAGE
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
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]"
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]"
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
,
187 return OVN_STAGE_BUILD(dp_type
, pipeline
, table
);
190 /* Returns the pipeline to which 'stage' belongs. */
191 static enum ovn_pipeline
192 ovn_stage_get_pipeline(enum ovn_stage stage
)
194 return (stage
>> 8) & 1;
197 /* Returns the pipeline name to which 'stage' belongs. */
199 ovn_stage_get_pipeline_name(enum ovn_stage stage
)
201 return ovn_stage_get_pipeline(stage
) == P_IN
? "ingress" : "egress";
204 /* Returns the table to which 'stage' belongs. */
206 ovn_stage_get_table(enum ovn_stage stage
)
211 /* Returns a string name for 'stage'. */
213 ovn_stage_to_str(enum ovn_stage stage
)
216 #define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
217 case S_##DP_TYPE##_##PIPELINE##_##STAGE: return NAME;
219 #undef PIPELINE_STAGE
220 default: return "<unknown>";
224 /* Returns the type of the datapath to which a flow with the given 'stage' may
226 static enum ovn_datapath_type
227 ovn_stage_to_datapath_type(enum ovn_stage stage
)
230 #define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
231 case S_##DP_TYPE##_##PIPELINE##_##STAGE: return DP_##DP_TYPE;
233 #undef PIPELINE_STAGE
234 default: OVS_NOT_REACHED();
242 %s: OVN northbound management daemon\n\
243 usage: %s [OPTIONS]\n\
246 --ovnnb-db=DATABASE connect to ovn-nb database at DATABASE\n\
248 --ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\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());
257 stream_usage("database", true, true, false);
261 struct hmap_node hmap_node
;
266 destroy_tnlids(struct hmap
*tnlids
)
268 struct tnlid_node
*node
;
269 HMAP_FOR_EACH_POP (node
, hmap_node
, tnlids
) {
272 hmap_destroy(tnlids
);
276 add_tnlid(struct hmap
*set
, uint32_t tnlid
)
278 struct tnlid_node
*node
= xmalloc(sizeof *node
);
279 hmap_insert(set
, &node
->hmap_node
, hash_int(tnlid
, 0));
284 tnlid_in_use(const struct hmap
*set
, uint32_t tnlid
)
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
) {
296 next_tnlid(uint32_t tnlid
, uint32_t max
)
298 return tnlid
+ 1 <= max
? tnlid
+ 1 : 1;
302 allocate_tnlid(struct hmap
*set
, const char *name
, uint32_t max
,
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
);
314 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
315 VLOG_WARN_RL(&rl
, "all %s tunnel ids exhausted", name
);
319 struct ovn_chassis_qdisc_queues
{
320 struct hmap_node key_node
;
322 struct uuid chassis_uuid
;
326 destroy_chassis_queues(struct hmap
*set
)
328 struct ovn_chassis_qdisc_queues
*node
;
329 HMAP_FOR_EACH_POP (node
, key_node
, set
) {
336 add_chassis_queue(struct hmap
*set
, struct uuid
*chassis_uuid
,
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
));
346 chassis_queueid_in_use(const struct hmap
*set
, struct uuid
*chassis_uuid
,
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
) {
360 allocate_chassis_queueid(struct hmap
*set
, struct sbrec_chassis
*chassis
)
362 for (uint32_t queue_id
= QDISC_MIN_QUEUE_ID
+ 1;
363 queue_id
<= QDISC_MAX_QUEUE_ID
;
365 if (!chassis_queueid_in_use(set
, &chassis
->header_
.uuid
, queue_id
)) {
366 add_chassis_queue(set
, &chassis
->header_
.uuid
, queue_id
);
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
);
377 free_chassis_queueid(struct hmap
*set
, struct sbrec_chassis
*chassis
,
380 struct ovn_chassis_qdisc_queues
*node
;
381 HMAP_FOR_EACH_WITH_HASH (node
, key_node
,
382 uuid_hash(&chassis
->header_
.uuid
),
384 if (uuid_equals(&chassis
->header_
.uuid
, &node
->chassis_uuid
)
385 && node
->queue_id
== queue_id
) {
386 hmap_remove(set
, &node
->key_node
);
393 port_has_qos_params(const struct smap
*opts
)
395 return (smap_get(opts
, "qos_max_rate") ||
396 smap_get(opts
, "qos_burst"));
403 unsigned long *allocated_ipv4s
; /* A bitmap of allocated IPv4s */
404 bool ipv6_prefix_set
;
405 struct in6_addr ipv6_prefix
;
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. */
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. */
418 struct ovs_list list
; /* In list of similar records. */
420 /* Logical switch data. */
421 struct ovn_port
**router_ports
;
422 size_t n_router_ports
;
424 struct hmap port_tnlids
;
425 uint32_t port_key_hint
;
430 struct ipam_info
*ipam_info
;
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
;
444 struct hmap_node hmap_node
;
445 struct eth_addr mac_addr
; /* Allocated MAC address. */
449 cleanup_macam(struct hmap
*macam_
)
451 struct macam_node
*node
;
452 HMAP_FOR_EACH_POP (node
, hmap_node
, macam_
) {
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
)
463 struct ovn_datapath
*od
= xzalloc(sizeof *od
);
468 hmap_init(&od
->port_tnlids
);
469 od
->port_key_hint
= 0;
470 hmap_insert(datapaths
, &od
->key_node
, uuid_hash(&od
->key
));
475 ovn_datapath_destroy(struct hmap
*datapaths
, struct ovn_datapath
*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
481 hmap_remove(datapaths
, &od
->key_node
);
482 destroy_tnlids(&od
->port_tnlids
);
484 bitmap_free(od
->ipam_info
->allocated_ipv4s
);
487 free(od
->router_ports
);
492 /* Returns 'od''s datapath type. */
493 static enum ovn_datapath_type
494 ovn_datapath_get_type(const struct ovn_datapath
*od
)
496 return od
->nbs
? DP_SWITCH
: DP_ROUTER
;
499 static struct ovn_datapath
*
500 ovn_datapath_find(struct hmap
*datapaths
, const struct uuid
*uuid
)
502 struct ovn_datapath
*od
;
504 HMAP_FOR_EACH_WITH_HASH (od
, key_node
, uuid_hash(uuid
), datapaths
) {
505 if (uuid_equals(uuid
, &od
->key
)) {
512 static struct ovn_datapath
*
513 ovn_datapath_from_sbrec(struct hmap
*datapaths
,
514 const struct sbrec_datapath_binding
*sb
)
518 if (!smap_get_uuid(&sb
->external_ids
, "logical-switch", &key
) &&
519 !smap_get_uuid(&sb
->external_ids
, "logical-router", &key
)) {
522 return ovn_datapath_find(datapaths
, &key
);
526 lrouter_is_enabled(const struct nbrec_logical_router
*lrouter
)
528 return !lrouter
->enabled
|| *lrouter
->enabled
;
532 init_ipam_info_for_datapath(struct ovn_datapath
*od
)
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");
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
);
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
);
561 if (!od
->ipam_info
) {
562 od
->ipam_info
= xzalloc(sizeof *od
->ipam_info
);
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
);
569 /* Mark first IP as taken */
570 bitmap_set1(od
->ipam_info
->allocated_ipv4s
, 0);
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
,
575 if (!exclude_ip_list
) {
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".
585 while (lexer
.token
.type
!= LEX_T_END
) {
586 if (lexer
.token
.type
!= LEX_T_INTEGER
) {
587 lexer_syntax_error(&lexer
, "expecting address");
590 uint32_t start
= ntohl(lexer
.token
.value
.ipv4
);
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");
599 end
= ntohl(lexer
.token
.value
.ipv4
) + 1;
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
);
607 bitmap_set_multiple(od
->ipam_info
->allocated_ipv4s
,
608 start
- od
->ipam_info
->start_ipv4
,
611 lexer_error(&lexer
, "excluded addresses not in subnet");
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
);
619 lexer_destroy(&lexer
);
623 ovn_datapath_update_external_ids(struct ovn_datapath
*od
)
625 /* Get the logical-switch or logical-router UUID to set in
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";
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"));
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
);
646 sbrec_datapath_binding_set_external_ids(od
->sb
, &ids
);
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
)
655 hmap_init(datapaths
);
656 ovs_list_init(sb_only
);
657 ovs_list_init(nb_only
);
660 const struct sbrec_datapath_binding
*sb
, *sb_next
;
661 SBREC_DATAPATH_BINDING_FOR_EACH_SAFE (sb
, sb_next
, ctx
->ovnsb_idl
) {
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(
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
);
675 if (ovn_datapath_find(datapaths
, &key
)) {
676 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
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
);
685 struct ovn_datapath
*od
= ovn_datapath_create(datapaths
, &key
,
687 ovs_list_push_back(sb_only
, &od
->list
);
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
,
696 ovs_list_remove(&od
->list
);
697 ovs_list_push_back(both
, &od
->list
);
698 ovn_datapath_update_external_ids(od
);
700 od
= ovn_datapath_create(datapaths
, &nbs
->header_
.uuid
,
702 ovs_list_push_back(nb_only
, &od
->list
);
705 init_ipam_info_for_datapath(od
);
708 const struct nbrec_logical_router
*nbr
;
709 NBREC_LOGICAL_ROUTER_FOR_EACH (nbr
, ctx
->ovnnb_idl
) {
710 if (!lrouter_is_enabled(nbr
)) {
714 struct ovn_datapath
*od
= ovn_datapath_find(datapaths
,
719 ovs_list_remove(&od
->list
);
720 ovs_list_push_back(both
, &od
->list
);
721 ovn_datapath_update_external_ids(od
);
724 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
726 "duplicate UUID "UUID_FMT
" in OVN_Northbound",
727 UUID_ARGS(&nbr
->header_
.uuid
));
731 od
= ovn_datapath_create(datapaths
, &nbr
->header_
.uuid
,
733 ovs_list_push_back(nb_only
, &od
->list
);
739 ovn_datapath_allocate_key(struct hmap
*dp_tnlids
)
741 static uint32_t hint
;
742 return allocate_tnlid(dp_tnlids
, "datapath", (1u << 24) - 1, &hint
);
745 /* Updates the southbound Datapath_Binding table so that it contains the
746 * logical switches and routers specified by the northbound database.
748 * Initializes 'datapaths' to contain a "struct ovn_datapath" for every logical
749 * switch and router. */
751 build_datapaths(struct northd_context
*ctx
, struct hmap
*datapaths
)
753 struct ovs_list sb_only
, nb_only
, both
;
755 join_datapaths(ctx
, datapaths
, &sb_only
, &nb_only
, &both
);
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
);
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
);
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
);
776 destroy_tnlids(&dp_tnlids
);
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
);
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. */
793 const struct sbrec_port_binding
*sb
; /* May be NULL. */
795 /* Logical switch port data. */
796 const struct nbrec_logical_switch_port
*nbsp
; /* May be NULL. */
798 struct lport_addresses
*lsp_addrs
; /* Logical switch port addresses. */
799 unsigned int n_lsp_addrs
;
801 struct lport_addresses
*ps_addrs
; /* Port security addresses. */
802 unsigned int n_ps_addrs
;
804 /* Logical router port data. */
805 const struct nbrec_logical_router_port
*nbrp
; /* May be NULL. */
807 struct lport_addresses lrp_networks
;
809 bool derived
; /* Indicates whether this is an additional port
810 * derived from nbsp or nbrp. */
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.
817 * - Two connected logical router ports have each other as peer. */
818 struct ovn_port
*peer
;
820 struct ovn_datapath
*od
;
822 struct ovs_list list
; /* In list of similar records. */
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
)
831 struct ovn_port
*op
= xzalloc(sizeof *op
);
833 struct ds json_key
= DS_EMPTY_INITIALIZER
;
834 json_string_escape(key
, &json_key
);
835 op
->json_key
= ds_steal_cstr(&json_key
);
837 op
->key
= xstrdup(key
);
842 hmap_insert(ports
, &op
->key_node
, hash_string(op
->key
, 0));
847 ovn_port_destroy(struct hmap
*ports
, struct ovn_port
*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
853 hmap_remove(ports
, &port
->key_node
);
855 for (int i
= 0; i
< port
->n_lsp_addrs
; i
++) {
856 destroy_lport_addresses(&port
->lsp_addrs
[i
]);
858 free(port
->lsp_addrs
);
860 for (int i
= 0; i
< port
->n_ps_addrs
; i
++) {
861 destroy_lport_addresses(&port
->ps_addrs
[i
]);
863 free(port
->ps_addrs
);
865 destroy_lport_addresses(&port
->lrp_networks
);
866 free(port
->json_key
);
872 static struct ovn_port
*
873 ovn_port_find(struct hmap
*ports
, const char *name
)
877 HMAP_FOR_EACH_WITH_HASH (op
, key_node
, hash_string(name
, 0), ports
) {
878 if (!strcmp(op
->key
, name
)) {
886 ovn_port_allocate_key(struct ovn_datapath
*od
)
888 return allocate_tnlid(&od
->port_tnlids
, "port",
889 (1u << 15) - 1, &od
->port_key_hint
);
893 chassis_redirect_name(const char *port_name
)
895 return xasprintf("cr-%s", port_name
);
899 ipam_is_duplicate_mac(struct eth_addr
*ea
, uint64_t mac64
, bool warn
)
901 struct macam_node
*macam_node
;
902 HMAP_FOR_EACH_WITH_HASH (macam_node
, hmap_node
, hash_uint64(mac64
),
904 if (eth_addr_equals(*ea
, macam_node
->mac_addr
)) {
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
));
917 ipam_insert_mac(struct eth_addr
*ea
, bool check
)
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
927 if (((mac64
^ MAC_ADDR_PREFIX
) >> 24)
928 || (check
&& ipam_is_duplicate_mac(ea
, mac64
, true))) {
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
));
938 ipam_insert_ip(struct ovn_datapath
*od
, uint32_t ip
)
940 if (!od
|| !od
->ipam_info
|| !od
->ipam_info
->allocated_ipv4s
) {
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
);
952 ipam_insert_lsp_addresses(struct ovn_datapath
*od
, struct ovn_port
*op
,
955 if (!od
|| !op
|| !address
|| !strcmp(address
, "unknown")
956 || !strcmp(address
, "router") || is_dynamic_lsp_address(address
)) {
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.");
966 ipam_insert_mac(&laddrs
.ea
, true);
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
);
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
);
980 destroy_lport_addresses(&laddrs
);
984 ipam_add_port_addresses(struct ovn_datapath
*od
, struct ovn_port
*op
)
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
]);
995 if (op
->nbsp
->dynamic_addresses
) {
996 ipam_insert_lsp_addresses(od
, op
, op
->nbsp
->dynamic_addresses
);
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.");
1006 ipam_insert_mac(&lrp_networks
.ea
, true);
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
);
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
);
1019 destroy_lport_addresses(&lrp_networks
);
1024 ipam_get_unused_mac(void)
1026 /* Stores the suffix of the most recently ipam-allocated MAC address. */
1027 static uint32_t last_mac
;
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
;
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.");
1053 ipam_get_unused_ip(struct ovn_datapath
*od
)
1055 if (!od
|| !od
->ipam_info
|| !od
->ipam_info
->allocated_ipv4s
) {
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.");
1067 return od
->ipam_info
->start_ipv4
+ new_ip_index
;
1071 ipam_allocate_addresses(struct ovn_datapath
*od
, struct ovn_port
*op
,
1072 const char *addrspec
)
1074 if (!op
->nbsp
|| !od
->ipam_info
) {
1078 /* Get or generate MAC address. */
1079 struct eth_addr mac
;
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;
1087 uint64_t mac64
= ipam_get_unused_mac();
1091 eth_addr_from_uint64(mac64
, &mac
);
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;
1099 /* Generate IPv6 address, if desirable. */
1100 bool dynamic_ip6
= od
->ipam_info
->ipv6_prefix_set
;
1101 struct in6_addr ip6
;
1103 in6_generate_eui64(mac
, &od
->ipam_info
->ipv6_prefix
, &ip6
);
1106 /* If we didn't generate anything, bail out. */
1107 if (!dynamic_ip4
&& !dynamic_ip6
) {
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
)));
1119 char ip6_s
[INET6_ADDRSTRLEN
+ 1];
1120 ipv6_string_mapped(ip6_s
, &ip6
);
1121 ds_put_format(&new_addr
, " %s", ip6_s
);
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
);
1131 build_ipam(struct hmap
*datapaths
, struct hmap
*ports
)
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
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
) {
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
=
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
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.");
1179 if (!nbsp
->n_addresses
&& nbsp
->dynamic_addresses
) {
1180 nbrec_logical_switch_port_set_dynamic_addresses(op
->nbsp
,
1187 /* Tag allocation for nested containers.
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
;
1194 unsigned long *allocated_tags
; /* A bitmap to track allocated tags. */
1198 tag_alloc_destroy(struct hmap
*tag_alloc_table
)
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
);
1206 hmap_destroy(tag_alloc_table
);
1209 static struct tag_alloc_node
*
1210 tag_alloc_get_node(struct hmap
*tag_alloc_table
, const char *parent_name
)
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),
1217 if (!strcmp(tag_alloc_node
->parent_name
, parent_name
)) {
1218 return tag_alloc_node
;
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));
1231 return tag_alloc_node
;
1235 tag_alloc_add_existing_tags(struct hmap
*tag_alloc_table
,
1236 const struct nbrec_logical_switch_port
*nbsp
)
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
) {
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
);
1250 tag_alloc_create_new_tag(struct hmap
*tag_alloc_table
,
1251 const struct nbrec_logical_switch_port
*nbsp
)
1253 if (!nbsp
->tag_request
) {
1257 if (nbsp
->parent_name
&& nbsp
->parent_name
[0]
1258 && *nbsp
->tag_request
== 0) {
1259 /* For nested containers that need allocation, do the allocation. */
1262 /* This has already been allocated. */
1266 struct tag_alloc_node
*tag_alloc_node
;
1268 tag_alloc_node
= tag_alloc_get_node(tag_alloc_table
,
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
);
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);
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
1292 check_and_update_mac_in_dynamic_addresses(
1293 const char *address
,
1294 const struct nbrec_logical_switch_port
*nbsp
)
1296 if (!nbsp
->dynamic_addresses
) {
1301 if (!ovs_scan_len(address
, &buf_index
,
1302 ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(ea
))) {
1306 struct eth_addr present_ea
;
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  
->dynamic_addresses
[buf_index
]);
1315 nbrec_logical_switch_port_set_dynamic_addresses(
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
)
1329 ovs_list_init(sb_only
);
1330 ovs_list_init(nb_only
);
1331 ovs_list_init(both
);
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
,
1337 ovs_list_push_back(sb_only
, &op
->list
);
1340 struct ovn_datapath
*od
;
1341 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
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
);
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",
1356 ovs_list_remove(&op
->list
);
1358 uint32_t queue_id
= smap_get_int(&op
->sb
->options
,
1359 "qdisc_queue_id", 0);
1360 if (queue_id
&& op
->sb
->chassis
) {
1362 chassis_qdisc_queues
, &op
->sb
->chassis
->header_
.uuid
,
1366 ovs_list_push_back(both
, &op
->list
);
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
);
1372 op
= ovn_port_create(ports
, nbsp
->name
, nbsp
, NULL
, NULL
);
1373 ovs_list_push_back(nb_only
, &op
->list
);
1376 if (!strcmp(nbsp
->type
, "localnet")) {
1377 od
->localnet_port
= op
;
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")) {
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
);
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 "
1412 op
->nbsp
->addresses
[j
]);
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
]);
1434 ipam_add_port_addresses(od
, op
);
1435 tag_alloc_add_existing_tags(tag_alloc_table
, nbsp
);
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
];
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
);
1450 if (!lrp_networks
.n_ipv4_addrs
&& !lrp_networks
.n_ipv6_addrs
) {
1454 struct ovn_port
*op
= ovn_port_find(ports
, nbrp
->name
);
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",
1464 ovs_list_remove(&op
->list
);
1465 ovs_list_push_back(both
, &op
->list
);
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
);
1472 op
= ovn_port_create(ports
, nbrp
->name
, NULL
, nbrp
, NULL
);
1473 ovs_list_push_back(nb_only
, &op
->list
);
1476 op
->lrp_networks
= lrp_networks
;
1478 ipam_add_port_addresses(op
->od
, op
);
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
,
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
);
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
);
1504 char *redirect_name
= chassis_redirect_name(nbrp
->name
);
1505 struct ovn_port
*crp
= ovn_port_find(ports
, redirect_name
);
1507 crp
->derived
= true;
1509 ovs_list_remove(&crp
->list
);
1510 ovs_list_push_back(both
, &crp
->list
);
1512 crp
= ovn_port_create(ports
, redirect_name
,
1514 crp
->derived
= true;
1515 ovs_list_push_back(nb_only
, &crp
->list
);
1518 free(redirect_name
);
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
;
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");
1539 struct ovn_port
*peer
= ovn_port_find(ports
, peer_name
);
1540 if (!peer
|| !peer
->nbrp
) {
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
;
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
])) {
1562 } else if (op
->nbrp
&& op
->nbrp
->peer
&& !op
->derived
) {
1563 struct ovn_port
*peer
= ovn_port_find(ports
, op
->nbrp
->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
);
1584 ip_address_and_port_from_lb_key(const char *key
, char **ip_address
,
1585 uint16_t *port
, int *addr_family
);
1588 get_router_load_balancer_ips(const struct ovn_datapath
*od
,
1589 struct sset
*all_ips
, int *addr_family
)
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
;
1600 SMAP_FOR_EACH (node
, vips
) {
1601 /* node->key contains IP:port or just IP. */
1602 char *ip_address
= NULL
;
1605 ip_address_and_port_from_lb_key(node
->key
, &ip_address
, &port
,
1611 if (!sset_contains(all_ips
, ip_address
)) {
1612 sset_add(all_ips
, ip_address
);
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.
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. */
1631 get_nat_addresses(const struct ovn_port
*op
, size_t *n
)
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
)) {
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;
1647 addresses
= xmalloc(sizeof *addresses
* (op
->od
->nbr
->n_nat
+ 1));
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
];
1654 char *error
= ip_parse_masked(nat
->external_ip
, &ip
, &mask
);
1655 if (error
|| mask
!= OVS_BE32_MAX
) {
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\")",
1671 addresses
[n_nats
++] = ds_steal_cstr(&address
);
1674 /* Centralized NAT rule, either on gateway router or distributed
1676 ds_put_format(&c_addresses
, " %s", nat
->external_ip
);
1677 central_ip_address
= true;
1681 /* A set to hold all load-balancer vips. */
1682 struct sset all_ips
= SSET_INITIALIZER(&all_ips
);
1684 get_router_load_balancer_ips(op
->od
, &all_ips
, &addr_family
);
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;
1691 sset_destroy(&all_ips
);
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
);
1701 addresses
[n_nats
++] = ds_steal_cstr(&c_addresses
);
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
)
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
);
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 */
1728 || (sb_gwc
->chassis
&& !strcmp(nb_gwc_c
->name
,
1729 sb_gwc
->chassis
->name
));
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
)
1738 if (!lrp
|| !port_binding
) {
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
*
1747 const struct sbrec_chassis
**lrp_gwc_c
= xzalloc(lrp
->n_gateway_chassis
*
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;
1754 for (n
= 0; n
< lrp
->n_gateway_chassis
; n
++) {
1756 if (!lrp
->gateway_chassis
[n
]->chassis_name
) {
1760 const struct sbrec_chassis
*chassis
=
1761 chassis_lookup_by_name(chassis_index
,
1762 lrp
->gateway_chassis
[n
]->chassis_name
);
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
++;
1768 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
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
);
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
) {
1784 for (n
= 0; n
< lrp_n_gateway_chassis
; n
++) {
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
1789 for (i
= 0; i
< port_binding
->n_gateway_chassis
; i
++) {
1790 if (gateway_chassis_equal(lrp_gwc
[n
],
1792 port_binding
->gateway_chassis
[i
])) {
1793 break; /* we found a match */
1797 /* if no Port_Binding gateway chassis matched for the entry... */
1798 if (i
== port_binding
->n_gateway_chassis
) {
1801 return true; /* found no match for this gateway chassis on lrp */
1805 /* no need for update, all ports matched */
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 */
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
) {
1822 if (!lrp
|| !port_binding
|| !lrp
->n_gateway_chassis
) {
1826 struct sbrec_gateway_chassis
**gw_chassis
= NULL
;
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
) {
1842 const struct sbrec_chassis
*chassis
=
1843 chassis_lookup_by_name(chassis_index
, lrp_gwc
->chassis_name
);
1845 gw_chassis
= xrealloc(gw_chassis
, (n_gwc
+ 1) * sizeof *gw_chassis
);
1847 struct sbrec_gateway_chassis
*pb_gwc
=
1848 sbrec_gateway_chassis_insert(ctx
->ovnsb_txn
);
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
);
1856 gw_chassis
[n_gwc
++] = pb_gwc
;
1858 sbrec_port_binding_set_gateway_chassis(port_binding
, gw_chassis
, n_gwc
);
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
)
1868 sbrec_port_binding_set_datapath(op
->sb
, op
->od
->sb
);
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");
1874 sbrec_port_binding_set_type(op
->sb
, "chassisredirect");
1875 } else if (chassis_name
) {
1876 sbrec_port_binding_set_type(op
->sb
, "l3gateway");
1878 sbrec_port_binding_set_type(op
->sb
, "patch");
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);
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
);
1895 if (op
->nbrp
->n_gateway_chassis
) {
1896 if (sbpb_gw_chassis_needs_update(op
->sb
, op
->nbrp
,
1898 copy_gw_chassis_from_nbrp_to_sbpb(ctx
, op
->nbrp
,
1899 chassis_index
, op
->sb
);
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
);
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
,
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
1919 struct sbrec_gateway_chassis
*gw_chassis
=
1920 sbrec_gateway_chassis_insert(ctx
->ovnsb_txn
);
1922 char *gwc_name
= xasprintf("%s_%s", op
->nbrp
->name
,
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
,
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
,
1947 smap_add(&new, "distributed-port", op
->nbrp
->name
);
1950 smap_add(&new, "peer", op
->peer
->key
);
1953 smap_add(&new, "l3gateway-chassis", chassis_name
);
1956 sbrec_port_binding_set_options(op
->sb
, &new);
1959 sbrec_port_binding_set_parent_port(op
->sb
, NULL
);
1960 sbrec_port_binding_set_tag(op
->sb
, NULL
, 0);
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
]);
1967 const char *addresses
= ds_cstr(&s
);
1968 sbrec_port_binding_set_mac(op
->sb
, &addresses
, 1);
1971 struct smap ids
= SMAP_INITIALIZER(&ids
);
1972 sbrec_port_binding_set_external_ids(op
->sb
, &ids
);
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
;
1980 if (op
->sb
->chassis
&& has_qos
&& !queue_id
) {
1981 queue_id
= allocate_chassis_queueid(chassis_qdisc_queues
,
1983 } else if (!has_qos
&& queue_id
) {
1984 free_chassis_queueid(chassis_qdisc_queues
,
1990 smap_clone(&options
, &op
->nbsp
->options
);
1992 smap_add_format(&options
,
1993 "qdisc_queue_id", "%d", queue_id
);
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
);
2000 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2002 &rl
, "Unknown port type '%s' set on logical switch '%s'.",
2003 op
->nbsp
->type
, op
->nbsp
->name
);
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");
2011 /* A switch port connected to a gateway router is also of
2012 * type "l3gateway". */
2014 sbrec_port_binding_set_type(op
->sb
, "l3gateway");
2016 sbrec_port_binding_set_type(op
->sb
, "patch");
2019 const char *router_port
= smap_get(&op
->nbsp
->options
,
2021 if (router_port
|| chassis
) {
2025 smap_add(&new, "peer", router_port
);
2028 smap_add(&new, "l3gateway-chassis", chassis
);
2030 sbrec_port_binding_set_options(op
->sb
, &new);
2034 const char *nat_addresses
= smap_get(&op
->nbsp
->options
,
2036 if (nat_addresses
&& !strcmp(nat_addresses
, "router")) {
2037 if (op
->peer
&& op
->peer
->od
2038 && (chassis
|| op
->peer
->od
->l3redirect_port
)) {
2040 char **nats
= get_nat_addresses(op
->peer
, &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
++) {
2049 sbrec_port_binding_set_nat_addresses(op
->sb
, NULL
, 0);
2052 sbrec_port_binding_set_nat_addresses(op
->sb
, NULL
, 0);
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);
2064 sbrec_port_binding_set_nat_addresses(op
->sb
,
2066 destroy_lport_addresses(&laddrs
);
2069 sbrec_port_binding_set_nat_addresses(op
->sb
, NULL
, 0);
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
);
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
);
2083 sbrec_port_binding_set_external_ids(op
->sb
, &ids
);
2088 /* Remove mac_binding entries that refer to logical_ports which are
2091 cleanup_mac_bindings(struct northd_context
*ctx
, struct hmap
*ports
)
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
);
2101 /* Updates the southbound Port_Binding table so that it contains the logical
2102 * switch ports specified by the northbound database.
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
2108 build_ports(struct northd_context
*ctx
, struct hmap
*datapaths
,
2109 const struct chassis_index
*chassis_index
, struct hmap
*ports
)
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
);
2115 join_logical_ports(ctx
, datapaths
, ports
, &chassis_qdisc_queues
,
2116 &tag_alloc_table
, &sb_only
, &nb_only
, &both
);
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
2123 LIST_FOR_EACH_SAFE (op
, next
, list
, &both
) {
2125 tag_alloc_create_new_tag(&tag_alloc_table
, op
->nbsp
);
2127 ovn_port_update_sbrec(ctx
, op
, chassis_index
, &chassis_qdisc_queues
);
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
;
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
);
2142 op
->sb
= sbrec_port_binding_insert(ctx
->ovnsb_txn
);
2143 ovn_port_update_sbrec(ctx
, op
, chassis_index
, &chassis_qdisc_queues
);
2145 sbrec_port_binding_set_logical_port(op
->sb
, op
->key
);
2146 sbrec_port_binding_set_tunnel_key(op
->sb
, tunnel_key
);
2149 bool remove_mac_bindings
= false;
2150 if (!ovs_list_is_empty(&sb_only
)) {
2151 remove_mac_bindings
= true;
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
);
2160 if (remove_mac_bindings
) {
2161 cleanup_mac_bindings(ctx
, ports
);
2164 tag_alloc_destroy(&tag_alloc_table
);
2165 destroy_chassis_queues(&chassis_qdisc_queues
);
2168 #define OVN_MIN_MULTICAST 32768
2169 #define OVN_MAX_MULTICAST 65535
2171 struct multicast_group
{
2173 uint16_t key
; /* OVN_MIN_MULTICAST...OVN_MAX_MULTICAST. */
2176 #define MC_FLOOD "_MC_flood"
2177 static const struct multicast_group mc_flood
= { MC_FLOOD
, 65535 };
2179 #define MC_UNKNOWN "_MC_unknown"
2180 static const struct multicast_group mc_unknown
= { MC_UNKNOWN
, 65534 };
2183 multicast_group_equal(const struct multicast_group
*a
,
2184 const struct multicast_group
*b
)
2186 return !strcmp(a
->name
, b
->name
) && a
->key
== b
->key
;
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
;
2195 struct ovn_port
**ports
;
2196 size_t n_ports
, allocated_ports
;
2200 ovn_multicast_hash(const struct ovn_datapath
*datapath
,
2201 const struct multicast_group
*group
)
2203 return hash_pointer(datapath
, group
->key
);
2206 static struct ovn_multicast
*
2207 ovn_multicast_find(struct hmap
*mcgroups
, struct ovn_datapath
*datapath
,
2208 const struct multicast_group
*group
)
2210 struct ovn_multicast
*mc
;
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
)) {
2223 ovn_multicast_add(struct hmap
*mcgroups
, const struct multicast_group
*group
,
2224 struct ovn_port
*port
)
2226 struct ovn_datapath
*od
= port
->od
;
2227 struct ovn_multicast
*mc
= ovn_multicast_find(mcgroups
, od
, group
);
2229 mc
= xmalloc(sizeof *mc
);
2230 hmap_insert(mcgroups
, &mc
->hmap_node
, ovn_multicast_hash(od
, group
));
2234 mc
->allocated_ports
= 4;
2235 mc
->ports
= xmalloc(mc
->allocated_ports
* sizeof *mc
->ports
);
2237 if (mc
->n_ports
>= mc
->allocated_ports
) {
2238 mc
->ports
= x2nrealloc(mc
->ports
, &mc
->allocated_ports
,
2241 mc
->ports
[mc
->n_ports
++] = port
;
2245 ovn_multicast_destroy(struct hmap
*mcgroups
, struct ovn_multicast
*mc
)
2248 hmap_remove(mcgroups
, &mc
->hmap_node
);
2255 ovn_multicast_update_sbrec(const struct ovn_multicast
*mc
,
2256 const struct sbrec_multicast_group
*sb
)
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
);
2262 sbrec_multicast_group_set_ports(sb
, ports
, mc
->n_ports
);
2266 /* Logical flow generation.
2268 * This code generates the Logical_Flow table in the southbound database, as a
2269 * function of most of the northbound database.
2273 struct hmap_node hmap_node
;
2275 struct ovn_datapath
*od
;
2276 enum ovn_stage stage
;
2285 ovn_lflow_hash(const struct ovn_lflow
*lflow
)
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
,
2295 ovn_lflow_equal(const struct ovn_lflow
*a
, const struct ovn_lflow
*b
)
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
));
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
,
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
;
2319 /* Adds a row with the specified contents to the Logical_Flow table. */
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
)
2326 ovs_assert(ovn_stage_to_datapath_type(stage
) == ovn_datapath_get_type(od
));
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
));
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)
2341 #define ovn_lflow_add(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, ACTIONS) \
2342 ovn_lflow_add_with_hint(LFLOW_MAP, OD, STAGE, PRIORITY, MATCH, \
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
)
2350 struct ovn_lflow target
;
2351 ovn_lflow_init(&target
, od
, stage
, priority
,
2352 CONST_CAST(char *, match
), CONST_CAST(char *, actions
),
2355 struct ovn_lflow
*lflow
;
2356 HMAP_FOR_EACH_WITH_HASH (lflow
, hmap_node
, hash
, lflows
) {
2357 if (ovn_lflow_equal(lflow
, &target
)) {
2365 ovn_lflow_destroy(struct hmap
*lflows
, struct ovn_lflow
*lflow
)
2368 hmap_remove(lflows
, &lflow
->hmap_node
);
2370 free(lflow
->actions
);
2371 free(lflow
->stage_hint
);
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(). */
2381 build_port_security_l2(const char *eth_addr_field
,
2382 struct lport_addresses
*ps_addrs
,
2383 unsigned int n_ps_addrs
,
2390 ds_put_format(match
, " && %s == {", eth_addr_field
);
2392 for (size_t i
= 0; i
< n_ps_addrs
; i
++) {
2393 ds_put_format(match
, "%s ", ps_addrs
[i
].ea_s
);
2395 ds_chomp(match
, ' ');
2396 ds_put_cstr(match
, "}");
2400 build_port_security_ipv6_nd_flow(
2401 struct ds
*match
, struct eth_addr ea
, struct ipv6_netaddr
*ipv6_addrs
,
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
),
2409 if (!n_ipv6_addrs
) {
2410 ds_put_cstr(match
, "))");
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
);
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
);
2427 ds_put_format(match
, ")))");
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
)
2435 char ip6_str
[INET6_ADDRSTRLEN
+ 1];
2437 ds_put_format(match
, " && %s == {",
2438 pipeline
== P_IN
? "ip6.src" : "ip6.dst");
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
);
2446 /* Allow ip6.dst=ff00::/8 for multicast packets */
2447 if (pipeline
== P_OUT
) {
2448 ds_put_cstr(match
, "ff00::/8, ");
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
);
2454 /* Replace ", " by "}". */
2455 ds_chomp(match
, ' ');
2456 ds_chomp(match
, ',');
2457 ds_put_cstr(match
, "}");
2461 * Build port security constraints on ARP and IPv6 ND fields
2462 * and add logical flows to S_SWITCH_IN_PORT_SEC_ND stage.
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.
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.
2479 * - Priority 80 flow to drop ARP and IPv6 ND packets.
2482 build_port_security_nd(struct ovn_port
*op
, struct hmap
*lflows
)
2484 struct ds match
= DS_EMPTY_INITIALIZER
;
2486 for (size_t i
= 0; i
< op
->n_ps_addrs
; i
++) {
2487 struct lport_addresses
*ps
= &op
->ps_addrs
[i
];
2489 bool no_ip
= !(ps
->n_ipv4_addrs
|| ps
->n_ipv6_addrs
);
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
);
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
);
2508 ds_put_format(&match
, "%s/%d",
2509 ps
->ipv4_addrs
[j
].network_s
,
2510 ps
->ipv4_addrs
[j
].plen
);
2512 ds_put_cstr(&match
, ", ");
2514 ds_chomp(&match
, ' ');
2515 ds_chomp(&match
, ',');
2516 ds_put_cstr(&match
, "}");
2518 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_PORT_SEC_ND
, 90,
2519 ds_cstr(&match
), "next;");
2522 if (ps
->n_ipv6_addrs
|| no_ip
) {
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
,
2528 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_PORT_SEC_ND
, 90,
2529 ds_cstr(&match
), "next;");
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;");
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.
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
2549 * - If the port security has IPv6 addresses,
2550 * - Priority 90 flow to allow IPv6 packets for known IPv6 addresses
2552 * - If the port security has IPv4 addresses or IPv6 addresses or both
2553 * - Priority 80 flow to drop all IPv4 and IPv6 traffic
2556 build_port_security_ip(enum ovn_pipeline pipeline
, struct ovn_port
*op
,
2557 struct hmap
*lflows
)
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
;
2565 port_direction
= "outport";
2566 stage
= S_SWITCH_OUT_PORT_SEC_IP
;
2569 for (size_t i
= 0; i
< op
->n_ps_addrs
; i
++) {
2570 struct lport_addresses
*ps
= &op
->ps_addrs
[i
];
2572 if (!(ps
->n_ipv4_addrs
|| ps
->n_ipv6_addrs
)) {
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"
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
,
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
);
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.
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
);
2616 /* host portion is zero */
2617 ds_put_format(&match
, "%s/%d", ps
->ipv4_addrs
[j
].network_s
,
2618 ps
->ipv4_addrs
[j
].plen
);
2620 ds_put_cstr(&match
, ", ");
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;");
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
2636 struct ds dad_match
= DS_EMPTY_INITIALIZER
;
2637 ds_put_format(&dad_match
, "inport == %s"
2640 " && ip6.dst == ff02::/16"
2641 " && icmp6.type == {131, 135, 143}", op
->json_key
,
2643 ovn_lflow_add(lflows
, op
->od
, stage
, 90,
2644 ds_cstr(&dad_match
), "next;");
2645 ds_destroy(&dad_match
);
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;");
2657 char *match
= xasprintf("%s == %s && %s == %s && ip",
2658 port_direction
, op
->json_key
,
2659 pipeline
== P_IN
? "eth.src" : "eth.dst",
2661 ovn_lflow_add(lflows
, op
->od
, stage
, 80, match
, "drop;");
2668 lsp_is_enabled(const struct nbrec_logical_switch_port
*lsp
)
2670 return !lsp
->enabled
|| *lsp
->enabled
;
2674 lsp_is_up(const struct nbrec_logical_switch_port
*lsp
)
2676 return !lsp
->up
|| *lsp
->up
;
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
)
2684 if (!op
->nbsp
->dhcpv4_options
) {
2685 /* CMS has disabled native DHCPv4 for this lport. */
2689 ovs_be32 host_ip
, mask
;
2690 char *error
= ip_parse_masked(op
->nbsp
->dhcpv4_options
->cidr
, &host_ip
,
2692 if (error
|| ((offer_ip
^ host_ip
) & mask
)) {
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.
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");
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",
2718 struct smap dhcpv4_options
= SMAP_INITIALIZER(&dhcpv4_options
);
2719 smap_clone(&dhcpv4_options
, &op
->nbsp
->dhcpv4_options
->options
);
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
);
2727 ds_put_format(options_action
,
2728 REGBIT_DHCP_OPTS_RESULT
" = put_dhcp_opts(offerip = "
2729 IP_FMT
", ", IP_ARGS(offer_ip
));
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
);
2740 ds_chomp(options_action
, ' ');
2741 ds_chomp(options_action
, ',');
2742 ds_put_cstr(options_action
, "); next;");
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; "
2748 server_mac
, IP_ARGS(offer_ip
), server_ip
);
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
);
2758 build_dhcpv6_action(struct ovn_port
*op
, struct in6_addr
*offer_ip
,
2759 struct ds
*options_action
, struct ds
*response_action
)
2761 if (!op
->nbsp
->dhcpv6_options
) {
2762 /* CMS has disabled native DHCPv6 for this lport. */
2766 struct in6_addr host_ip
, mask
;
2768 char *error
= ipv6_parse_masked(op
->nbsp
->dhcpv6_options
->cidr
, &host_ip
,
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
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");
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
);
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
);
2798 char server_ip
[INET6_ADDRSTRLEN
+ 1];
2799 ipv6_string_mapped(server_ip
, &lla
);
2801 char ia_addr
[INET6_ADDRSTRLEN
+ 1];
2802 ipv6_string_mapped(ia_addr
, offer_ip
);
2804 ds_put_format(options_action
,
2805 REGBIT_DHCP_OPTS_RESULT
" = put_dhcpv6_opts(");
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
);
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
);
2825 ds_chomp(options_action
, ' ');
2826 ds_chomp(options_action
, ',');
2827 ds_put_cstr(options_action
, "); next;");
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; "
2833 server_mac
, server_ip
);
2839 has_stateful_acl(struct ovn_datapath
*od
)
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")) {
2852 build_pre_acls(struct ovn_datapath
*od
, struct hmap
*lflows
)
2854 bool has_stateful
= has_stateful_acl(od
);
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;");
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. */
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
;
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;");
2889 ds_destroy(&match_in
);
2890 ds_destroy(&match_out
);
2892 if (od
->localnet_port
) {
2893 struct ds match_in
= DS_EMPTY_INITIALIZER
;
2894 struct ds match_out
= DS_EMPTY_INITIALIZER
;
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;");
2905 ds_destroy(&match_in
);
2906 ds_destroy(&match_out
);
2909 /* Ingress and Egress Pre-ACL Table (Priority 110).
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)",
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)",
2922 /* Ingress and Egress Pre-ACL Table (Priority 100).
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.
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;");
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
2941 ip_address_and_port_from_lb_key(const char *key
, char **ip_address
,
2942 uint16_t *port
, int *addr_family
)
2944 struct sockaddr_storage ss
;
2945 char ip_addr_buf
[INET6_ADDRSTRLEN
];
2948 error
= ipv46_parse(key
, PORT_OPTIONAL
, &ss
);
2950 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
2951 VLOG_WARN_RL(&rl
, "bad ip address or port for load balancer key %s",
2957 if (ss
.ss_family
== AF_INET
) {
2958 struct sockaddr_in
*sin
= ALIGNED_CAST(struct sockaddr_in
*, &ss
);
2959 *port
= sin
->sin_port
== 0 ? 0 : ntohs(sin
->sin_port
);
2960 inet_ntop(AF_INET
, &sin
->sin_addr
, ip_addr_buf
, sizeof ip_addr_buf
);
2962 struct sockaddr_in6
*sin6
= ALIGNED_CAST(struct sockaddr_in6
*, &ss
);
2963 *port
= sin6
->sin6_port
== 0 ? 0 : ntohs(sin6
->sin6_port
);
2964 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, ip_addr_buf
, sizeof ip_addr_buf
);
2967 *ip_address
= xstrdup(ip_addr_buf
);
2968 *addr_family
= ss
.ss_family
;
2972 * Returns true if logical switch is configured with DNS records, false
2976 ls_has_dns_records(const struct nbrec_logical_switch
*nbs
)
2978 for (size_t i
= 0; i
< nbs
->n_dns_records
; i
++) {
2979 if (!smap_is_empty(&nbs
->dns_records
[i
]->records
)) {
2988 build_pre_lb(struct ovn_datapath
*od
, struct hmap
*lflows
)
2990 /* Allow all packets to go to next tables by default. */
2991 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PRE_LB
, 0, "1", "next;");
2992 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PRE_LB
, 0, "1", "next;");
2994 struct sset all_ips
= SSET_INITIALIZER(&all_ips
);
2995 bool vip_configured
= false;
2996 int addr_family
= AF_INET
;
2997 for (int i
= 0; i
< od
->nbs
->n_load_balancer
; i
++) {
2998 struct nbrec_load_balancer
*lb
= od
->nbs
->load_balancer
[i
];
2999 struct smap
*vips
= &lb
->vips
;
3000 struct smap_node
*node
;
3002 SMAP_FOR_EACH (node
, vips
) {
3003 vip_configured
= true;
3005 /* node->key contains IP:port or just IP. */
3006 char *ip_address
= NULL
;
3008 ip_address_and_port_from_lb_key(node
->key
, &ip_address
, &port
,
3014 if (!sset_contains(&all_ips
, ip_address
)) {
3015 sset_add(&all_ips
, ip_address
);
3020 /* Ignore L4 port information in the key because fragmented packets
3021 * may not have L4 information. The pre-stateful table will send
3022 * the packet through ct() action to de-fragment. In stateful
3023 * table, we will eventually look at L4 information. */
3027 /* 'REGBIT_CONNTRACK_DEFRAG' is set to let the pre-stateful table send
3028 * packet to conntrack for defragmentation. */
3029 const char *ip_address
;
3030 SSET_FOR_EACH(ip_address
, &all_ips
) {
3033 if (addr_family
== AF_INET
) {
3034 match
= xasprintf("ip && ip4.dst == %s", ip_address
);
3036 match
= xasprintf("ip && ip6.dst == %s", ip_address
);
3038 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PRE_LB
,
3039 100, match
, REGBIT_CONNTRACK_DEFRAG
" = 1; next;");
3043 sset_destroy(&all_ips
);
3045 if (vip_configured
) {
3046 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PRE_LB
,
3047 100, "ip", REGBIT_CONNTRACK_DEFRAG
" = 1; next;");
3052 build_pre_stateful(struct ovn_datapath
*od
, struct hmap
*lflows
)
3054 /* Ingress and Egress pre-stateful Table (Priority 0): Packets are
3055 * allowed by default. */
3056 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PRE_STATEFUL
, 0, "1", "next;");
3057 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PRE_STATEFUL
, 0, "1", "next;");
3059 /* If REGBIT_CONNTRACK_DEFRAG is set as 1, then the packets should be
3060 * sent to conntrack for tracking and defragmentation. */
3061 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PRE_STATEFUL
, 100,
3062 REGBIT_CONNTRACK_DEFRAG
" == 1", "ct_next;");
3063 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PRE_STATEFUL
, 100,
3064 REGBIT_CONNTRACK_DEFRAG
" == 1", "ct_next;");
3068 build_acl_log(struct ds
*actions
, const struct nbrec_acl
*acl
)
3074 ds_put_cstr(actions
, "log(");
3077 ds_put_format(actions
, "name=\"%s\", ", acl
->name
);
3080 /* If a severity level isn't specified, default to "info". */
3081 if (acl
->severity
) {
3082 ds_put_format(actions
, "severity=%s, ", acl
->severity
);
3084 ds_put_format(actions
, "severity=info, ");
3087 if (!strcmp(acl
->action
, "drop")) {
3088 ds_put_cstr(actions
, "verdict=drop, ");
3089 } else if (!strcmp(acl
->action
, "reject")) {
3090 ds_put_cstr(actions
, "verdict=reject, ");
3091 } else if (!strcmp(acl
->action
, "allow")
3092 || !strcmp(acl
->action
, "allow-related")) {
3093 ds_put_cstr(actions
, "verdict=allow, ");
3096 ds_chomp(actions
, ' ');
3097 ds_chomp(actions
, ',');
3098 ds_put_cstr(actions
, "); ");
3102 build_reject_acl_rules(struct ovn_datapath
*od
, struct hmap
*lflows
,
3103 enum ovn_stage stage
, struct nbrec_acl
*acl
,
3104 struct ds
*extra_match
, struct ds
*extra_actions
)
3106 struct ds match
= DS_EMPTY_INITIALIZER
;
3107 struct ds actions
= DS_EMPTY_INITIALIZER
;
3108 bool ingress
= (stage
== S_SWITCH_IN_ACL
);
3111 build_acl_log(&actions
, acl
);
3112 if (extra_match
->length
> 0) {
3113 ds_put_format(&match
, "(%s) && ", extra_match
->string
);
3115 ds_put_format(&match
, "ip4 && tcp && (%s)", acl
->match
);
3116 ds_put_format(&actions
, "reg0 = 0; "
3117 "eth.dst <-> eth.src; ip4.dst <-> ip4.src; "
3118 "tcp_reset { outport <-> inport; %s };",
3119 ingress
? "output;" : "next(pipeline=ingress,table=0);");
3120 ovn_lflow_add(lflows
, od
, stage
, acl
->priority
+ OVN_ACL_PRI_OFFSET
+ 10,
3121 ds_cstr(&match
), ds_cstr(&actions
));
3124 build_acl_log(&actions
, acl
);
3125 if (extra_match
->length
> 0) {
3126 ds_put_format(&match
, "(%s) && ", extra_match
->string
);
3128 ds_put_format(&match
, "ip6 && tcp && (%s)", acl
->match
);
3129 ds_put_format(&actions
, "reg0 = 0; "
3130 "eth.dst <-> eth.src; ip6.dst <-> ip6.src; "
3131 "tcp_reset { outport <-> inport; %s };",
3132 ingress
? "output;" : "next(pipeline=ingress,table=0);");
3133 ovn_lflow_add(lflows
, od
, stage
, acl
->priority
+ OVN_ACL_PRI_OFFSET
+ 10,
3134 ds_cstr(&match
), ds_cstr(&actions
));
3139 build_acl_log(&actions
, acl
);
3140 if (extra_match
->length
> 0) {
3141 ds_put_format(&match
, "(%s) && ", extra_match
->string
);
3143 ds_put_format(&match
, "ip4 && (%s)", acl
->match
);
3144 if (extra_actions
->length
> 0) {
3145 ds_put_format(&actions
, "%s ", extra_actions
->string
);
3147 ds_put_format(&actions
, "reg0 = 0; "
3148 "eth.dst <-> eth.src; ip4.dst <-> ip4.src; "
3149 "icmp4 { outport <-> inport; %s };",
3150 ingress
? "output;" : "next(pipeline=ingress,table=0);");
3151 ovn_lflow_add(lflows
, od
, stage
, acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3152 ds_cstr(&match
), ds_cstr(&actions
));
3155 build_acl_log(&actions
, acl
);
3156 if (extra_match
->length
> 0) {
3157 ds_put_format(&match
, "(%s) && ", extra_match
->string
);
3159 ds_put_format(&match
, "ip6 && (%s)", acl
->match
);
3160 if (extra_actions
->length
> 0) {
3161 ds_put_format(&actions
, "%s ", extra_actions
->string
);
3163 ds_put_format(&actions
, "reg0 = 0; icmp6 { "
3164 "eth.dst <-> eth.src; ip6.dst <-> ip6.src; "
3165 "outport <-> inport; %s };",
3166 ingress
? "output;" : "next(pipeline=ingress,table=0);");
3167 ovn_lflow_add(lflows
, od
, stage
, acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3168 ds_cstr(&match
), ds_cstr(&actions
));
3171 ds_destroy(&actions
);
3175 build_acls(struct ovn_datapath
*od
, struct hmap
*lflows
)
3177 bool has_stateful
= has_stateful_acl(od
);
3179 /* Ingress and Egress ACL Table (Priority 0): Packets are allowed by
3180 * default. A related rule at priority 1 is added below if there
3181 * are any stateful ACLs in this datapath. */
3182 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, 0, "1", "next;");
3183 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, 0, "1", "next;");
3186 /* Ingress and Egress ACL Table (Priority 1).
3188 * By default, traffic is allowed. This is partially handled by
3189 * the Priority 0 ACL flows added earlier, but we also need to
3190 * commit IP flows. This is because, while the initiater's
3191 * direction may not have any stateful rules, the server's may
3192 * and then its return traffic would not have an associated
3193 * conntrack entry and would return "+invalid".
3195 * We use "ct_commit" for a connection that is not already known
3196 * by the connection tracker. Once a connection is committed,
3197 * subsequent packets will hit the flow at priority 0 that just
3200 * We also check for established connections that have ct_label.blocked
3201 * set on them. That's a connection that was disallowed, but is
3202 * now allowed by policy again since it hit this default-allow flow.
3203 * We need to set ct_label.blocked=0 to let the connection continue,
3204 * which will be done by ct_commit() in the "stateful" stage.
3205 * Subsequent packets will hit the flow at priority 0 that just
3207 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, 1,
3208 "ip && (!ct.est || (ct.est && ct_label.blocked == 1))",
3209 REGBIT_CONNTRACK_COMMIT
" = 1; next;");
3210 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, 1,
3211 "ip && (!ct.est || (ct.est && ct_label.blocked == 1))",
3212 REGBIT_CONNTRACK_COMMIT
" = 1; next;");
3214 /* Ingress and Egress ACL Table (Priority 65535).
3216 * Always drop traffic that's in an invalid state. Also drop
3217 * reply direction packets for connections that have been marked
3218 * for deletion (bit 0 of ct_label is set).
3220 * This is enforced at a higher priority than ACLs can be defined. */
3221 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, UINT16_MAX
,
3222 "ct.inv || (ct.est && ct.rpl && ct_label.blocked == 1)",
3224 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, UINT16_MAX
,
3225 "ct.inv || (ct.est && ct.rpl && ct_label.blocked == 1)",
3228 /* Ingress and Egress ACL Table (Priority 65535).
3230 * Allow reply traffic that is part of an established
3231 * conntrack entry that has not been marked for deletion
3232 * (bit 0 of ct_label). We only match traffic in the
3233 * reply direction because we want traffic in the request
3234 * direction to hit the currently defined policy from ACLs.
3236 * This is enforced at a higher priority than ACLs can be defined. */
3237 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, UINT16_MAX
,
3238 "ct.est && !ct.rel && !ct.new && !ct.inv "
3239 "&& ct.rpl && ct_label.blocked == 0",
3241 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, UINT16_MAX
,
3242 "ct.est && !ct.rel && !ct.new && !ct.inv "
3243 "&& ct.rpl && ct_label.blocked == 0",
3246 /* Ingress and Egress ACL Table (Priority 65535).
3248 * Allow traffic that is related to an existing conntrack entry that
3249 * has not been marked for deletion (bit 0 of ct_label).
3251 * This is enforced at a higher priority than ACLs can be defined.
3253 * NOTE: This does not support related data sessions (eg,
3254 * a dynamically negotiated FTP data channel), but will allow
3255 * related traffic such as an ICMP Port Unreachable through
3256 * that's generated from a non-listening UDP port. */
3257 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, UINT16_MAX
,
3258 "!ct.est && ct.rel && !ct.new && !ct.inv "
3259 "&& ct_label.blocked == 0",
3261 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, UINT16_MAX
,
3262 "!ct.est && ct.rel && !ct.new && !ct.inv "
3263 "&& ct_label.blocked == 0",
3266 /* Ingress and Egress ACL Table (Priority 65535).
3268 * Not to do conntrack on ND packets. */
3269 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ACL
, UINT16_MAX
, "nd", "next;");
3270 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_ACL
, UINT16_MAX
, "nd", "next;");
3273 /* Ingress or Egress ACL Table (Various priorities). */
3274 for (size_t i
= 0; i
< od
->nbs
->n_acls
; i
++) {
3275 struct nbrec_acl
*acl
= od
->nbs
->acls
[i
];
3276 bool ingress
= !strcmp(acl
->direction
, "from-lport") ? true :false;
3277 enum ovn_stage stage
= ingress
? S_SWITCH_IN_ACL
: S_SWITCH_OUT_ACL
;
3279 char *stage_hint
= xasprintf("%08x", acl
->header_
.uuid
.parts
[0]);
3280 if (!strcmp(acl
->action
, "allow")
3281 || !strcmp(acl
->action
, "allow-related")) {
3282 /* If there are any stateful flows, we must even commit "allow"
3283 * actions. This is because, while the initiater's
3284 * direction may not have any stateful rules, the server's
3285 * may and then its return traffic would not have an
3286 * associated conntrack entry and would return "+invalid". */
3287 if (!has_stateful
) {
3288 struct ds actions
= DS_EMPTY_INITIALIZER
;
3289 build_acl_log(&actions
, acl
);
3290 ds_put_cstr(&actions
, "next;");
3291 ovn_lflow_add_with_hint(lflows
, od
, stage
,
3292 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3293 acl
->match
, ds_cstr(&actions
),
3295 ds_destroy(&actions
);
3297 struct ds match
= DS_EMPTY_INITIALIZER
;
3298 struct ds actions
= DS_EMPTY_INITIALIZER
;
3300 /* Commit the connection tracking entry if it's a new
3301 * connection that matches this ACL. After this commit,
3302 * the reply traffic is allowed by a flow we create at
3303 * priority 65535, defined earlier.
3305 * It's also possible that a known connection was marked for
3306 * deletion after a policy was deleted, but the policy was
3307 * re-added while that connection is still known. We catch
3308 * that case here and un-set ct_label.blocked (which will be done
3309 * by ct_commit in the "stateful" stage) to indicate that the
3310 * connection should be allowed to resume.
3312 ds_put_format(&match
, "((ct.new && !ct.est)"
3313 " || (!ct.new && ct.est && !ct.rpl "
3314 "&& ct_label.blocked == 1)) "
3315 "&& (%s)", acl
->match
);
3316 ds_put_cstr(&actions
, REGBIT_CONNTRACK_COMMIT
" = 1; ");
3317 build_acl_log(&actions
, acl
);
3318 ds_put_cstr(&actions
, "next;");
3319 ovn_lflow_add_with_hint(lflows
, od
, stage
,
3320 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3325 /* Match on traffic in the request direction for an established
3326 * connection tracking entry that has not been marked for
3327 * deletion. There is no need to commit here, so we can just
3328 * proceed to the next table. We use this to ensure that this
3329 * connection is still allowed by the currently defined
3333 ds_put_format(&match
,
3334 "!ct.new && ct.est && !ct.rpl"
3335 " && ct_label.blocked == 0 && (%s)",
3338 build_acl_log(&actions
, acl
);
3339 ds_put_cstr(&actions
, "next;");
3340 ovn_lflow_add_with_hint(lflows
, od
, stage
,
3341 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3342 ds_cstr(&match
), ds_cstr(&actions
),
3346 ds_destroy(&actions
);
3348 } else if (!strcmp(acl
->action
, "drop")
3349 || !strcmp(acl
->action
, "reject")) {
3350 struct ds match
= DS_EMPTY_INITIALIZER
;
3351 struct ds actions
= DS_EMPTY_INITIALIZER
;
3353 /* The implementation of "drop" differs if stateful ACLs are in
3354 * use for this datapath. In that case, the actions differ
3355 * depending on whether the connection was previously committed
3356 * to the connection tracker with ct_commit. */
3358 /* If the packet is not part of an established connection, then
3359 * we can simply reject/drop it. */
3361 "(!ct.est || (ct.est && ct_label.blocked == 1))");
3362 if (!strcmp(acl
->action
, "reject")) {
3363 build_reject_acl_rules(od
, lflows
, stage
, acl
, &match
,
3366 ds_put_format(&match
, " && (%s)", acl
->match
);
3367 build_acl_log(&actions
, acl
);
3368 ds_put_cstr(&actions
, "/* drop */");
3369 ovn_lflow_add(lflows
, od
, stage
,
3370 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3371 ds_cstr(&match
), ds_cstr(&actions
));
3373 /* For an existing connection without ct_label set, we've
3374 * encountered a policy change. ACLs previously allowed
3375 * this connection and we committed the connection tracking
3376 * entry. Current policy says that we should drop this
3377 * connection. First, we set bit 0 of ct_label to indicate
3378 * that this connection is set for deletion. By not
3379 * specifying "next;", we implicitly drop the packet after
3380 * updating conntrack state. We would normally defer
3381 * ct_commit() to the "stateful" stage, but since we're
3382 * rejecting/dropping the packet, we go ahead and do it here.
3386 ds_put_cstr(&match
, "ct.est && ct_label.blocked == 0");
3387 ds_put_cstr(&actions
, "ct_commit(ct_label=1/1); ");
3388 if (!strcmp(acl
->action
, "reject")) {
3389 build_reject_acl_rules(od
, lflows
, stage
, acl
, &match
,
3392 ds_put_format(&match
, " && (%s)", acl
->match
);
3393 build_acl_log(&actions
, acl
);
3394 ds_put_cstr(&actions
, "/* drop */");
3395 ovn_lflow_add(lflows
, od
, stage
,
3396 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3397 ds_cstr(&match
), ds_cstr(&actions
));
3400 /* There are no stateful ACLs in use on this datapath,
3401 * so a "reject/drop" ACL is simply the "reject/drop"
3402 * logical flow action in all cases. */
3403 if (!strcmp(acl
->action
, "reject")) {
3404 build_reject_acl_rules(od
, lflows
, stage
, acl
, &match
,
3407 build_acl_log(&actions
, acl
);
3408 ds_put_cstr(&actions
, "/* drop */");
3409 ovn_lflow_add(lflows
, od
, stage
,
3410 acl
->priority
+ OVN_ACL_PRI_OFFSET
,
3411 acl
->match
, ds_cstr(&actions
));
3415 ds_destroy(&actions
);
3420 /* Add 34000 priority flow to allow DHCP reply from ovn-controller to all
3421 * logical ports of the datapath if the CMS has configured DHCPv4 options.
3423 for (size_t i
= 0; i
< od
->nbs
->n_ports
; i
++) {
3424 if (od
->nbs
->ports
[i
]->dhcpv4_options
) {
3425 const char *server_id
= smap_get(
3426 &od
->nbs
->ports
[i
]->dhcpv4_options
->options
, "server_id");
3427 const char *server_mac
= smap_get(
3428 &od
->nbs
->ports
[i
]->dhcpv4_options
->options
, "server_mac");
3429 const char *lease_time
= smap_get(
3430 &od
->nbs
->ports
[i
]->dhcpv4_options
->options
, "lease_time");
3431 if (server_id
&& server_mac
&& lease_time
) {
3432 struct ds match
= DS_EMPTY_INITIALIZER
;
3433 const char *actions
=
3434 has_stateful
? "ct_commit; next;" : "next;";
3435 ds_put_format(&match
, "outport == \"%s\" && eth.src == %s "
3436 "&& ip4.src == %s && udp && udp.src == 67 "
3437 "&& udp.dst == 68", od
->nbs
->ports
[i
]->name
,
3438 server_mac
, server_id
);
3440 lflows
, od
, S_SWITCH_OUT_ACL
, 34000, ds_cstr(&match
),
3446 if (od
->nbs
->ports
[i
]->dhcpv6_options
) {
3447 const char *server_mac
= smap_get(
3448 &od
->nbs
->ports
[i
]->dhcpv6_options
->options
, "server_id");
3450 if (server_mac
&& eth_addr_from_string(server_mac
, &ea
)) {
3451 /* Get the link local IP of the DHCPv6 server from the
3453 struct in6_addr lla
;
3454 in6_generate_lla(ea
, &lla
);
3456 char server_ip
[INET6_ADDRSTRLEN
+ 1];
3457 ipv6_string_mapped(server_ip
, &lla
);
3459 struct ds match
= DS_EMPTY_INITIALIZER
;
3460 const char *actions
= has_stateful
? "ct_commit; next;" :
3462 ds_put_format(&match
, "outport == \"%s\" && eth.src == %s "
3463 "&& ip6.src == %s && udp && udp.src == 547 "
3464 "&& udp.dst == 546", od
->nbs
->ports
[i
]->name
,
3465 server_mac
, server_ip
);
3467 lflows
, od
, S_SWITCH_OUT_ACL
, 34000, ds_cstr(&match
),
3474 /* Add a 34000 priority flow to advance the DNS reply from ovn-controller,
3475 * if the CMS has configured DNS records for the datapath.
3477 if (ls_has_dns_records(od
->nbs
)) {
3478 const char *actions
= has_stateful
? "ct_commit; next;" : "next;";
3480 lflows
, od
, S_SWITCH_OUT_ACL
, 34000, "udp.src == 53",
3486 build_qos(struct ovn_datapath
*od
, struct hmap
*lflows
) {
3487 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_QOS_MARK
, 0, "1", "next;");
3488 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_QOS_MARK
, 0, "1", "next;");
3489 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_QOS_METER
, 0, "1", "next;");
3490 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_QOS_METER
, 0, "1", "next;");
3492 for (size_t i
= 0; i
< od
->nbs
->n_qos_rules
; i
++) {
3493 struct nbrec_qos
*qos
= od
->nbs
->qos_rules
[i
];
3494 bool ingress
= !strcmp(qos
->direction
, "from-lport") ? true :false;
3495 enum ovn_stage stage
= ingress
? S_SWITCH_IN_QOS_MARK
: S_SWITCH_OUT_QOS_MARK
;
3499 for (size_t j
= 0; j
< qos
->n_action
; j
++) {
3500 if (!strcmp(qos
->key_action
[j
], "dscp")) {
3501 struct ds dscp_action
= DS_EMPTY_INITIALIZER
;
3503 ds_put_format(&dscp_action
, "ip.dscp = %"PRId64
"; next;",
3504 qos
->value_action
[j
]);
3505 ovn_lflow_add(lflows
, od
, stage
,
3507 qos
->match
, ds_cstr(&dscp_action
));
3508 ds_destroy(&dscp_action
);
3512 for (size_t n
= 0; n
< qos
->n_bandwidth
; n
++) {
3513 if (!strcmp(qos
->key_bandwidth
[n
], "rate")) {
3514 rate
= qos
->value_bandwidth
[n
];
3515 } else if (!strcmp(qos
->key_bandwidth
[n
], "burst")) {
3516 burst
= qos
->value_bandwidth
[n
];
3520 struct ds meter_action
= DS_EMPTY_INITIALIZER
;
3521 stage
= ingress
? S_SWITCH_IN_QOS_METER
: S_SWITCH_OUT_QOS_METER
;
3523 ds_put_format(&meter_action
,
3524 "set_meter(%"PRId64
", %"PRId64
"); next;",
3527 ds_put_format(&meter_action
,
3528 "set_meter(%"PRId64
"); next;",
3532 /* Ingress and Egress QoS Meter Table.
3534 * We limit the bandwidth of this flow by adding a meter table.
3536 ovn_lflow_add(lflows
, od
, stage
,
3538 qos
->match
, ds_cstr(&meter_action
));
3539 ds_destroy(&meter_action
);
3545 build_lb(struct ovn_datapath
*od
, struct hmap
*lflows
)
3547 /* Ingress and Egress LB Table (Priority 0): Packets are allowed by
3549 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_LB
, 0, "1", "next;");
3550 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_LB
, 0, "1", "next;");
3552 if (od
->nbs
->load_balancer
) {
3553 /* Ingress and Egress LB Table (Priority 65535).
3555 * Send established traffic through conntrack for just NAT. */
3556 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_LB
, UINT16_MAX
,
3557 "ct.est && !ct.rel && !ct.new && !ct.inv",
3558 REGBIT_CONNTRACK_NAT
" = 1; next;");
3559 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_LB
, UINT16_MAX
,
3560 "ct.est && !ct.rel && !ct.new && !ct.inv",
3561 REGBIT_CONNTRACK_NAT
" = 1; next;");
3566 build_stateful(struct ovn_datapath
*od
, struct hmap
*lflows
)
3568 /* Ingress and Egress stateful Table (Priority 0): Packets are
3569 * allowed by default. */
3570 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_STATEFUL
, 0, "1", "next;");
3571 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_STATEFUL
, 0, "1", "next;");
3573 /* If REGBIT_CONNTRACK_COMMIT is set as 1, then the packets should be
3574 * committed to conntrack. We always set ct_label.blocked to 0 here as
3575 * any packet that makes it this far is part of a connection we
3576 * want to allow to continue. */
3577 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_STATEFUL
, 100,
3578 REGBIT_CONNTRACK_COMMIT
" == 1", "ct_commit(ct_label=0/1); next;");
3579 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_STATEFUL
, 100,
3580 REGBIT_CONNTRACK_COMMIT
" == 1", "ct_commit(ct_label=0/1); next;");
3582 /* If REGBIT_CONNTRACK_NAT is set as 1, then packets should just be sent
3583 * through nat (without committing).
3585 * REGBIT_CONNTRACK_COMMIT is set for new connections and
3586 * REGBIT_CONNTRACK_NAT is set for established connections. So they
3589 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_STATEFUL
, 100,
3590 REGBIT_CONNTRACK_NAT
" == 1", "ct_lb;");
3591 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_STATEFUL
, 100,
3592 REGBIT_CONNTRACK_NAT
" == 1", "ct_lb;");
3594 /* Load balancing rules for new connections get committed to conntrack
3595 * table. So even if REGBIT_CONNTRACK_COMMIT is set in a previous table
3596 * a higher priority rule for load balancing below also commits the
3597 * connection, so it is okay if we do not hit the above match on
3598 * REGBIT_CONNTRACK_COMMIT. */
3599 for (int i
= 0; i
< od
->nbs
->n_load_balancer
; i
++) {
3600 struct nbrec_load_balancer
*lb
= od
->nbs
->load_balancer
[i
];
3601 struct smap
*vips
= &lb
->vips
;
3602 struct smap_node
*node
;
3604 SMAP_FOR_EACH (node
, vips
) {
3608 /* node->key contains IP:port or just IP. */
3609 char *ip_address
= NULL
;
3610 ip_address_and_port_from_lb_key(node
->key
, &ip_address
, &port
,
3616 /* New connections in Ingress table. */
3617 char *action
= xasprintf("ct_lb(%s);", node
->value
);
3618 struct ds match
= DS_EMPTY_INITIALIZER
;
3619 if (addr_family
== AF_INET
) {
3620 ds_put_format(&match
, "ct.new && ip4.dst == %s", ip_address
);
3622 ds_put_format(&match
, "ct.new && ip6.dst == %s", ip_address
);
3625 if (lb
->protocol
&& !strcmp(lb
->protocol
, "udp")) {
3626 ds_put_format(&match
, " && udp.dst == %d", port
);
3628 ds_put_format(&match
, " && tcp.dst == %d", port
);
3630 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_STATEFUL
,
3631 120, ds_cstr(&match
), action
);
3633 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_STATEFUL
,
3634 110, ds_cstr(&match
), action
);
3645 build_lswitch_flows(struct hmap
*datapaths
, struct hmap
*ports
,
3646 struct hmap
*lflows
, struct hmap
*mcgroups
)
3648 /* This flow table structure is documented in ovn-northd(8), so please
3649 * update ovn-northd.8.xml if you change anything. */
3651 struct ds match
= DS_EMPTY_INITIALIZER
;
3652 struct ds actions
= DS_EMPTY_INITIALIZER
;
3654 /* Build pre-ACL and ACL tables for both ingress and egress.
3655 * Ingress tables 3 through 10. Egress tables 0 through 7. */
3656 struct ovn_datapath
*od
;
3657 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
3662 build_pre_acls(od
, lflows
);
3663 build_pre_lb(od
, lflows
);
3664 build_pre_stateful(od
, lflows
);
3665 build_acls(od
, lflows
);
3666 build_qos(od
, lflows
);
3667 build_lb(od
, lflows
);
3668 build_stateful(od
, lflows
);
3671 /* Logical switch ingress table 0: Admission control framework (priority
3673 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
3678 /* Logical VLANs not supported. */
3679 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PORT_SEC_L2
, 100, "vlan.present",
3682 /* Broadcast/multicast source address is invalid. */
3683 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PORT_SEC_L2
, 100, "eth.src[40]",
3686 /* Port security flows have priority 50 (see below) and will continue
3687 * to the next table if packet source is acceptable. */
3690 /* Logical switch ingress table 0: Ingress port security - L2
3692 * Ingress table 1: Ingress port security - IP (priority 90 and 80)
3693 * Ingress table 2: Ingress port security - ND (priority 90 and 80)
3695 struct ovn_port
*op
;
3696 HMAP_FOR_EACH (op
, key_node
, ports
) {
3701 if (!lsp_is_enabled(op
->nbsp
)) {
3702 /* Drop packets from disabled logical ports (since logical flow
3703 * tables are default-drop). */
3709 ds_put_format(&match
, "inport == %s", op
->json_key
);
3710 build_port_security_l2("eth.src", op
->ps_addrs
, op
->n_ps_addrs
,
3713 const char *queue_id
= smap_get(&op
->sb
->options
, "qdisc_queue_id");
3715 ds_put_format(&actions
, "set_queue(%s); ", queue_id
);
3717 ds_put_cstr(&actions
, "next;");
3718 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_PORT_SEC_L2
, 50,
3719 ds_cstr(&match
), ds_cstr(&actions
));
3721 if (op
->nbsp
->n_port_security
) {
3722 build_port_security_ip(P_IN
, op
, lflows
);
3723 build_port_security_nd(op
, lflows
);
3727 /* Ingress table 1 and 2: Port security - IP and ND, by default goto next.
3729 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
3734 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PORT_SEC_ND
, 0, "1", "next;");
3735 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_PORT_SEC_IP
, 0, "1", "next;");
3738 /* Ingress table 11: ARP/ND responder, skip requests coming from localnet
3739 * and vtep ports. (priority 100); see ovn-northd.8.xml for the
3741 HMAP_FOR_EACH (op
, key_node
, ports
) {
3746 if ((!strcmp(op
->nbsp
->type
, "localnet")) ||
3747 (!strcmp(op
->nbsp
->type
, "vtep"))) {
3749 ds_put_format(&match
, "inport == %s", op
->json_key
);
3750 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_ARP_ND_RSP
, 100,
3751 ds_cstr(&match
), "next;");
3755 /* Ingress table 11: ARP/ND responder, reply for known IPs.
3757 HMAP_FOR_EACH (op
, key_node
, ports
) {
3763 * Add ARP/ND reply flows if either the
3765 * - port type is router or
3766 * - port type is localport
3768 if (!lsp_is_up(op
->nbsp
) && strcmp(op
->nbsp
->type
, "router") &&
3769 strcmp(op
->nbsp
->type
, "localport")) {
3773 for (size_t i
= 0; i
< op
->n_lsp_addrs
; i
++) {
3774 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv4_addrs
; j
++) {
3776 ds_put_format(&match
, "arp.tpa == %s && arp.op == 1",
3777 op
->lsp_addrs
[i
].ipv4_addrs
[j
].addr_s
);
3779 ds_put_format(&actions
,
3780 "eth.dst = eth.src; "
3782 "arp.op = 2; /* ARP reply */ "
3783 "arp.tha = arp.sha; "
3785 "arp.tpa = arp.spa; "
3787 "outport = inport; "
3788 "flags.loopback = 1; "
3790 op
->lsp_addrs
[i
].ea_s
, op
->lsp_addrs
[i
].ea_s
,
3791 op
->lsp_addrs
[i
].ipv4_addrs
[j
].addr_s
);
3792 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_ARP_ND_RSP
, 50,
3793 ds_cstr(&match
), ds_cstr(&actions
));
3795 /* Do not reply to an ARP request from the port that owns the
3796 * address (otherwise a DHCP client that ARPs to check for a
3797 * duplicate address will fail). Instead, forward it the usual
3800 * (Another alternative would be to simply drop the packet. If
3801 * everything is working as it is configured, then this would
3802 * produce equivalent results, since no one should reply to the
3803 * request. But ARPing for one's own IP address is intended to
3804 * detect situations where the network is not working as
3805 * configured, so dropping the request would frustrate that
3807 ds_put_format(&match
, " && inport == %s", op
->json_key
);
3808 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_ARP_ND_RSP
, 100,
3809 ds_cstr(&match
), "next;");
3812 /* For ND solicitations, we need to listen for both the
3813 * unicast IPv6 address and its all-nodes multicast address,
3814 * but always respond with the unicast IPv6 address. */
3815 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv6_addrs
; j
++) {
3817 ds_put_format(&match
,
3818 "nd_ns && ip6.dst == {%s, %s} && nd.target == %s",
3819 op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr_s
,
3820 op
->lsp_addrs
[i
].ipv6_addrs
[j
].sn_addr_s
,
3821 op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr_s
);
3824 ds_put_format(&actions
,
3830 "outport = inport; "
3831 "flags.loopback = 1; "
3834 op
->lsp_addrs
[i
].ea_s
,
3835 op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr_s
,
3836 op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr_s
,
3837 op
->lsp_addrs
[i
].ea_s
);
3838 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_ARP_ND_RSP
, 50,
3839 ds_cstr(&match
), ds_cstr(&actions
));
3841 /* Do not reply to a solicitation from the port that owns the
3842 * address (otherwise DAD detection will fail). */
3843 ds_put_format(&match
, " && inport == %s", op
->json_key
);
3844 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_ARP_ND_RSP
, 100,
3845 ds_cstr(&match
), "next;");
3850 /* Ingress table 11: ARP/ND responder, by default goto next.
3852 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
3857 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_ARP_ND_RSP
, 0, "1", "next;");
3860 /* Logical switch ingress table 12 and 13: DHCP options and response
3861 * priority 100 flows. */
3862 HMAP_FOR_EACH (op
, key_node
, ports
) {
3867 if (!lsp_is_enabled(op
->nbsp
) || !strcmp(op
->nbsp
->type
, "router")) {
3868 /* Don't add the DHCP flows if the port is not enabled or if the
3869 * port is a router port. */
3873 if (!op
->nbsp
->dhcpv4_options
&& !op
->nbsp
->dhcpv6_options
) {
3874 /* CMS has disabled both native DHCPv4 and DHCPv6 for this lport.
3879 for (size_t i
= 0; i
< op
->n_lsp_addrs
; i
++) {
3880 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv4_addrs
; j
++) {
3881 struct ds options_action
= DS_EMPTY_INITIALIZER
;
3882 struct ds response_action
= DS_EMPTY_INITIALIZER
;
3883 struct ds ipv4_addr_match
= DS_EMPTY_INITIALIZER
;
3884 if (build_dhcpv4_action(
3885 op
, op
->lsp_addrs
[i
].ipv4_addrs
[j
].addr
,
3886 &options_action
, &response_action
, &ipv4_addr_match
)) {
3889 &match
, "inport == %s && eth.src == %s && "
3890 "ip4.src == 0.0.0.0 && ip4.dst == 255.255.255.255 && "
3891 "udp.src == 68 && udp.dst == 67", op
->json_key
,
3892 op
->lsp_addrs
[i
].ea_s
);
3894 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_DHCP_OPTIONS
,
3895 100, ds_cstr(&match
),
3896 ds_cstr(&options_action
));
3898 /* Allow ip4.src = OFFER_IP and
3899 * ip4.dst = {SERVER_IP, 255.255.255.255} for the below
3901 * - When the client wants to renew the IP by sending
3902 * the DHCPREQUEST to the server ip.
3903 * - When the client wants to renew the IP by
3904 * broadcasting the DHCPREQUEST.
3907 &match
, "inport == %s && eth.src == %s && "
3908 "%s && udp.src == 68 && udp.dst == 67", op
->json_key
,
3909 op
->lsp_addrs
[i
].ea_s
, ds_cstr(&ipv4_addr_match
));
3911 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_DHCP_OPTIONS
,
3912 100, ds_cstr(&match
),
3913 ds_cstr(&options_action
));
3916 /* If REGBIT_DHCP_OPTS_RESULT is set, it means the
3917 * put_dhcp_opts action is successful. */
3919 &match
, "inport == %s && eth.src == %s && "
3920 "ip4 && udp.src == 68 && udp.dst == 67"
3921 " && "REGBIT_DHCP_OPTS_RESULT
, op
->json_key
,
3922 op
->lsp_addrs
[i
].ea_s
);
3923 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_DHCP_RESPONSE
,
3924 100, ds_cstr(&match
),
3925 ds_cstr(&response_action
));
3926 ds_destroy(&options_action
);
3927 ds_destroy(&response_action
);
3928 ds_destroy(&ipv4_addr_match
);
3933 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv6_addrs
; j
++) {
3934 struct ds options_action
= DS_EMPTY_INITIALIZER
;
3935 struct ds response_action
= DS_EMPTY_INITIALIZER
;
3936 if (build_dhcpv6_action(
3937 op
, &op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr
,
3938 &options_action
, &response_action
)) {
3941 &match
, "inport == %s && eth.src == %s"
3942 " && ip6.dst == ff02::1:2 && udp.src == 546 &&"
3943 " udp.dst == 547", op
->json_key
,
3944 op
->lsp_addrs
[i
].ea_s
);
3946 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_DHCP_OPTIONS
, 100,
3947 ds_cstr(&match
), ds_cstr(&options_action
));
3949 /* If REGBIT_DHCP_OPTS_RESULT is set to 1, it means the
3950 * put_dhcpv6_opts action is successful */
3951 ds_put_cstr(&match
, " && "REGBIT_DHCP_OPTS_RESULT
);
3952 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_DHCP_RESPONSE
, 100,
3953 ds_cstr(&match
), ds_cstr(&response_action
));
3954 ds_destroy(&options_action
);
3955 ds_destroy(&response_action
);
3962 /* Logical switch ingress table 14 and 15: DNS lookup and response
3963 * priority 100 flows.
3965 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
3966 if (!od
->nbs
|| !ls_has_dns_records(od
->nbs
)) {
3970 struct ds action
= DS_EMPTY_INITIALIZER
;
3973 ds_put_cstr(&match
, "udp.dst == 53");
3974 ds_put_format(&action
,
3975 REGBIT_DNS_LOOKUP_RESULT
" = dns_lookup(); next;");
3976 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DNS_LOOKUP
, 100,
3977 ds_cstr(&match
), ds_cstr(&action
));
3979 ds_put_cstr(&match
, " && "REGBIT_DNS_LOOKUP_RESULT
);
3980 ds_put_format(&action
, "eth.dst <-> eth.src; ip4.src <-> ip4.dst; "
3981 "udp.dst = udp.src; udp.src = 53; outport = inport; "
3982 "flags.loopback = 1; output;");
3983 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DNS_RESPONSE
, 100,
3984 ds_cstr(&match
), ds_cstr(&action
));
3986 ds_put_format(&action
, "eth.dst <-> eth.src; ip6.src <-> ip6.dst; "
3987 "udp.dst = udp.src; udp.src = 53; outport = inport; "
3988 "flags.loopback = 1; output;");
3989 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DNS_RESPONSE
, 100,
3990 ds_cstr(&match
), ds_cstr(&action
));
3991 ds_destroy(&action
);
3994 /* Ingress table 12 and 13: DHCP options and response, by default goto
3995 * next. (priority 0).
3996 * Ingress table 14 and 15: DNS lookup and response, by default goto next.
3999 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4004 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DHCP_OPTIONS
, 0, "1", "next;");
4005 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DHCP_RESPONSE
, 0, "1", "next;");
4006 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DNS_LOOKUP
, 0, "1", "next;");
4007 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_DNS_RESPONSE
, 0, "1", "next;");
4010 /* Ingress table 16: Destination lookup, broadcast and multicast handling
4011 * (priority 100). */
4012 HMAP_FOR_EACH (op
, key_node
, ports
) {
4017 if (lsp_is_enabled(op
->nbsp
)) {
4018 ovn_multicast_add(mcgroups
, &mc_flood
, op
);
4021 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4026 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_L2_LKUP
, 100, "eth.mcast",
4027 "outport = \""MC_FLOOD
"\"; output;");
4030 /* Ingress table 16: Destination lookup, unicast handling (priority 50), */
4031 HMAP_FOR_EACH (op
, key_node
, ports
) {
4036 for (size_t i
= 0; i
< op
->nbsp
->n_addresses
; i
++) {
4037 /* Addresses are owned by the logical port.
4038 * Ethernet address followed by zero or more IPv4
4039 * or IPv6 addresses (or both). */
4040 struct eth_addr mac
;
4041 if (ovs_scan(op
->nbsp
->addresses
[i
],
4042 ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))) {
4044 ds_put_format(&match
, "eth.dst == "ETH_ADDR_FMT
,
4045 ETH_ADDR_ARGS(mac
));
4048 ds_put_format(&actions
, "outport = %s; output;", op
->json_key
);
4049 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_L2_LKUP
, 50,
4050 ds_cstr(&match
), ds_cstr(&actions
));
4051 } else if (!strcmp(op
->nbsp
->addresses
[i
], "unknown")) {
4052 if (lsp_is_enabled(op
->nbsp
)) {
4053 ovn_multicast_add(mcgroups
, &mc_unknown
, op
);
4054 op
->od
->has_unknown
= true;
4056 } else if (is_dynamic_lsp_address(op
->nbsp
->addresses
[i
])) {
4057 if (!op
->nbsp
->dynamic_addresses
4058 || !ovs_scan(op
->nbsp
->dynamic_addresses
,
4059 ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))) {
4063 ds_put_format(&match
, "eth.dst == "ETH_ADDR_FMT
,
4064 ETH_ADDR_ARGS(mac
));
4067 ds_put_format(&actions
, "outport = %s; output;", op
->json_key
);
4068 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_L2_LKUP
, 50,
4069 ds_cstr(&match
), ds_cstr(&actions
));
4070 } else if (!strcmp(op
->nbsp
->addresses
[i
], "router")) {
4071 if (!op
->peer
|| !op
->peer
->nbrp
4072 || !ovs_scan(op
->peer
->nbrp
->mac
,
4073 ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))) {
4077 ds_put_format(&match
, "eth.dst == "ETH_ADDR_FMT
,
4078 ETH_ADDR_ARGS(mac
));
4079 if (op
->peer
->od
->l3dgw_port
4080 && op
->peer
== op
->peer
->od
->l3dgw_port
4081 && op
->peer
->od
->l3redirect_port
) {
4082 /* The destination lookup flow for the router's
4083 * distributed gateway port MAC address should only be
4084 * programmed on the "redirect-chassis". */
4085 ds_put_format(&match
, " && is_chassis_resident(%s)",
4086 op
->peer
->od
->l3redirect_port
->json_key
);
4090 ds_put_format(&actions
, "outport = %s; output;", op
->json_key
);
4091 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_L2_LKUP
, 50,
4092 ds_cstr(&match
), ds_cstr(&actions
));
4094 /* Add ethernet addresses specified in NAT rules on
4095 * distributed logical routers. */
4096 if (op
->peer
->od
->l3dgw_port
4097 && op
->peer
== op
->peer
->od
->l3dgw_port
) {
4098 for (int j
= 0; j
< op
->peer
->od
->nbr
->n_nat
; j
++) {
4099 const struct nbrec_nat
*nat
4100 = op
->peer
->od
->nbr
->nat
[j
];
4101 if (!strcmp(nat
->type
, "dnat_and_snat")
4102 && nat
->logical_port
&& nat
->external_mac
4103 && eth_addr_from_string(nat
->external_mac
, &mac
)) {
4106 ds_put_format(&match
, "eth.dst == "ETH_ADDR_FMT
4107 " && is_chassis_resident(\"%s\")",
4112 ds_put_format(&actions
, "outport = %s; output;",
4114 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_IN_L2_LKUP
,
4115 50, ds_cstr(&match
),
4121 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4124 "%s: invalid syntax '%s' in addresses column",
4125 op
->nbsp
->name
, op
->nbsp
->addresses
[i
]);
4130 /* Ingress table 16: Destination lookup for unknown MACs (priority 0). */
4131 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4136 if (od
->has_unknown
) {
4137 ovn_lflow_add(lflows
, od
, S_SWITCH_IN_L2_LKUP
, 0, "1",
4138 "outport = \""MC_UNKNOWN
"\"; output;");
4142 /* Egress tables 8: Egress port security - IP (priority 0)
4143 * Egress table 9: Egress port security L2 - multicast/broadcast
4144 * (priority 100). */
4145 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4150 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PORT_SEC_IP
, 0, "1", "next;");
4151 ovn_lflow_add(lflows
, od
, S_SWITCH_OUT_PORT_SEC_L2
, 100, "eth.mcast",
4155 /* Egress table 8: Egress port security - IP (priorities 90 and 80)
4156 * if port security enabled.
4158 * Egress table 9: Egress port security - L2 (priorities 50 and 150).
4160 * Priority 50 rules implement port security for enabled logical port.
4162 * Priority 150 rules drop packets to disabled logical ports, so that they
4163 * don't even receive multicast or broadcast packets. */
4164 HMAP_FOR_EACH (op
, key_node
, ports
) {
4170 ds_put_format(&match
, "outport == %s", op
->json_key
);
4171 if (lsp_is_enabled(op
->nbsp
)) {
4172 build_port_security_l2("eth.dst", op
->ps_addrs
, op
->n_ps_addrs
,
4174 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_OUT_PORT_SEC_L2
, 50,
4175 ds_cstr(&match
), "output;");
4177 ovn_lflow_add(lflows
, op
->od
, S_SWITCH_OUT_PORT_SEC_L2
, 150,
4178 ds_cstr(&match
), "drop;");
4181 if (op
->nbsp
->n_port_security
) {
4182 build_port_security_ip(P_OUT
, op
, lflows
);
4187 ds_destroy(&actions
);
4191 lrport_is_enabled(const struct nbrec_logical_router_port
*lrport
)
4193 return !lrport
->enabled
|| *lrport
->enabled
;
4196 /* Returns a string of the IP address of the router port 'op' that
4197 * overlaps with 'ip_s". If one is not found, returns NULL.
4199 * The caller must not free the returned string. */
4201 find_lrp_member_ip(const struct ovn_port
*op
, const char *ip_s
)
4203 bool is_ipv4
= strchr(ip_s
, '.') ? true : false;
4208 if (!ip_parse(ip_s
, &ip
)) {
4209 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4210 VLOG_WARN_RL(&rl
, "bad ip address %s", ip_s
);
4214 for (int i
= 0; i
< op
->lrp_networks
.n_ipv4_addrs
; i
++) {
4215 const struct ipv4_netaddr
*na
= &op
->lrp_networks
.ipv4_addrs
[i
];
4217 if (!((na
->network
^ ip
) & na
->mask
)) {
4218 /* There should be only 1 interface that matches the
4219 * supplied IP. Otherwise, it's a configuration error,
4220 * because subnets of a router's interfaces should NOT
4226 struct in6_addr ip6
;
4228 if (!ipv6_parse(ip_s
, &ip6
)) {
4229 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4230 VLOG_WARN_RL(&rl
, "bad ipv6 address %s", ip_s
);
4234 for (int i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; i
++) {
4235 const struct ipv6_netaddr
*na
= &op
->lrp_networks
.ipv6_addrs
[i
];
4236 struct in6_addr xor_addr
= ipv6_addr_bitxor(&na
->network
, &ip6
);
4237 struct in6_addr and_addr
= ipv6_addr_bitand(&xor_addr
, &na
->mask
);
4239 if (ipv6_is_zero(&and_addr
)) {
4240 /* There should be only 1 interface that matches the
4241 * supplied IP. Otherwise, it's a configuration error,
4242 * because subnets of a router's interfaces should NOT
4253 add_route(struct hmap
*lflows
, const struct ovn_port
*op
,
4254 const char *lrp_addr_s
, const char *network_s
, int plen
,
4255 const char *gateway
, const char *policy
)
4257 bool is_ipv4
= strchr(network_s
, '.') ? true : false;
4258 struct ds match
= DS_EMPTY_INITIALIZER
;
4262 if (policy
&& !strcmp(policy
, "src-ip")) {
4264 priority
= plen
* 2;
4267 priority
= (plen
* 2) + 1;
4270 /* IPv6 link-local addresses must be scoped to the local router port. */
4272 struct in6_addr network
;
4273 ovs_assert(ipv6_parse(network_s
, &network
));
4274 if (in6_is_lla(&network
)) {
4275 ds_put_format(&match
, "inport == %s && ", op
->json_key
);
4278 ds_put_format(&match
, "ip%s.%s == %s/%d", is_ipv4
? "4" : "6", dir
,
4281 struct ds actions
= DS_EMPTY_INITIALIZER
;
4282 ds_put_format(&actions
, "ip.ttl--; %sreg0 = ", is_ipv4
? "" : "xx");
4285 ds_put_cstr(&actions
, gateway
);
4287 ds_put_format(&actions
, "ip%s.dst", is_ipv4
? "4" : "6");
4289 ds_put_format(&actions
, "; "
4293 "flags.loopback = 1; "
4295 is_ipv4
? "" : "xx",
4297 op
->lrp_networks
.ea_s
,
4300 /* The priority here is calculated to implement longest-prefix-match
4302 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_ROUTING
, priority
,
4303 ds_cstr(&match
), ds_cstr(&actions
));
4305 ds_destroy(&actions
);
4309 build_static_route_flow(struct hmap
*lflows
, struct ovn_datapath
*od
,
4311 const struct nbrec_logical_router_static_route
*route
)
4314 const char *lrp_addr_s
= NULL
;
4318 /* Verify that the next hop is an IP address with an all-ones mask. */
4319 char *error
= ip_parse_cidr(route
->nexthop
, &nexthop
, &plen
);
4322 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4323 VLOG_WARN_RL(&rl
, "bad next hop mask %s", route
->nexthop
);
4330 struct in6_addr ip6
;
4331 error
= ipv6_parse_cidr(route
->nexthop
, &ip6
, &plen
);
4334 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4335 VLOG_WARN_RL(&rl
, "bad next hop mask %s", route
->nexthop
);
4340 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4341 VLOG_WARN_RL(&rl
, "bad next hop ip address %s", route
->nexthop
);
4350 /* Verify that ip prefix is a valid IPv4 address. */
4351 error
= ip_parse_cidr(route
->ip_prefix
, &prefix
, &plen
);
4353 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4354 VLOG_WARN_RL(&rl
, "bad 'ip_prefix' in static routes %s",
4359 prefix_s
= xasprintf(IP_FMT
, IP_ARGS(prefix
& be32_prefix_mask(plen
)));
4361 /* Verify that ip prefix is a valid IPv6 address. */
4362 struct in6_addr prefix
;
4363 error
= ipv6_parse_cidr(route
->ip_prefix
, &prefix
, &plen
);
4365 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4366 VLOG_WARN_RL(&rl
, "bad 'ip_prefix' in static routes %s",
4371 struct in6_addr mask
= ipv6_create_mask(plen
);
4372 struct in6_addr network
= ipv6_addr_bitand(&prefix
, &mask
);
4373 prefix_s
= xmalloc(INET6_ADDRSTRLEN
);
4374 inet_ntop(AF_INET6
, &network
, prefix_s
, INET6_ADDRSTRLEN
);
4377 /* Find the outgoing port. */
4378 struct ovn_port
*out_port
= NULL
;
4379 if (route
->output_port
) {
4380 out_port
= ovn_port_find(ports
, route
->output_port
);
4382 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4383 VLOG_WARN_RL(&rl
, "Bad out port %s for static route %s",
4384 route
->output_port
, route
->ip_prefix
);
4387 lrp_addr_s
= find_lrp_member_ip(out_port
, route
->nexthop
);
4389 /* There are no IP networks configured on the router's port via
4390 * which 'route->nexthop' is theoretically reachable. But since
4391 * 'out_port' has been specified, we honor it by trying to reach
4392 * 'route->nexthop' via the first IP address of 'out_port'.
4393 * (There are cases, e.g in GCE, where each VM gets a /32 IP
4394 * address and the default gateway is still reachable from it.) */
4396 if (out_port
->lrp_networks
.n_ipv4_addrs
) {
4397 lrp_addr_s
= out_port
->lrp_networks
.ipv4_addrs
[0].addr_s
;
4400 if (out_port
->lrp_networks
.n_ipv6_addrs
) {
4401 lrp_addr_s
= out_port
->lrp_networks
.ipv6_addrs
[0].addr_s
;
4406 /* output_port is not specified, find the
4407 * router port matching the next hop. */
4409 for (i
= 0; i
< od
->nbr
->n_ports
; i
++) {
4410 struct nbrec_logical_router_port
*lrp
= od
->nbr
->ports
[i
];
4411 out_port
= ovn_port_find(ports
, lrp
->name
);
4413 /* This should not happen. */
4417 lrp_addr_s
= find_lrp_member_ip(out_port
, route
->nexthop
);
4424 if (!out_port
|| !lrp_addr_s
) {
4425 /* There is no matched out port. */
4426 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4427 VLOG_WARN_RL(&rl
, "No path for static route %s; next hop %s",
4428 route
->ip_prefix
, route
->nexthop
);
4432 char *policy
= route
->policy
? route
->policy
: "dst-ip";
4433 add_route(lflows
, out_port
, lrp_addr_s
, prefix_s
, plen
, route
->nexthop
,
4441 op_put_v4_networks(struct ds
*ds
, const struct ovn_port
*op
, bool add_bcast
)
4443 if (!add_bcast
&& op
->lrp_networks
.n_ipv4_addrs
== 1) {
4444 ds_put_format(ds
, "%s", op
->lrp_networks
.ipv4_addrs
[0].addr_s
);
4448 ds_put_cstr(ds
, "{");
4449 for (int i
= 0; i
< op
->lrp_networks
.n_ipv4_addrs
; i
++) {
4450 ds_put_format(ds
, "%s, ", op
->lrp_networks
.ipv4_addrs
[i
].addr_s
);
4452 ds_put_format(ds
, "%s, ", op
->lrp_networks
.ipv4_addrs
[i
].bcast_s
);
4457 ds_put_cstr(ds
, "}");
4461 op_put_v6_networks(struct ds
*ds
, const struct ovn_port
*op
)
4463 if (op
->lrp_networks
.n_ipv6_addrs
== 1) {
4464 ds_put_format(ds
, "%s", op
->lrp_networks
.ipv6_addrs
[0].addr_s
);
4468 ds_put_cstr(ds
, "{");
4469 for (int i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; i
++) {
4470 ds_put_format(ds
, "%s, ", op
->lrp_networks
.ipv6_addrs
[i
].addr_s
);
4474 ds_put_cstr(ds
, "}");
4478 get_force_snat_ip(struct ovn_datapath
*od
, const char *key_type
, ovs_be32
*ip
)
4480 char *key
= xasprintf("%s_force_snat_ip", key_type
);
4481 const char *ip_address
= smap_get(&od
->nbr
->options
, key
);
4486 char *error
= ip_parse_masked(ip_address
, ip
, &mask
);
4487 if (error
|| mask
!= OVS_BE32_MAX
) {
4488 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4489 VLOG_WARN_RL(&rl
, "bad ip %s in options of router "UUID_FMT
"",
4490 ip_address
, UUID_ARGS(&od
->key
));
4503 add_router_lb_flow(struct hmap
*lflows
, struct ovn_datapath
*od
,
4504 struct ds
*match
, struct ds
*actions
, int priority
,
4505 const char *lb_force_snat_ip
, char *backend_ips
,
4506 bool is_udp
, int addr_family
)
4508 /* A match and actions for new connections. */
4509 char *new_match
= xasprintf("ct.new && %s", ds_cstr(match
));
4510 if (lb_force_snat_ip
) {
4511 char *new_actions
= xasprintf("flags.force_snat_for_lb = 1; %s",
4513 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, priority
, new_match
,
4517 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, priority
, new_match
,
4521 /* A match and actions for established connections. */
4522 char *est_match
= xasprintf("ct.est && %s", ds_cstr(match
));
4523 if (lb_force_snat_ip
) {
4524 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, priority
, est_match
,
4525 "flags.force_snat_for_lb = 1; ct_dnat;");
4527 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, priority
, est_match
,
4534 if (!od
->l3dgw_port
|| !od
->l3redirect_port
|| !backend_ips
4535 || addr_family
!= AF_INET
) {
4539 /* Add logical flows to UNDNAT the load balanced reverse traffic in
4540 * the router egress pipleine stage - S_ROUTER_OUT_UNDNAT if the logical
4541 * router has a gateway router port associated.
4543 struct ds undnat_match
= DS_EMPTY_INITIALIZER
;
4544 ds_put_cstr(&undnat_match
, "ip4 && (");
4545 char *start
, *next
, *ip_str
;
4546 start
= next
= xstrdup(backend_ips
);
4547 ip_str
= strsep(&next
, ",");
4548 bool backend_ips_found
= false;
4549 while (ip_str
&& ip_str
[0]) {
4550 char *ip_address
= NULL
;
4553 ip_address_and_port_from_lb_key(ip_str
, &ip_address
, &port
,
4559 ds_put_format(&undnat_match
, "(ip4.src == %s", ip_address
);
4562 ds_put_format(&undnat_match
, " && %s.src == %d) || ",
4563 is_udp
? "udp" : "tcp", port
);
4565 ds_put_cstr(&undnat_match
, ") || ");
4567 ip_str
= strsep(&next
, ",");
4568 backend_ips_found
= true;
4572 if (!backend_ips_found
) {
4573 ds_destroy(&undnat_match
);
4576 ds_chomp(&undnat_match
, ' ');
4577 ds_chomp(&undnat_match
, '|');
4578 ds_chomp(&undnat_match
, '|');
4579 ds_chomp(&undnat_match
, ' ');
4580 ds_put_format(&undnat_match
, ") && outport == %s && "
4581 "is_chassis_resident(%s)", od
->l3dgw_port
->json_key
,
4582 od
->l3redirect_port
->json_key
);
4583 if (lb_force_snat_ip
) {
4584 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_UNDNAT
, 120,
4585 ds_cstr(&undnat_match
),
4586 "flags.force_snat_for_lb = 1; ct_dnat;");
4588 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_UNDNAT
, 120,
4589 ds_cstr(&undnat_match
), "ct_dnat;");
4592 ds_destroy(&undnat_match
);
4595 #define ND_RA_MAX_INTERVAL_MAX 1800
4596 #define ND_RA_MAX_INTERVAL_MIN 4
4598 #define ND_RA_MIN_INTERVAL_MAX(max) ((max) * 3 / 4)
4599 #define ND_RA_MIN_INTERVAL_MIN 3
4602 copy_ra_to_sb(struct ovn_port
*op
, const char *address_mode
)
4604 struct smap options
;
4605 smap_clone(&options
, &op
->sb
->options
);
4607 smap_add(&options
, "ipv6_ra_send_periodic", "true");
4608 smap_add(&options
, "ipv6_ra_address_mode", address_mode
);
4610 int max_interval
= smap_get_int(&op
->nbrp
->ipv6_ra_configs
,
4611 "max_interval", ND_RA_MAX_INTERVAL_DEFAULT
);
4612 if (max_interval
> ND_RA_MAX_INTERVAL_MAX
) {
4613 max_interval
= ND_RA_MAX_INTERVAL_MAX
;
4615 if (max_interval
< ND_RA_MAX_INTERVAL_MIN
) {
4616 max_interval
= ND_RA_MAX_INTERVAL_MIN
;
4618 smap_add_format(&options
, "ipv6_ra_max_interval", "%d", max_interval
);
4620 int min_interval
= smap_get_int(&op
->nbrp
->ipv6_ra_configs
,
4621 "min_interval", nd_ra_min_interval_default(max_interval
));
4622 if (min_interval
> ND_RA_MIN_INTERVAL_MAX(max_interval
)) {
4623 min_interval
= ND_RA_MIN_INTERVAL_MAX(max_interval
);
4625 if (min_interval
< ND_RA_MIN_INTERVAL_MIN
) {
4626 min_interval
= ND_RA_MIN_INTERVAL_MIN
;
4628 smap_add_format(&options
, "ipv6_ra_min_interval", "%d", min_interval
);
4630 int mtu
= smap_get_int(&op
->nbrp
->ipv6_ra_configs
, "mtu", ND_MTU_DEFAULT
);
4631 /* RFC 2460 requires the MTU for IPv6 to be at least 1280 */
4632 if (mtu
&& mtu
>= 1280) {
4633 smap_add_format(&options
, "ipv6_ra_mtu", "%d", mtu
);
4636 struct ds s
= DS_EMPTY_INITIALIZER
;
4637 for (int i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; ++i
) {
4638 struct ipv6_netaddr
*addrs
= &op
->lrp_networks
.ipv6_addrs
[i
];
4639 if (in6_is_lla(&addrs
->network
)) {
4640 smap_add(&options
, "ipv6_ra_src_addr", addrs
->addr_s
);
4643 ds_put_format(&s
, "%s/%u ", addrs
->network_s
, addrs
->plen
);
4645 /* Remove trailing space */
4647 smap_add(&options
, "ipv6_ra_prefixes", ds_cstr(&s
));
4650 smap_add(&options
, "ipv6_ra_src_eth", op
->lrp_networks
.ea_s
);
4652 sbrec_port_binding_set_options(op
->sb
, &options
);
4653 smap_destroy(&options
);
4657 build_lrouter_flows(struct hmap
*datapaths
, struct hmap
*ports
,
4658 struct hmap
*lflows
)
4660 /* This flow table structure is documented in ovn-northd(8), so please
4661 * update ovn-northd.8.xml if you change anything. */
4663 struct ds match
= DS_EMPTY_INITIALIZER
;
4664 struct ds actions
= DS_EMPTY_INITIALIZER
;
4666 /* Logical router ingress table 0: Admission control framework. */
4667 struct ovn_datapath
*od
;
4668 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4673 /* Logical VLANs not supported.
4674 * Broadcast/multicast source address is invalid. */
4675 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ADMISSION
, 100,
4676 "vlan.present || eth.src[40]", "drop;");
4679 /* Logical router ingress table 0: match (priority 50). */
4680 struct ovn_port
*op
;
4681 HMAP_FOR_EACH (op
, key_node
, ports
) {
4686 if (!lrport_is_enabled(op
->nbrp
)) {
4687 /* Drop packets from disabled logical ports (since logical flow
4688 * tables are default-drop). */
4693 /* No ingress packets should be received on a chassisredirect
4699 ds_put_format(&match
, "eth.mcast && inport == %s", op
->json_key
);
4700 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_ADMISSION
, 50,
4701 ds_cstr(&match
), "next;");
4704 ds_put_format(&match
, "eth.dst == %s && inport == %s",
4705 op
->lrp_networks
.ea_s
, op
->json_key
);
4706 if (op
->od
->l3dgw_port
&& op
== op
->od
->l3dgw_port
4707 && op
->od
->l3redirect_port
) {
4708 /* Traffic with eth.dst = l3dgw_port->lrp_networks.ea_s
4709 * should only be received on the "redirect-chassis". */
4710 ds_put_format(&match
, " && is_chassis_resident(%s)",
4711 op
->od
->l3redirect_port
->json_key
);
4713 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_ADMISSION
, 50,
4714 ds_cstr(&match
), "next;");
4717 /* Logical router ingress table 1: IP Input. */
4718 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
4723 /* L3 admission control: drop multicast and broadcast source, localhost
4724 * source or destination, and zero network source or destination
4725 * (priority 100). */
4726 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 100,
4728 "ip4.src == 255.255.255.255 || "
4729 "ip4.src == 127.0.0.0/8 || "
4730 "ip4.dst == 127.0.0.0/8 || "
4731 "ip4.src == 0.0.0.0/8 || "
4732 "ip4.dst == 0.0.0.0/8",
4735 /* ARP reply handling. Use ARP replies to populate the logical
4736 * router's ARP table. */
4737 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 90, "arp.op == 2",
4738 "put_arp(inport, arp.spa, arp.sha);");
4740 /* Drop Ethernet local broadcast. By definition this traffic should
4741 * not be forwarded.*/
4742 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 50,
4743 "eth.bcast", "drop;");
4747 * XXX Need to send ICMP time exceeded if !ip.later_frag. */
4749 ds_put_cstr(&match
, "ip4 && ip.ttl == {0, 1}");
4750 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 30,
4751 ds_cstr(&match
), "drop;");
4753 /* ND advertisement handling. Use advertisements to populate
4754 * the logical router's ARP/ND table. */
4755 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 90, "nd_na",
4756 "put_nd(inport, nd.target, nd.tll);");
4758 /* Lean from neighbor solicitations that were not directed at
4759 * us. (A priority-90 flow will respond to requests to us and
4760 * learn the sender's mac address. */
4761 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 80, "nd_ns",
4762 "put_nd(inport, ip6.src, nd.sll);");
4764 /* Pass other traffic not already handled to the next table for
4766 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_INPUT
, 0, "1", "next;");
4769 /* Logical router ingress table 1: IP Input for IPv4. */
4770 HMAP_FOR_EACH (op
, key_node
, ports
) {
4776 /* No ingress packets are accepted on a chassisredirect
4777 * port, so no need to program flows for that port. */
4781 if (op
->lrp_networks
.n_ipv4_addrs
) {
4782 /* L3 admission control: drop packets that originate from an
4783 * IPv4 address owned by the router or a broadcast address
4784 * known to the router (priority 100). */
4786 ds_put_cstr(&match
, "ip4.src == ");
4787 op_put_v4_networks(&match
, op
, true);
4788 ds_put_cstr(&match
, " && "REGBIT_EGRESS_LOOPBACK
" == 0");
4789 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 100,
4790 ds_cstr(&match
), "drop;");
4792 /* ICMP echo reply. These flows reply to ICMP echo requests
4793 * received for the router's IP address. Since packets only
4794 * get here as part of the logical router datapath, the inport
4795 * (i.e. the incoming locally attached net) does not matter.
4796 * The ip.ttl also does not matter (RFC1812 section 4.2.2.9) */
4798 ds_put_cstr(&match
, "ip4.dst == ");
4799 op_put_v4_networks(&match
, op
, false);
4800 ds_put_cstr(&match
, " && icmp4.type == 8 && icmp4.code == 0");
4803 ds_put_format(&actions
,
4804 "ip4.dst <-> ip4.src; "
4807 "flags.loopback = 1; "
4809 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
4810 ds_cstr(&match
), ds_cstr(&actions
));
4813 /* ARP reply. These flows reply to ARP requests for the router's own
4815 for (int i
= 0; i
< op
->lrp_networks
.n_ipv4_addrs
; i
++) {
4817 ds_put_format(&match
,
4818 "inport == %s && arp.tpa == %s && arp.op == 1",
4819 op
->json_key
, op
->lrp_networks
.ipv4_addrs
[i
].addr_s
);
4820 if (op
->od
->l3dgw_port
&& op
== op
->od
->l3dgw_port
4821 && op
->od
->l3redirect_port
) {
4822 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
4823 * should only be sent from the "redirect-chassis", so that
4824 * upstream MAC learning points to the "redirect-chassis".
4825 * Also need to avoid generation of multiple ARP responses
4826 * from different chassis. */
4827 ds_put_format(&match
, " && is_chassis_resident(%s)",
4828 op
->od
->l3redirect_port
->json_key
);
4832 ds_put_format(&actions
,
4833 "eth.dst = eth.src; "
4835 "arp.op = 2; /* ARP reply */ "
4836 "arp.tha = arp.sha; "
4838 "arp.tpa = arp.spa; "
4841 "flags.loopback = 1; "
4843 op
->lrp_networks
.ea_s
,
4844 op
->lrp_networks
.ea_s
,
4845 op
->lrp_networks
.ipv4_addrs
[i
].addr_s
,
4847 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
4848 ds_cstr(&match
), ds_cstr(&actions
));
4851 /* A set to hold all load-balancer vips that need ARP responses. */
4852 struct sset all_ips
= SSET_INITIALIZER(&all_ips
);
4854 get_router_load_balancer_ips(op
->od
, &all_ips
, &addr_family
);
4856 const char *ip_address
;
4857 SSET_FOR_EACH(ip_address
, &all_ips
) {
4859 if (addr_family
== AF_INET
) {
4860 ds_put_format(&match
,
4861 "inport == %s && arp.tpa == %s && arp.op == 1",
4862 op
->json_key
, ip_address
);
4864 ds_put_format(&match
,
4865 "inport == %s && nd_ns && nd.target == %s",
4866 op
->json_key
, ip_address
);
4870 if (addr_family
== AF_INET
) {
4871 ds_put_format(&actions
,
4872 "eth.dst = eth.src; "
4874 "arp.op = 2; /* ARP reply */ "
4875 "arp.tha = arp.sha; "
4877 "arp.tpa = arp.spa; "
4880 "flags.loopback = 1; "
4882 op
->lrp_networks
.ea_s
,
4883 op
->lrp_networks
.ea_s
,
4887 ds_put_format(&actions
,
4893 "outport = inport; "
4894 "flags.loopback = 1; "
4897 op
->lrp_networks
.ea_s
,
4900 op
->lrp_networks
.ea_s
);
4902 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
4903 ds_cstr(&match
), ds_cstr(&actions
));
4906 sset_destroy(&all_ips
);
4908 /* A gateway router can have 2 SNAT IP addresses to force DNATed and
4909 * LBed traffic respectively to be SNATed. In addition, there can be
4910 * a number of SNAT rules in the NAT table. */
4911 ovs_be32
*snat_ips
= xmalloc(sizeof *snat_ips
*
4912 (op
->od
->nbr
->n_nat
+ 2));
4913 size_t n_snat_ips
= 0;
4916 const char *dnat_force_snat_ip
= get_force_snat_ip(op
->od
, "dnat",
4918 if (dnat_force_snat_ip
) {
4919 snat_ips
[n_snat_ips
++] = snat_ip
;
4922 const char *lb_force_snat_ip
= get_force_snat_ip(op
->od
, "lb",
4924 if (lb_force_snat_ip
) {
4925 snat_ips
[n_snat_ips
++] = snat_ip
;
4928 for (int i
= 0; i
< op
->od
->nbr
->n_nat
; i
++) {
4929 const struct nbrec_nat
*nat
;
4931 nat
= op
->od
->nbr
->nat
[i
];
4934 if (!ip_parse(nat
->external_ip
, &ip
) || !ip
) {
4935 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
4936 VLOG_WARN_RL(&rl
, "bad ip address %s in nat configuration "
4937 "for router %s", nat
->external_ip
, op
->key
);
4941 if (!strcmp(nat
->type
, "snat")) {
4942 snat_ips
[n_snat_ips
++] = ip
;
4946 /* ARP handling for external IP addresses.
4948 * DNAT IP addresses are external IP addresses that need ARP
4951 ds_put_format(&match
,
4952 "inport == %s && arp.tpa == "IP_FMT
" && arp.op == 1",
4953 op
->json_key
, IP_ARGS(ip
));
4956 ds_put_format(&actions
,
4957 "eth.dst = eth.src; "
4958 "arp.op = 2; /* ARP reply */ "
4959 "arp.tha = arp.sha; ");
4961 if (op
->od
->l3dgw_port
&& op
== op
->od
->l3dgw_port
) {
4962 struct eth_addr mac
;
4963 if (nat
->external_mac
&&
4964 eth_addr_from_string(nat
->external_mac
, &mac
)
4965 && nat
->logical_port
) {
4966 /* distributed NAT case, use nat->external_mac */
4967 ds_put_format(&actions
,
4968 "eth.src = "ETH_ADDR_FMT
"; "
4969 "arp.sha = "ETH_ADDR_FMT
"; ",
4971 ETH_ADDR_ARGS(mac
));
4972 /* Traffic with eth.src = nat->external_mac should only be
4973 * sent from the chassis where nat->logical_port is
4974 * resident, so that upstream MAC learning points to the
4975 * correct chassis. Also need to avoid generation of
4976 * multiple ARP responses from different chassis. */
4977 ds_put_format(&match
, " && is_chassis_resident(\"%s\")",
4980 ds_put_format(&actions
,
4983 op
->lrp_networks
.ea_s
,
4984 op
->lrp_networks
.ea_s
);
4985 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
4986 * should only be sent from the "redirect-chassis", so that
4987 * upstream MAC learning points to the "redirect-chassis".
4988 * Also need to avoid generation of multiple ARP responses
4989 * from different chassis. */
4990 if (op
->od
->l3redirect_port
) {
4991 ds_put_format(&match
, " && is_chassis_resident(%s)",
4992 op
->od
->l3redirect_port
->json_key
);
4996 ds_put_format(&actions
,
4999 op
->lrp_networks
.ea_s
,
5000 op
->lrp_networks
.ea_s
);
5002 ds_put_format(&actions
,
5003 "arp.tpa = arp.spa; "
5004 "arp.spa = "IP_FMT
"; "
5006 "flags.loopback = 1; "
5010 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
5011 ds_cstr(&match
), ds_cstr(&actions
));
5015 ds_put_cstr(&match
, "ip4.dst == {");
5016 bool has_drop_ips
= false;
5017 for (int i
= 0; i
< op
->lrp_networks
.n_ipv4_addrs
; i
++) {
5018 bool snat_ip_is_router_ip
= false;
5019 for (int j
= 0; j
< n_snat_ips
; j
++) {
5020 /* Packets to SNAT IPs should not be dropped. */
5021 if (op
->lrp_networks
.ipv4_addrs
[i
].addr
== snat_ips
[j
]) {
5022 snat_ip_is_router_ip
= true;
5026 if (snat_ip_is_router_ip
) {
5029 ds_put_format(&match
, "%s, ",
5030 op
->lrp_networks
.ipv4_addrs
[i
].addr_s
);
5031 has_drop_ips
= true;
5033 ds_chomp(&match
, ' ');
5034 ds_chomp(&match
, ',');
5035 ds_put_cstr(&match
, "}");
5038 /* Drop IP traffic to this router. */
5039 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 60,
5040 ds_cstr(&match
), "drop;");
5046 /* Logical router ingress table 1: IP Input for IPv6. */
5047 HMAP_FOR_EACH (op
, key_node
, ports
) {
5053 /* No ingress packets are accepted on a chassisredirect
5054 * port, so no need to program flows for that port. */
5058 if (op
->lrp_networks
.n_ipv6_addrs
) {
5059 /* L3 admission control: drop packets that originate from an
5060 * IPv6 address owned by the router (priority 100). */
5062 ds_put_cstr(&match
, "ip6.src == ");
5063 op_put_v6_networks(&match
, op
);
5064 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 100,
5065 ds_cstr(&match
), "drop;");
5067 /* ICMPv6 echo reply. These flows reply to echo requests
5068 * received for the router's IP address. */
5070 ds_put_cstr(&match
, "ip6.dst == ");
5071 op_put_v6_networks(&match
, op
);
5072 ds_put_cstr(&match
, " && icmp6.type == 128 && icmp6.code == 0");
5075 ds_put_cstr(&actions
,
5076 "ip6.dst <-> ip6.src; "
5078 "icmp6.type = 129; "
5079 "flags.loopback = 1; "
5081 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
5082 ds_cstr(&match
), ds_cstr(&actions
));
5084 /* Drop IPv6 traffic to this router. */
5086 ds_put_cstr(&match
, "ip6.dst == ");
5087 op_put_v6_networks(&match
, op
);
5088 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 60,
5089 ds_cstr(&match
), "drop;");
5092 /* ND reply. These flows reply to ND solicitations for the
5093 * router's own IP address. */
5094 for (int i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; i
++) {
5096 ds_put_format(&match
,
5097 "inport == %s && nd_ns && ip6.dst == {%s, %s} "
5098 "&& nd.target == %s",
5100 op
->lrp_networks
.ipv6_addrs
[i
].addr_s
,
5101 op
->lrp_networks
.ipv6_addrs
[i
].sn_addr_s
,
5102 op
->lrp_networks
.ipv6_addrs
[i
].addr_s
);
5103 if (op
->od
->l3dgw_port
&& op
== op
->od
->l3dgw_port
5104 && op
->od
->l3redirect_port
) {
5105 /* Traffic with eth.src = l3dgw_port->lrp_networks.ea_s
5106 * should only be sent from the "redirect-chassis", so that
5107 * upstream MAC learning points to the "redirect-chassis".
5108 * Also need to avoid generation of multiple ND replies
5109 * from different chassis. */
5110 ds_put_format(&match
, " && is_chassis_resident(%s)",
5111 op
->od
->l3redirect_port
->json_key
);
5115 ds_put_format(&actions
,
5116 "put_nd(inport, ip6.src, nd.sll); "
5122 "outport = inport; "
5123 "flags.loopback = 1; "
5126 op
->lrp_networks
.ea_s
,
5127 op
->lrp_networks
.ipv6_addrs
[i
].addr_s
,
5128 op
->lrp_networks
.ipv6_addrs
[i
].addr_s
,
5129 op
->lrp_networks
.ea_s
);
5130 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_IP_INPUT
, 90,
5131 ds_cstr(&match
), ds_cstr(&actions
));
5135 /* NAT, Defrag and load balancing. */
5136 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
5141 /* Packets are allowed by default. */
5142 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DEFRAG
, 0, "1", "next;");
5143 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 0, "1", "next;");
5144 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_SNAT
, 0, "1", "next;");
5145 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, 0, "1", "next;");
5146 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_UNDNAT
, 0, "1", "next;");
5147 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_EGR_LOOP
, 0, "1", "next;");
5149 /* NAT rules are only valid on Gateway routers and routers with
5150 * l3dgw_port (router has a port with "redirect-chassis"
5152 if (!smap_get(&od
->nbr
->options
, "chassis") && !od
->l3dgw_port
) {
5157 const char *dnat_force_snat_ip
= get_force_snat_ip(od
, "dnat",
5159 const char *lb_force_snat_ip
= get_force_snat_ip(od
, "lb",
5162 for (int i
= 0; i
< od
->nbr
->n_nat
; i
++) {
5163 const struct nbrec_nat
*nat
;
5165 nat
= od
->nbr
->nat
[i
];
5169 char *error
= ip_parse_masked(nat
->external_ip
, &ip
, &mask
);
5170 if (error
|| mask
!= OVS_BE32_MAX
) {
5171 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
5172 VLOG_WARN_RL(&rl
, "bad external ip %s for nat",
5178 /* Check the validity of nat->logical_ip. 'logical_ip' can
5179 * be a subnet when the type is "snat". */
5180 error
= ip_parse_masked(nat
->logical_ip
, &ip
, &mask
);
5181 if (!strcmp(nat
->type
, "snat")) {
5183 static struct vlog_rate_limit rl
=
5184 VLOG_RATE_LIMIT_INIT(5, 1);
5185 VLOG_WARN_RL(&rl
, "bad ip network or ip %s for snat "
5186 "in router "UUID_FMT
"",
5187 nat
->logical_ip
, UUID_ARGS(&od
->key
));
5192 if (error
|| mask
!= OVS_BE32_MAX
) {
5193 static struct vlog_rate_limit rl
=
5194 VLOG_RATE_LIMIT_INIT(5, 1);
5195 VLOG_WARN_RL(&rl
, "bad ip %s for dnat in router "
5196 ""UUID_FMT
"", nat
->logical_ip
, UUID_ARGS(&od
->key
));
5202 /* For distributed router NAT, determine whether this NAT rule
5203 * satisfies the conditions for distributed NAT processing. */
5204 bool distributed
= false;
5205 struct eth_addr mac
;
5206 if (od
->l3dgw_port
&& !strcmp(nat
->type
, "dnat_and_snat") &&
5207 nat
->logical_port
&& nat
->external_mac
) {
5208 if (eth_addr_from_string(nat
->external_mac
, &mac
)) {
5211 static struct vlog_rate_limit rl
=
5212 VLOG_RATE_LIMIT_INIT(5, 1);
5213 VLOG_WARN_RL(&rl
, "bad mac %s for dnat in router "
5214 ""UUID_FMT
"", nat
->external_mac
, UUID_ARGS(&od
->key
));
5219 /* Ingress UNSNAT table: It is for already established connections'
5220 * reverse traffic. i.e., SNAT has already been done in egress
5221 * pipeline and now the packet has entered the ingress pipeline as
5222 * part of a reply. We undo the SNAT here.
5224 * Undoing SNAT has to happen before DNAT processing. This is
5225 * because when the packet was DNATed in ingress pipeline, it did
5226 * not know about the possibility of eventual additional SNAT in
5227 * egress pipeline. */
5228 if (!strcmp(nat
->type
, "snat")
5229 || !strcmp(nat
->type
, "dnat_and_snat")) {
5230 if (!od
->l3dgw_port
) {
5231 /* Gateway router. */
5233 ds_put_format(&match
, "ip && ip4.dst == %s",
5235 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 90,
5236 ds_cstr(&match
), "ct_snat;");
5238 /* Distributed router. */
5240 /* Traffic received on l3dgw_port is subject to NAT. */
5242 ds_put_format(&match
, "ip && ip4.dst == %s"
5245 od
->l3dgw_port
->json_key
);
5246 if (!distributed
&& od
->l3redirect_port
) {
5247 /* Flows for NAT rules that are centralized are only
5248 * programmed on the "redirect-chassis". */
5249 ds_put_format(&match
, " && is_chassis_resident(%s)",
5250 od
->l3redirect_port
->json_key
);
5252 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 100,
5253 ds_cstr(&match
), "ct_snat;");
5255 /* Traffic received on other router ports must be
5256 * redirected to the central instance of the l3dgw_port
5257 * for NAT processing. */
5259 ds_put_format(&match
, "ip && ip4.dst == %s",
5261 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 50,
5263 REGBIT_NAT_REDIRECT
" = 1; next;");
5267 /* Ingress DNAT table: Packets enter the pipeline with destination
5268 * IP address that needs to be DNATted from a external IP address
5269 * to a logical IP address. */
5270 if (!strcmp(nat
->type
, "dnat")
5271 || !strcmp(nat
->type
, "dnat_and_snat")) {
5272 if (!od
->l3dgw_port
) {
5273 /* Gateway router. */
5274 /* Packet when it goes from the initiator to destination.
5275 * We need to set flags.loopback because the router can
5276 * send the packet back through the same interface. */
5278 ds_put_format(&match
, "ip && ip4.dst == %s",
5281 if (dnat_force_snat_ip
) {
5282 /* Indicate to the future tables that a DNAT has taken
5283 * place and a force SNAT needs to be done in the
5284 * Egress SNAT table. */
5285 ds_put_format(&actions
,
5286 "flags.force_snat_for_dnat = 1; ");
5288 ds_put_format(&actions
, "flags.loopback = 1; ct_dnat(%s);",
5290 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, 100,
5291 ds_cstr(&match
), ds_cstr(&actions
));
5293 /* Distributed router. */
5295 /* Traffic received on l3dgw_port is subject to NAT. */
5297 ds_put_format(&match
, "ip && ip4.dst == %s"
5300 od
->l3dgw_port
->json_key
);
5301 if (!distributed
&& od
->l3redirect_port
) {
5302 /* Flows for NAT rules that are centralized are only
5303 * programmed on the "redirect-chassis". */
5304 ds_put_format(&match
, " && is_chassis_resident(%s)",
5305 od
->l3redirect_port
->json_key
);
5308 ds_put_format(&actions
, "ct_dnat(%s);",
5310 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, 100,
5311 ds_cstr(&match
), ds_cstr(&actions
));
5313 /* Traffic received on other router ports must be
5314 * redirected to the central instance of the l3dgw_port
5315 * for NAT processing. */
5317 ds_put_format(&match
, "ip && ip4.dst == %s",
5319 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, 50,
5321 REGBIT_NAT_REDIRECT
" = 1; next;");
5325 /* Egress UNDNAT table: It is for already established connections'
5326 * reverse traffic. i.e., DNAT has already been done in ingress
5327 * pipeline and now the packet has entered the egress pipeline as
5328 * part of a reply. We undo the DNAT here.
5330 * Note that this only applies for NAT on a distributed router.
5331 * Undo DNAT on a gateway router is done in the ingress DNAT
5332 * pipeline stage. */
5333 if (od
->l3dgw_port
&& (!strcmp(nat
->type
, "dnat")
5334 || !strcmp(nat
->type
, "dnat_and_snat"))) {
5336 ds_put_format(&match
, "ip && ip4.src == %s"
5337 " && outport == %s",
5339 od
->l3dgw_port
->json_key
);
5340 if (!distributed
&& od
->l3redirect_port
) {
5341 /* Flows for NAT rules that are centralized are only
5342 * programmed on the "redirect-chassis". */
5343 ds_put_format(&match
, " && is_chassis_resident(%s)",
5344 od
->l3redirect_port
->json_key
);
5348 ds_put_format(&actions
, "eth.src = "ETH_ADDR_FMT
"; ",
5349 ETH_ADDR_ARGS(mac
));
5351 ds_put_format(&actions
, "ct_dnat;");
5352 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_UNDNAT
, 100,
5353 ds_cstr(&match
), ds_cstr(&actions
));
5356 /* Egress SNAT table: Packets enter the egress pipeline with
5357 * source ip address that needs to be SNATted to a external ip
5359 if (!strcmp(nat
->type
, "snat")
5360 || !strcmp(nat
->type
, "dnat_and_snat")) {
5361 if (!od
->l3dgw_port
) {
5362 /* Gateway router. */
5364 ds_put_format(&match
, "ip && ip4.src == %s",
5367 ds_put_format(&actions
, "ct_snat(%s);", nat
->external_ip
);
5369 /* The priority here is calculated such that the
5370 * nat->logical_ip with the longest mask gets a higher
5372 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_SNAT
,
5373 count_1bits(ntohl(mask
)) + 1,
5374 ds_cstr(&match
), ds_cstr(&actions
));
5376 /* Distributed router. */
5378 ds_put_format(&match
, "ip && ip4.src == %s"
5379 " && outport == %s",
5381 od
->l3dgw_port
->json_key
);
5382 if (!distributed
&& od
->l3redirect_port
) {
5383 /* Flows for NAT rules that are centralized are only
5384 * programmed on the "redirect-chassis". */
5385 ds_put_format(&match
, " && is_chassis_resident(%s)",
5386 od
->l3redirect_port
->json_key
);
5390 ds_put_format(&actions
, "eth.src = "ETH_ADDR_FMT
"; ",
5391 ETH_ADDR_ARGS(mac
));
5393 ds_put_format(&actions
, "ct_snat(%s);", nat
->external_ip
);
5395 /* The priority here is calculated such that the
5396 * nat->logical_ip with the longest mask gets a higher
5398 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_SNAT
,
5399 count_1bits(ntohl(mask
)) + 1,
5400 ds_cstr(&match
), ds_cstr(&actions
));
5404 /* Logical router ingress table 0:
5405 * For NAT on a distributed router, add rules allowing
5406 * ingress traffic with eth.dst matching nat->external_mac
5407 * on the l3dgw_port instance where nat->logical_port is
5411 ds_put_format(&match
,
5412 "eth.dst == "ETH_ADDR_FMT
" && inport == %s"
5413 " && is_chassis_resident(\"%s\")",
5415 od
->l3dgw_port
->json_key
,
5417 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ADMISSION
, 50,
5418 ds_cstr(&match
), "next;");
5421 /* Ingress Gateway Redirect Table: For NAT on a distributed
5422 * router, add flows that are specific to a NAT rule. These
5423 * flows indicate the presence of an applicable NAT rule that
5424 * can be applied in a distributed manner. */
5427 ds_put_format(&match
, "ip4.src == %s && outport == %s",
5429 od
->l3dgw_port
->json_key
);
5430 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_GW_REDIRECT
, 100,
5431 ds_cstr(&match
), "next;");
5434 /* Egress Loopback table: For NAT on a distributed router.
5435 * If packets in the egress pipeline on the distributed
5436 * gateway port have ip.dst matching a NAT external IP, then
5437 * loop a clone of the packet back to the beginning of the
5438 * ingress pipeline with inport = outport. */
5439 if (od
->l3dgw_port
) {
5440 /* Distributed router. */
5442 ds_put_format(&match
, "ip4.dst == %s && outport == %s",
5444 od
->l3dgw_port
->json_key
);
5446 ds_put_format(&actions
,
5447 "clone { ct_clear; "
5448 "inport = outport; outport = \"\"; "
5449 "flags = 0; flags.loopback = 1; ");
5450 for (int j
= 0; j
< MFF_N_LOG_REGS
; j
++) {
5451 ds_put_format(&actions
, "reg%d = 0; ", j
);
5453 ds_put_format(&actions
, REGBIT_EGRESS_LOOPBACK
" = 1; "
5454 "next(pipeline=ingress, table=0); };");
5455 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_EGR_LOOP
, 100,
5456 ds_cstr(&match
), ds_cstr(&actions
));
5460 /* Handle force SNAT options set in the gateway router. */
5461 if (dnat_force_snat_ip
&& !od
->l3dgw_port
) {
5462 /* If a packet with destination IP address as that of the
5463 * gateway router (as set in options:dnat_force_snat_ip) is seen,
5466 ds_put_format(&match
, "ip && ip4.dst == %s", dnat_force_snat_ip
);
5467 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 110,
5468 ds_cstr(&match
), "ct_snat;");
5470 /* Higher priority rules to force SNAT with the IP addresses
5471 * configured in the Gateway router. This only takes effect
5472 * when the packet has already been DNATed once. */
5474 ds_put_format(&match
, "flags.force_snat_for_dnat == 1 && ip");
5476 ds_put_format(&actions
, "ct_snat(%s);", dnat_force_snat_ip
);
5477 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_SNAT
, 100,
5478 ds_cstr(&match
), ds_cstr(&actions
));
5480 if (lb_force_snat_ip
&& !od
->l3dgw_port
) {
5481 /* If a packet with destination IP address as that of the
5482 * gateway router (as set in options:lb_force_snat_ip) is seen,
5485 ds_put_format(&match
, "ip && ip4.dst == %s", lb_force_snat_ip
);
5486 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_UNSNAT
, 100,
5487 ds_cstr(&match
), "ct_snat;");
5489 /* Load balanced traffic will have flags.force_snat_for_lb set.
5492 ds_put_format(&match
, "flags.force_snat_for_lb == 1 && ip");
5494 ds_put_format(&actions
, "ct_snat(%s);", lb_force_snat_ip
);
5495 ovn_lflow_add(lflows
, od
, S_ROUTER_OUT_SNAT
, 100,
5496 ds_cstr(&match
), ds_cstr(&actions
));
5499 if (!od
->l3dgw_port
) {
5500 /* For gateway router, re-circulate every packet through
5501 * the DNAT zone. This helps with the following.
5503 * Any packet that needs to be unDNATed in the reverse
5504 * direction gets unDNATed. Ideally this could be done in
5505 * the egress pipeline. But since the gateway router
5506 * does not have any feature that depends on the source
5507 * ip address being external IP address for IP routing,
5508 * we can do it here, saving a future re-circulation. */
5509 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DNAT
, 50,
5510 "ip", "flags.loopback = 1; ct_dnat;");
5512 /* For NAT on a distributed router, add flows to Ingress
5513 * IP Routing table, Ingress ARP Resolution table, and
5514 * Ingress Gateway Redirect Table that are not specific to a
5517 /* The highest priority IN_IP_ROUTING rule matches packets
5518 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages),
5519 * with action "ip.ttl--; next;". The IN_GW_REDIRECT table
5520 * will take care of setting the outport. */
5521 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_IP_ROUTING
, 300,
5522 REGBIT_NAT_REDIRECT
" == 1", "ip.ttl--; next;");
5524 /* The highest priority IN_ARP_RESOLVE rule matches packets
5525 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages),
5526 * then sets eth.dst to the distributed gateway port's
5527 * ethernet address. */
5529 ds_put_format(&actions
, "eth.dst = %s; next;",
5530 od
->l3dgw_port
->lrp_networks
.ea_s
);
5531 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_RESOLVE
, 200,
5532 REGBIT_NAT_REDIRECT
" == 1", ds_cstr(&actions
));
5534 /* The highest priority IN_GW_REDIRECT rule redirects packets
5535 * with REGBIT_NAT_REDIRECT (set in DNAT or UNSNAT stages) to
5536 * the central instance of the l3dgw_port for NAT processing. */
5538 ds_put_format(&actions
, "outport = %s; next;",
5539 od
->l3redirect_port
->json_key
);
5540 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_GW_REDIRECT
, 200,
5541 REGBIT_NAT_REDIRECT
" == 1", ds_cstr(&actions
));
5544 /* Load balancing and packet defrag are only valid on
5545 * Gateway routers or router with gateway port. */
5546 if (!smap_get(&od
->nbr
->options
, "chassis") && !od
->l3dgw_port
) {
5550 /* A set to hold all ips that need defragmentation and tracking. */
5551 struct sset all_ips
= SSET_INITIALIZER(&all_ips
);
5553 for (int i
= 0; i
< od
->nbr
->n_load_balancer
; i
++) {
5554 struct nbrec_load_balancer
*lb
= od
->nbr
->load_balancer
[i
];
5555 struct smap
*vips
= &lb
->vips
;
5556 struct smap_node
*node
;
5558 SMAP_FOR_EACH (node
, vips
) {
5562 /* node->key contains IP:port or just IP. */
5563 char *ip_address
= NULL
;
5564 ip_address_and_port_from_lb_key(node
->key
, &ip_address
, &port
,
5570 if (!sset_contains(&all_ips
, ip_address
)) {
5571 sset_add(&all_ips
, ip_address
);
5572 /* If there are any load balancing rules, we should send
5573 * the packet to conntrack for defragmentation and
5574 * tracking. This helps with two things.
5576 * 1. With tracking, we can send only new connections to
5577 * pick a DNAT ip address from a group.
5578 * 2. If there are L4 ports in load balancing rules, we
5579 * need the defragmentation to match on L4 ports. */
5581 if (addr_family
== AF_INET
) {
5582 ds_put_format(&match
, "ip && ip4.dst == %s",
5585 ds_put_format(&match
, "ip && ip6.dst == %s",
5588 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_DEFRAG
,
5589 100, ds_cstr(&match
), "ct_next;");
5592 /* Higher priority rules are added for load-balancing in DNAT
5593 * table. For every match (on a VIP[:port]), we add two flows
5594 * via add_router_lb_flow(). One flow is for specific matching
5595 * on ct.new with an action of "ct_lb($targets);". The other
5596 * flow is for ct.est with an action of "ct_dnat;". */
5598 ds_put_format(&actions
, "ct_lb(%s);", node
->value
);
5601 if (addr_family
== AF_INET
) {
5602 ds_put_format(&match
, "ip && ip4.dst == %s",
5605 ds_put_format(&match
, "ip && ip6.dst == %s",
5611 bool is_udp
= lb
->protocol
&& !strcmp(lb
->protocol
, "udp") ?
5615 ds_put_format(&match
, " && udp && udp.dst == %d",
5618 ds_put_format(&match
, " && tcp && tcp.dst == %d",
5624 if (od
->l3redirect_port
) {
5625 ds_put_format(&match
, " && is_chassis_resident(%s)",
5626 od
->l3redirect_port
->json_key
);
5628 add_router_lb_flow(lflows
, od
, &match
, &actions
, prio
,
5629 lb_force_snat_ip
, node
->value
, is_udp
,
5633 sset_destroy(&all_ips
);
5636 /* Logical router ingress table 5 and 6: IPv6 Router Adv (RA) options and
5638 HMAP_FOR_EACH (op
, key_node
, ports
) {
5639 if (!op
->nbrp
|| op
->nbrp
->peer
|| !op
->peer
) {
5643 if (!op
->lrp_networks
.n_ipv6_addrs
) {
5647 const char *address_mode
= smap_get(
5648 &op
->nbrp
->ipv6_ra_configs
, "address_mode");
5650 if (!address_mode
) {
5653 if (strcmp(address_mode
, "slaac") &&
5654 strcmp(address_mode
, "dhcpv6_stateful") &&
5655 strcmp(address_mode
, "dhcpv6_stateless")) {
5656 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5657 VLOG_WARN_RL(&rl
, "Invalid address mode [%s] defined",
5662 if (smap_get_bool(&op
->nbrp
->ipv6_ra_configs
, "send_periodic",
5664 copy_ra_to_sb(op
, address_mode
);
5668 ds_put_format(&match
, "inport == %s && ip6.dst == ff02::2 && nd_rs",
5672 const char *mtu_s
= smap_get(
5673 &op
->nbrp
->ipv6_ra_configs
, "mtu");
5675 /* As per RFC 2460, 1280 is minimum IPv6 MTU. */
5676 uint32_t mtu
= (mtu_s
&& atoi(mtu_s
) >= 1280) ? atoi(mtu_s
) : 0;
5678 ds_put_format(&actions
, REGBIT_ND_RA_OPTS_RESULT
" = put_nd_ra_opts("
5679 "addr_mode = \"%s\", slla = %s",
5680 address_mode
, op
->lrp_networks
.ea_s
);
5682 ds_put_format(&actions
, ", mtu = %u", mtu
);
5685 bool add_rs_response_flow
= false;
5687 for (size_t i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; i
++) {
5688 if (in6_is_lla(&op
->lrp_networks
.ipv6_addrs
[i
].network
)) {
5692 /* Add the prefix option if the address mode is slaac or
5693 * dhcpv6_stateless. */
5694 if (strcmp(address_mode
, "dhcpv6_stateful")) {
5695 ds_put_format(&actions
, ", prefix = %s/%u",
5696 op
->lrp_networks
.ipv6_addrs
[i
].network_s
,
5697 op
->lrp_networks
.ipv6_addrs
[i
].plen
);
5699 add_rs_response_flow
= true;
5702 if (add_rs_response_flow
) {
5703 ds_put_cstr(&actions
, "); next;");
5704 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_ND_RA_OPTIONS
, 50,
5705 ds_cstr(&match
), ds_cstr(&actions
));
5708 ds_put_format(&match
, "inport == %s && ip6.dst == ff02::2 && "
5709 "nd_ra && "REGBIT_ND_RA_OPTS_RESULT
, op
->json_key
);
5711 char ip6_str
[INET6_ADDRSTRLEN
+ 1];
5712 struct in6_addr lla
;
5713 in6_generate_lla(op
->lrp_networks
.ea
, &lla
);
5714 memset(ip6_str
, 0, sizeof(ip6_str
));
5715 ipv6_string_mapped(ip6_str
, &lla
);
5716 ds_put_format(&actions
, "eth.dst = eth.src; eth.src = %s; "
5717 "ip6.dst = ip6.src; ip6.src = %s; "
5718 "outport = inport; flags.loopback = 1; "
5720 op
->lrp_networks
.ea_s
, ip6_str
);
5721 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_IN_ND_RA_RESPONSE
, 50,
5722 ds_cstr(&match
), ds_cstr(&actions
));
5726 /* Logical router ingress table 5, 6: RS responder, by default goto next.
5728 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
5733 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ND_RA_OPTIONS
, 0, "1", "next;");
5734 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ND_RA_RESPONSE
, 0, "1", "next;");
5737 /* Logical router ingress table 7: IP Routing.
5739 * A packet that arrives at this table is an IP packet that should be
5740 * routed to the address in 'ip[46].dst'. This table sets outport to
5741 * the correct output port, eth.src to the output port's MAC
5742 * address, and '[xx]reg0' to the next-hop IP address (leaving
5743 * 'ip[46].dst', the packet’s final destination, unchanged), and
5744 * advances to the next table for ARP/ND resolution. */
5745 HMAP_FOR_EACH (op
, key_node
, ports
) {
5750 for (int i
= 0; i
< op
->lrp_networks
.n_ipv4_addrs
; i
++) {
5751 add_route(lflows
, op
, op
->lrp_networks
.ipv4_addrs
[i
].addr_s
,
5752 op
->lrp_networks
.ipv4_addrs
[i
].network_s
,
5753 op
->lrp_networks
.ipv4_addrs
[i
].plen
, NULL
, NULL
);
5756 for (int i
= 0; i
< op
->lrp_networks
.n_ipv6_addrs
; i
++) {
5757 add_route(lflows
, op
, op
->lrp_networks
.ipv6_addrs
[i
].addr_s
,
5758 op
->lrp_networks
.ipv6_addrs
[i
].network_s
,
5759 op
->lrp_networks
.ipv6_addrs
[i
].plen
, NULL
, NULL
);
5763 /* Convert the static routes to flows. */
5764 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
5769 for (int i
= 0; i
< od
->nbr
->n_static_routes
; i
++) {
5770 const struct nbrec_logical_router_static_route
*route
;
5772 route
= od
->nbr
->static_routes
[i
];
5773 build_static_route_flow(lflows
, od
, ports
, route
);
5777 /* XXX destination unreachable */
5779 /* Local router ingress table 8: ARP Resolution.
5781 * Any packet that reaches this table is an IP packet whose next-hop IP
5782 * address is in reg0. (ip4.dst is the final destination.) This table
5783 * resolves the IP address in reg0 into an output port in outport and an
5784 * Ethernet address in eth.dst. */
5785 HMAP_FOR_EACH (op
, key_node
, ports
) {
5786 if (op
->nbsp
&& !lsp_is_enabled(op
->nbsp
)) {
5791 /* This is a logical router port. If next-hop IP address in
5792 * '[xx]reg0' matches IP address of this router port, then
5793 * the packet is intended to eventually be sent to this
5794 * logical port. Set the destination mac address using this
5795 * port's mac address.
5797 * The packet is still in peer's logical pipeline. So the match
5798 * should be on peer's outport. */
5799 if (op
->peer
&& op
->nbrp
->peer
) {
5800 if (op
->lrp_networks
.n_ipv4_addrs
) {
5802 ds_put_format(&match
, "outport == %s && reg0 == ",
5803 op
->peer
->json_key
);
5804 op_put_v4_networks(&match
, op
, false);
5807 ds_put_format(&actions
, "eth.dst = %s; next;",
5808 op
->lrp_networks
.ea_s
);
5809 ovn_lflow_add(lflows
, op
->peer
->od
, S_ROUTER_IN_ARP_RESOLVE
,
5810 100, ds_cstr(&match
), ds_cstr(&actions
));
5813 if (op
->lrp_networks
.n_ipv6_addrs
) {
5815 ds_put_format(&match
, "outport == %s && xxreg0 == ",
5816 op
->peer
->json_key
);
5817 op_put_v6_networks(&match
, op
);
5820 ds_put_format(&actions
, "eth.dst = %s; next;",
5821 op
->lrp_networks
.ea_s
);
5822 ovn_lflow_add(lflows
, op
->peer
->od
, S_ROUTER_IN_ARP_RESOLVE
,
5823 100, ds_cstr(&match
), ds_cstr(&actions
));
5826 } else if (op
->od
->n_router_ports
&& strcmp(op
->nbsp
->type
, "router")) {
5827 /* This is a logical switch port that backs a VM or a container.
5828 * Extract its addresses. For each of the address, go through all
5829 * the router ports attached to the switch (to which this port
5830 * connects) and if the address in question is reachable from the
5831 * router port, add an ARP/ND entry in that router's pipeline. */
5833 for (size_t i
= 0; i
< op
->n_lsp_addrs
; i
++) {
5834 const char *ea_s
= op
->lsp_addrs
[i
].ea_s
;
5835 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv4_addrs
; j
++) {
5836 const char *ip_s
= op
->lsp_addrs
[i
].ipv4_addrs
[j
].addr_s
;
5837 for (size_t k
= 0; k
< op
->od
->n_router_ports
; k
++) {
5838 /* Get the Logical_Router_Port that the
5839 * Logical_Switch_Port is connected to, as
5841 const char *peer_name
= smap_get(
5842 &op
->od
->router_ports
[k
]->nbsp
->options
,
5848 struct ovn_port
*peer
= ovn_port_find(ports
, peer_name
);
5849 if (!peer
|| !peer
->nbrp
) {
5853 if (!find_lrp_member_ip(peer
, ip_s
)) {
5858 ds_put_format(&match
, "outport == %s && reg0 == %s",
5859 peer
->json_key
, ip_s
);
5862 ds_put_format(&actions
, "eth.dst = %s; next;", ea_s
);
5863 ovn_lflow_add(lflows
, peer
->od
,
5864 S_ROUTER_IN_ARP_RESOLVE
, 100,
5865 ds_cstr(&match
), ds_cstr(&actions
));
5869 for (size_t j
= 0; j
< op
->lsp_addrs
[i
].n_ipv6_addrs
; j
++) {
5870 const char *ip_s
= op
->lsp_addrs
[i
].ipv6_addrs
[j
].addr_s
;
5871 for (size_t k
= 0; k
< op
->od
->n_router_ports
; k
++) {
5872 /* Get the Logical_Router_Port that the
5873 * Logical_Switch_Port is connected to, as
5875 const char *peer_name
= smap_get(
5876 &op
->od
->router_ports
[k
]->nbsp
->options
,
5882 struct ovn_port
*peer
= ovn_port_find(ports
, peer_name
);
5883 if (!peer
|| !peer
->nbrp
) {
5887 if (!find_lrp_member_ip(peer
, ip_s
)) {
5892 ds_put_format(&match
, "outport == %s && xxreg0 == %s",
5893 peer
->json_key
, ip_s
);
5896 ds_put_format(&actions
, "eth.dst = %s; next;", ea_s
);
5897 ovn_lflow_add(lflows
, peer
->od
,
5898 S_ROUTER_IN_ARP_RESOLVE
, 100,
5899 ds_cstr(&match
), ds_cstr(&actions
));
5903 } else if (!strcmp(op
->nbsp
->type
, "router")) {
5904 /* This is a logical switch port that connects to a router. */
5906 /* The peer of this switch port is the router port for which
5907 * we need to add logical flows such that it can resolve
5908 * ARP entries for all the other router ports connected to
5909 * the switch in question. */
5911 const char *peer_name
= smap_get(&op
->nbsp
->options
,
5917 struct ovn_port
*peer
= ovn_port_find(ports
, peer_name
);
5918 if (!peer
|| !peer
->nbrp
) {
5922 for (size_t i
= 0; i
< op
->od
->n_router_ports
; i
++) {
5923 const char *router_port_name
= smap_get(
5924 &op
->od
->router_ports
[i
]->nbsp
->options
,
5926 struct ovn_port
*router_port
= ovn_port_find(ports
,
5928 if (!router_port
|| !router_port
->nbrp
) {
5932 /* Skip the router port under consideration. */
5933 if (router_port
== peer
) {
5937 if (router_port
->lrp_networks
.n_ipv4_addrs
) {
5939 ds_put_format(&match
, "outport == %s && reg0 == ",
5941 op_put_v4_networks(&match
, router_port
, false);
5944 ds_put_format(&actions
, "eth.dst = %s; next;",
5945 router_port
->lrp_networks
.ea_s
);
5946 ovn_lflow_add(lflows
, peer
->od
, S_ROUTER_IN_ARP_RESOLVE
,
5947 100, ds_cstr(&match
), ds_cstr(&actions
));
5950 if (router_port
->lrp_networks
.n_ipv6_addrs
) {
5952 ds_put_format(&match
, "outport == %s && xxreg0 == ",
5954 op_put_v6_networks(&match
, router_port
);
5957 ds_put_format(&actions
, "eth.dst = %s; next;",
5958 router_port
->lrp_networks
.ea_s
);
5959 ovn_lflow_add(lflows
, peer
->od
, S_ROUTER_IN_ARP_RESOLVE
,
5960 100, ds_cstr(&match
), ds_cstr(&actions
));
5966 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
5971 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_RESOLVE
, 0, "ip4",
5972 "get_arp(outport, reg0); next;");
5974 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_RESOLVE
, 0, "ip6",
5975 "get_nd(outport, xxreg0); next;");
5978 /* Logical router ingress table 9: Gateway redirect.
5980 * For traffic with outport equal to the l3dgw_port
5981 * on a distributed router, this table redirects a subset
5982 * of the traffic to the l3redirect_port which represents
5983 * the central instance of the l3dgw_port.
5985 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
5989 if (od
->l3dgw_port
&& od
->l3redirect_port
) {
5990 /* For traffic with outport == l3dgw_port, if the
5991 * packet did not match any higher priority redirect
5992 * rule, then the traffic is redirected to the central
5993 * instance of the l3dgw_port. */
5995 ds_put_format(&match
, "outport == %s",
5996 od
->l3dgw_port
->json_key
);
5998 ds_put_format(&actions
, "outport = %s; next;",
5999 od
->l3redirect_port
->json_key
);
6000 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_GW_REDIRECT
, 50,
6001 ds_cstr(&match
), ds_cstr(&actions
));
6003 /* If the Ethernet destination has not been resolved,
6004 * redirect to the central instance of the l3dgw_port.
6005 * Such traffic will be replaced by an ARP request or ND
6006 * Neighbor Solicitation in the ARP request ingress
6007 * table, before being redirected to the central instance.
6009 ds_put_format(&match
, " && eth.dst == 00:00:00:00:00:00");
6010 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_GW_REDIRECT
, 150,
6011 ds_cstr(&match
), ds_cstr(&actions
));
6014 /* Packets are allowed by default. */
6015 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_GW_REDIRECT
, 0, "1", "next;");
6018 /* Local router ingress table 10: ARP request.
6020 * In the common case where the Ethernet destination has been resolved,
6021 * this table outputs the packet (priority 0). Otherwise, it composes
6022 * and sends an ARP/IPv6 NA request (priority 100). */
6023 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
6028 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_REQUEST
, 100,
6029 "eth.dst == 00:00:00:00:00:00",
6031 "eth.dst = ff:ff:ff:ff:ff:ff; "
6034 "arp.op = 1; " /* ARP request */
6037 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_REQUEST
, 100,
6038 "eth.dst == 00:00:00:00:00:00",
6040 "nd.target = xxreg0; "
6043 ovn_lflow_add(lflows
, od
, S_ROUTER_IN_ARP_REQUEST
, 0, "1", "output;");
6046 /* Logical router egress table 1: Delivery (priority 100).
6048 * Priority 100 rules deliver packets to enabled logical ports. */
6049 HMAP_FOR_EACH (op
, key_node
, ports
) {
6054 if (!lrport_is_enabled(op
->nbrp
)) {
6055 /* Drop packets to disabled logical ports (since logical flow
6056 * tables are default-drop). */
6061 /* No egress packets should be processed in the context of
6062 * a chassisredirect port. The chassisredirect port should
6063 * be replaced by the l3dgw port in the local output
6064 * pipeline stage before egress processing. */
6069 ds_put_format(&match
, "outport == %s", op
->json_key
);
6070 ovn_lflow_add(lflows
, op
->od
, S_ROUTER_OUT_DELIVERY
, 100,
6071 ds_cstr(&match
), "output;");
6075 ds_destroy(&actions
);
6078 /* Updates the Logical_Flow and Multicast_Group tables in the OVN_SB database,
6079 * constructing their contents based on the OVN_NB database. */
6081 build_lflows(struct northd_context
*ctx
, struct hmap
*datapaths
,
6084 struct hmap lflows
= HMAP_INITIALIZER(&lflows
);
6085 struct hmap mcgroups
= HMAP_INITIALIZER(&mcgroups
);
6087 build_lswitch_flows(datapaths
, ports
, &lflows
, &mcgroups
);
6088 build_lrouter_flows(datapaths
, ports
, &lflows
);
6090 /* Push changes to the Logical_Flow table to database. */
6091 const struct sbrec_logical_flow
*sbflow
, *next_sbflow
;
6092 SBREC_LOGICAL_FLOW_FOR_EACH_SAFE (sbflow
, next_sbflow
, ctx
->ovnsb_idl
) {
6093 struct ovn_datapath
*od
6094 = ovn_datapath_from_sbrec(datapaths
, sbflow
->logical_datapath
);
6096 sbrec_logical_flow_delete(sbflow
);
6100 enum ovn_datapath_type dp_type
= od
->nbs
? DP_SWITCH
: DP_ROUTER
;
6101 enum ovn_pipeline pipeline
6102 = !strcmp(sbflow
->pipeline
, "ingress") ? P_IN
: P_OUT
;
6103 struct ovn_lflow
*lflow
= ovn_lflow_find(
6104 &lflows
, od
, ovn_stage_build(dp_type
, pipeline
, sbflow
->table_id
),
6105 sbflow
->priority
, sbflow
->match
, sbflow
->actions
, sbflow
->hash
);
6107 ovn_lflow_destroy(&lflows
, lflow
);
6109 sbrec_logical_flow_delete(sbflow
);
6112 struct ovn_lflow
*lflow
, *next_lflow
;
6113 HMAP_FOR_EACH_SAFE (lflow
, next_lflow
, hmap_node
, &lflows
) {
6114 const char *pipeline
= ovn_stage_get_pipeline_name(lflow
->stage
);
6115 uint8_t table
= ovn_stage_get_table(lflow
->stage
);
6117 sbflow
= sbrec_logical_flow_insert(ctx
->ovnsb_txn
);
6118 sbrec_logical_flow_set_logical_datapath(sbflow
, lflow
->od
->sb
);
6119 sbrec_logical_flow_set_pipeline(sbflow
, pipeline
);
6120 sbrec_logical_flow_set_table_id(sbflow
, table
);
6121 sbrec_logical_flow_set_priority(sbflow
, lflow
->priority
);
6122 sbrec_logical_flow_set_match(sbflow
, lflow
->match
);
6123 sbrec_logical_flow_set_actions(sbflow
, lflow
->actions
);
6125 /* Trim the source locator lflow->where, which looks something like
6126 * "ovn/northd/ovn-northd.c:1234", down to just the part following the
6127 * last slash, e.g. "ovn-northd.c:1234". */
6128 const char *slash
= strrchr(lflow
->where
, '/');
6130 const char *backslash
= strrchr(lflow
->where
, '\\');
6131 if (!slash
|| backslash
> slash
) {
6135 const char *where
= slash
? slash
+ 1 : lflow
->where
;
6137 struct smap ids
= SMAP_INITIALIZER(&ids
);
6138 smap_add(&ids
, "stage-name", ovn_stage_to_str(lflow
->stage
));
6139 smap_add(&ids
, "source", where
);
6140 if (lflow
->stage_hint
) {
6141 smap_add(&ids
, "stage-hint", lflow
->stage_hint
);
6143 sbrec_logical_flow_set_external_ids(sbflow
, &ids
);
6146 ovn_lflow_destroy(&lflows
, lflow
);
6148 hmap_destroy(&lflows
);
6150 /* Push changes to the Multicast_Group table to database. */
6151 const struct sbrec_multicast_group
*sbmc
, *next_sbmc
;
6152 SBREC_MULTICAST_GROUP_FOR_EACH_SAFE (sbmc
, next_sbmc
, ctx
->ovnsb_idl
) {
6153 struct ovn_datapath
*od
= ovn_datapath_from_sbrec(datapaths
,
6156 sbrec_multicast_group_delete(sbmc
);
6160 struct multicast_group group
= { .name
= sbmc
->name
,
6161 .key
= sbmc
->tunnel_key
};
6162 struct ovn_multicast
*mc
= ovn_multicast_find(&mcgroups
, od
, &group
);
6164 ovn_multicast_update_sbrec(mc
, sbmc
);
6165 ovn_multicast_destroy(&mcgroups
, mc
);
6167 sbrec_multicast_group_delete(sbmc
);
6170 struct ovn_multicast
*mc
, *next_mc
;
6171 HMAP_FOR_EACH_SAFE (mc
, next_mc
, hmap_node
, &mcgroups
) {
6172 sbmc
= sbrec_multicast_group_insert(ctx
->ovnsb_txn
);
6173 sbrec_multicast_group_set_datapath(sbmc
, mc
->datapath
->sb
);
6174 sbrec_multicast_group_set_name(sbmc
, mc
->group
->name
);
6175 sbrec_multicast_group_set_tunnel_key(sbmc
, mc
->group
->key
);
6176 ovn_multicast_update_sbrec(mc
, sbmc
);
6177 ovn_multicast_destroy(&mcgroups
, mc
);
6179 hmap_destroy(&mcgroups
);
6183 sync_address_set(struct northd_context
*ctx
, const char *name
,
6184 const char **addrs
, size_t n_addrs
,
6185 struct shash
*sb_address_sets
)
6187 const struct sbrec_address_set
*sb_address_set
;
6188 sb_address_set
= shash_find_and_delete(sb_address_sets
,
6190 if (!sb_address_set
) {
6191 sb_address_set
= sbrec_address_set_insert(ctx
->ovnsb_txn
);
6192 sbrec_address_set_set_name(sb_address_set
, name
);
6195 sbrec_address_set_set_addresses(sb_address_set
,
6199 /* OVN_Southbound Address_Set table contains same records as in north
6200 * bound, plus the records generated from Port_Group table in north bound.
6202 * There are 2 records generated from each port group, one for IPv4, and
6203 * one for IPv6, named in the format: <port group name>_ip4 and
6204 * <port group name>_ip6 respectively. MAC addresses are ignored.
6206 * We always update OVN_Southbound to match the Address_Set and Port_Group
6207 * in OVN_Northbound, so that the address sets used in Logical_Flows in
6208 * OVN_Southbound is checked against the proper set.*/
6210 sync_address_sets(struct northd_context
*ctx
)
6212 struct shash sb_address_sets
= SHASH_INITIALIZER(&sb_address_sets
);
6214 const struct sbrec_address_set
*sb_address_set
;
6215 SBREC_ADDRESS_SET_FOR_EACH (sb_address_set
, ctx
->ovnsb_idl
) {
6216 shash_add(&sb_address_sets
, sb_address_set
->name
, sb_address_set
);
6219 /* sync port group generated address sets first */
6220 const struct nbrec_port_group
*nb_port_group
;
6221 NBREC_PORT_GROUP_FOR_EACH (nb_port_group
, ctx
->ovnnb_idl
) {
6222 char **ipv4_addrs
= xcalloc(1, sizeof *ipv4_addrs
);
6223 size_t n_ipv4_addrs
= 0;
6224 size_t n_ipv4_addrs_buf
= 1;
6225 char **ipv6_addrs
= xcalloc(1, sizeof *ipv6_addrs
);
6226 size_t n_ipv6_addrs
= 0;
6227 size_t n_ipv6_addrs_buf
= 1;
6228 for (size_t i
= 0; i
< nb_port_group
->n_ports
; i
++) {
6229 for (size_t j
= 0; j
< nb_port_group
->ports
[i
]->n_addresses
; j
++) {
6230 struct lport_addresses laddrs
;
6231 extract_lsp_addresses(nb_port_group
->ports
[i
]->addresses
[j
],
6233 while (n_ipv4_addrs_buf
< n_ipv4_addrs
+ laddrs
.n_ipv4_addrs
) {
6234 n_ipv4_addrs_buf
*= 2;
6235 ipv4_addrs
= xrealloc(ipv4_addrs
,
6236 n_ipv4_addrs_buf
* sizeof *ipv4_addrs
);
6238 for (size_t k
= 0; k
< laddrs
.n_ipv4_addrs
; k
++) {
6239 ipv4_addrs
[n_ipv4_addrs
++] =
6240 xstrdup(laddrs
.ipv4_addrs
[k
].addr_s
);
6242 while (n_ipv6_addrs_buf
< n_ipv6_addrs
+ laddrs
.n_ipv6_addrs
) {
6243 n_ipv6_addrs_buf
*= 2;
6244 ipv6_addrs
= xrealloc(ipv6_addrs
,
6245 n_ipv6_addrs_buf
* sizeof *ipv6_addrs
);
6247 for (size_t k
= 0; k
< laddrs
.n_ipv6_addrs
; k
++) {
6248 ipv6_addrs
[n_ipv6_addrs
++] =
6249 xstrdup(laddrs
.ipv6_addrs
[k
].addr_s
);
6251 destroy_lport_addresses(&laddrs
);
6254 char *ipv4_addrs_name
= xasprintf("%s_ip4", nb_port_group
->name
);
6255 char *ipv6_addrs_name
= xasprintf("%s_ip6", nb_port_group
->name
);
6256 sync_address_set(ctx
, ipv4_addrs_name
, (const char **)ipv4_addrs
,
6257 n_ipv4_addrs
, &sb_address_sets
);
6258 sync_address_set(ctx
, ipv6_addrs_name
, (const char **)ipv6_addrs
,
6259 n_ipv6_addrs
, &sb_address_sets
);
6260 free(ipv4_addrs_name
);
6261 free(ipv6_addrs_name
);
6262 for (size_t i
= 0; i
< n_ipv4_addrs
; i
++) {
6263 free(ipv4_addrs
[i
]);
6266 for (size_t i
= 0; i
< n_ipv6_addrs
; i
++) {
6267 free(ipv6_addrs
[i
]);
6272 /* sync user defined address sets, which may overwrite port group
6273 * generated address sets if same name is used */
6274 const struct nbrec_address_set
*nb_address_set
;
6275 NBREC_ADDRESS_SET_FOR_EACH (nb_address_set
, ctx
->ovnnb_idl
) {
6276 sync_address_set(ctx
, nb_address_set
->name
,
6277 /* "char **" is not compatible with "const char **" */
6278 (const char **)nb_address_set
->addresses
,
6279 nb_address_set
->n_addresses
, &sb_address_sets
);
6282 struct shash_node
*node
, *next
;
6283 SHASH_FOR_EACH_SAFE (node
, next
, &sb_address_sets
) {
6284 sbrec_address_set_delete(node
->data
);
6285 shash_delete(&sb_address_sets
, node
);
6287 shash_destroy(&sb_address_sets
);
6290 /* Each port group in Port_Group table in OVN_Northbound has a corresponding
6291 * entry in Port_Group table in OVN_Southbound. In OVN_Northbound the entries
6292 * contains lport uuids, while in OVN_Southbound we store the lport names.
6295 sync_port_groups(struct northd_context
*ctx
)
6297 struct shash sb_port_groups
= SHASH_INITIALIZER(&sb_port_groups
);
6299 const struct sbrec_port_group
*sb_port_group
;
6300 SBREC_PORT_GROUP_FOR_EACH (sb_port_group
, ctx
->ovnsb_idl
) {
6301 shash_add(&sb_port_groups
, sb_port_group
->name
, sb_port_group
);
6304 const struct nbrec_port_group
*nb_port_group
;
6305 NBREC_PORT_GROUP_FOR_EACH (nb_port_group
, ctx
->ovnnb_idl
) {
6306 sb_port_group
= shash_find_and_delete(&sb_port_groups
,
6307 nb_port_group
->name
);
6308 if (!sb_port_group
) {
6309 sb_port_group
= sbrec_port_group_insert(ctx
->ovnsb_txn
);
6310 sbrec_port_group_set_name(sb_port_group
, nb_port_group
->name
);
6313 const char **nb_port_names
= xcalloc(nb_port_group
->n_ports
,
6314 sizeof *nb_port_names
);
6316 for (i
= 0; i
< nb_port_group
->n_ports
; i
++) {
6317 nb_port_names
[i
] = nb_port_group
->ports
[i
]->name
;
6319 sbrec_port_group_set_ports(sb_port_group
,
6321 nb_port_group
->n_ports
);
6322 free(nb_port_names
);
6325 struct shash_node
*node
, *next
;
6326 SHASH_FOR_EACH_SAFE (node
, next
, &sb_port_groups
) {
6327 sbrec_port_group_delete(node
->data
);
6328 shash_delete(&sb_port_groups
, node
);
6330 shash_destroy(&sb_port_groups
);
6334 * struct 'dns_info' is used to sync the DNS records between OVN Northbound db
6335 * and Southbound db.
6338 struct hmap_node hmap_node
;
6339 const struct nbrec_dns
*nb_dns
; /* DNS record in the Northbound db. */
6340 const struct sbrec_dns
*sb_dns
; /* DNS record in the Soutbound db. */
6342 /* Datapaths to which the DNS entry is associated with it. */
6343 const struct sbrec_datapath_binding
**sbs
;
6347 static inline struct dns_info
*
6348 get_dns_info_from_hmap(struct hmap
*dns_map
, struct uuid
*uuid
)
6350 struct dns_info
*dns_info
;
6351 size_t hash
= uuid_hash(uuid
);
6352 HMAP_FOR_EACH_WITH_HASH (dns_info
, hmap_node
, hash
, dns_map
) {
6353 if (uuid_equals(&dns_info
->nb_dns
->header_
.uuid
, uuid
)) {
6362 sync_dns_entries(struct northd_context
*ctx
, struct hmap
*datapaths
)
6364 struct hmap dns_map
= HMAP_INITIALIZER(&dns_map
);
6365 struct ovn_datapath
*od
;
6366 HMAP_FOR_EACH (od
, key_node
, datapaths
) {
6367 if (!od
->nbs
|| !od
->nbs
->n_dns_records
) {
6371 for (size_t i
= 0; i
< od
->nbs
->n_dns_records
; i
++) {
6372 struct dns_info
*dns_info
= get_dns_info_from_hmap(
6373 &dns_map
, &od
->nbs
->dns_records
[i
]->header_
.uuid
);
6375 size_t hash
= uuid_hash(
6376 &od
->nbs
->dns_records
[i
]->header_
.uuid
);
6377 dns_info
= xzalloc(sizeof *dns_info
);;
6378 dns_info
->nb_dns
= od
->nbs
->dns_records
[i
];
6379 hmap_insert(&dns_map
, &dns_info
->hmap_node
, hash
);
6383 dns_info
->sbs
= xrealloc(dns_info
->sbs
,
6384 dns_info
->n_sbs
* sizeof *dns_info
->sbs
);
6385 dns_info
->sbs
[dns_info
->n_sbs
- 1] = od
->sb
;
6389 const struct sbrec_dns
*sbrec_dns
, *next
;
6390 SBREC_DNS_FOR_EACH_SAFE (sbrec_dns
, next
, ctx
->ovnsb_idl
) {
6391 const char *nb_dns_uuid
= smap_get(&sbrec_dns
->external_ids
, "dns_id");
6392 struct uuid dns_uuid
;
6393 if (!nb_dns_uuid
|| !uuid_from_string(&dns_uuid
, nb_dns_uuid
)) {
6394 sbrec_dns_delete(sbrec_dns
);
6398 struct dns_info
*dns_info
=
6399 get_dns_info_from_hmap(&dns_map
, &dns_uuid
);
6401 dns_info
->sb_dns
= sbrec_dns
;
6403 sbrec_dns_delete(sbrec_dns
);
6407 struct dns_info
*dns_info
;
6408 HMAP_FOR_EACH_POP (dns_info
, hmap_node
, &dns_map
) {
6409 if (!dns_info
->sb_dns
) {
6410 sbrec_dns
= sbrec_dns_insert(ctx
->ovnsb_txn
);
6411 dns_info
->sb_dns
= sbrec_dns
;
6412 char *dns_id
= xasprintf(
6413 UUID_FMT
, UUID_ARGS(&dns_info
->nb_dns
->header_
.uuid
));
6414 const struct smap external_ids
=
6415 SMAP_CONST1(&external_ids
, "dns_id", dns_id
);
6416 sbrec_dns_set_external_ids(sbrec_dns
, &external_ids
);
6420 /* Set the datapaths and records. If nothing has changed, then
6421 * this will be a no-op.
6423 sbrec_dns_set_datapaths(
6425 (struct sbrec_datapath_binding
**)dns_info
->sbs
,
6427 sbrec_dns_set_records(dns_info
->sb_dns
, &dns_info
->nb_dns
->records
);
6428 free(dns_info
->sbs
);
6431 hmap_destroy(&dns_map
);
6436 ovnnb_db_run(struct northd_context
*ctx
, struct chassis_index
*chassis_index
,
6437 struct ovsdb_idl_loop
*sb_loop
)
6439 if (!ctx
->ovnsb_txn
|| !ctx
->ovnnb_txn
) {
6442 struct hmap datapaths
, ports
;
6443 build_datapaths(ctx
, &datapaths
);
6444 build_ports(ctx
, &datapaths
, chassis_index
, &ports
);
6445 build_ipam(&datapaths
, &ports
);
6446 build_lflows(ctx
, &datapaths
, &ports
);
6448 sync_address_sets(ctx
);
6449 sync_port_groups(ctx
);
6450 sync_dns_entries(ctx
, &datapaths
);
6452 struct ovn_datapath
*dp
, *next_dp
;
6453 HMAP_FOR_EACH_SAFE (dp
, next_dp
, key_node
, &datapaths
) {
6454 ovn_datapath_destroy(&datapaths
, dp
);
6456 hmap_destroy(&datapaths
);
6458 struct ovn_port
*port
, *next_port
;
6459 HMAP_FOR_EACH_SAFE (port
, next_port
, key_node
, &ports
) {
6460 ovn_port_destroy(&ports
, port
);
6462 hmap_destroy(&ports
);
6464 /* Copy nb_cfg from northbound to southbound database.
6466 * Also set up to update sb_cfg once our southbound transaction commits. */
6467 const struct nbrec_nb_global
*nb
= nbrec_nb_global_first(ctx
->ovnnb_idl
);
6469 nb
= nbrec_nb_global_insert(ctx
->ovnnb_txn
);
6471 const struct sbrec_sb_global
*sb
= sbrec_sb_global_first(ctx
->ovnsb_idl
);
6473 sb
= sbrec_sb_global_insert(ctx
->ovnsb_txn
);
6475 sbrec_sb_global_set_nb_cfg(sb
, nb
->nb_cfg
);
6476 sb_loop
->next_cfg
= nb
->nb_cfg
;
6478 cleanup_macam(&macam
);
6481 /* Handle changes to the 'chassis' column of the 'Port_Binding' table. When
6482 * this column is not empty, it means we need to set the corresponding logical
6483 * port as 'up' in the northbound DB. */
6485 update_logical_port_status(struct northd_context
*ctx
)
6487 struct hmap lports_hmap
;
6488 const struct sbrec_port_binding
*sb
;
6489 const struct nbrec_logical_switch_port
*nbsp
;
6491 struct lport_hash_node
{
6492 struct hmap_node node
;
6493 const struct nbrec_logical_switch_port
*nbsp
;
6496 hmap_init(&lports_hmap
);
6498 NBREC_LOGICAL_SWITCH_PORT_FOR_EACH(nbsp
, ctx
->ovnnb_idl
) {
6499 hash_node
= xzalloc(sizeof *hash_node
);
6500 hash_node
->nbsp
= nbsp
;
6501 hmap_insert(&lports_hmap
, &hash_node
->node
, hash_string(nbsp
->name
, 0));
6504 SBREC_PORT_BINDING_FOR_EACH(sb
, ctx
->ovnsb_idl
) {
6506 HMAP_FOR_EACH_WITH_HASH(hash_node
, node
,
6507 hash_string(sb
->logical_port
, 0),
6509 if (!strcmp(sb
->logical_port
, hash_node
->nbsp
->name
)) {
6510 nbsp
= hash_node
->nbsp
;
6516 /* The logical port doesn't exist for this port binding. This can
6517 * happen under normal circumstances when ovn-northd hasn't gotten
6518 * around to pruning the Port_Binding yet. */
6522 bool up
= (sb
->chassis
|| !strcmp(nbsp
->type
, "router"));
6523 if (!nbsp
->up
|| *nbsp
->up
!= up
) {
6524 nbrec_logical_switch_port_set_up(nbsp
, &up
, 1);
6528 HMAP_FOR_EACH_POP(hash_node
, node
, &lports_hmap
) {
6531 hmap_destroy(&lports_hmap
);
6534 static struct gen_opts_map supported_dhcp_opts
[] = {
6538 DHCP_OPT_DNS_SERVER
,
6539 DHCP_OPT_LOG_SERVER
,
6540 DHCP_OPT_LPR_SERVER
,
6541 DHCP_OPT_SWAP_SERVER
,
6542 DHCP_OPT_POLICY_FILTER
,
6543 DHCP_OPT_ROUTER_SOLICITATION
,
6544 DHCP_OPT_NIS_SERVER
,
6545 DHCP_OPT_NTP_SERVER
,
6547 DHCP_OPT_TFTP_SERVER
,
6548 DHCP_OPT_CLASSLESS_STATIC_ROUTE
,
6549 DHCP_OPT_MS_CLASSLESS_STATIC_ROUTE
,
6550 DHCP_OPT_IP_FORWARD_ENABLE
,
6551 DHCP_OPT_ROUTER_DISCOVERY
,
6552 DHCP_OPT_ETHERNET_ENCAP
,
6553 DHCP_OPT_DEFAULT_TTL
,
6556 DHCP_OPT_LEASE_TIME
,
6561 static struct gen_opts_map supported_dhcpv6_opts
[] = {
6563 DHCPV6_OPT_SERVER_ID
,
6564 DHCPV6_OPT_DOMAIN_SEARCH
,
6565 DHCPV6_OPT_DNS_SERVER
6569 check_and_add_supported_dhcp_opts_to_sb_db(struct northd_context
*ctx
)
6571 struct hmap dhcp_opts_to_add
= HMAP_INITIALIZER(&dhcp_opts_to_add
);
6572 for (size_t i
= 0; (i
< sizeof(supported_dhcp_opts
) /
6573 sizeof(supported_dhcp_opts
[0])); i
++) {
6574 hmap_insert(&dhcp_opts_to_add
, &supported_dhcp_opts
[i
].hmap_node
,
6575 dhcp_opt_hash(supported_dhcp_opts
[i
].name
));
6578 const struct sbrec_dhcp_options
*opt_row
, *opt_row_next
;
6579 SBREC_DHCP_OPTIONS_FOR_EACH_SAFE(opt_row
, opt_row_next
, ctx
->ovnsb_idl
) {
6580 struct gen_opts_map
*dhcp_opt
=
6581 dhcp_opts_find(&dhcp_opts_to_add
, opt_row
->name
);
6583 hmap_remove(&dhcp_opts_to_add
, &dhcp_opt
->hmap_node
);
6585 sbrec_dhcp_options_delete(opt_row
);
6589 struct gen_opts_map
*opt
;
6590 HMAP_FOR_EACH (opt
, hmap_node
, &dhcp_opts_to_add
) {
6591 struct sbrec_dhcp_options
*sbrec_dhcp_option
=
6592 sbrec_dhcp_options_insert(ctx
->ovnsb_txn
);
6593 sbrec_dhcp_options_set_name(sbrec_dhcp_option
, opt
->name
);
6594 sbrec_dhcp_options_set_code(sbrec_dhcp_option
, opt
->code
);
6595 sbrec_dhcp_options_set_type(sbrec_dhcp_option
, opt
->type
);
6598 hmap_destroy(&dhcp_opts_to_add
);
6602 check_and_add_supported_dhcpv6_opts_to_sb_db(struct northd_context
*ctx
)
6604 struct hmap dhcpv6_opts_to_add
= HMAP_INITIALIZER(&dhcpv6_opts_to_add
);
6605 for (size_t i
= 0; (i
< sizeof(supported_dhcpv6_opts
) /
6606 sizeof(supported_dhcpv6_opts
[0])); i
++) {
6607 hmap_insert(&dhcpv6_opts_to_add
, &supported_dhcpv6_opts
[i
].hmap_node
,
6608 dhcp_opt_hash(supported_dhcpv6_opts
[i
].name
));
6611 const struct sbrec_dhcpv6_options
*opt_row
, *opt_row_next
;
6612 SBREC_DHCPV6_OPTIONS_FOR_EACH_SAFE(opt_row
, opt_row_next
, ctx
->ovnsb_idl
) {
6613 struct gen_opts_map
*dhcp_opt
=
6614 dhcp_opts_find(&dhcpv6_opts_to_add
, opt_row
->name
);
6616 hmap_remove(&dhcpv6_opts_to_add
, &dhcp_opt
->hmap_node
);
6618 sbrec_dhcpv6_options_delete(opt_row
);
6622 struct gen_opts_map
*opt
;
6623 HMAP_FOR_EACH(opt
, hmap_node
, &dhcpv6_opts_to_add
) {
6624 struct sbrec_dhcpv6_options
*sbrec_dhcpv6_option
=
6625 sbrec_dhcpv6_options_insert(ctx
->ovnsb_txn
);
6626 sbrec_dhcpv6_options_set_name(sbrec_dhcpv6_option
, opt
->name
);
6627 sbrec_dhcpv6_options_set_code(sbrec_dhcpv6_option
, opt
->code
);
6628 sbrec_dhcpv6_options_set_type(sbrec_dhcpv6_option
, opt
->type
);
6631 hmap_destroy(&dhcpv6_opts_to_add
);
6634 static const char *rbac_chassis_auth
[] =
6636 static const char *rbac_chassis_update
[] =
6637 {"nb_cfg", "external_ids", "encaps", "vtep_logical_switches"};
6639 static const char *rbac_encap_auth
[] =
6641 static const char *rbac_encap_update
[] =
6642 {"type", "options", "ip"};
6644 static const char *rbac_port_binding_auth
[] =
6646 static const char *rbac_port_binding_update
[] =
6649 static const char *rbac_mac_binding_auth
[] =
6651 static const char *rbac_mac_binding_update
[] =
6652 {"logical_port", "ip", "mac", "datapath"};
6654 static struct rbac_perm_cfg
{
6659 const char **update
;
6661 const struct sbrec_rbac_permission
*row
;
6662 } rbac_perm_cfg
[] = {
6665 .auth
= rbac_chassis_auth
,
6666 .n_auth
= ARRAY_SIZE(rbac_chassis_auth
),
6668 .update
= rbac_chassis_update
,
6669 .n_update
= ARRAY_SIZE(rbac_chassis_update
),
6673 .auth
= rbac_encap_auth
,
6674 .n_auth
= ARRAY_SIZE(rbac_encap_auth
),
6676 .update
= rbac_encap_update
,
6677 .n_update
= ARRAY_SIZE(rbac_encap_update
),
6680 .table
= "Port_Binding",
6681 .auth
= rbac_port_binding_auth
,
6682 .n_auth
= ARRAY_SIZE(rbac_port_binding_auth
),
6684 .update
= rbac_port_binding_update
,
6685 .n_update
= ARRAY_SIZE(rbac_port_binding_update
),
6688 .table
= "MAC_Binding",
6689 .auth
= rbac_mac_binding_auth
,
6690 .n_auth
= ARRAY_SIZE(rbac_mac_binding_auth
),
6692 .update
= rbac_mac_binding_update
,
6693 .n_update
= ARRAY_SIZE(rbac_mac_binding_update
),
6707 ovn_rbac_validate_perm(const struct sbrec_rbac_permission
*perm
)
6709 struct rbac_perm_cfg
*pcfg
;
6712 for (pcfg
= rbac_perm_cfg
; pcfg
->table
; pcfg
++) {
6713 if (!strcmp(perm
->table
, pcfg
->table
)) {
6720 if (perm
->n_authorization
!= pcfg
->n_auth
||
6721 perm
->n_update
!= pcfg
->n_update
) {
6724 if (perm
->insert_delete
!= pcfg
->insdel
) {
6727 /* verify perm->authorization vs. pcfg->auth */
6729 for (i
= 0; i
< pcfg
->n_auth
; i
++) {
6730 for (j
= 0; j
< perm
->n_authorization
; j
++) {
6731 if (!strcmp(pcfg
->auth
[i
], perm
->authorization
[j
])) {
6737 if (n_found
!= pcfg
->n_auth
) {
6741 /* verify perm->update vs. pcfg->update */
6743 for (i
= 0; i
< pcfg
->n_update
; i
++) {
6744 for (j
= 0; j
< perm
->n_update
; j
++) {
6745 if (!strcmp(pcfg
->update
[i
], perm
->update
[j
])) {
6751 if (n_found
!= pcfg
->n_update
) {
6755 /* Success, db state matches expected state */
6761 ovn_rbac_create_perm(struct rbac_perm_cfg
*pcfg
,
6762 struct northd_context
*ctx
,
6763 const struct sbrec_rbac_role
*rbac_role
)
6765 struct sbrec_rbac_permission
*rbac_perm
;
6767 rbac_perm
= sbrec_rbac_permission_insert(ctx
->ovnsb_txn
);
6768 sbrec_rbac_permission_set_table(rbac_perm
, pcfg
->table
);
6769 sbrec_rbac_permission_set_authorization(rbac_perm
,
6772 sbrec_rbac_permission_set_insert_delete(rbac_perm
, pcfg
->insdel
);
6773 sbrec_rbac_permission_set_update(rbac_perm
,
6776 sbrec_rbac_role_update_permissions_setkey(rbac_role
, pcfg
->table
,
6781 check_and_update_rbac(struct northd_context
*ctx
)
6783 const struct sbrec_rbac_role
*rbac_role
= NULL
;
6784 const struct sbrec_rbac_permission
*perm_row
, *perm_next
;
6785 const struct sbrec_rbac_role
*role_row
, *role_row_next
;
6786 struct rbac_perm_cfg
*pcfg
;
6788 for (pcfg
= rbac_perm_cfg
; pcfg
->table
; pcfg
++) {
6792 SBREC_RBAC_PERMISSION_FOR_EACH_SAFE (perm_row
, perm_next
, ctx
->ovnsb_idl
) {
6793 if (!ovn_rbac_validate_perm(perm_row
)) {
6794 sbrec_rbac_permission_delete(perm_row
);
6797 SBREC_RBAC_ROLE_FOR_EACH_SAFE (role_row
, role_row_next
, ctx
->ovnsb_idl
) {
6798 if (strcmp(role_row
->name
, "ovn-controller")) {
6799 sbrec_rbac_role_delete(role_row
);
6801 rbac_role
= role_row
;
6806 rbac_role
= sbrec_rbac_role_insert(ctx
->ovnsb_txn
);
6807 sbrec_rbac_role_set_name(rbac_role
, "ovn-controller");
6810 for (pcfg
= rbac_perm_cfg
; pcfg
->table
; pcfg
++) {
6812 ovn_rbac_create_perm(pcfg
, ctx
, rbac_role
);
6817 /* Updates the sb_cfg and hv_cfg columns in the northbound NB_Global table. */
6819 update_northbound_cfg(struct northd_context
*ctx
,
6820 struct ovsdb_idl_loop
*sb_loop
)
6822 /* Update northbound sb_cfg if appropriate. */
6823 const struct nbrec_nb_global
*nbg
= nbrec_nb_global_first(ctx
->ovnnb_idl
);
6824 int64_t sb_cfg
= sb_loop
->cur_cfg
;
6825 if (nbg
&& sb_cfg
&& nbg
->sb_cfg
!= sb_cfg
) {
6826 nbrec_nb_global_set_sb_cfg(nbg
, sb_cfg
);
6829 /* Update northbound hv_cfg if appropriate. */
6831 /* Find minimum nb_cfg among all chassis. */
6832 const struct sbrec_chassis
*chassis
;
6833 int64_t hv_cfg
= nbg
->nb_cfg
;
6834 SBREC_CHASSIS_FOR_EACH (chassis
, ctx
->ovnsb_idl
) {
6835 if (chassis
->nb_cfg
< hv_cfg
) {
6836 hv_cfg
= chassis
->nb_cfg
;
6840 /* Update hv_cfg. */
6841 if (nbg
->hv_cfg
!= hv_cfg
) {
6842 nbrec_nb_global_set_hv_cfg(nbg
, hv_cfg
);
6847 /* Handle a fairly small set of changes in the southbound database. */
6849 ovnsb_db_run(struct northd_context
*ctx
, struct ovsdb_idl_loop
*sb_loop
)
6851 if (!ctx
->ovnnb_txn
|| !ovsdb_idl_has_ever_connected(ctx
->ovnsb_idl
)) {
6855 update_logical_port_status(ctx
);
6856 update_northbound_cfg(ctx
, sb_loop
);
6860 parse_options(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
6863 DAEMON_OPTION_ENUMS
,
6867 static const struct option long_options
[] = {
6868 {"ovnsb-db", required_argument
, NULL
, 'd'},
6869 {"ovnnb-db", required_argument
, NULL
, 'D'},
6870 {"unixctl", required_argument
, NULL
, 'u'},
6871 {"help", no_argument
, NULL
, 'h'},
6872 {"options", no_argument
, NULL
, 'o'},
6873 {"version", no_argument
, NULL
, 'V'},
6874 DAEMON_LONG_OPTIONS
,
6876 STREAM_SSL_LONG_OPTIONS
,
6879 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
6884 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
6890 DAEMON_OPTION_HANDLERS
;
6891 VLOG_OPTION_HANDLERS
;
6892 STREAM_SSL_OPTION_HANDLERS
;
6903 unixctl_path
= optarg
;
6911 ovs_cmdl_print_options(long_options
);
6915 ovs_print_version(0, 0);
6924 ovnsb_db
= default_sb_db();
6928 ovnnb_db
= default_nb_db();
6931 free(short_options
);
6935 add_column_noalert(struct ovsdb_idl
*idl
,
6936 const struct ovsdb_idl_column
*column
)
6938 ovsdb_idl_add_column(idl
, column
);
6939 ovsdb_idl_omit_alert(idl
, column
);
6943 main(int argc
, char *argv
[])
6945 int res
= EXIT_SUCCESS
;
6946 struct unixctl_server
*unixctl
;
6950 fatal_ignore_sigpipe();
6951 ovs_cmdl_proctitle_init(argc
, argv
);
6952 set_program_name(argv
[0]);
6953 service_start(&argc
, &argv
);
6954 parse_options(argc
, argv
);
6956 daemonize_start(false);
6958 retval
= unixctl_server_create(unixctl_path
, &unixctl
);
6962 unixctl_command_register("exit", "", 0, 0, ovn_northd_exit
, &exiting
);
6964 daemonize_complete();
6966 /* We want to detect (almost) all changes to the ovn-nb db. */
6967 struct ovsdb_idl_loop ovnnb_idl_loop
= OVSDB_IDL_LOOP_INITIALIZER(
6968 ovsdb_idl_create(ovnnb_db
, &nbrec_idl_class
, true, true));
6969 ovsdb_idl_omit_alert(ovnnb_idl_loop
.idl
, &nbrec_nb_global_col_sb_cfg
);
6970 ovsdb_idl_omit_alert(ovnnb_idl_loop
.idl
, &nbrec_nb_global_col_hv_cfg
);
6972 /* We want to detect only selected changes to the ovn-sb db. */
6973 struct ovsdb_idl_loop ovnsb_idl_loop
= OVSDB_IDL_LOOP_INITIALIZER(
6974 ovsdb_idl_create(ovnsb_db
, &sbrec_idl_class
, false, true));
6976 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_sb_global
);
6977 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_sb_global_col_nb_cfg
);
6979 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_logical_flow
);
6980 add_column_noalert(ovnsb_idl_loop
.idl
,
6981 &sbrec_logical_flow_col_logical_datapath
);
6982 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_logical_flow_col_pipeline
);
6983 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_logical_flow_col_table_id
);
6984 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_logical_flow_col_priority
);
6985 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_logical_flow_col_match
);
6986 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_logical_flow_col_actions
);
6988 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_multicast_group
);
6989 add_column_noalert(ovnsb_idl_loop
.idl
,
6990 &sbrec_multicast_group_col_datapath
);
6991 add_column_noalert(ovnsb_idl_loop
.idl
,
6992 &sbrec_multicast_group_col_tunnel_key
);
6993 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_multicast_group_col_name
);
6994 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_multicast_group_col_ports
);
6996 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_datapath_binding
);
6997 add_column_noalert(ovnsb_idl_loop
.idl
,
6998 &sbrec_datapath_binding_col_tunnel_key
);
6999 add_column_noalert(ovnsb_idl_loop
.idl
,
7000 &sbrec_datapath_binding_col_external_ids
);
7002 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_port_binding
);
7003 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_datapath
);
7004 add_column_noalert(ovnsb_idl_loop
.idl
,
7005 &sbrec_port_binding_col_logical_port
);
7006 add_column_noalert(ovnsb_idl_loop
.idl
,
7007 &sbrec_port_binding_col_tunnel_key
);
7008 add_column_noalert(ovnsb_idl_loop
.idl
,
7009 &sbrec_port_binding_col_parent_port
);
7010 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_tag
);
7011 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_type
);
7012 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_options
);
7013 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_mac
);
7014 add_column_noalert(ovnsb_idl_loop
.idl
,
7015 &sbrec_port_binding_col_nat_addresses
);
7016 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
, &sbrec_port_binding_col_chassis
);
7017 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
,
7018 &sbrec_port_binding_col_gateway_chassis
);
7019 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
,
7020 &sbrec_gateway_chassis_col_chassis
);
7021 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
, &sbrec_gateway_chassis_col_name
);
7022 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
,
7023 &sbrec_gateway_chassis_col_priority
);
7024 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
,
7025 &sbrec_gateway_chassis_col_external_ids
);
7026 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
,
7027 &sbrec_gateway_chassis_col_options
);
7028 add_column_noalert(ovnsb_idl_loop
.idl
,
7029 &sbrec_port_binding_col_external_ids
);
7030 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_mac_binding
);
7031 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_mac_binding_col_datapath
);
7032 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_mac_binding_col_ip
);
7033 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_mac_binding_col_mac
);
7034 add_column_noalert(ovnsb_idl_loop
.idl
,
7035 &sbrec_mac_binding_col_logical_port
);
7036 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_dhcp_options
);
7037 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcp_options_col_code
);
7038 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcp_options_col_type
);
7039 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcp_options_col_name
);
7040 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_dhcpv6_options
);
7041 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcpv6_options_col_code
);
7042 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcpv6_options_col_type
);
7043 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dhcpv6_options_col_name
);
7044 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_address_set
);
7045 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_address_set_col_name
);
7046 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_address_set_col_addresses
);
7047 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_port_group
);
7048 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_group_col_name
);
7049 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_port_group_col_ports
);
7051 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_dns
);
7052 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dns_col_datapaths
);
7053 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dns_col_records
);
7054 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_dns_col_external_ids
);
7056 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_rbac_role
);
7057 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_rbac_role_col_name
);
7058 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_rbac_role_col_permissions
);
7060 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_rbac_permission
);
7061 add_column_noalert(ovnsb_idl_loop
.idl
,
7062 &sbrec_rbac_permission_col_table
);
7063 add_column_noalert(ovnsb_idl_loop
.idl
,
7064 &sbrec_rbac_permission_col_authorization
);
7065 add_column_noalert(ovnsb_idl_loop
.idl
,
7066 &sbrec_rbac_permission_col_insert_delete
);
7067 add_column_noalert(ovnsb_idl_loop
.idl
, &sbrec_rbac_permission_col_update
);
7069 ovsdb_idl_add_table(ovnsb_idl_loop
.idl
, &sbrec_table_chassis
);
7070 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
, &sbrec_chassis_col_nb_cfg
);
7071 ovsdb_idl_add_column(ovnsb_idl_loop
.idl
, &sbrec_chassis_col_name
);
7073 /* Ensure that only a single ovn-northd is active in the deployment by
7074 * acquiring a lock called "ovn_northd" on the southbound database
7075 * and then only performing DB transactions if the lock is held. */
7076 ovsdb_idl_set_lock(ovnsb_idl_loop
.idl
, "ovn_northd");
7077 bool had_lock
= false;
7082 struct northd_context ctx
= {
7083 .ovnnb_idl
= ovnnb_idl_loop
.idl
,
7084 .ovnnb_txn
= ovsdb_idl_loop_run(&ovnnb_idl_loop
),
7085 .ovnsb_idl
= ovnsb_idl_loop
.idl
,
7086 .ovnsb_txn
= ovsdb_idl_loop_run(&ovnsb_idl_loop
),
7089 if (!had_lock
&& ovsdb_idl_has_lock(ovnsb_idl_loop
.idl
)) {
7090 VLOG_INFO("ovn-northd lock acquired. "
7091 "This ovn-northd instance is now active.");
7093 } else if (had_lock
&& !ovsdb_idl_has_lock(ovnsb_idl_loop
.idl
)) {
7094 VLOG_INFO("ovn-northd lock lost. "
7095 "This ovn-northd instance is now on standby.");
7099 struct chassis_index chassis_index
;
7100 bool destroy_chassis_index
= false;
7101 if (ovsdb_idl_has_lock(ovnsb_idl_loop
.idl
)) {
7102 chassis_index_init(&chassis_index
, ctx
.ovnsb_idl
);
7103 destroy_chassis_index
= true;
7105 ovnnb_db_run(&ctx
, &chassis_index
, &ovnsb_idl_loop
);
7106 ovnsb_db_run(&ctx
, &ovnsb_idl_loop
);
7107 if (ctx
.ovnsb_txn
) {
7108 check_and_add_supported_dhcp_opts_to_sb_db(&ctx
);
7109 check_and_add_supported_dhcpv6_opts_to_sb_db(&ctx
);
7110 check_and_update_rbac(&ctx
);
7114 unixctl_server_run(unixctl
);
7115 unixctl_server_wait(unixctl
);
7117 poll_immediate_wake();
7119 ovsdb_idl_loop_commit_and_wait(&ovnnb_idl_loop
);
7120 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop
);
7123 if (should_service_stop()) {
7127 if (destroy_chassis_index
) {
7128 chassis_index_destroy(&chassis_index
);
7132 unixctl_server_destroy(unixctl
);
7133 ovsdb_idl_loop_destroy(&ovnnb_idl_loop
);
7134 ovsdb_idl_loop_destroy(&ovnsb_idl_loop
);
7141 ovn_northd_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
7142 const char *argv
[] OVS_UNUSED
, void *exiting_
)
7144 bool *exiting
= exiting_
;
7147 unixctl_command_reply(conn
, NULL
);