2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <arpa/inet.h>
22 #include <sys/socket.h>
26 #include "classifier.h"
35 #include "openflow/openflow.h"
37 #include "poll-loop.h"
42 VLOG_DEFINE_THIS_MODULE(in_band);
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. */
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. */
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. */
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. */
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. */
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. */
77 struct ofproto *ofproto;
78 int queue_id, prev_queue_id;
80 /* Remote information. */
81 time_t next_remote_refresh; /* Refresh timer. */
82 struct in_band_remote *remotes;
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. */
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;
98 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
101 refresh_remote(struct in_band *ib, struct in_band_remote *r)
103 struct in_addr next_hop_inaddr;
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);
112 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
113 IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
116 if (!next_hop_inaddr.s_addr) {
117 next_hop_inaddr = r->remote_addr.sin_addr;
120 /* Open the next-hop network device. */
121 if (!r->remote_netdev
122 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
124 netdev_close(r->remote_netdev);
126 retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
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),
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,
142 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
143 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
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
149 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
153 refresh_remotes(struct in_band *ib)
155 struct in_band_remote *r;
158 if (time_now() < ib->next_remote_refresh) {
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];
169 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
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);
175 /* If the MAC changed, log the changes. */
176 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
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
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);
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. */
195 refresh_local(struct in_band *ib)
197 uint8_t ea[ETH_ADDR_LEN];
201 if (now < ib->next_local_refresh) {
204 ib->next_local_refresh = now + 1;
206 if (netdev_get_etheraddr(ib->local_netdev, ea)
207 || eth_addr_equals(ea, ib->local_mac)) {
211 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
215 /* Returns true if 'packet' should be sent to the local port regardless
216 * of the flow table. */
218 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
219 const struct ofpbuf *packet)
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)
228 struct dhcp_header *dhcp;
230 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
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)) {
246 /* Returns true if the rule that would match 'flow' with 'actions' is
247 * allowed to be set up in the datapath. */
249 in_band_rule_check(const struct flow *flow,
250 const struct nlattr *actions, size_t actions_len)
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;
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) {
274 make_rules(struct in_band *ib,
275 void (*cb)(struct in_band *, const struct cls_rule *))
277 struct cls_rule rule;
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));
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);
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);
306 for (i = 0; i < ib->n_remote_macs; i++) {
307 const uint8_t *remote_mac = &ib->remote_macs[i * ETH_ADDR_LEN];
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. */
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);
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);
332 for (i = 0; i < ib->n_remote_addrs; i++) {
333 const struct sockaddr_in *a = &ib->remote_addrs[i];
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
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);
344 /* (g) Allow ARP requests containing the remote's IP address as a
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);
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);
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);
376 drop_rule(struct in_band *ib, const struct cls_rule *rule)
378 ofproto_delete_flow(ib->ofproto, rule);
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. */
385 drop_rules(struct in_band *ib)
388 make_rules(ib, drop_rule);
390 /* Clear out state. */
391 memset(ib->installed_local_mac, 0, sizeof ib->installed_local_mac);
393 free(ib->remote_addrs);
394 ib->remote_addrs = NULL;
395 ib->n_remote_addrs = 0;
397 free(ib->remote_macs);
398 ib->remote_macs = NULL;
399 ib->n_remote_macs = 0;
403 add_rule(struct in_band *ib, const struct cls_rule *rule)
406 struct nx_action_set_queue nxsq;
410 memset(&actions, 0, sizeof actions);
412 actions.oa.output.type = htons(OFPAT_OUTPUT);
413 actions.oa.output.len = htons(sizeof actions.oa);
414 actions.oa.output.port = htons(OFPP_NORMAL);
415 actions.oa.output.max_len = htons(0);
417 if (ib->queue_id < 0) {
418 ofproto_add_flow(ib->ofproto, rule, &actions.oa, 1);
420 actions.nxsq.type = htons(OFPAT_VENDOR);
421 actions.nxsq.len = htons(sizeof actions.nxsq);
422 actions.nxsq.vendor = htonl(NX_VENDOR_ID);
423 actions.nxsq.subtype = htons(NXAST_SET_QUEUE);
424 actions.nxsq.queue_id = htonl(ib->queue_id);
426 ofproto_add_flow(ib->ofproto, rule, (union ofp_action *) &actions,
427 sizeof actions / sizeof(union ofp_action));
431 /* Inserts flows into the flow table for the current state of 'ib'. */
433 add_rules(struct in_band *ib)
435 make_rules(ib, add_rule);
439 compare_addrs(const void *a_, const void *b_)
441 const struct sockaddr_in *a = a_;
442 const struct sockaddr_in *b = b_;
445 cmp = memcmp(&a->sin_addr.s_addr,
447 sizeof a->sin_addr.s_addr);
451 return memcmp(&a->sin_port, &b->sin_port, sizeof a->sin_port);
455 compare_macs(const void *a, const void *b)
457 return eth_addr_compare_3way(a, b);
461 in_band_run(struct in_band *ib)
463 bool local_change, remote_change, queue_id_change;
464 struct in_band_remote *r;
466 local_change = refresh_local(ib);
467 remote_change = refresh_remotes(ib);
468 queue_id_change = ib->queue_id != ib->prev_queue_id;
469 if (!local_change && !remote_change && !queue_id_change) {
470 /* Nothing changed, nothing to do. */
473 ib->prev_queue_id = ib->queue_id;
475 /* Drop old rules. */
478 /* Figure out new rules. */
479 memcpy(ib->installed_local_mac, ib->local_mac, ETH_ADDR_LEN);
480 ib->remote_addrs = xmalloc(ib->n_remotes * sizeof *ib->remote_addrs);
481 ib->n_remote_addrs = 0;
482 ib->remote_macs = xmalloc(ib->n_remotes * ETH_ADDR_LEN);
483 ib->n_remote_macs = 0;
484 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
485 ib->remote_addrs[ib->n_remote_addrs++] = r->remote_addr;
486 if (!eth_addr_is_zero(r->remote_mac)) {
487 memcpy(&ib->remote_macs[ib->n_remote_macs * ETH_ADDR_LEN],
488 r->remote_mac, ETH_ADDR_LEN);
493 /* Sort, to allow make_rules() to easily skip duplicates. */
494 qsort(ib->remote_addrs, ib->n_remote_addrs, sizeof *ib->remote_addrs,
496 qsort(ib->remote_macs, ib->n_remote_macs, ETH_ADDR_LEN, compare_macs);
503 in_band_wait(struct in_band *in_band)
506 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
507 poll_timer_wait_until(wakeup * 1000);
510 /* ofproto has flushed all flows from the flow table and it is calling us back
511 * to allow us to reinstall the ones that are important to us. */
513 in_band_flushed(struct in_band *in_band)
519 in_band_create(struct ofproto *ofproto, const char *local_name,
520 struct in_band **in_bandp)
522 struct in_band *in_band;
523 struct netdev *local_netdev;
527 error = netdev_open_default(local_name, &local_netdev);
529 VLOG_ERR("failed to initialize in-band control: cannot open "
530 "datapath local port %s (%s)", local_name, strerror(error));
534 in_band = xzalloc(sizeof *in_band);
535 in_band->ofproto = ofproto;
536 in_band->queue_id = in_band->prev_queue_id = -1;
537 in_band->next_remote_refresh = TIME_MIN;
538 in_band->next_local_refresh = TIME_MIN;
539 in_band->local_netdev = local_netdev;
547 in_band_destroy(struct in_band *ib)
551 in_band_set_remotes(ib, NULL, 0);
552 netdev_close(ib->local_netdev);
558 any_addresses_changed(struct in_band *ib,
559 const struct sockaddr_in *addresses, size_t n)
563 if (n != ib->n_remotes) {
567 for (i = 0; i < n; i++) {
568 const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
569 const struct sockaddr_in *new = &addresses[i];
571 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
572 old->sin_port != new->sin_port) {
581 in_band_set_remotes(struct in_band *ib,
582 const struct sockaddr_in *addresses, size_t n)
586 if (!any_addresses_changed(ib, addresses, n)) {
590 /* Clear old remotes. */
591 for (i = 0; i < ib->n_remotes; i++) {
592 netdev_close(ib->remotes[i].remote_netdev);
596 /* Set up new remotes. */
597 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
599 for (i = 0; i < n; i++) {
600 ib->remotes[i].remote_addr = addresses[i];
603 /* Force refresh in next call to in_band_run(). */
604 ib->next_remote_refresh = TIME_MIN;
607 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'. If
608 * 'queue_id' is negative, 'ib' will not set any queue (which is also the
611 in_band_set_queue(struct in_band *ib, int queue_id)
613 ib->queue_id = queue_id;