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