764b25296f2de367599245bbf13ee78fc605b1d7
[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 "ofproto-provider.h"
36 #include "openflow/openflow.h"
37 #include "packets.h"
38 #include "poll-loop.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 /* What to do to an in_band_rule. */
77 enum in_band_op {
78     ADD,                       /* Add the rule to ofproto's flow table. */
79     DELETE                     /* Delete the rule from ofproto's flow table. */
80 };
81
82 /* A rule to add to or delete from ofproto's flow table.  */
83 struct in_band_rule {
84     struct cls_rule cls_rule;
85     enum in_band_op op;
86 };
87
88 struct in_band {
89     struct ofproto *ofproto;
90     int queue_id;
91
92     /* Remote information. */
93     time_t next_remote_refresh; /* Refresh timer. */
94     struct in_band_remote *remotes;
95     size_t n_remotes;
96
97     /* Local information. */
98     time_t next_local_refresh;       /* Refresh timer. */
99     uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
100     struct netdev *local_netdev;     /* Local port's network device. */
101
102     /* Flow tracking. */
103     struct hmap rules;          /* Contains "struct in_band_rule"s. */
104 };
105
106 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
107
108 static int
109 refresh_remote(struct in_band *ib, struct in_band_remote *r)
110 {
111     struct in_addr next_hop_inaddr;
112     char *next_hop_dev;
113     int retval;
114
115     /* Find the next-hop IP address. */
116     memset(r->remote_mac, 0, sizeof r->remote_mac);
117     retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
118                                  &next_hop_inaddr, &next_hop_dev);
119     if (retval) {
120         VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
121                   IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
122         return 1;
123     }
124     if (!next_hop_inaddr.s_addr) {
125         next_hop_inaddr = r->remote_addr.sin_addr;
126     }
127
128     /* Open the next-hop network device. */
129     if (!r->remote_netdev
130         || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
131     {
132         netdev_close(r->remote_netdev);
133
134         retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
135         if (retval) {
136             VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
137                          "to controller "IP_FMT"): %s",
138                          next_hop_dev, IP_ARGS(&r->remote_addr.sin_addr),
139                          strerror(retval));
140             free(next_hop_dev);
141             return 1;
142         }
143     }
144     free(next_hop_dev);
145
146     /* Look up the MAC address of the next-hop IP address. */
147     retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
148                                r->remote_mac);
149     if (retval) {
150         VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
151                     IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
152     }
153
154     /* If we don't have a MAC address, then refresh quickly, since we probably
155      * will get a MAC address soon (via ARP).  Otherwise, we can afford to wait
156      * a little while. */
157     return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
158 }
159
160 static bool
161 refresh_remotes(struct in_band *ib)
162 {
163     struct in_band_remote *r;
164     bool any_changes;
165
166     if (time_now() < ib->next_remote_refresh) {
167         return false;
168     }
169
170     any_changes = false;
171     ib->next_remote_refresh = TIME_MAX;
172     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
173         uint8_t old_remote_mac[ETH_ADDR_LEN];
174         time_t next_refresh;
175
176         /* Save old MAC. */
177         memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
178
179         /* Refresh remote information. */
180         next_refresh = refresh_remote(ib, r) + time_now();
181         ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
182
183         /* If the MAC changed, log the changes. */
184         if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
185             any_changes = true;
186             if (!eth_addr_is_zero(r->remote_mac)
187                 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
188                 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
189                          " to "ETH_ADDR_FMT,
190                          ETH_ADDR_ARGS(r->last_remote_mac),
191                          ETH_ADDR_ARGS(r->remote_mac));
192                 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
193             }
194         }
195     }
196
197     return any_changes;
198 }
199
200 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
201  * for a refresh.  Returns true if anything changed, otherwise false.  */
202 static bool
203 refresh_local(struct in_band *ib)
204 {
205     uint8_t ea[ETH_ADDR_LEN];
206     time_t now;
207
208     now = time_now();
209     if (now < ib->next_local_refresh) {
210         return false;
211     }
212     ib->next_local_refresh = now + 1;
213
214     if (netdev_get_etheraddr(ib->local_netdev, ea)
215         || eth_addr_equals(ea, ib->local_mac)) {
216         return false;
217     }
218
219     memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
220     return true;
221 }
222
223 /* Returns true if 'packet' should be sent to the local port regardless
224  * of the flow table. */
225 bool
226 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
227                     const struct ofpbuf *packet)
228 {
229     /* Regardless of how the flow table is configured, we want to be
230      * able to see replies to our DHCP requests. */
231     if (flow->dl_type == htons(ETH_TYPE_IP)
232             && flow->nw_proto == IPPROTO_UDP
233             && flow->tp_src == htons(DHCP_SERVER_PORT)
234             && flow->tp_dst == htons(DHCP_CLIENT_PORT)
235             && packet->l7) {
236         struct dhcp_header *dhcp;
237
238         dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
239                          sizeof *dhcp);
240         if (!dhcp) {
241             return false;
242         }
243
244         refresh_local(in_band);
245         if (!eth_addr_is_zero(in_band->local_mac)
246             && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
247             return true;
248         }
249     }
250
251     return false;
252 }
253
254 /* Returns true if the rule that would match 'flow' with 'actions' is
255  * allowed to be set up in the datapath. */
256 bool
257 in_band_rule_check(const struct flow *flow,
258                    const struct nlattr *actions, size_t actions_len)
259 {
260     /* Don't allow flows that would prevent DHCP replies from being seen
261      * by the local port. */
262     if (flow->dl_type == htons(ETH_TYPE_IP)
263             && flow->nw_proto == IPPROTO_UDP
264             && flow->tp_src == htons(DHCP_SERVER_PORT)
265             && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
266         const struct nlattr *a;
267         unsigned int left;
268
269         NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
270             if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT
271                 && nl_attr_get_u32(a) == ODPP_LOCAL) {
272                 return true;
273             }
274         }
275         return false;
276     }
277
278     return true;
279 }
280
281 static void
282 add_rule(struct in_band *ib, const struct cls_rule *cls_rule)
283 {
284     uint32_t hash = cls_rule_hash(cls_rule, 0);
285     struct in_band_rule *rule;
286
287     HMAP_FOR_EACH_WITH_HASH (rule, cls_rule.hmap_node, hash, &ib->rules) {
288         if (cls_rule_equal(&rule->cls_rule, cls_rule)) {
289             rule->op = ADD;
290             return;
291         }
292     }
293
294     rule = xmalloc(sizeof *rule);
295     rule->cls_rule = *cls_rule;
296     rule->op = ADD;
297     hmap_insert(&ib->rules, &rule->cls_rule.hmap_node, hash);
298 }
299
300 static void
301 update_rules(struct in_band *ib)
302 {
303     struct in_band_rule *ib_rule;
304     struct in_band_remote *r;
305     struct cls_rule rule;
306
307     /* Mark all the existing rules for deletion.  (Afterward we will re-add any
308      * rules that are still valid.) */
309     HMAP_FOR_EACH (ib_rule, cls_rule.hmap_node, &ib->rules) {
310         ib_rule->op = DELETE;
311     }
312
313     if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) {
314         /* (a) Allow DHCP requests sent from the local port. */
315         cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_DHCP);
316         cls_rule_set_in_port(&rule, ODPP_LOCAL);
317         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
318         cls_rule_set_dl_src(&rule, ib->local_mac);
319         cls_rule_set_nw_proto(&rule, IPPROTO_UDP);
320         cls_rule_set_tp_src(&rule, htons(DHCP_CLIENT_PORT));
321         cls_rule_set_tp_dst(&rule, htons(DHCP_SERVER_PORT));
322         add_rule(ib, &rule);
323
324         /* (b) Allow ARP replies to the local port's MAC address. */
325         cls_rule_init_catchall(&rule, IBR_TO_LOCAL_ARP);
326         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
327         cls_rule_set_dl_dst(&rule, ib->local_mac);
328         cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
329         add_rule(ib, &rule);
330
331         /* (c) Allow ARP requests from the local port's MAC address.  */
332         cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_ARP);
333         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
334         cls_rule_set_dl_src(&rule, ib->local_mac);
335         cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
336         add_rule(ib, &rule);
337     }
338
339     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
340         const uint8_t *remote_mac = r->remote_mac;
341
342         if (eth_addr_is_zero(remote_mac)) {
343             continue;
344         }
345
346         /* (d) Allow ARP replies to the next hop's MAC address. */
347         cls_rule_init_catchall(&rule, IBR_TO_NEXT_HOP_ARP);
348         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
349         cls_rule_set_dl_dst(&rule, remote_mac);
350         cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
351         add_rule(ib, &rule);
352
353         /* (e) Allow ARP requests from the next hop's MAC address. */
354         cls_rule_init_catchall(&rule, IBR_FROM_NEXT_HOP_ARP);
355         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
356         cls_rule_set_dl_src(&rule, remote_mac);
357         cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
358         add_rule(ib, &rule);
359     }
360
361     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
362         const struct sockaddr_in *a = &r->remote_addr;
363
364         /* (f) Allow ARP replies containing the remote's IP address as a
365          * target. */
366         cls_rule_init_catchall(&rule, IBR_TO_REMOTE_ARP);
367         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
368         cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
369         cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
370         add_rule(ib, &rule);
371
372         /* (g) Allow ARP requests containing the remote's IP address as a
373          * source. */
374         cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_ARP);
375         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
376         cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
377         cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
378         add_rule(ib, &rule);
379
380         /* (h) Allow TCP traffic to the remote's IP and port. */
381         cls_rule_init_catchall(&rule, IBR_TO_REMOTE_TCP);
382         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
383         cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
384         cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
385         cls_rule_set_tp_dst(&rule, a->sin_port);
386         add_rule(ib, &rule);
387
388         /* (i) Allow TCP traffic from the remote's IP and port. */
389         cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_TCP);
390         cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
391         cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
392         cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
393         cls_rule_set_tp_src(&rule, a->sin_port);
394         add_rule(ib, &rule);
395     }
396 }
397
398 /* Updates the OpenFlow flow table for the current state of in-band control.
399  * Returns true ordinarily.  Returns false if no remotes are configured on 'ib'
400  * and 'ib' doesn't have any rules left to remove from the OpenFlow flow
401  * table.  Thus, a false return value means that the caller can destroy 'ib'
402  * without leaving extra flows hanging around in the flow table. */
403 bool
404 in_band_run(struct in_band *ib)
405 {
406     struct {
407         struct nx_action_set_queue nxsq;
408         union ofp_action oa;
409     } actions;
410     const void *a;
411     size_t na;
412
413     struct in_band_rule *rule, *next;
414
415     memset(&actions, 0, sizeof actions);
416     actions.oa.output.type = htons(OFPAT_OUTPUT);
417     actions.oa.output.len = htons(sizeof actions.oa);
418     actions.oa.output.port = htons(OFPP_NORMAL);
419     actions.oa.output.max_len = htons(0);
420     if (ib->queue_id < 0) {
421         a = &actions.oa;
422         na = sizeof actions.oa / sizeof(union ofp_action);
423     } else {
424         actions.nxsq.type = htons(OFPAT_VENDOR);
425         actions.nxsq.len = htons(sizeof actions.nxsq);
426         actions.nxsq.vendor = htonl(NX_VENDOR_ID);
427         actions.nxsq.subtype = htons(NXAST_SET_QUEUE);
428         actions.nxsq.queue_id = htonl(ib->queue_id);
429         a = &actions;
430         na = sizeof actions / sizeof(union ofp_action);
431     }
432
433     refresh_local(ib);
434     refresh_remotes(ib);
435
436     update_rules(ib);
437
438     HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
439         switch (rule->op) {
440         case ADD:
441             ofproto_add_flow(ib->ofproto, &rule->cls_rule, a, na);
442             break;
443
444         case DELETE:
445             if (ofproto_delete_flow(ib->ofproto, &rule->cls_rule)) {
446                 /* ofproto doesn't have the rule anymore so there's no reason
447                  * for us to track it any longer. */
448                 hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
449                 free(rule);
450             }
451             break;
452         }
453     }
454
455     return ib->n_remotes || !hmap_is_empty(&ib->rules);
456 }
457
458 void
459 in_band_wait(struct in_band *in_band)
460 {
461     long long int wakeup
462             = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
463     poll_timer_wait_until(wakeup * 1000);
464 }
465
466 int
467 in_band_create(struct ofproto *ofproto, const char *local_name,
468                struct in_band **in_bandp)
469 {
470     struct in_band *in_band;
471     struct netdev *local_netdev;
472     int error;
473
474     *in_bandp = NULL;
475     error = netdev_open_default(local_name, &local_netdev);
476     if (error) {
477         VLOG_ERR("failed to initialize in-band control: cannot open "
478                  "datapath local port %s (%s)", local_name, strerror(error));
479         return error;
480     }
481
482     in_band = xzalloc(sizeof *in_band);
483     in_band->ofproto = ofproto;
484     in_band->queue_id = -1;
485     in_band->next_remote_refresh = TIME_MIN;
486     in_band->next_local_refresh = TIME_MIN;
487     in_band->local_netdev = local_netdev;
488     hmap_init(&in_band->rules);
489
490     *in_bandp = in_band;
491
492     return 0;
493 }
494
495 void
496 in_band_destroy(struct in_band *ib)
497 {
498     if (ib) {
499         struct in_band_rule *rule, *next;
500
501         HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
502             hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
503             free(rule);
504         }
505         hmap_destroy(&ib->rules);
506         in_band_set_remotes(ib, NULL, 0);
507         netdev_close(ib->local_netdev);
508         free(ib);
509     }
510 }
511
512 static bool
513 any_addresses_changed(struct in_band *ib,
514                       const struct sockaddr_in *addresses, size_t n)
515 {
516     size_t i;
517
518     if (n != ib->n_remotes) {
519         return true;
520     }
521
522     for (i = 0; i < n; i++) {
523         const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
524         const struct sockaddr_in *new = &addresses[i];
525
526         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
527             old->sin_port != new->sin_port) {
528             return true;
529         }
530     }
531
532     return false;
533 }
534
535 void
536 in_band_set_remotes(struct in_band *ib,
537                     const struct sockaddr_in *addresses, size_t n)
538 {
539     size_t i;
540
541     if (!any_addresses_changed(ib, addresses, n)) {
542         return;
543     }
544
545     /* Clear old remotes. */
546     for (i = 0; i < ib->n_remotes; i++) {
547         netdev_close(ib->remotes[i].remote_netdev);
548     }
549     free(ib->remotes);
550
551     /* Set up new remotes. */
552     ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
553     ib->n_remotes = n;
554     for (i = 0; i < n; i++) {
555         ib->remotes[i].remote_addr = addresses[i];
556     }
557
558     /* Force refresh in next call to in_band_run(). */
559     ib->next_remote_refresh = TIME_MIN;
560 }
561
562 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'.  If
563  * 'queue_id' is negative, 'ib' will not set any queue (which is also the
564  * default). */
565 void
566 in_band_set_queue(struct in_band *ib, int queue_id)
567 {
568     ib->queue_id = queue_id;
569 }
570