2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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"
32 #include "ofp-actions.h"
35 #include "ofproto-provider.h"
36 #include "openflow/openflow.h"
38 #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. */
76 /* What to do to an in_band_rule. */
78 ADD, /* Add the rule to ofproto's flow table. */
79 DELETE /* Delete the rule from ofproto's flow table. */
82 /* A rule to add to or delete from ofproto's flow table. */
84 struct cls_rule cls_rule;
89 struct ofproto *ofproto;
92 /* Remote information. */
93 time_t next_remote_refresh; /* Refresh timer. */
94 struct in_band_remote *remotes;
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. */
103 struct hmap rules; /* Contains "struct in_band_rule"s. */
106 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
109 refresh_remote(struct in_band *ib, struct in_band_remote *r)
111 struct in_addr next_hop_inaddr;
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);
120 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
121 IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
124 if (!next_hop_inaddr.s_addr) {
125 next_hop_inaddr = r->remote_addr.sin_addr;
128 /* Open the next-hop network device. */
129 if (!r->remote_netdev
130 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
132 netdev_close(r->remote_netdev);
134 retval = netdev_open(next_hop_dev, "system", &r->remote_netdev);
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),
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,
150 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
151 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
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
157 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
161 refresh_remotes(struct in_band *ib)
163 struct in_band_remote *r;
166 if (time_now() < ib->next_remote_refresh) {
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];
177 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
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);
183 /* If the MAC changed, log the changes. */
184 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
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
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);
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. */
203 refresh_local(struct in_band *ib)
205 uint8_t ea[ETH_ADDR_LEN];
209 if (now < ib->next_local_refresh) {
212 ib->next_local_refresh = now + 1;
214 if (netdev_get_etheraddr(ib->local_netdev, ea)
215 || eth_addr_equals(ea, ib->local_mac)) {
219 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
223 /* Returns true if 'packet' should be sent to the local port regardless
224 * of the flow table. */
226 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
227 const struct ofpbuf *packet)
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)
236 struct dhcp_header *dhcp;
238 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
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)) {
254 /* Returns true if the rule that would match 'flow' with 'actions' is
255 * allowed to be set up in the datapath. */
257 in_band_rule_check(const struct flow *flow,
258 const struct nlattr *actions, size_t actions_len)
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;
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) {
282 add_rule(struct in_band *ib, const struct cls_rule *cls_rule)
284 uint32_t hash = cls_rule_hash(cls_rule, 0);
285 struct in_band_rule *rule;
287 HMAP_FOR_EACH_WITH_HASH (rule, cls_rule.hmap_node, hash, &ib->rules) {
288 if (cls_rule_equal(&rule->cls_rule, cls_rule)) {
294 rule = xmalloc(sizeof *rule);
295 rule->cls_rule = *cls_rule;
297 hmap_insert(&ib->rules, &rule->cls_rule.hmap_node, hash);
301 update_rules(struct in_band *ib)
303 struct in_band_rule *ib_rule;
304 struct in_band_remote *r;
305 struct cls_rule rule;
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;
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));
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);
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);
339 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
340 const uint8_t *remote_mac = r->remote_mac;
342 if (eth_addr_is_zero(remote_mac)) {
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);
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);
361 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
362 const struct sockaddr_in *a = &r->remote_addr;
364 /* (f) Allow ARP replies containing the remote's IP address as a
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);
372 /* (g) Allow ARP requests containing the remote's IP address as a
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);
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);
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);
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. */
404 in_band_run(struct in_band *ib)
406 uint64_t ofpacts_stub[128 / 8];
407 struct ofpbuf ofpacts;
409 struct in_band_rule *rule, *next;
411 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
413 if (ib->queue_id >= 0) {
414 ofpact_put_SET_QUEUE(&ofpacts)->queue_id = ib->queue_id;
416 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
423 HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
426 ofproto_add_flow(ib->ofproto, &rule->cls_rule,
427 ofpacts.data, ofpacts.size);
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);
441 ofpbuf_uninit(&ofpacts);
443 return ib->n_remotes || !hmap_is_empty(&ib->rules);
447 in_band_wait(struct in_band *in_band)
450 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
451 poll_timer_wait_until(wakeup * 1000);
455 in_band_create(struct ofproto *ofproto, const char *local_name,
456 struct in_band **in_bandp)
458 struct in_band *in_band;
459 struct netdev *local_netdev;
463 error = netdev_open(local_name, "system", &local_netdev);
465 VLOG_ERR("failed to initialize in-band control: cannot open "
466 "datapath local port %s (%s)", local_name, strerror(error));
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);
484 in_band_destroy(struct in_band *ib)
487 struct in_band_rule *rule, *next;
489 HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
490 hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
493 hmap_destroy(&ib->rules);
494 in_band_set_remotes(ib, NULL, 0);
495 netdev_close(ib->local_netdev);
501 any_addresses_changed(struct in_band *ib,
502 const struct sockaddr_in *addresses, size_t n)
506 if (n != ib->n_remotes) {
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];
514 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
515 old->sin_port != new->sin_port) {
524 in_band_set_remotes(struct in_band *ib,
525 const struct sockaddr_in *addresses, size_t n)
529 if (!any_addresses_changed(ib, addresses, n)) {
533 /* Clear old remotes. */
534 for (i = 0; i < ib->n_remotes; i++) {
535 netdev_close(ib->remotes[i].remote_netdev);
539 /* Set up new remotes. */
540 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
542 for (i = 0; i < n; i++) {
543 ib->remotes[i].remote_addr = addresses[i];
546 /* Force refresh in next call to in_band_run(). */
547 ib->next_remote_refresh = TIME_MIN;
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
554 in_band_set_queue(struct in_band *ib, int queue_id)
556 ib->queue_id = queue_id;