Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.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 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(next_hop_dev, "system", &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) == OVS_ACTION_ATTR_OUTPUT
271                 && nl_attr_get_u32(a) == OVSP_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, OFPP_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     uint64_t ofpacts_stub[128 / 8];
407     struct ofpbuf ofpacts;
408
409     struct in_band_rule *rule, *next;
410
411     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
412
413     if (ib->queue_id >= 0) {
414         ofpact_put_SET_QUEUE(&ofpacts)->queue_id = ib->queue_id;
415     }
416     ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
417
418     refresh_local(ib);
419     refresh_remotes(ib);
420
421     update_rules(ib);
422
423     HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
424         switch (rule->op) {
425         case ADD:
426             ofproto_add_flow(ib->ofproto, &rule->cls_rule,
427                              ofpacts.data, ofpacts.size);
428             break;
429
430         case DELETE:
431             if (ofproto_delete_flow(ib->ofproto, &rule->cls_rule)) {
432                 /* ofproto doesn't have the rule anymore so there's no reason
433                  * for us to track it any longer. */
434                 hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
435                 free(rule);
436             }
437             break;
438         }
439     }
440
441     ofpbuf_uninit(&ofpacts);
442
443     return ib->n_remotes || !hmap_is_empty(&ib->rules);
444 }
445
446 void
447 in_band_wait(struct in_band *in_band)
448 {
449     long long int wakeup
450             = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
451     poll_timer_wait_until(wakeup * 1000);
452 }
453
454 int
455 in_band_create(struct ofproto *ofproto, const char *local_name,
456                struct in_band **in_bandp)
457 {
458     struct in_band *in_band;
459     struct netdev *local_netdev;
460     int error;
461
462     *in_bandp = NULL;
463     error = netdev_open(local_name, "system", &local_netdev);
464     if (error) {
465         VLOG_ERR("failed to initialize in-band control: cannot open "
466                  "datapath local port %s (%s)", local_name, strerror(error));
467         return error;
468     }
469
470     in_band = xzalloc(sizeof *in_band);
471     in_band->ofproto = ofproto;
472     in_band->queue_id = -1;
473     in_band->next_remote_refresh = TIME_MIN;
474     in_band->next_local_refresh = TIME_MIN;
475     in_band->local_netdev = local_netdev;
476     hmap_init(&in_band->rules);
477
478     *in_bandp = in_band;
479
480     return 0;
481 }
482
483 void
484 in_band_destroy(struct in_band *ib)
485 {
486     if (ib) {
487         struct in_band_rule *rule, *next;
488
489         HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
490             hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
491             free(rule);
492         }
493         hmap_destroy(&ib->rules);
494         in_band_set_remotes(ib, NULL, 0);
495         netdev_close(ib->local_netdev);
496         free(ib);
497     }
498 }
499
500 static bool
501 any_addresses_changed(struct in_band *ib,
502                       const struct sockaddr_in *addresses, size_t n)
503 {
504     size_t i;
505
506     if (n != ib->n_remotes) {
507         return true;
508     }
509
510     for (i = 0; i < n; i++) {
511         const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
512         const struct sockaddr_in *new = &addresses[i];
513
514         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
515             old->sin_port != new->sin_port) {
516             return true;
517         }
518     }
519
520     return false;
521 }
522
523 void
524 in_band_set_remotes(struct in_band *ib,
525                     const struct sockaddr_in *addresses, size_t n)
526 {
527     size_t i;
528
529     if (!any_addresses_changed(ib, addresses, n)) {
530         return;
531     }
532
533     /* Clear old remotes. */
534     for (i = 0; i < ib->n_remotes; i++) {
535         netdev_close(ib->remotes[i].remote_netdev);
536     }
537     free(ib->remotes);
538
539     /* Set up new remotes. */
540     ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
541     ib->n_remotes = n;
542     for (i = 0; i < n; i++) {
543         ib->remotes[i].remote_addr = addresses[i];
544     }
545
546     /* Force refresh in next call to in_band_run(). */
547     ib->next_remote_refresh = TIME_MIN;
548 }
549
550 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'.  If
551  * 'queue_id' is negative, 'ib' will not set any queue (which is also the
552  * default). */
553 void
554 in_band_set_queue(struct in_band *ib, int queue_id)
555 {
556     ib->queue_id = queue_id;
557 }
558