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