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"
41 VLOG_DEFINE_THIS_MODULE(in_band);
43 /* In-band control allows a single network to be used for OpenFlow traffic and
44 * other data traffic. See ovs-vswitchd.conf.db(5) for a description of
45 * configuring in-band control.
47 * This comment is an attempt to describe how in-band control works at a
48 * wire- and implementation-level. Correctly implementing in-band
49 * control has proven difficult due to its many subtleties, and has thus
50 * gone through many iterations. Please read through and understand the
51 * reasoning behind the chosen rules before making modifications.
53 * In Open vSwitch, in-band control is implemented as "hidden" flows (in that
54 * they are not visible through OpenFlow) and at a higher priority than
55 * wildcarded flows can be set up by through OpenFlow. This is done so that
56 * the OpenFlow controller cannot interfere with them and possibly break
57 * connectivity with its switches. It is possible to see all flows, including
58 * in-band ones, with the ovs-appctl "bridge/dump-flows" command.
60 * The Open vSwitch implementation of in-band control can hide traffic to
61 * arbitrary "remotes", where each remote is one TCP port on one IP address.
62 * Currently the remotes are automatically configured as the in-band OpenFlow
63 * controllers plus the OVSDB managers, if any. (The latter is a requirement
64 * because OVSDB managers are responsible for configuring OpenFlow controllers,
65 * so if the manager cannot be reached then OpenFlow cannot be reconfigured.)
67 * The following rules (with the OFPP_NORMAL action) are set up on any bridge
68 * that has any remotes:
70 * (a) DHCP requests sent from the local port.
71 * (b) ARP replies to the local port's MAC address.
72 * (c) ARP requests from the local port's MAC address.
74 * In-band also sets up the following rules for each unique next-hop MAC
75 * address for the remotes' IPs (the "next hop" is either the remote
76 * itself, if it is on a local subnet, or the gateway to reach the remote):
78 * (d) ARP replies to the next hop's MAC address.
79 * (e) ARP requests from the next hop's MAC address.
81 * In-band also sets up the following rules for each unique remote IP address:
83 * (f) ARP replies containing the remote's IP address as a target.
84 * (g) ARP requests containing the remote's IP address as a source.
86 * In-band also sets up the following rules for each unique remote (IP,port)
89 * (h) TCP traffic to the remote's IP and port.
90 * (i) TCP traffic from the remote's IP and port.
92 * The goal of these rules is to be as narrow as possible to allow a
93 * switch to join a network and be able to communicate with the
94 * remotes. As mentioned earlier, these rules have higher priority
95 * than the controller's rules, so if they are too broad, they may
96 * prevent the controller from implementing its policy. As such,
97 * in-band actively monitors some aspects of flow and packet processing
98 * so that the rules can be made more precise.
100 * In-band control monitors attempts to add flows into the datapath that
101 * could interfere with its duties. The datapath only allows exact
102 * match entries, so in-band control is able to be very precise about
103 * the flows it prevents. Flows that miss in the datapath are sent to
104 * userspace to be processed, so preventing these flows from being
105 * cached in the "fast path" does not affect correctness. The only type
106 * of flow that is currently prevented is one that would prevent DHCP
107 * replies from being seen by the local port. For example, a rule that
108 * forwarded all DHCP traffic to the controller would not be allowed,
109 * but one that forwarded to all ports (including the local port) would.
111 * As mentioned earlier, packets that miss in the datapath are sent to
112 * the userspace for processing. The userspace has its own flow table,
113 * the "classifier", so in-band checks whether any special processing
114 * is needed before the classifier is consulted. If a packet is a DHCP
115 * response to a request from the local port, the packet is forwarded to
116 * the local port, regardless of the flow table. Note that this requires
117 * L7 processing of DHCP replies to determine whether the 'chaddr' field
118 * matches the MAC address of the local port.
120 * It is interesting to note that for an L3-based in-band control
121 * mechanism, the majority of rules are devoted to ARP traffic. At first
122 * glance, some of these rules appear redundant. However, each serves an
123 * important role. First, in order to determine the MAC address of the
124 * remote side (controller or gateway) for other ARP rules, we must allow
125 * ARP traffic for our local port with rules (b) and (c). If we are
126 * between a switch and its connection to the remote, we have to
127 * allow the other switch's ARP traffic to through. This is done with
128 * rules (d) and (e), since we do not know the addresses of the other
129 * switches a priori, but do know the remote's or gateway's. Finally,
130 * if the remote is running in a local guest VM that is not reached
131 * through the local port, the switch that is connected to the VM must
132 * allow ARP traffic based on the remote's IP address, since it will
133 * not know the MAC address of the local port that is sending the traffic
134 * or the MAC address of the remote in the guest VM.
136 * With a few notable exceptions below, in-band should work in most
137 * network setups. The following are considered "supported' in the
138 * current implementation:
140 * - Locally Connected. The switch and remote are on the same
141 * subnet. This uses rules (a), (b), (c), (h), and (i).
143 * - Reached through Gateway. The switch and remote are on
144 * different subnets and must go through a gateway. This uses
145 * rules (a), (b), (c), (h), and (i).
147 * - Between Switch and Remote. This switch is between another
148 * switch and the remote, and we want to allow the other
149 * switch's traffic through. This uses rules (d), (e), (h), and
150 * (i). It uses (b) and (c) indirectly in order to know the MAC
151 * address for rules (d) and (e). Note that DHCP for the other
152 * switch will not work unless an OpenFlow controller explicitly lets this
153 * switch pass the traffic.
155 * - Between Switch and Gateway. This switch is between another
156 * switch and the gateway, and we want to allow the other switch's
157 * traffic through. This uses the same rules and logic as the
158 * "Between Switch and Remote" configuration described earlier.
160 * - Remote on Local VM. The remote is a guest VM on the
161 * system running in-band control. This uses rules (a), (b), (c),
164 * - Remote on Local VM with Different Networks. The remote
165 * is a guest VM on the system running in-band control, but the
166 * local port is not used to connect to the remote. For
167 * example, an IP address is configured on eth0 of the switch. The
168 * remote's VM is connected through eth1 of the switch, but an
169 * IP address has not been configured for that port on the switch.
170 * As such, the switch will use eth0 to connect to the remote,
171 * and eth1's rules about the local port will not work. In the
172 * example, the switch attached to eth0 would use rules (a), (b),
173 * (c), (h), and (i) on eth0. The switch attached to eth1 would use
174 * rules (f), (g), (h), and (i).
176 * The following are explicitly *not* supported by in-band control:
178 * - Specify Remote by Name. Currently, the remote must be
179 * identified by IP address. A naive approach would be to permit
180 * all DNS traffic. Unfortunately, this would prevent the
181 * controller from defining any policy over DNS. Since switches
182 * that are located behind us need to connect to the remote,
183 * in-band cannot simply add a rule that allows DNS traffic from
184 * the local port. The "correct" way to support this is to parse
185 * DNS requests to allow all traffic related to a request for the
186 * remote's name through. Due to the potential security
187 * problems and amount of processing, we decided to hold off for
190 * - Differing Remotes for Switches. All switches must know
191 * the L3 addresses for all the remotes that other switches
192 * may use, since rules need to be set up to allow traffic related
193 * to those remotes through. See rules (f), (g), (h), and (i).
195 * - Differing Routes for Switches. In order for the switch to
196 * allow other switches to connect to a remote through a
197 * gateway, it allows the gateway's traffic through with rules (d)
198 * and (e). If the routes to the remote differ for the two
199 * switches, we will not know the MAC address of the alternate
203 /* Priorities used in classifier for in-band rules. These values are higher
204 * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
205 * The ordering of priorities is not important because all of the rules set up
206 * by in-band control have the same action. The only reason to use more than
207 * one priority is to make the kind of flow easier to see during debugging. */
209 /* One set per bridge. */
210 IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
211 IBR_TO_LOCAL_ARP, /* (b) To local port, ARP. */
212 IBR_FROM_LOCAL_ARP, /* (c) From local port, ARP. */
214 /* One set per unique next-hop MAC. */
215 IBR_TO_NEXT_HOP_ARP, /* (d) To remote MAC, ARP. */
216 IBR_FROM_NEXT_HOP_ARP, /* (e) From remote MAC, ARP. */
218 /* One set per unique remote IP address. */
219 IBR_TO_REMOTE_ARP, /* (f) To remote IP, ARP. */
220 IBR_FROM_REMOTE_ARP, /* (g) From remote IP, ARP. */
222 /* One set per unique remote (IP,port) pair. */
223 IBR_TO_REMOTE_TCP, /* (h) To remote IP, TCP port. */
224 IBR_FROM_REMOTE_TCP /* (i) From remote IP, TCP port. */
227 /* Track one remote IP and next hop information. */
228 struct in_band_remote {
229 struct sockaddr_in remote_addr; /* IP address, in network byte order. */
230 uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
231 uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
232 struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
236 struct ofproto *ofproto;
237 int queue_id, prev_queue_id;
239 /* Remote information. */
240 time_t next_remote_refresh; /* Refresh timer. */
241 struct in_band_remote *remotes;
244 /* Local information. */
245 time_t next_local_refresh; /* Refresh timer. */
246 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
247 struct netdev *local_netdev; /* Local port's network device. */
249 /* Local and remote addresses that are installed as flows. */
250 uint8_t installed_local_mac[ETH_ADDR_LEN];
251 struct sockaddr_in *remote_addrs;
252 size_t n_remote_addrs;
253 uint8_t *remote_macs;
254 size_t n_remote_macs;
257 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
260 refresh_remote(struct in_band *ib, struct in_band_remote *r)
262 struct in_addr next_hop_inaddr;
266 /* Find the next-hop IP address. */
267 memset(r->remote_mac, 0, sizeof r->remote_mac);
268 retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
269 &next_hop_inaddr, &next_hop_dev);
271 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
272 IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
275 if (!next_hop_inaddr.s_addr) {
276 next_hop_inaddr = r->remote_addr.sin_addr;
279 /* Open the next-hop network device. */
280 if (!r->remote_netdev
281 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
283 netdev_close(r->remote_netdev);
285 retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
287 VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
288 "to controller "IP_FMT"): %s",
289 next_hop_dev, IP_ARGS(&r->remote_addr.sin_addr),
297 /* Look up the MAC address of the next-hop IP address. */
298 retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
301 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
302 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
305 /* If we don't have a MAC address, then refresh quickly, since we probably
306 * will get a MAC address soon (via ARP). Otherwise, we can afford to wait
308 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
312 refresh_remotes(struct in_band *ib)
314 struct in_band_remote *r;
317 if (time_now() < ib->next_remote_refresh) {
322 ib->next_remote_refresh = TIME_MAX;
323 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
324 uint8_t old_remote_mac[ETH_ADDR_LEN];
328 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
330 /* Refresh remote information. */
331 next_refresh = refresh_remote(ib, r) + time_now();
332 ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
334 /* If the MAC changed, log the changes. */
335 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
337 if (!eth_addr_is_zero(r->remote_mac)
338 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
339 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
341 ETH_ADDR_ARGS(r->last_remote_mac),
342 ETH_ADDR_ARGS(r->remote_mac));
343 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
351 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
352 * for a refresh. Returns true if anything changed, otherwise false. */
354 refresh_local(struct in_band *ib)
356 uint8_t ea[ETH_ADDR_LEN];
360 if (now < ib->next_local_refresh) {
363 ib->next_local_refresh = now + 1;
365 if (netdev_get_etheraddr(ib->local_netdev, ea)
366 || eth_addr_equals(ea, ib->local_mac)) {
370 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
374 /* Returns true if 'packet' should be sent to the local port regardless
375 * of the flow table. */
377 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
378 const struct ofpbuf *packet)
380 /* Regardless of how the flow table is configured, we want to be
381 * able to see replies to our DHCP requests. */
382 if (flow->dl_type == htons(ETH_TYPE_IP)
383 && flow->nw_proto == IPPROTO_UDP
384 && flow->tp_src == htons(DHCP_SERVER_PORT)
385 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
387 struct dhcp_header *dhcp;
389 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
395 refresh_local(in_band);
396 if (!eth_addr_is_zero(in_band->local_mac)
397 && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
405 /* Returns true if the rule that would match 'flow' with 'actions' is
406 * allowed to be set up in the datapath. */
408 in_band_rule_check(const struct flow *flow,
409 const struct nlattr *actions, size_t actions_len)
411 /* Don't allow flows that would prevent DHCP replies from being seen
412 * by the local port. */
413 if (flow->dl_type == htons(ETH_TYPE_IP)
414 && flow->nw_proto == IPPROTO_UDP
415 && flow->tp_src == htons(DHCP_SERVER_PORT)
416 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
417 const struct nlattr *a;
420 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
421 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT
422 && nl_attr_get_u32(a) == ODPP_LOCAL) {
433 make_rules(struct in_band *ib,
434 void (*cb)(struct in_band *, const struct cls_rule *))
436 struct cls_rule rule;
439 if (!eth_addr_is_zero(ib->installed_local_mac)) {
440 /* (a) Allow DHCP requests sent from the local port. */
441 cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_DHCP);
442 cls_rule_set_in_port(&rule, ODPP_LOCAL);
443 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
444 cls_rule_set_dl_src(&rule, ib->installed_local_mac);
445 cls_rule_set_nw_proto(&rule, IPPROTO_UDP);
446 cls_rule_set_tp_src(&rule, htons(DHCP_CLIENT_PORT));
447 cls_rule_set_tp_dst(&rule, htons(DHCP_SERVER_PORT));
450 /* (b) Allow ARP replies to the local port's MAC address. */
451 cls_rule_init_catchall(&rule, IBR_TO_LOCAL_ARP);
452 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
453 cls_rule_set_dl_dst(&rule, ib->installed_local_mac);
454 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
457 /* (c) Allow ARP requests from the local port's MAC address. */
458 cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_ARP);
459 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
460 cls_rule_set_dl_src(&rule, ib->installed_local_mac);
461 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
465 for (i = 0; i < ib->n_remote_macs; i++) {
466 const uint8_t *remote_mac = &ib->remote_macs[i * ETH_ADDR_LEN];
469 const uint8_t *prev_mac = &ib->remote_macs[(i - 1) * ETH_ADDR_LEN];
470 if (eth_addr_equals(remote_mac, prev_mac)) {
471 /* Skip duplicates. */
476 /* (d) Allow ARP replies to the next hop's MAC address. */
477 cls_rule_init_catchall(&rule, IBR_TO_NEXT_HOP_ARP);
478 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
479 cls_rule_set_dl_dst(&rule, remote_mac);
480 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
483 /* (e) Allow ARP requests from the next hop's MAC address. */
484 cls_rule_init_catchall(&rule, IBR_FROM_NEXT_HOP_ARP);
485 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
486 cls_rule_set_dl_src(&rule, remote_mac);
487 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
491 for (i = 0; i < ib->n_remote_addrs; i++) {
492 const struct sockaddr_in *a = &ib->remote_addrs[i];
494 if (!i || a->sin_addr.s_addr != a[-1].sin_addr.s_addr) {
495 /* (f) Allow ARP replies containing the remote's IP address as a
497 cls_rule_init_catchall(&rule, IBR_TO_REMOTE_ARP);
498 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
499 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
500 cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
503 /* (g) Allow ARP requests containing the remote's IP address as a
505 cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_ARP);
506 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
507 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
508 cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
513 || a->sin_addr.s_addr != a[-1].sin_addr.s_addr
514 || a->sin_port != a[-1].sin_port) {
515 /* (h) Allow TCP traffic to the remote's IP and port. */
516 cls_rule_init_catchall(&rule, IBR_TO_REMOTE_TCP);
517 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
518 cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
519 cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
520 cls_rule_set_tp_dst(&rule, a->sin_port);
523 /* (i) Allow TCP traffic from the remote's IP and port. */
524 cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_TCP);
525 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
526 cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
527 cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
528 cls_rule_set_tp_src(&rule, a->sin_port);
535 drop_rule(struct in_band *ib, const struct cls_rule *rule)
537 ofproto_delete_flow(ib->ofproto, rule);
540 /* Drops from the flow table all of the flows set up by 'ib', then clears out
541 * the information about the installed flows so that they can be filled in
542 * again if necessary. */
544 drop_rules(struct in_band *ib)
547 make_rules(ib, drop_rule);
549 /* Clear out state. */
550 memset(ib->installed_local_mac, 0, sizeof ib->installed_local_mac);
552 free(ib->remote_addrs);
553 ib->remote_addrs = NULL;
554 ib->n_remote_addrs = 0;
556 free(ib->remote_macs);
557 ib->remote_macs = NULL;
558 ib->n_remote_macs = 0;
562 add_rule(struct in_band *ib, const struct cls_rule *rule)
565 struct nx_action_set_queue nxsq;
566 struct ofp_action_output oao;
569 memset(&actions, 0, sizeof actions);
571 actions.oao.type = htons(OFPAT_OUTPUT);
572 actions.oao.len = htons(sizeof actions.oao);
573 actions.oao.port = htons(OFPP_NORMAL);
574 actions.oao.max_len = htons(0);
576 if (ib->queue_id < 0) {
577 ofproto_add_flow(ib->ofproto, rule,
578 (union ofp_action *) &actions.oao, 1);
580 actions.nxsq.type = htons(OFPAT_VENDOR);
581 actions.nxsq.len = htons(sizeof actions.nxsq);
582 actions.nxsq.vendor = htonl(NX_VENDOR_ID);
583 actions.nxsq.subtype = htons(NXAST_SET_QUEUE);
584 actions.nxsq.queue_id = htonl(ib->queue_id);
586 ofproto_add_flow(ib->ofproto, rule, (union ofp_action *) &actions,
587 sizeof actions / sizeof(union ofp_action));
591 /* Inserts flows into the flow table for the current state of 'ib'. */
593 add_rules(struct in_band *ib)
595 make_rules(ib, add_rule);
599 compare_addrs(const void *a_, const void *b_)
601 const struct sockaddr_in *a = a_;
602 const struct sockaddr_in *b = b_;
605 cmp = memcmp(&a->sin_addr.s_addr,
607 sizeof a->sin_addr.s_addr);
611 return memcmp(&a->sin_port, &b->sin_port, sizeof a->sin_port);
615 compare_macs(const void *a, const void *b)
617 return eth_addr_compare_3way(a, b);
621 in_band_run(struct in_band *ib)
623 bool local_change, remote_change, queue_id_change;
624 struct in_band_remote *r;
626 local_change = refresh_local(ib);
627 remote_change = refresh_remotes(ib);
628 queue_id_change = ib->queue_id != ib->prev_queue_id;
629 if (!local_change && !remote_change && !queue_id_change) {
630 /* Nothing changed, nothing to do. */
633 ib->prev_queue_id = ib->queue_id;
635 /* Drop old rules. */
638 /* Figure out new rules. */
639 memcpy(ib->installed_local_mac, ib->local_mac, ETH_ADDR_LEN);
640 ib->remote_addrs = xmalloc(ib->n_remotes * sizeof *ib->remote_addrs);
641 ib->n_remote_addrs = 0;
642 ib->remote_macs = xmalloc(ib->n_remotes * ETH_ADDR_LEN);
643 ib->n_remote_macs = 0;
644 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
645 ib->remote_addrs[ib->n_remote_addrs++] = r->remote_addr;
646 if (!eth_addr_is_zero(r->remote_mac)) {
647 memcpy(&ib->remote_macs[ib->n_remote_macs * ETH_ADDR_LEN],
648 r->remote_mac, ETH_ADDR_LEN);
653 /* Sort, to allow make_rules() to easily skip duplicates. */
654 qsort(ib->remote_addrs, ib->n_remote_addrs, sizeof *ib->remote_addrs,
656 qsort(ib->remote_macs, ib->n_remote_macs, ETH_ADDR_LEN, compare_macs);
663 in_band_wait(struct in_band *in_band)
666 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
667 poll_timer_wait_until(wakeup * 1000);
670 /* ofproto has flushed all flows from the flow table and it is calling us back
671 * to allow us to reinstall the ones that are important to us. */
673 in_band_flushed(struct in_band *in_band)
679 in_band_create(struct ofproto *ofproto, const char *local_name,
680 struct in_band **in_bandp)
682 struct in_band *in_band;
683 struct netdev *local_netdev;
687 error = netdev_open_default(local_name, &local_netdev);
689 VLOG_ERR("failed to initialize in-band control: cannot open "
690 "datapath local port %s (%s)", local_name, strerror(error));
694 in_band = xzalloc(sizeof *in_band);
695 in_band->ofproto = ofproto;
696 in_band->queue_id = in_band->prev_queue_id = -1;
697 in_band->next_remote_refresh = TIME_MIN;
698 in_band->next_local_refresh = TIME_MIN;
699 in_band->local_netdev = local_netdev;
707 in_band_destroy(struct in_band *ib)
711 in_band_set_remotes(ib, NULL, 0);
712 netdev_close(ib->local_netdev);
718 any_addresses_changed(struct in_band *ib,
719 const struct sockaddr_in *addresses, size_t n)
723 if (n != ib->n_remotes) {
727 for (i = 0; i < n; i++) {
728 const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
729 const struct sockaddr_in *new = &addresses[i];
731 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
732 old->sin_port != new->sin_port) {
741 in_band_set_remotes(struct in_band *ib,
742 const struct sockaddr_in *addresses, size_t n)
746 if (!any_addresses_changed(ib, addresses, n)) {
750 /* Clear old remotes. */
751 for (i = 0; i < ib->n_remotes; i++) {
752 netdev_close(ib->remotes[i].remote_netdev);
756 /* Set up new remotes. */
757 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
759 for (i = 0; i < n; i++) {
760 ib->remotes[i].remote_addr = addresses[i];
763 /* Force refresh in next call to in_band_run(). */
764 ib->next_remote_refresh = TIME_MIN;
767 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'. If
768 * 'queue_id' is negative, 'ib' will not set any queue (which is also the
771 in_band_set_queue(struct in_band *ib, int queue_id)
773 ib->queue_id = queue_id;