137030198f40ba8d248715dff1cf586a79d52e48
[cascardo/ovs.git] / ovn / controller / pinctrl.c
1 /* Copyright (c) 2015, 2016 Red Hat, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "pinctrl.h"
19
20 #include "coverage.h"
21 #include "csum.h"
22 #include "dirs.h"
23 #include "dp-packet.h"
24 #include "flow.h"
25 #include "lport.h"
26 #include "nx-match.h"
27 #include "ovn-controller.h"
28 #include "lib/packets.h"
29 #include "lib/sset.h"
30 #include "openvswitch/ofp-actions.h"
31 #include "openvswitch/ofp-msgs.h"
32 #include "openvswitch/ofp-print.h"
33 #include "openvswitch/ofp-util.h"
34 #include "openvswitch/vlog.h"
35
36 #include "lib/dhcp.h"
37 #include "ovn-controller.h"
38 #include "ovn/lib/actions.h"
39 #include "ovn/lib/logical-fields.h"
40 #include "ovn/lib/ovn-util.h"
41 #include "poll-loop.h"
42 #include "rconn.h"
43 #include "socket-util.h"
44 #include "timeval.h"
45 #include "vswitch-idl.h"
46
47 VLOG_DEFINE_THIS_MODULE(pinctrl);
48
49 /* OpenFlow connection to the switch. */
50 static struct rconn *swconn;
51
52 /* Last seen sequence number for 'swconn'.  When this differs from
53  * rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
54 static unsigned int conn_seq_no;
55
56 static void pinctrl_handle_put_arp(const struct flow *md,
57                                    const struct flow *headers);
58 static void init_put_arps(void);
59 static void destroy_put_arps(void);
60 static void run_put_arps(struct controller_ctx *,
61                          const struct lport_index *lports);
62 static void wait_put_arps(struct controller_ctx *);
63 static void flush_put_arps(void);
64
65 static void init_send_garps(void);
66 static void destroy_send_garps(void);
67 static void send_garp_wait(void);
68 static void send_garp_run(const struct ovsrec_bridge *,
69                           const char *chassis_id,
70                           const struct lport_index *lports,
71                           struct hmap *local_datapaths);
72 static void pinctrl_handle_na(const struct flow *ip_flow,
73                               const struct match *md,
74                               struct ofpbuf *userdata);
75 static void reload_metadata(struct ofpbuf *ofpacts,
76                             const struct match *md);
77
78 COVERAGE_DEFINE(pinctrl_drop_put_arp);
79
80 void
81 pinctrl_init(void)
82 {
83     swconn = rconn_create(5, 0, DSCP_DEFAULT, 1 << OFP13_VERSION);
84     conn_seq_no = 0;
85     init_put_arps();
86     init_send_garps();
87 }
88
89 static ovs_be32
90 queue_msg(struct ofpbuf *msg)
91 {
92     const struct ofp_header *oh = msg->data;
93     ovs_be32 xid = oh->xid;
94
95     rconn_send(swconn, msg, NULL);
96     return xid;
97 }
98
99 /* Sets up 'swconn', a newly (re)connected connection to a switch. */
100 static void
101 pinctrl_setup(struct rconn *swconn)
102 {
103     /* Fetch the switch configuration.  The response later will allow us to
104      * change the miss_send_len to UINT16_MAX, so that we can enable
105      * asynchronous messages. */
106     queue_msg(ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
107                            rconn_get_version(swconn), 0));
108
109     /* Set a packet-in format that supports userdata.  */
110     queue_msg(ofputil_make_set_packet_in_format(rconn_get_version(swconn),
111                                                 NXPIF_NXT_PACKET_IN2));
112 }
113
114 static void
115 set_switch_config(struct rconn *swconn,
116                   const struct ofputil_switch_config *config)
117 {
118     enum ofp_version version = rconn_get_version(swconn);
119     struct ofpbuf *request = ofputil_encode_set_config(config, version);
120     queue_msg(request);
121 }
122
123 static void
124 pinctrl_handle_arp(const struct flow *ip_flow, const struct match *md,
125                    struct ofpbuf *userdata)
126 {
127     /* This action only works for IP packets, and the switch should only send
128      * us IP packets this way, but check here just to be sure. */
129     if (ip_flow->dl_type != htons(ETH_TYPE_IP)) {
130         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
131         VLOG_WARN_RL(&rl, "ARP action on non-IP packet (Ethertype %"PRIx16")",
132                      ntohs(ip_flow->dl_type));
133         return;
134     }
135
136     /* Compose an ARP packet. */
137     uint64_t packet_stub[128 / 8];
138     struct dp_packet packet;
139     dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
140     compose_arp__(&packet);
141
142     struct eth_header *eth = dp_packet_l2(&packet);
143     eth->eth_dst = ip_flow->dl_dst;
144     eth->eth_src = ip_flow->dl_src;
145
146     struct arp_eth_header *arp = dp_packet_l3(&packet);
147     arp->ar_op = htons(ARP_OP_REQUEST);
148     arp->ar_sha = ip_flow->dl_src;
149     put_16aligned_be32(&arp->ar_spa, ip_flow->nw_src);
150     arp->ar_tha = eth_addr_zero;
151     put_16aligned_be32(&arp->ar_tpa, ip_flow->nw_dst);
152
153     if (ip_flow->vlan_tci & htons(VLAN_CFI)) {
154         eth_push_vlan(&packet, htons(ETH_TYPE_VLAN_8021Q), ip_flow->vlan_tci);
155     }
156
157     /* Compose actions.
158      *
159      * First, copy metadata from 'md' into the packet-out via "set_field"
160      * actions, then add actions from 'userdata'.
161      */
162     uint64_t ofpacts_stub[4096 / 8];
163     struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
164     enum ofp_version version = rconn_get_version(swconn);
165
166     reload_metadata(&ofpacts, md);
167     enum ofperr error = ofpacts_pull_openflow_actions(userdata, userdata->size, version, &ofpacts);
168     if (error) {
169         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
170         VLOG_WARN_RL(&rl, "failed to parse arp actions (%s)",
171                      ofperr_to_string(error));
172         goto exit;
173     }
174
175     struct ofputil_packet_out po = {
176         .packet = dp_packet_data(&packet),
177         .packet_len = dp_packet_size(&packet),
178         .buffer_id = UINT32_MAX,
179         .in_port = OFPP_CONTROLLER,
180         .ofpacts = ofpacts.data,
181         .ofpacts_len = ofpacts.size,
182     };
183     enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
184     queue_msg(ofputil_encode_packet_out(&po, proto));
185
186 exit:
187     dp_packet_uninit(&packet);
188     ofpbuf_uninit(&ofpacts);
189 }
190
191 static void
192 pinctrl_handle_put_dhcp_opts(
193     struct dp_packet *pkt_in, struct ofputil_packet_in *pin,
194     struct ofpbuf *userdata, struct ofpbuf *continuation)
195 {
196     enum ofp_version version = rconn_get_version(swconn);
197     enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
198     struct dp_packet *pkt_out_ptr = NULL;
199     uint32_t success = 0;
200
201     /* Parse result field. */
202     const struct mf_field *f;
203     enum ofperr ofperr = nx_pull_header(userdata, &f, NULL);
204     if (ofperr) {
205         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
206         VLOG_WARN_RL(&rl, "bad result OXM (%s)", ofperr_to_string(ofperr));
207         goto exit;
208     }
209
210     /* Parse result offset and offer IP. */
211     ovs_be32 *ofsp = ofpbuf_try_pull(userdata, sizeof *ofsp);
212     ovs_be32 *offer_ip = ofpbuf_try_pull(userdata, sizeof *offer_ip);
213     if (!ofsp || !offer_ip) {
214         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
215         VLOG_WARN_RL(&rl, "offset or offer_ip not present in the userdata");
216         goto exit;
217     }
218
219     /* Check that the result is valid and writable. */
220     struct mf_subfield dst = { .field = f, .ofs = ntohl(*ofsp), .n_bits = 1 };
221     ofperr = mf_check_dst(&dst, NULL);
222     if (ofperr) {
223         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
224         VLOG_WARN_RL(&rl, "bad result bit (%s)", ofperr_to_string(ofperr));
225         goto exit;
226     }
227
228     if (!userdata->size) {
229         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
230         VLOG_WARN_RL(&rl, "DHCP options not present in the userdata");
231         goto exit;
232     }
233
234     /* Validate the DHCP request packet.
235      * Format of the DHCP packet is
236      * ------------------------------------------------------------------------
237      *| UDP HEADER  | DHCP HEADER  | 4 Byte DHCP Cookie | DHCP OPTIONS(var len)|
238      * ------------------------------------------------------------------------
239      */
240     if (dp_packet_l4_size(pkt_in) < (UDP_HEADER_LEN +
241         sizeof (struct dhcp_header) + sizeof(uint32_t) + 3)) {
242         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
243         VLOG_WARN_RL(&rl, "Invalid or incomplete DHCP packet recieved");
244         goto exit;
245     }
246
247     struct dhcp_header const *in_dhcp_data = dp_packet_get_udp_payload(pkt_in);
248     if (in_dhcp_data->op != DHCP_OP_REQUEST) {
249         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
250         VLOG_WARN_RL(&rl, "Invalid opcode in the DHCP packet : %d",
251                      in_dhcp_data->op);
252         goto exit;
253     }
254
255     /* DHCP options follow the DHCP header. The first 4 bytes of the DHCP
256      * options is the DHCP magic cookie followed by the actual DHCP options.
257      */
258     const uint8_t *in_dhcp_opt =
259         (const uint8_t *)dp_packet_get_udp_payload(pkt_in) +
260         sizeof (struct dhcp_header);
261
262     ovs_be32 magic_cookie = htonl(DHCP_MAGIC_COOKIE);
263     if (memcmp(in_dhcp_opt, &magic_cookie, sizeof(ovs_be32))) {
264         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
265         VLOG_WARN_RL(&rl, "DHCP magic cookie not present in the DHCP packet");
266         goto exit;
267     }
268
269     in_dhcp_opt += 4;
270     /* Check that the DHCP Message Type (opt 53) is present or not with
271      * valid values - DHCP_MSG_DISCOVER or DHCP_MSG_REQUEST as the first
272      * DHCP option.
273      */
274     if (!(in_dhcp_opt[0] == DHCP_OPT_MSG_TYPE && in_dhcp_opt[1] == 1 && (
275             in_dhcp_opt[2] == DHCP_MSG_DISCOVER ||
276             in_dhcp_opt[2] == DHCP_MSG_REQUEST))) {
277         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
278         VLOG_WARN_RL(&rl, "Invalid DHCP message type : opt code = %d,"
279                      " opt value = %d", in_dhcp_opt[0], in_dhcp_opt[2]);
280         goto exit;
281     }
282
283     uint8_t msg_type;
284     if (in_dhcp_opt[2] == DHCP_MSG_DISCOVER) {
285         msg_type = DHCP_MSG_OFFER;
286     } else {
287         msg_type = DHCP_MSG_ACK;
288     }
289
290     /* Frame the DHCP reply packet
291      * Total DHCP options length will be options stored in the userdata +
292      * 16 bytes.
293      *
294      * --------------------------------------------------------------
295      *| 4 Bytes (dhcp cookie) | 3 Bytes (option type) | DHCP options |
296      * --------------------------------------------------------------
297      *| 4 Bytes padding | 1 Byte (option end 0xFF ) | 4 Bytes padding|
298      * --------------------------------------------------------------
299      */
300     uint16_t new_l4_size = UDP_HEADER_LEN + DHCP_HEADER_LEN + \
301                            userdata->size + 16;
302     size_t new_packet_size = pkt_in->l4_ofs + new_l4_size;
303
304     struct dp_packet pkt_out;
305     dp_packet_init(&pkt_out, new_packet_size);
306     dp_packet_clear(&pkt_out);
307     dp_packet_prealloc_tailroom(&pkt_out, new_packet_size);
308     pkt_out_ptr = &pkt_out;
309
310     /* Copy the L2 and L3 headers from the pkt_in as they would remain same*/
311     dp_packet_put(
312         &pkt_out, dp_packet_pull(pkt_in, pkt_in->l4_ofs), pkt_in->l4_ofs);
313
314     pkt_out.l2_5_ofs = pkt_in->l2_5_ofs;
315     pkt_out.l2_pad_size = pkt_in->l2_pad_size;
316     pkt_out.l3_ofs = pkt_in->l3_ofs;
317     pkt_out.l4_ofs = pkt_in->l4_ofs;
318
319     struct udp_header *udp = dp_packet_put(
320         &pkt_out, dp_packet_pull(pkt_in, UDP_HEADER_LEN), UDP_HEADER_LEN);
321
322     struct dhcp_header *dhcp_data = dp_packet_put(
323         &pkt_out, dp_packet_pull(pkt_in, DHCP_HEADER_LEN), DHCP_HEADER_LEN);
324     dhcp_data->op = DHCP_OP_REPLY;
325     dhcp_data->yiaddr = *offer_ip;
326     dp_packet_put(&pkt_out, &magic_cookie, sizeof(ovs_be32));
327
328     uint8_t *out_dhcp_opts = dp_packet_put_zeros(&pkt_out,
329                                                  userdata->size + 12);
330     /* DHCP option - type */
331     out_dhcp_opts[0] = DHCP_OPT_MSG_TYPE;
332     out_dhcp_opts[1] = 1;
333     out_dhcp_opts[2] = msg_type;
334     out_dhcp_opts += 3;
335
336     memcpy(out_dhcp_opts, userdata->data, userdata->size);
337     out_dhcp_opts += userdata->size;
338     /* Padding */
339     out_dhcp_opts += 4;
340     /* End */
341     out_dhcp_opts[0] = DHCP_OPT_END;
342
343     udp->udp_len = htons(new_l4_size);
344
345     struct ip_header *out_ip = dp_packet_l3(&pkt_out);
346     out_ip->ip_tot_len = htons(pkt_out.l4_ofs - pkt_out.l3_ofs + new_l4_size);
347     udp->udp_csum = 0;
348     out_ip->ip_csum = 0;
349     out_ip->ip_csum = csum(out_ip, sizeof *out_ip);
350
351     pin->packet = dp_packet_data(&pkt_out);
352     pin->packet_len = dp_packet_size(&pkt_out);
353
354     success = 1;
355 exit:
356     if (!ofperr) {
357         union mf_subvalue sv;
358         sv.u8_val = success;
359         mf_write_subfield(&dst, &sv, &pin->flow_metadata);
360     }
361     queue_msg(ofputil_encode_resume(pin, continuation, proto));
362     if (pkt_out_ptr) {
363         dp_packet_uninit(pkt_out_ptr);
364     }
365 }
366
367 static void
368 process_packet_in(const struct ofp_header *msg)
369 {
370     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
371
372     struct ofputil_packet_in pin;
373     struct ofpbuf continuation;
374     enum ofperr error = ofputil_decode_packet_in(msg, true, &pin,
375                                                  NULL, NULL, &continuation);
376
377     if (error) {
378         VLOG_WARN_RL(&rl, "error decoding packet-in: %s",
379                      ofperr_to_string(error));
380         return;
381     }
382     if (pin.reason != OFPR_ACTION) {
383         return;
384     }
385
386     struct ofpbuf userdata = ofpbuf_const_initializer(pin.userdata,
387                                                       pin.userdata_len);
388     const struct action_header *ah = ofpbuf_pull(&userdata, sizeof *ah);
389     if (!ah) {
390         VLOG_WARN_RL(&rl, "packet-in userdata lacks action header");
391         return;
392     }
393
394     struct dp_packet packet;
395     dp_packet_use_const(&packet, pin.packet, pin.packet_len);
396     struct flow headers;
397     flow_extract(&packet, &headers);
398
399     switch (ntohl(ah->opcode)) {
400     case ACTION_OPCODE_ARP:
401         pinctrl_handle_arp(&headers, &pin.flow_metadata, &userdata);
402         break;
403
404     case ACTION_OPCODE_PUT_ARP:
405         pinctrl_handle_put_arp(&pin.flow_metadata.flow, &headers);
406         break;
407
408     case ACTION_OPCODE_PUT_DHCP_OPTS:
409         pinctrl_handle_put_dhcp_opts(&packet, &pin, &userdata, &continuation);
410         break;
411
412     case ACTION_OPCODE_NA:
413         pinctrl_handle_na(&headers, &pin.flow_metadata, &userdata);
414         break;
415
416     default:
417         VLOG_WARN_RL(&rl, "unrecognized packet-in opcode %"PRIu32,
418                      ntohl(ah->opcode));
419         break;
420     }
421 }
422
423 static void
424 pinctrl_recv(const struct ofp_header *oh, enum ofptype type)
425 {
426     if (type == OFPTYPE_ECHO_REQUEST) {
427         queue_msg(make_echo_reply(oh));
428     } else if (type == OFPTYPE_GET_CONFIG_REPLY) {
429         /* Enable asynchronous messages (see "Asynchronous Messages" in
430          * DESIGN.md for more information). */
431         struct ofputil_switch_config config;
432
433         ofputil_decode_get_config_reply(oh, &config);
434         config.miss_send_len = UINT16_MAX;
435         set_switch_config(swconn, &config);
436     } else if (type == OFPTYPE_PACKET_IN) {
437         process_packet_in(oh);
438     } else if (type != OFPTYPE_ECHO_REPLY && type != OFPTYPE_BARRIER_REPLY) {
439         if (VLOG_IS_DBG_ENABLED()) {
440             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
441
442             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
443
444             VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", s);
445             free(s);
446         }
447     }
448 }
449
450 void
451 pinctrl_run(struct controller_ctx *ctx, const struct lport_index *lports,
452             const struct ovsrec_bridge *br_int,
453             const char *chassis_id,
454             struct hmap *local_datapaths)
455 {
456     if (br_int) {
457         char *target;
458
459         target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
460         if (strcmp(target, rconn_get_target(swconn))) {
461             VLOG_INFO("%s: connecting to switch", target);
462             rconn_connect(swconn, target, target);
463         }
464         free(target);
465     } else {
466         rconn_disconnect(swconn);
467     }
468
469     rconn_run(swconn);
470
471     if (rconn_is_connected(swconn)) {
472         if (conn_seq_no != rconn_get_connection_seqno(swconn)) {
473             pinctrl_setup(swconn);
474             conn_seq_no = rconn_get_connection_seqno(swconn);
475             flush_put_arps();
476         }
477
478         /* Process a limited number of messages per call. */
479         for (int i = 0; i < 50; i++) {
480             struct ofpbuf *msg = rconn_recv(swconn);
481             if (!msg) {
482                 break;
483             }
484
485             const struct ofp_header *oh = msg->data;
486             enum ofptype type;
487
488             ofptype_decode(&type, oh);
489             pinctrl_recv(oh, type);
490             ofpbuf_delete(msg);
491         }
492     }
493
494     run_put_arps(ctx, lports);
495     send_garp_run(br_int, chassis_id, lports, local_datapaths);
496 }
497
498 void
499 pinctrl_wait(struct controller_ctx *ctx)
500 {
501     wait_put_arps(ctx);
502     rconn_run_wait(swconn);
503     rconn_recv_wait(swconn);
504     send_garp_wait();
505 }
506
507 void
508 pinctrl_destroy(void)
509 {
510     rconn_destroy(swconn);
511     destroy_put_arps();
512     destroy_send_garps();
513 }
514 \f
515 /* Implementation of the "put_arp" OVN action.  This action sends a packet to
516  * ovn-controller, using the flow as an API (see actions.h for details).  This
517  * code implements the action by updating the MAC_Binding table in the
518  * southbound database.
519  *
520  * This code could be a lot simpler if the database could always be updated,
521  * but in fact we can only update it when ctx->ovnsb_idl_txn is nonnull.  Thus,
522  * we buffer up a few put_arps (but we don't keep them longer than 1 second)
523  * and apply them whenever a database transaction is available. */
524
525 /* Buffered "put_arp" operation. */
526 struct put_arp {
527     struct hmap_node hmap_node; /* In 'put_arps'. */
528
529     long long int timestamp;    /* In milliseconds. */
530
531     /* Key. */
532     uint32_t dp_key;
533     uint32_t port_key;
534     ovs_be32 ip;
535
536     /* Value. */
537     struct eth_addr mac;
538 };
539
540 /* Contains "struct put_arp"s. */
541 static struct hmap put_arps;
542
543 static void
544 init_put_arps(void)
545 {
546     hmap_init(&put_arps);
547 }
548
549 static void
550 destroy_put_arps(void)
551 {
552     flush_put_arps();
553     hmap_destroy(&put_arps);
554 }
555
556 static struct put_arp *
557 pinctrl_find_put_arp(uint32_t dp_key, uint32_t port_key, ovs_be32 ip,
558                      uint32_t hash)
559 {
560     struct put_arp *pa;
561     HMAP_FOR_EACH_WITH_HASH (pa, hmap_node, hash, &put_arps) {
562         if (pa->dp_key == dp_key
563             && pa->port_key == port_key
564             && pa->ip == ip) {
565             return pa;
566         }
567     }
568     return NULL;
569 }
570
571 static void
572 pinctrl_handle_put_arp(const struct flow *md, const struct flow *headers)
573 {
574     uint32_t dp_key = ntohll(md->metadata);
575     uint32_t port_key = md->regs[MFF_LOG_INPORT - MFF_REG0];
576     ovs_be32 ip = htonl(md->regs[0]);
577     uint32_t hash = hash_3words(dp_key, port_key, (OVS_FORCE uint32_t) ip);
578     struct put_arp *pa = pinctrl_find_put_arp(dp_key, port_key, ip, hash);
579     if (!pa) {
580         if (hmap_count(&put_arps) >= 1000) {
581             COVERAGE_INC(pinctrl_drop_put_arp);
582             return;
583         }
584
585         pa = xmalloc(sizeof *pa);
586         hmap_insert(&put_arps, &pa->hmap_node, hash);
587         pa->dp_key = dp_key;
588         pa->port_key = port_key;
589         pa->ip = ip;
590     }
591     pa->timestamp = time_msec();
592     pa->mac = headers->dl_src;
593 }
594
595 static void
596 run_put_arp(struct controller_ctx *ctx, const struct lport_index *lports,
597             const struct put_arp *pa)
598 {
599     if (time_msec() > pa->timestamp + 1000) {
600         return;
601     }
602
603     /* Convert logical datapath and logical port key into lport. */
604     const struct sbrec_port_binding *pb
605         = lport_lookup_by_key(lports, pa->dp_key, pa->port_key);
606     if (!pb) {
607         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
608
609         VLOG_WARN_RL(&rl, "unknown logical port with datapath %"PRIu32" "
610                      "and port %"PRIu32, pa->dp_key, pa->port_key);
611         return;
612     }
613
614     /* Convert arguments to string form for database. */
615     char ip_string[INET_ADDRSTRLEN + 1];
616     snprintf(ip_string, sizeof ip_string, IP_FMT, IP_ARGS(pa->ip));
617
618     char mac_string[ETH_ADDR_STRLEN + 1];
619     snprintf(mac_string, sizeof mac_string,
620              ETH_ADDR_FMT, ETH_ADDR_ARGS(pa->mac));
621
622     /* Check for and update an existing IP-MAC binding for this logical
623      * port.
624      *
625      * XXX This is not very efficient. */
626     const struct sbrec_mac_binding *b;
627     SBREC_MAC_BINDING_FOR_EACH (b, ctx->ovnsb_idl) {
628         if (!strcmp(b->logical_port, pb->logical_port)
629             && !strcmp(b->ip, ip_string)) {
630             if (strcmp(b->mac, mac_string)) {
631                 sbrec_mac_binding_set_mac(b, mac_string);
632             }
633             return;
634         }
635     }
636
637     /* Add new IP-MAC binding for this logical port. */
638     b = sbrec_mac_binding_insert(ctx->ovnsb_idl_txn);
639     sbrec_mac_binding_set_logical_port(b, pb->logical_port);
640     sbrec_mac_binding_set_ip(b, ip_string);
641     sbrec_mac_binding_set_mac(b, mac_string);
642 }
643
644 static void
645 run_put_arps(struct controller_ctx *ctx, const struct lport_index *lports)
646 {
647     if (!ctx->ovnsb_idl_txn) {
648         return;
649     }
650
651     const struct put_arp *pa;
652     HMAP_FOR_EACH (pa, hmap_node, &put_arps) {
653         run_put_arp(ctx, lports, pa);
654     }
655     flush_put_arps();
656 }
657
658 static void
659 wait_put_arps(struct controller_ctx *ctx)
660 {
661     if (ctx->ovnsb_idl_txn && !hmap_is_empty(&put_arps)) {
662         poll_immediate_wake();
663     }
664 }
665
666 static void
667 flush_put_arps(void)
668 {
669     struct put_arp *pa;
670     HMAP_FOR_EACH_POP (pa, hmap_node, &put_arps) {
671         free(pa);
672     }
673 }
674 \f
675 /*
676  * Send gratuitous ARP for vif on localnet.
677  *
678  * When a new vif on localnet is added, gratuitous ARPs are sent announcing
679  * the port's mac,ip mapping.  On localnet, such announcements are needed for
680  * switches and routers on the broadcast segment to update their port-mac
681  * and ARP tables.
682  */
683 struct garp_data {
684     struct eth_addr ea;          /* Ethernet address of port. */
685     ovs_be32 ipv4;               /* Ipv4 address of port. */
686     long long int announce_time; /* Next announcement in ms. */
687     int backoff;                 /* Backoff for the next announcement. */
688     ofp_port_t ofport;           /* ofport used to output this GARP. */
689 };
690
691 /* Contains GARPs to be sent. */
692 static struct shash send_garp_data;
693
694 /* Next GARP announcement in ms. */
695 static long long int send_garp_time;
696
697 static void
698 init_send_garps(void)
699 {
700     shash_init(&send_garp_data);
701     send_garp_time = LLONG_MAX;
702 }
703
704 static void
705 destroy_send_garps(void)
706 {
707     shash_destroy_free_data(&send_garp_data);
708 }
709
710 /* Add or update a vif for which GARPs need to be announced. */
711 static void
712 send_garp_update(const struct sbrec_port_binding *binding_rec,
713                  struct simap *localnet_ofports, struct hmap *local_datapaths)
714 {
715     /* Find the localnet ofport to send this GARP. */
716     struct local_datapath *ld
717         = get_local_datapath(local_datapaths,
718                              binding_rec->datapath->tunnel_key);
719     if (!ld || !ld->localnet_port) {
720         return;
721     }
722     ofp_port_t ofport = u16_to_ofp(simap_get(localnet_ofports,
723                                              ld->localnet_port->logical_port));
724
725     /* Update GARP if it exists. */
726     struct garp_data *garp = shash_find_data(&send_garp_data,
727                                              binding_rec->logical_port);
728     if (garp) {
729         garp->ofport = ofport;
730         return;
731     }
732
733     /* Add GARP for new vif. */
734     int i;
735     for (i = 0; i < binding_rec->n_mac; i++) {
736         struct lport_addresses laddrs;
737         if (!extract_lsp_addresses(binding_rec->mac[i], &laddrs)
738             || !laddrs.n_ipv4_addrs) {
739             continue;
740         }
741
742         struct garp_data *garp = xmalloc(sizeof *garp);
743         garp->ea = laddrs.ea;
744         garp->ipv4 = laddrs.ipv4_addrs[0].addr;
745         garp->announce_time = time_msec() + 1000;
746         garp->backoff = 1;
747         garp->ofport = ofport;
748         shash_add(&send_garp_data, binding_rec->logical_port, garp);
749
750         destroy_lport_addresses(&laddrs);
751         break;
752     }
753 }
754
755 /* Remove a vif from GARP announcements. */
756 static void
757 send_garp_delete(const char *lport)
758 {
759     struct garp_data *garp = shash_find_and_delete(&send_garp_data, lport);
760     free(garp);
761 }
762
763 static long long int
764 send_garp(struct garp_data *garp, long long int current_time)
765 {
766     if (current_time < garp->announce_time) {
767         return garp->announce_time;
768     }
769
770     /* Compose a GARP request packet. */
771     uint64_t packet_stub[128 / 8];
772     struct dp_packet packet;
773     dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
774     compose_arp(&packet, ARP_OP_REQUEST, garp->ea, eth_addr_zero,
775                 true, garp->ipv4, garp->ipv4);
776
777     /* Compose actions.  The garp request is output on localnet ofport. */
778     uint64_t ofpacts_stub[4096 / 8];
779     struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
780     enum ofp_version version = rconn_get_version(swconn);
781     ofpact_put_OUTPUT(&ofpacts)->port = garp->ofport;
782
783     struct ofputil_packet_out po = {
784         .packet = dp_packet_data(&packet),
785         .packet_len = dp_packet_size(&packet),
786         .buffer_id = UINT32_MAX,
787         .in_port = OFPP_CONTROLLER,
788         .ofpacts = ofpacts.data,
789         .ofpacts_len = ofpacts.size,
790     };
791     enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
792     queue_msg(ofputil_encode_packet_out(&po, proto));
793     dp_packet_uninit(&packet);
794     ofpbuf_uninit(&ofpacts);
795
796     /* Set the next announcement.  At most 5 announcements are sent for a
797      * vif. */
798     if (garp->backoff < 16) {
799         garp->backoff *= 2;
800         garp->announce_time = current_time + garp->backoff * 1000;
801     } else {
802         garp->announce_time = LLONG_MAX;
803     }
804     return garp->announce_time;
805 }
806
807 /* Get localnet vifs, and ofport for localnet patch ports. */
808 static void
809 get_localnet_vifs(const struct ovsrec_bridge *br_int,
810                   const char *this_chassis_id,
811                   const struct lport_index *lports,
812                   struct hmap *local_datapaths,
813                   struct sset *localnet_vifs,
814                   struct simap *localnet_ofports)
815 {
816     for (int i = 0; i < br_int->n_ports; i++) {
817         const struct ovsrec_port *port_rec = br_int->ports[i];
818         if (!strcmp(port_rec->name, br_int->name)) {
819             continue;
820         }
821         const char *chassis_id = smap_get(&port_rec->external_ids,
822                                           "ovn-chassis-id");
823         if (chassis_id && !strcmp(chassis_id, this_chassis_id)) {
824             continue;
825         }
826         const char *localnet = smap_get(&port_rec->external_ids,
827                                         "ovn-localnet-port");
828         for (int j = 0; j < port_rec->n_interfaces; j++) {
829             const struct ovsrec_interface *iface_rec = port_rec->interfaces[j];
830             if (!iface_rec->n_ofport) {
831                 continue;
832             }
833             if (localnet) {
834                 int64_t ofport = iface_rec->ofport[0];
835                 if (ofport < 1 || ofport > ofp_to_u16(OFPP_MAX)) {
836                     continue;
837                 }
838                 simap_put(localnet_ofports, localnet, ofport);
839                 continue;
840             }
841             const char *iface_id = smap_get(&iface_rec->external_ids,
842                                             "iface-id");
843             if (!iface_id) {
844                 continue;
845             }
846             const struct sbrec_port_binding *pb
847                 = lport_lookup_by_name(lports, iface_id);
848             if (!pb) {
849                 continue;
850             }
851             struct local_datapath *ld
852                 = get_local_datapath(local_datapaths,
853                                      pb->datapath->tunnel_key);
854             if (ld && ld->localnet_port) {
855                 sset_add(localnet_vifs, iface_id);
856             }
857         }
858     }
859 }
860
861 static void
862 send_garp_wait(void)
863 {
864     poll_timer_wait_until(send_garp_time);
865 }
866
867 static void
868 send_garp_run(const struct ovsrec_bridge *br_int, const char *chassis_id,
869               const struct lport_index *lports,
870               struct hmap *local_datapaths)
871 {
872     struct sset localnet_vifs = SSET_INITIALIZER(&localnet_vifs);
873     struct simap localnet_ofports = SIMAP_INITIALIZER(&localnet_ofports);
874
875     get_localnet_vifs(br_int, chassis_id, lports, local_datapaths,
876                       &localnet_vifs, &localnet_ofports);
877
878     /* For deleted ports, remove from send_garp_data. */
879     struct shash_node *iter, *next;
880     SHASH_FOR_EACH_SAFE (iter, next, &send_garp_data) {
881         if (!sset_contains(&localnet_vifs, iter->name)) {
882             send_garp_delete(iter->name);
883         }
884     }
885
886     /* Update send_garp_data. */
887     const char *iface_id;
888     SSET_FOR_EACH (iface_id, &localnet_vifs) {
889         const struct sbrec_port_binding *pb = lport_lookup_by_name(lports,
890                                                                    iface_id);
891         if (pb) {
892             send_garp_update(pb, &localnet_ofports, local_datapaths);
893         }
894     }
895
896     /* Send GARPs, and update the next announcement. */
897     long long int current_time = time_msec();
898     send_garp_time = LLONG_MAX;
899     SHASH_FOR_EACH (iter, &send_garp_data) {
900         long long int next_announce = send_garp(iter->data, current_time);
901         if (send_garp_time > next_announce) {
902             send_garp_time = next_announce;
903         }
904     }
905     sset_destroy(&localnet_vifs);
906     simap_destroy(&localnet_ofports);
907 }
908
909 static void
910 reload_metadata(struct ofpbuf *ofpacts, const struct match *md)
911 {
912     enum mf_field_id md_fields[] = {
913 #if FLOW_N_REGS == 16
914         MFF_REG0,
915         MFF_REG1,
916         MFF_REG2,
917         MFF_REG3,
918         MFF_REG4,
919         MFF_REG5,
920         MFF_REG6,
921         MFF_REG7,
922         MFF_REG8,
923         MFF_REG9,
924         MFF_REG10,
925         MFF_REG11,
926         MFF_REG12,
927         MFF_REG13,
928         MFF_REG14,
929         MFF_REG15,
930 #else
931 #error
932 #endif
933         MFF_METADATA,
934     };
935     for (size_t i = 0; i < ARRAY_SIZE(md_fields); i++) {
936         const struct mf_field *field = mf_from_id(md_fields[i]);
937         if (!mf_is_all_wild(field, &md->wc)) {
938             struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
939             sf->field = field;
940             sf->flow_has_vlan = false;
941             mf_get_value(field, &md->flow, &sf->value);
942             memset(&sf->mask, 0xff, field->n_bytes);
943         }
944     }
945 }
946
947 static void
948 pinctrl_handle_na(const struct flow *ip_flow,
949                   const struct match *md,
950                   struct ofpbuf *userdata)
951 {
952     /* This action only works for IPv6 ND packets, and the switch should only
953      * send us ND packets this way, but check here just to be sure. */
954     if (!is_nd(ip_flow, NULL)) {
955         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
956         VLOG_WARN_RL(&rl, "NA action on non-ND packet");
957         return;
958     }
959
960     enum ofp_version version = rconn_get_version(swconn);
961     enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
962
963     uint64_t packet_stub[128 / 8];
964     struct dp_packet packet;
965     dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
966
967     ovs_be32 ipv6_src[4], ipv6_dst[4];
968     memcpy(ipv6_dst, &ip_flow->ipv6_src, sizeof ipv6_src);
969     memcpy(ipv6_src, &ip_flow->nd_target, sizeof ipv6_dst);
970
971     /* Frame the NA packet with RSO=011. */
972     compose_na(&packet,
973                ip_flow->dl_dst, ip_flow->dl_src,
974                ipv6_src, ipv6_dst,
975                htonl(0x60000000));
976
977     /* Reload previous packet metadata. */
978     uint64_t ofpacts_stub[4096 / 8];
979     struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
980     reload_metadata(&ofpacts, md);
981
982     enum ofperr error = ofpacts_pull_openflow_actions(userdata, userdata->size,
983                                                       version, &ofpacts);
984     if (error) {
985         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
986         VLOG_WARN_RL(&rl, "failed to parse actions for 'na' (%s)",
987                      ofperr_to_string(error));
988         goto exit;
989     }
990
991     struct ofputil_packet_out po = {
992         .packet = dp_packet_data(&packet),
993         .packet_len = dp_packet_size(&packet),
994         .buffer_id = UINT32_MAX,
995         .in_port = OFPP_CONTROLLER,
996         .ofpacts = ofpacts.data,
997         .ofpacts_len = ofpacts.size,
998     };
999
1000     queue_msg(ofputil_encode_packet_out(&po, proto));
1001
1002 exit:
1003     dp_packet_uninit(&packet);
1004     ofpbuf_uninit(&ofpacts);
1005 }