DESIGN: Move in-band control design discussion here.
[sliver-openvswitch.git] / ofproto / in-band.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "in-band.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "classifier.h"
27 #include "dhcp.h"
28 #include "dpif.h"
29 #include "flow.h"
30 #include "netdev.h"
31 #include "netlink.h"
32 #include "odp-util.h"
33 #include "ofproto.h"
34 #include "ofpbuf.h"
35 #include "openflow/openflow.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "timeval.h"
39 #include "vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(in_band);
42
43 /* Priorities used in classifier for in-band rules.  These values are higher
44  * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
45  * The ordering of priorities is not important because all of the rules set up
46  * by in-band control have the same action.  The only reason to use more than
47  * one priority is to make the kind of flow easier to see during debugging. */
48 enum {
49     /* One set per bridge. */
50     IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
51     IBR_TO_LOCAL_ARP,             /* (b) To local port, ARP. */
52     IBR_FROM_LOCAL_ARP,           /* (c) From local port, ARP. */
53
54     /* One set per unique next-hop MAC. */
55     IBR_TO_NEXT_HOP_ARP,          /* (d) To remote MAC, ARP. */
56     IBR_FROM_NEXT_HOP_ARP,        /* (e) From remote MAC, ARP. */
57
58     /* One set per unique remote IP address. */
59     IBR_TO_REMOTE_ARP,            /* (f) To remote IP, ARP. */
60     IBR_FROM_REMOTE_ARP,          /* (g) From remote IP, ARP. */
61
62     /* One set per unique remote (IP,port) pair. */
63     IBR_TO_REMOTE_TCP,            /* (h) To remote IP, TCP port. */
64     IBR_FROM_REMOTE_TCP           /* (i) From remote IP, TCP port. */
65 };
66
67 /* Track one remote IP and next hop information. */
68 struct in_band_remote {
69     struct sockaddr_in remote_addr; /* IP address, in network byte order. */
70     uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
71     uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
72     struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
73 };
74
75 struct in_band {
76     struct ofproto *ofproto;
77     int queue_id, prev_queue_id;
78
79     /* Remote information. */
80     time_t next_remote_refresh; /* Refresh timer. */
81     struct in_band_remote *remotes;
82     size_t n_remotes;
83
84     /* Local information. */
85     time_t next_local_refresh;       /* Refresh timer. */
86     uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
87     struct netdev *local_netdev;     /* Local port's network device. */
88
89     /* Local and remote addresses that are installed as flows. */
90     uint8_t installed_local_mac[ETH_ADDR_LEN];
91     struct sockaddr_in *remote_addrs;
92     size_t n_remote_addrs;
93     uint8_t *remote_macs;
94     size_t n_remote_macs;
95 };
96
97 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
98
99 static int
100 refresh_remote(struct in_band *ib, struct in_band_remote *r)
101 {
102     struct in_addr next_hop_inaddr;
103     char *next_hop_dev;
104     int retval;
105
106     /* Find the next-hop IP address. */
107     memset(r->remote_mac, 0, sizeof r->remote_mac);
108     retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
109                                  &next_hop_inaddr, &next_hop_dev);
110     if (retval) {
111         VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
112                   IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
113         return 1;
114     }
115     if (!next_hop_inaddr.s_addr) {
116         next_hop_inaddr = r->remote_addr.sin_addr;
117     }
118
119     /* Open the next-hop network device. */
120     if (!r->remote_netdev
121         || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
122     {
123         netdev_close(r->remote_netdev);
124
125         retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
126         if (retval) {
127             VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
128                          "to controller "IP_FMT"): %s",
129                          next_hop_dev, IP_ARGS(&r->remote_addr.sin_addr),
130                          strerror(retval));
131             free(next_hop_dev);
132             return 1;
133         }
134     }
135     free(next_hop_dev);
136
137     /* Look up the MAC address of the next-hop IP address. */
138     retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
139                                r->remote_mac);
140     if (retval) {
141         VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
142                     IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
143     }
144
145     /* If we don't have a MAC address, then refresh quickly, since we probably
146      * will get a MAC address soon (via ARP).  Otherwise, we can afford to wait
147      * a little while. */
148     return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
149 }
150
151 static bool
152 refresh_remotes(struct in_band *ib)
153 {
154     struct in_band_remote *r;
155     bool any_changes;
156
157     if (time_now() < ib->next_remote_refresh) {
158         return false;
159     }
160
161     any_changes = false;
162     ib->next_remote_refresh = TIME_MAX;
163     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
164         uint8_t old_remote_mac[ETH_ADDR_LEN];
165         time_t next_refresh;
166
167         /* Save old MAC. */
168         memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
169
170         /* Refresh remote information. */
171         next_refresh = refresh_remote(ib, r) + time_now();
172         ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
173
174         /* If the MAC changed, log the changes. */
175         if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
176             any_changes = true;
177             if (!eth_addr_is_zero(r->remote_mac)
178                 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
179                 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
180                          " to "ETH_ADDR_FMT,
181                          ETH_ADDR_ARGS(r->last_remote_mac),
182                          ETH_ADDR_ARGS(r->remote_mac));
183                 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
184             }
185         }
186     }
187
188     return any_changes;
189 }
190
191 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
192  * for a refresh.  Returns true if anything changed, otherwise false.  */
193 static bool
194 refresh_local(struct in_band *ib)
195 {
196     uint8_t ea[ETH_ADDR_LEN];
197     time_t now;
198
199     now = time_now();
200     if (now < ib->next_local_refresh) {
201         return false;
202     }
203     ib->next_local_refresh = now + 1;
204
205     if (netdev_get_etheraddr(ib->local_netdev, ea)
206         || eth_addr_equals(ea, ib->local_mac)) {
207         return false;
208     }
209
210     memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
211     return true;
212 }
213
214 /* Returns true if 'packet' should be sent to the local port regardless
215  * of the flow table. */
216 bool
217 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
218                     const struct ofpbuf *packet)
219 {
220     /* Regardless of how the flow table is configured, we want to be
221      * able to see replies to our DHCP requests. */
222     if (flow->dl_type == htons(ETH_TYPE_IP)
223             && flow->nw_proto == IPPROTO_UDP
224             && flow->tp_src == htons(DHCP_SERVER_PORT)
225             && flow->tp_dst == htons(DHCP_CLIENT_PORT)
226             && packet->l7) {
227         struct dhcp_header *dhcp;
228
229         dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
230                          sizeof *dhcp);
231         if (!dhcp) {
232             return false;
233         }
234
235         refresh_local(in_band);
236         if (!eth_addr_is_zero(in_band->local_mac)
237             && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
238             return true;
239         }
240     }
241
242     return false;
243 }
244
245 /* Returns true if the rule that would match 'flow' with 'actions' is
246  * allowed to be set up in the datapath. */
247 bool
248 in_band_rule_check(const struct flow *flow,
249                    const struct nlattr *actions, size_t actions_len)
250 {
251     /* Don't allow flows that would prevent DHCP replies from being seen
252      * by the local port. */
253     if (flow->dl_type == htons(ETH_TYPE_IP)
254             && flow->nw_proto == IPPROTO_UDP
255             && flow->tp_src == htons(DHCP_SERVER_PORT)
256             && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
257         const struct nlattr *a;
258         unsigned int left;
259
260         NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
261             if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT
262                 && nl_attr_get_u32(a) == ODPP_LOCAL) {
263                 return true;
264             }
265         }
266         return false;
267     }
268
269     return true;
270 }
271
272 static void
273 make_rules(struct in_band *ib,
274            void (*cb)(struct in_band *, const struct cls_rule *))
275 {
276     struct cls_rule rule;
277     size_t i;
278
279     if (!eth_addr_is_zero(ib->installed_local_mac)) {
280         /* (a) Allow DHCP requests sent from the local port. */
281         cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_DHCP);
282         cls_rule_set_in_port(&rule, ODPP_LOCAL);
283         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
284         cls_rule_set_dl_src(&rule, ib->installed_local_mac);
285         cls_rule_set_nw_proto(&rule, IPPROTO_UDP);
286         cls_rule_set_tp_src(&rule, htons(DHCP_CLIENT_PORT));
287         cls_rule_set_tp_dst(&rule, htons(DHCP_SERVER_PORT));
288         cb(ib, &rule);
289
290         /* (b) Allow ARP replies to the local port's MAC address. */
291         cls_rule_init_catchall(&rule, IBR_TO_LOCAL_ARP);
292         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
293         cls_rule_set_dl_dst(&rule, ib->installed_local_mac);
294         cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
295         cb(ib, &rule);
296
297         /* (c) Allow ARP requests from the local port's MAC address.  */
298         cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_ARP);
299         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
300         cls_rule_set_dl_src(&rule, ib->installed_local_mac);
301         cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
302         cb(ib, &rule);
303     }
304
305     for (i = 0; i < ib->n_remote_macs; i++) {
306         const uint8_t *remote_mac = &ib->remote_macs[i * ETH_ADDR_LEN];
307
308         if (i > 0) {
309             const uint8_t *prev_mac = &ib->remote_macs[(i - 1) * ETH_ADDR_LEN];
310             if (eth_addr_equals(remote_mac, prev_mac)) {
311                 /* Skip duplicates. */
312                 continue;
313             }
314         }
315
316         /* (d) Allow ARP replies to the next hop's MAC address. */
317         cls_rule_init_catchall(&rule, IBR_TO_NEXT_HOP_ARP);
318         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
319         cls_rule_set_dl_dst(&rule, remote_mac);
320         cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
321         cb(ib, &rule);
322
323         /* (e) Allow ARP requests from the next hop's MAC address. */
324         cls_rule_init_catchall(&rule, IBR_FROM_NEXT_HOP_ARP);
325         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
326         cls_rule_set_dl_src(&rule, remote_mac);
327         cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
328         cb(ib, &rule);
329     }
330
331     for (i = 0; i < ib->n_remote_addrs; i++) {
332         const struct sockaddr_in *a = &ib->remote_addrs[i];
333
334         if (!i || a->sin_addr.s_addr != a[-1].sin_addr.s_addr) {
335             /* (f) Allow ARP replies containing the remote's IP address as a
336              * target. */
337             cls_rule_init_catchall(&rule, IBR_TO_REMOTE_ARP);
338             cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
339             cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
340             cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
341             cb(ib, &rule);
342
343             /* (g) Allow ARP requests containing the remote's IP address as a
344              * source. */
345             cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_ARP);
346             cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
347             cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
348             cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
349             cb(ib, &rule);
350         }
351
352         if (!i
353             || a->sin_addr.s_addr != a[-1].sin_addr.s_addr
354             || a->sin_port != a[-1].sin_port) {
355             /* (h) Allow TCP traffic to the remote's IP and port. */
356             cls_rule_init_catchall(&rule, IBR_TO_REMOTE_TCP);
357             cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
358             cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
359             cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
360             cls_rule_set_tp_dst(&rule, a->sin_port);
361             cb(ib, &rule);
362
363             /* (i) Allow TCP traffic from the remote's IP and port. */
364             cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_TCP);
365             cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
366             cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
367             cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
368             cls_rule_set_tp_src(&rule, a->sin_port);
369             cb(ib, &rule);
370         }
371     }
372 }
373
374 static void
375 drop_rule(struct in_band *ib, const struct cls_rule *rule)
376 {
377     ofproto_delete_flow(ib->ofproto, rule);
378 }
379
380 /* Drops from the flow table all of the flows set up by 'ib', then clears out
381  * the information about the installed flows so that they can be filled in
382  * again if necessary. */
383 static void
384 drop_rules(struct in_band *ib)
385 {
386     /* Drop rules. */
387     make_rules(ib, drop_rule);
388
389     /* Clear out state. */
390     memset(ib->installed_local_mac, 0, sizeof ib->installed_local_mac);
391
392     free(ib->remote_addrs);
393     ib->remote_addrs = NULL;
394     ib->n_remote_addrs = 0;
395
396     free(ib->remote_macs);
397     ib->remote_macs = NULL;
398     ib->n_remote_macs = 0;
399 }
400
401 static void
402 add_rule(struct in_band *ib, const struct cls_rule *rule)
403 {
404     struct {
405         struct nx_action_set_queue nxsq;
406         struct ofp_action_output oao;
407     } actions;
408
409     memset(&actions, 0, sizeof actions);
410
411     actions.oao.type = htons(OFPAT_OUTPUT);
412     actions.oao.len = htons(sizeof actions.oao);
413     actions.oao.port = htons(OFPP_NORMAL);
414     actions.oao.max_len = htons(0);
415
416     if (ib->queue_id < 0) {
417         ofproto_add_flow(ib->ofproto, rule,
418                          (union ofp_action *) &actions.oao, 1);
419     } else {
420         actions.nxsq.type = htons(OFPAT_VENDOR);
421         actions.nxsq.len = htons(sizeof actions.nxsq);
422         actions.nxsq.vendor = htonl(NX_VENDOR_ID);
423         actions.nxsq.subtype = htons(NXAST_SET_QUEUE);
424         actions.nxsq.queue_id = htonl(ib->queue_id);
425
426         ofproto_add_flow(ib->ofproto, rule, (union ofp_action *) &actions,
427                          sizeof actions / sizeof(union ofp_action));
428     }
429 }
430
431 /* Inserts flows into the flow table for the current state of 'ib'. */
432 static void
433 add_rules(struct in_band *ib)
434 {
435     make_rules(ib, add_rule);
436 }
437
438 static int
439 compare_addrs(const void *a_, const void *b_)
440 {
441     const struct sockaddr_in *a = a_;
442     const struct sockaddr_in *b = b_;
443     int cmp;
444
445     cmp = memcmp(&a->sin_addr.s_addr,
446                  &b->sin_addr.s_addr,
447                  sizeof a->sin_addr.s_addr);
448     if (cmp) {
449         return cmp;
450     }
451     return memcmp(&a->sin_port, &b->sin_port, sizeof a->sin_port);
452 }
453
454 static int
455 compare_macs(const void *a, const void *b)
456 {
457     return eth_addr_compare_3way(a, b);
458 }
459
460 void
461 in_band_run(struct in_band *ib)
462 {
463     bool local_change, remote_change, queue_id_change;
464     struct in_band_remote *r;
465
466     local_change = refresh_local(ib);
467     remote_change = refresh_remotes(ib);
468     queue_id_change = ib->queue_id != ib->prev_queue_id;
469     if (!local_change && !remote_change && !queue_id_change) {
470         /* Nothing changed, nothing to do. */
471         return;
472     }
473     ib->prev_queue_id = ib->queue_id;
474
475     /* Drop old rules. */
476     drop_rules(ib);
477
478     /* Figure out new rules. */
479     memcpy(ib->installed_local_mac, ib->local_mac, ETH_ADDR_LEN);
480     ib->remote_addrs = xmalloc(ib->n_remotes * sizeof *ib->remote_addrs);
481     ib->n_remote_addrs = 0;
482     ib->remote_macs = xmalloc(ib->n_remotes * ETH_ADDR_LEN);
483     ib->n_remote_macs = 0;
484     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
485         ib->remote_addrs[ib->n_remote_addrs++] = r->remote_addr;
486         if (!eth_addr_is_zero(r->remote_mac)) {
487             memcpy(&ib->remote_macs[ib->n_remote_macs * ETH_ADDR_LEN],
488                    r->remote_mac, ETH_ADDR_LEN);
489             ib->n_remote_macs++;
490         }
491     }
492
493     /* Sort, to allow make_rules() to easily skip duplicates. */
494     qsort(ib->remote_addrs, ib->n_remote_addrs, sizeof *ib->remote_addrs,
495           compare_addrs);
496     qsort(ib->remote_macs, ib->n_remote_macs, ETH_ADDR_LEN, compare_macs);
497
498     /* Add new rules. */
499     add_rules(ib);
500 }
501
502 void
503 in_band_wait(struct in_band *in_band)
504 {
505     long long int wakeup
506             = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
507     poll_timer_wait_until(wakeup * 1000);
508 }
509
510 /* ofproto has flushed all flows from the flow table and it is calling us back
511  * to allow us to reinstall the ones that are important to us. */
512 void
513 in_band_flushed(struct in_band *in_band)
514 {
515     add_rules(in_band);
516 }
517
518 int
519 in_band_create(struct ofproto *ofproto, const char *local_name,
520                struct in_band **in_bandp)
521 {
522     struct in_band *in_band;
523     struct netdev *local_netdev;
524     int error;
525
526     *in_bandp = NULL;
527     error = netdev_open_default(local_name, &local_netdev);
528     if (error) {
529         VLOG_ERR("failed to initialize in-band control: cannot open "
530                  "datapath local port %s (%s)", local_name, strerror(error));
531         return error;
532     }
533
534     in_band = xzalloc(sizeof *in_band);
535     in_band->ofproto = ofproto;
536     in_band->queue_id = in_band->prev_queue_id = -1;
537     in_band->next_remote_refresh = TIME_MIN;
538     in_band->next_local_refresh = TIME_MIN;
539     in_band->local_netdev = local_netdev;
540
541     *in_bandp = in_band;
542
543     return 0;
544 }
545
546 void
547 in_band_destroy(struct in_band *ib)
548 {
549     if (ib) {
550         drop_rules(ib);
551         in_band_set_remotes(ib, NULL, 0);
552         netdev_close(ib->local_netdev);
553         free(ib);
554     }
555 }
556
557 static bool
558 any_addresses_changed(struct in_band *ib,
559                       const struct sockaddr_in *addresses, size_t n)
560 {
561     size_t i;
562
563     if (n != ib->n_remotes) {
564         return true;
565     }
566
567     for (i = 0; i < n; i++) {
568         const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
569         const struct sockaddr_in *new = &addresses[i];
570
571         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
572             old->sin_port != new->sin_port) {
573             return true;
574         }
575     }
576
577     return false;
578 }
579
580 void
581 in_band_set_remotes(struct in_band *ib,
582                     const struct sockaddr_in *addresses, size_t n)
583 {
584     size_t i;
585
586     if (!any_addresses_changed(ib, addresses, n)) {
587         return;
588     }
589
590     /* Clear old remotes. */
591     for (i = 0; i < ib->n_remotes; i++) {
592         netdev_close(ib->remotes[i].remote_netdev);
593     }
594     free(ib->remotes);
595
596     /* Set up new remotes. */
597     ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
598     ib->n_remotes = n;
599     for (i = 0; i < n; i++) {
600         ib->remotes[i].remote_addr = addresses[i];
601     }
602
603     /* Force refresh in next call to in_band_run(). */
604     ib->next_remote_refresh = TIME_MIN;
605 }
606
607 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'.  If
608  * 'queue_id' is negative, 'ib' will not set any queue (which is also the
609  * default). */
610 void
611 in_band_set_queue(struct in_band *ib, int queue_id)
612 {
613     ib->queue_id = queue_id;
614 }
615