]> git.proxmox.com Git - ovs.git/blob - ofproto/in-band.c
4efcbca66359fa1f3d189cf63068741a0899ef60
[ovs.git] / ofproto / in-band.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 <net/if.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "dhcp.h"
26 #include "dpif.h"
27 #include "flow.h"
28 #include "mac-learning.h"
29 #include "netdev.h"
30 #include "odp-util.h"
31 #include "ofp-print.h"
32 #include "ofproto.h"
33 #include "ofpbuf.h"
34 #include "openflow/openflow.h"
35 #include "openvswitch/datapath-protocol.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "rconn.h"
39 #include "status.h"
40 #include "timeval.h"
41 #include "vconn.h"
42
43 #define THIS_MODULE VLM_in_band
44 #include "vlog.h"
45
46 /* In-band control allows a single network to be used for OpenFlow
47 * traffic and other data traffic. Refer to ovs-vswitchd.conf(5) and
48 * secchan(8) for a description of configuring in-band control.
49 *
50 * This comment is an attempt to describe how in-band control works at a
51 * wire- and implementation-level. Correctly implementing in-band
52 * control has proven difficult due to its many subtleties, and has thus
53 * gone through many iterations. Please read through and understand the
54 * reasoning behind the chosen rules before making modifications.
55 *
56 * In Open vSwitch, in-band control is implemented as "hidden" flows (in
57 * that they are not visible through OpenFlow) and at a higher priority
58 * than wildcarded flows can be set up by the controller. This is done
59 * so that the controller cannot interfere with them and possibly break
60 * connectivity with its switches. It is possible to see all flows,
61 * including in-band ones, with the ovs-appctl "bridge/dump-flows"
62 * command.
63 *
64 * The following rules are always enabled with the "normal" action by a
65 * switch with in-band control:
66 *
67 * a. DHCP requests sent from the local port.
68 * b. ARP replies to the local port's MAC address.
69 * c. ARP requests from the local port's MAC address.
70 * d. ARP replies to the remote side's MAC address. Note that the
71 * remote side is either the controller or the gateway to reach
72 * the controller.
73 * e. ARP requests from the remote side's MAC address. Note that
74 * like (d), the MAC is either for the controller or gateway.
75 * f. ARP replies containing the controller's IP address as a target.
76 * g. ARP requests containing the controller's IP address as a source.
77 * h. OpenFlow (6633/tcp) traffic to the controller's IP.
78 * i. OpenFlow (6633/tcp) traffic from the controller's IP.
79 *
80 * The goal of these rules is to be as narrow as possible to allow a
81 * switch to join a network and be able to communicate with a
82 * controller. As mentioned earlier, these rules have higher priority
83 * than the controller's rules, so if they are too broad, they may
84 * prevent the controller from implementing its policy. As such,
85 * in-band actively monitors some aspects of flow and packet processing
86 * so that the rules can be made more precise.
87 *
88 * In-band control monitors attempts to add flows into the datapath that
89 * could interfere with its duties. The datapath only allows exact
90 * match entries, so in-band control is able to be very precise about
91 * the flows it prevents. Flows that miss in the datapath are sent to
92 * userspace to be processed, so preventing these flows from being
93 * cached in the "fast path" does not affect correctness. The only type
94 * of flow that is currently prevented is one that would prevent DHCP
95 * replies from being seen by the local port. For example, a rule that
96 * forwarded all DHCP traffic to the controller would not be allowed,
97 * but one that forwarded to all ports (including the local port) would.
98 *
99 * As mentioned earlier, packets that miss in the datapath are sent to
100 * the userspace for processing. The userspace has its own flow table,
101 * the "classifier", so in-band checks whether any special processing
102 * is needed before the classifier is consulted. If a packet is a DHCP
103 * response to a request from the local port, the packet is forwarded to
104 * the local port, regardless of the flow table. Note that this requires
105 * L7 processing of DHCP replies to determine whether the 'chaddr' field
106 * matches the MAC address of the local port.
107 *
108 * It is interesting to note that for an L3-based in-band control
109 * mechanism, the majority of rules are devoted to ARP traffic. At first
110 * glance, some of these rules appear redundant. However, each serves an
111 * important role. First, in order to determine the MAC address of the
112 * remote side (controller or gateway) for other ARP rules, we must allow
113 * ARP traffic for our local port with rules (b) and (c). If we are
114 * between a switch and its connection to the controller, we have to
115 * allow the other switch's ARP traffic to through. This is done with
116 * rules (d) and (e), since we do not know the addresses of the other
117 * switches a priori, but do know the controller's or gateway's. Finally,
118 * if the controller is running in a local guest VM that is not reached
119 * through the local port, the switch that is connected to the VM must
120 * allow ARP traffic based on the controller's IP address, since it will
121 * not know the MAC address of the local port that is sending the traffic
122 * or the MAC address of the controller in the guest VM.
123 *
124 * With a few notable exceptions below, in-band should work in most
125 * network setups. The following are considered "supported' in the
126 * current implementation:
127 *
128 * - Locally Connected. The switch and controller are on the same
129 * subnet. This uses rules (a), (b), (c), (h), and (i).
130 *
131 * - Reached through Gateway. The switch and controller are on
132 * different subnets and must go through a gateway. This uses
133 * rules (a), (b), (c), (h), and (i).
134 *
135 * - Between Switch and Controller. This switch is between another
136 * switch and the controller, and we want to allow the other
137 * switch's traffic through. This uses rules (d), (e), (h), and
138 * (i). It uses (b) and (c) indirectly in order to know the MAC
139 * address for rules (d) and (e). Note that DHCP for the other
140 * switch will not work unless the controller explicitly lets this
141 * switch pass the traffic.
142 *
143 * - Between Switch and Gateway. This switch is between another
144 * switch and the gateway, and we want to allow the other switch's
145 * traffic through. This uses the same rules and logic as the
146 * "Between Switch and Controller" configuration described earlier.
147 *
148 * - Controller on Local VM. The controller is a guest VM on the
149 * system running in-band control. This uses rules (a), (b), (c),
150 * (h), and (i).
151 *
152 * - Controller on Local VM with Different Networks. The controller
153 * is a guest VM on the system running in-band control, but the
154 * local port is not used to connect to the controller. For
155 * example, an IP address is configured on eth0 of the switch. The
156 * controller's VM is connected through eth1 of the switch, but an
157 * IP address has not been configured for that port on the switch.
158 * As such, the switch will use eth0 to connect to the controller,
159 * and eth1's rules about the local port will not work. In the
160 * example, the switch attached to eth0 would use rules (a), (b),
161 * (c), (h), and (i) on eth0. The switch attached to eth1 would use
162 * rules (f), (g), (h), and (i).
163 *
164 * The following are explicitly *not* supported by in-band control:
165 *
166 * - Specify Controller by Name. Currently, the controller must be
167 * identified by IP address. A naive approach would be to permit
168 * all DNS traffic. Unfortunately, this would prevent the
169 * controller from defining any policy over DNS. Since switches
170 * that are located behind us need to connect to the controller,
171 * in-band cannot simply add a rule that allows DNS traffic from
172 * the local port. The "correct" way to support this is to parse
173 * DNS requests to allow all traffic related to a request for the
174 * controller's name through. Due to the potential security
175 * problems and amount of processing, we decided to hold off for
176 * the time-being.
177 *
178 * - Differing Controllers for Switches. All switches must know
179 * the L3 addresses for all the controllers that other switches
180 * may use, since rules need to be set up to allow traffic related
181 * to those controllers through. See rules (f), (g), (h), and (i).
182 *
183 * - Differing Routes for Switches. In order for the switch to
184 * allow other switches to connect to a controller through a
185 * gateway, it allows the gateway's traffic through with rules (d)
186 * and (e). If the routes to the controller differ for the two
187 * switches, we will not know the MAC address of the alternate
188 * gateway.
189 */
190
191 /* Priorities used in classifier for in-band rules. These values are higher
192 * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
193 * The ordering of priorities is not important because all of the rules set up
194 * by in-band control have the same action. The only reason to use more than
195 * one priority is to make the kind of flow easier to see during debugging. */
196 enum {
197 IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
198 IBR_TO_LOCAL_ARP, /* (b) To local port, ARP. */
199 IBR_FROM_LOCAL_ARP, /* (c) From local port, ARP. */
200 IBR_TO_REMOTE_ARP, /* (d) To remote MAC, ARP. */
201 IBR_FROM_REMOTE_ARP, /* (e) From remote MAC, ARP. */
202 IBR_TO_CTL_ARP, /* (f) To controller IP, ARP. */
203 IBR_FROM_CTL_ARP, /* (g) From controller IP, ARP. */
204 IBR_TO_CTL_OFP, /* (h) To controller, OpenFlow port. */
205 IBR_FROM_CTL_OFP /* (i) From controller, OpenFlow port. */
206 };
207
208 struct in_band_rule {
209 flow_t flow;
210 uint32_t wildcards;
211 unsigned int priority;
212 };
213
214 /* Track one remote IP and next hop information. */
215 struct in_band_remote {
216 struct rconn *rconn; /* Connection to remote. */
217 uint32_t remote_ip; /* Remote IP, 0 if unknown. */
218 uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
219 uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
220 struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
221 };
222
223 struct in_band {
224 struct ofproto *ofproto;
225 struct status_category *ss_cat;
226
227 /* Remote information. */
228 time_t next_remote_refresh; /* Refresh timer. */
229 struct in_band_remote *remotes;
230 size_t n_remotes;
231
232 /* Local information. */
233 time_t next_local_refresh; /* Refresh timer. */
234 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
235 struct netdev *local_netdev; /* Local port's network device. */
236
237 /* Local and remote addresses that are installed as flows. */
238 uint8_t installed_local_mac[ETH_ADDR_LEN];
239 uint32_t *remote_ips;
240 uint32_t n_remote_ips;
241 uint8_t *remote_macs;
242 size_t n_remote_macs;
243 };
244
245 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
246
247 static int
248 refresh_remote(struct in_band *ib, struct in_band_remote *r)
249 {
250 struct in_addr remote_inaddr;
251 struct in_addr next_hop_inaddr;
252 char *next_hop_dev;
253 int retval;
254
255 memset(r->remote_mac, 0, sizeof r->remote_mac);
256
257 /* Get remote IP address. */
258 r->remote_ip = rconn_get_remote_ip(r->rconn);
259 if (!r->remote_ip) {
260 /* No remote IP address means that this rconn is probably either
261 * configured for a non-IP based protocol (e.g. "unix:") or
262 * misconfigured entirely. No point in refreshing quickly. */
263 return 10;
264 }
265
266 /* Find the next-hop IP address. */
267 remote_inaddr.s_addr = r->remote_ip;
268 retval = netdev_get_next_hop(ib->local_netdev, &remote_inaddr,
269 &next_hop_inaddr, &next_hop_dev);
270 if (retval) {
271 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
272 IP_ARGS(&r->remote_ip), strerror(retval));
273 return 1;
274 }
275 if (!next_hop_inaddr.s_addr) {
276 next_hop_inaddr.s_addr = remote_inaddr.s_addr;
277 }
278
279 /* Get the next-hop IP and network device. */
280 if (!r->remote_netdev
281 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
282 {
283 netdev_close(r->remote_netdev);
284
285 retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
286 if (retval) {
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_ip),
290 strerror(retval));
291 free(next_hop_dev);
292 return 1;
293 }
294 }
295 free(next_hop_dev);
296
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,
299 r->remote_mac);
300 if (retval) {
301 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
302 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
303 }
304
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
307 * a little while. */
308 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
309 }
310
311 static bool
312 refresh_remotes(struct in_band *ib)
313 {
314 struct in_band_remote *r;
315 bool any_changes;
316
317 if (time_now() < ib->next_remote_refresh) {
318 return false;
319 }
320
321 any_changes = false;
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];
325 time_t next_refresh;
326
327 /* Save old MAC. */
328 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
329
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);
333
334 /* If the MAC changed, log the changes. */
335 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
336 any_changes = true;
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
340 " to "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);
344 }
345 }
346 }
347
348 return any_changes;
349 }
350
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. */
353 static bool
354 refresh_local(struct in_band *ib)
355 {
356 uint8_t ea[ETH_ADDR_LEN];
357 time_t now;
358
359 now = time_now();
360 if (now < ib->next_local_refresh) {
361 return false;
362 }
363 ib->next_local_refresh = now + 1;
364
365 if (netdev_get_etheraddr(ib->local_netdev, ea)
366 || eth_addr_equals(ea, ib->local_mac)) {
367 return false;
368 }
369
370 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
371 return true;
372 }
373
374 static void
375 in_band_status_cb(struct status_reply *sr, void *in_band_)
376 {
377 struct in_band *in_band = in_band_;
378
379 if (!eth_addr_is_zero(in_band->local_mac)) {
380 status_reply_put(sr, "local-mac="ETH_ADDR_FMT,
381 ETH_ADDR_ARGS(in_band->local_mac));
382 }
383
384 if (in_band->n_remotes
385 && !eth_addr_is_zero(in_band->remotes[0].remote_mac)) {
386 status_reply_put(sr, "remote-mac="ETH_ADDR_FMT,
387 ETH_ADDR_ARGS(in_band->remotes[0].remote_mac));
388 }
389 }
390
391 /* Returns true if 'packet' should be sent to the local port regardless
392 * of the flow table. */
393 bool
394 in_band_msg_in_hook(struct in_band *in_band, const flow_t *flow,
395 const struct ofpbuf *packet)
396 {
397 if (!in_band) {
398 return false;
399 }
400
401 /* Regardless of how the flow table is configured, we want to be
402 * able to see replies to our DHCP requests. */
403 if (flow->dl_type == htons(ETH_TYPE_IP)
404 && flow->nw_proto == IP_TYPE_UDP
405 && flow->tp_src == htons(DHCP_SERVER_PORT)
406 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
407 && packet->l7) {
408 struct dhcp_header *dhcp;
409
410 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
411 sizeof *dhcp);
412 if (!dhcp) {
413 return false;
414 }
415
416 refresh_local(in_band);
417 if (!eth_addr_is_zero(in_band->local_mac)
418 && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
419 return true;
420 }
421 }
422
423 return false;
424 }
425
426 /* Returns true if the rule that would match 'flow' with 'actions' is
427 * allowed to be set up in the datapath. */
428 bool
429 in_band_rule_check(struct in_band *in_band, const flow_t *flow,
430 const struct odp_actions *actions)
431 {
432 if (!in_band) {
433 return true;
434 }
435
436 /* Don't allow flows that would prevent DHCP replies from being seen
437 * by the local port. */
438 if (flow->dl_type == htons(ETH_TYPE_IP)
439 && flow->nw_proto == IP_TYPE_UDP
440 && flow->tp_src == htons(DHCP_SERVER_PORT)
441 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
442 int i;
443
444 for (i=0; i<actions->n_actions; i++) {
445 if (actions->actions[i].output.type == ODPAT_OUTPUT
446 && actions->actions[i].output.port == ODPP_LOCAL) {
447 return true;
448 }
449 }
450 return false;
451 }
452
453 return true;
454 }
455
456 static void
457 init_rule(struct in_band_rule *rule, unsigned int priority)
458 {
459 rule->wildcards = OVSFW_ALL;
460 rule->priority = priority;
461
462 /* Not strictly necessary but seems cleaner. */
463 memset(&rule->flow, 0, sizeof rule->flow);
464 }
465
466 static void
467 set_in_port(struct in_band_rule *rule, uint16_t odp_port)
468 {
469 rule->wildcards &= ~OFPFW_IN_PORT;
470 rule->flow.in_port = odp_port;
471 }
472
473 static void
474 set_dl_type(struct in_band_rule *rule, uint16_t dl_type)
475 {
476 rule->wildcards &= ~OFPFW_DL_TYPE;
477 rule->flow.dl_type = htons(dl_type);
478 }
479
480 static void
481 set_dl_src(struct in_band_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
482 {
483 rule->wildcards &= ~OFPFW_DL_SRC;
484 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
485 }
486
487 static void
488 set_dl_dst(struct in_band_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
489 {
490 rule->wildcards &= ~OFPFW_DL_DST;
491 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
492 }
493
494 static void
495 set_tp_src(struct in_band_rule *rule, uint16_t tp_src)
496 {
497 rule->wildcards &= ~OFPFW_TP_SRC;
498 rule->flow.tp_src = htons(tp_src);
499 }
500
501 static void
502 set_tp_dst(struct in_band_rule *rule, uint16_t tp_dst)
503 {
504 rule->wildcards &= ~OFPFW_TP_DST;
505 rule->flow.tp_dst = htons(tp_dst);
506 }
507
508 static void
509 set_nw_proto(struct in_band_rule *rule, uint8_t nw_proto)
510 {
511 rule->wildcards &= ~OFPFW_NW_PROTO;
512 rule->flow.nw_proto = nw_proto;
513 }
514
515 static void
516 set_nw_src(struct in_band_rule *rule, uint32_t nw_src)
517 {
518 rule->wildcards &= ~OFPFW_NW_SRC_MASK;
519 rule->flow.nw_src = nw_src;
520 }
521
522 static void
523 set_nw_dst(struct in_band_rule *rule, uint32_t nw_dst)
524 {
525 rule->wildcards &= ~OFPFW_NW_DST_MASK;
526 rule->flow.nw_dst = nw_dst;
527 }
528
529 static void
530 make_rules(struct in_band *ib,
531 void (*cb)(struct in_band *, const struct in_band_rule *))
532 {
533 struct in_band_rule rule;
534 size_t i;
535
536 if (!eth_addr_is_zero(ib->installed_local_mac)) {
537 /* Allow DHCP requests to be sent from the local port. */
538 init_rule(&rule, IBR_FROM_LOCAL_DHCP);
539 set_in_port(&rule, ODPP_LOCAL);
540 set_dl_type(&rule, ETH_TYPE_IP);
541 set_dl_src(&rule, ib->installed_local_mac);
542 set_nw_proto(&rule, IP_TYPE_UDP);
543 set_tp_src(&rule, DHCP_CLIENT_PORT);
544 set_tp_dst(&rule, DHCP_SERVER_PORT);
545 cb(ib, &rule);
546
547 /* Allow the connection's interface to receive directed ARP traffic. */
548 init_rule(&rule, IBR_TO_LOCAL_ARP);
549 set_dl_type(&rule, ETH_TYPE_ARP);
550 set_dl_dst(&rule, ib->installed_local_mac);
551 set_nw_proto(&rule, ARP_OP_REPLY);
552 cb(ib, &rule);
553
554 /* Allow the connection's interface to be the source of ARP traffic. */
555 init_rule(&rule, IBR_FROM_LOCAL_ARP);
556 set_dl_type(&rule, ETH_TYPE_ARP);
557 set_dl_src(&rule, ib->installed_local_mac);
558 set_nw_proto(&rule, ARP_OP_REQUEST);
559 cb(ib, &rule);
560 }
561
562 for (i = 0; i < ib->n_remote_macs; i++) {
563 const uint8_t *remote_mac = &ib->remote_macs[i * ETH_ADDR_LEN];
564
565 if (i > 0) {
566 const uint8_t *prev_mac = &ib->remote_macs[(i - 1) * ETH_ADDR_LEN];
567 if (eth_addr_equals(remote_mac, prev_mac)) {
568 /* Skip duplicates. */
569 continue;
570 }
571 }
572
573 /* Allow ARP replies to the remote side's MAC. */
574 init_rule(&rule, IBR_TO_REMOTE_ARP);
575 set_dl_type(&rule, ETH_TYPE_ARP);
576 set_dl_dst(&rule, remote_mac);
577 set_nw_proto(&rule, ARP_OP_REPLY);
578 cb(ib, &rule);
579
580 /* Allow ARP requests from the remote side's MAC. */
581 init_rule(&rule, IBR_FROM_REMOTE_ARP);
582 set_dl_type(&rule, ETH_TYPE_ARP);
583 set_dl_src(&rule, remote_mac);
584 set_nw_proto(&rule, ARP_OP_REQUEST);
585 cb(ib, &rule);
586 }
587
588 for (i = 0; i < ib->n_remote_ips; i++) {
589 uint32_t remote_ip = ib->remote_ips[i];
590
591 if (i > 0 && ib->remote_ips[i - 1] == remote_ip) {
592 /* Skip duplicates. */
593 continue;
594 }
595
596 /* Allow ARP replies to the controller's IP. */
597 init_rule(&rule, IBR_TO_CTL_ARP);
598 set_dl_type(&rule, ETH_TYPE_ARP);
599 set_nw_proto(&rule, ARP_OP_REPLY);
600 set_nw_dst(&rule, remote_ip);
601 cb(ib, &rule);
602
603 /* Allow ARP requests from the controller's IP. */
604 init_rule(&rule, IBR_FROM_CTL_ARP);
605 set_dl_type(&rule, ETH_TYPE_ARP);
606 set_nw_proto(&rule, ARP_OP_REQUEST);
607 set_nw_src(&rule, remote_ip);
608 cb(ib, &rule);
609
610 /* OpenFlow traffic to the controller. */
611 init_rule(&rule, IBR_TO_CTL_OFP);
612 set_dl_type(&rule, ETH_TYPE_IP);
613 set_nw_proto(&rule, IP_TYPE_TCP);
614 set_nw_dst(&rule, remote_ip);
615 set_tp_dst(&rule, OFP_TCP_PORT);
616 cb(ib, &rule);
617
618 /* OpenFlow traffic from the controller. */
619 init_rule(&rule, IBR_FROM_CTL_OFP);
620 set_dl_type(&rule, ETH_TYPE_IP);
621 set_nw_proto(&rule, IP_TYPE_TCP);
622 set_nw_src(&rule, remote_ip);
623 set_tp_src(&rule, OFP_TCP_PORT);
624 cb(ib, &rule);
625 }
626 }
627
628 static void
629 drop_rule(struct in_band *ib, const struct in_band_rule *rule)
630 {
631 ofproto_delete_flow(ib->ofproto, &rule->flow,
632 rule->wildcards, rule->priority);
633 }
634
635 /* Drops from the flow table all of the flows set up by 'ib', then clears out
636 * the information about the installed flows so that they can be filled in
637 * again if necessary. */
638 static void
639 drop_rules(struct in_band *ib)
640 {
641 /* Drop rules. */
642 make_rules(ib, drop_rule);
643
644 /* Clear out state. */
645 memset(ib->installed_local_mac, 0, sizeof ib->installed_local_mac);
646
647 free(ib->remote_ips);
648 ib->remote_ips = NULL;
649 ib->n_remote_ips = 0;
650
651 free(ib->remote_macs);
652 ib->remote_macs = NULL;
653 ib->n_remote_macs = 0;
654 }
655
656 static void
657 add_rule(struct in_band *ib, const struct in_band_rule *rule)
658 {
659 union ofp_action action;
660
661 action.type = htons(OFPAT_OUTPUT);
662 action.output.len = htons(sizeof action);
663 action.output.port = htons(OFPP_NORMAL);
664 action.output.max_len = htons(0);
665 ofproto_add_flow(ib->ofproto, &rule->flow, rule->wildcards,
666 rule->priority, &action, 1, 0);
667 }
668
669 /* Inserts flows into the flow table for the current state of 'ib'. */
670 static void
671 add_rules(struct in_band *ib)
672 {
673 make_rules(ib, add_rule);
674 }
675
676 static int
677 compare_ips(const void *a, const void *b)
678 {
679 return memcmp(a, b, sizeof(uint32_t));
680 }
681
682 static int
683 compare_macs(const void *a, const void *b)
684 {
685 return memcmp(a, b, ETH_ADDR_LEN);
686 }
687
688 void
689 in_band_run(struct in_band *ib)
690 {
691 struct in_band_remote *r;
692 bool local_change, remote_change;
693
694 local_change = refresh_local(ib);
695 remote_change = refresh_remotes(ib);
696 if (!local_change && !remote_change) {
697 /* Nothing changed, nothing to do. */
698 return;
699 }
700
701 /* Drop old rules. */
702 drop_rules(ib);
703
704 /* Figure out new rules. */
705 memcpy(ib->installed_local_mac, ib->local_mac, ETH_ADDR_LEN);
706 ib->remote_ips = xmalloc(ib->n_remotes * sizeof *ib->remote_ips);
707 ib->n_remote_ips = 0;
708 ib->remote_macs = xmalloc(ib->n_remotes * ETH_ADDR_LEN);
709 ib->n_remote_macs = 0;
710 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
711 if (r->remote_ip) {
712 ib->remote_ips[ib->n_remote_ips++] = r->remote_ip;
713 }
714 if (!eth_addr_is_zero(r->remote_mac)) {
715 memcpy(&ib->remote_macs[ib->n_remote_macs * ETH_ADDR_LEN],
716 r->remote_mac, ETH_ADDR_LEN);
717 ib->n_remote_macs++;
718 }
719 }
720
721 /* Sort, to allow make_rules() to easily skip duplicates. */
722 qsort(ib->remote_ips, ib->n_remote_ips, sizeof *ib->remote_ips,
723 compare_ips);
724 qsort(ib->remote_macs, ib->n_remote_macs, ETH_ADDR_LEN, compare_macs);
725
726 /* Add new rules. */
727 add_rules(ib);
728 }
729
730 void
731 in_band_wait(struct in_band *in_band)
732 {
733 time_t now = time_now();
734 time_t wakeup
735 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
736 if (wakeup > now) {
737 poll_timer_wait((wakeup - now) * 1000);
738 } else {
739 poll_immediate_wake();
740 }
741 }
742
743 /* ofproto has flushed all flows from the flow table and it is calling us back
744 * to allow us to reinstall the ones that are important to us. */
745 void
746 in_band_flushed(struct in_band *in_band)
747 {
748 add_rules(in_band);
749 }
750
751 int
752 in_band_create(struct ofproto *ofproto, struct dpif *dpif,
753 struct switch_status *ss, struct in_band **in_bandp)
754 {
755 struct in_band *in_band;
756 char local_name[IF_NAMESIZE];
757 struct netdev *local_netdev;
758 int error;
759
760 error = dpif_port_get_name(dpif, ODPP_LOCAL,
761 local_name, sizeof local_name);
762 if (error) {
763 VLOG_ERR("failed to initialize in-band control: cannot get name "
764 "of datapath local port (%s)", strerror(error));
765 return error;
766 }
767
768 error = netdev_open_default(local_name, &local_netdev);
769 if (error) {
770 VLOG_ERR("failed to initialize in-band control: cannot open "
771 "datapath local port %s (%s)", local_name, strerror(error));
772 return error;
773 }
774
775 in_band = xzalloc(sizeof *in_band);
776 in_band->ofproto = ofproto;
777 in_band->ss_cat = switch_status_register(ss, "in-band",
778 in_band_status_cb, in_band);
779 in_band->next_remote_refresh = TIME_MIN;
780 in_band->next_local_refresh = TIME_MIN;
781 in_band->local_netdev = local_netdev;
782
783 *in_bandp = in_band;
784
785 return 0;
786 }
787
788 void
789 in_band_destroy(struct in_band *ib)
790 {
791 if (ib) {
792 drop_rules(ib);
793 in_band_set_remotes(ib, NULL, 0);
794 switch_status_unregister(ib->ss_cat);
795 netdev_close(ib->local_netdev);
796 free(ib);
797 }
798 }
799
800 void
801 in_band_set_remotes(struct in_band *ib, struct rconn **remotes, size_t n)
802 {
803 size_t i;
804
805 /* Optimize the case where the rconns are the same as last time. */
806 if (n == ib->n_remotes) {
807 for (i = 0; i < n; i++) {
808 if (ib->remotes[i].rconn != remotes[i]) {
809 goto different;
810 }
811 }
812 return;
813
814 different:;
815 }
816
817 for (i = 0; i < ib->n_remotes; i++) {
818 /* We don't own the rconn. */
819 netdev_close(ib->remotes[i].remote_netdev);
820 }
821 free(ib->remotes);
822
823 ib->next_remote_refresh = TIME_MIN;
824 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : 0;
825 ib->n_remotes = n;
826 for (i = 0; i < n; i++) {
827 ib->remotes[i].rconn = remotes[i];
828 }
829 }