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