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"
34 #include "ofproto-provider.h"
35 #include "openflow/openflow.h"
37 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(in_band);
43 /* Priorities used in classifier for in-band rules. These values are higher
44 * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
45 * The ordering of priorities is not important because all of the rules set up
46 * by in-band control have the same action. The only reason to use more than
47 * one priority is to make the kind of flow easier to see during debugging. */
49 /* One set per bridge. */
50 IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
51 IBR_TO_LOCAL_ARP, /* (b) To local port, ARP. */
52 IBR_FROM_LOCAL_ARP, /* (c) From local port, ARP. */
54 /* One set per unique next-hop MAC. */
55 IBR_TO_NEXT_HOP_ARP, /* (d) To remote MAC, ARP. */
56 IBR_FROM_NEXT_HOP_ARP, /* (e) From remote MAC, ARP. */
58 /* One set per unique remote IP address. */
59 IBR_TO_REMOTE_ARP, /* (f) To remote IP, ARP. */
60 IBR_FROM_REMOTE_ARP, /* (g) From remote IP, ARP. */
62 /* One set per unique remote (IP,port) pair. */
63 IBR_TO_REMOTE_TCP, /* (h) To remote IP, TCP port. */
64 IBR_FROM_REMOTE_TCP /* (i) From remote IP, TCP port. */
67 /* Track one remote IP and next hop information. */
68 struct in_band_remote {
69 struct sockaddr_in remote_addr; /* IP address, in network byte order. */
70 uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
71 uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
72 struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
75 /* What to do to an in_band_rule. */
77 ADD, /* Add the rule to ofproto's flow table. */
78 DELETE /* Delete the rule from ofproto's flow table. */
81 /* A rule to add to or delete from ofproto's flow table. */
83 struct cls_rule cls_rule;
88 struct ofproto *ofproto;
91 /* Remote information. */
92 time_t next_remote_refresh; /* Refresh timer. */
93 struct in_band_remote *remotes;
96 /* Local information. */
97 time_t next_local_refresh; /* Refresh timer. */
98 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
99 struct netdev *local_netdev; /* Local port's network device. */
102 struct hmap rules; /* Contains "struct in_band_rule"s. */
105 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
108 refresh_remote(struct in_band *ib, struct in_band_remote *r)
110 struct in_addr next_hop_inaddr;
114 /* Find the next-hop IP address. */
115 memset(r->remote_mac, 0, sizeof r->remote_mac);
116 retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
117 &next_hop_inaddr, &next_hop_dev);
119 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
120 IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
123 if (!next_hop_inaddr.s_addr) {
124 next_hop_inaddr = r->remote_addr.sin_addr;
127 /* Open the next-hop network device. */
128 if (!r->remote_netdev
129 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
131 netdev_close(r->remote_netdev);
133 retval = netdev_open(next_hop_dev, "system", &r->remote_netdev);
135 VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
136 "to controller "IP_FMT"): %s",
137 next_hop_dev, IP_ARGS(&r->remote_addr.sin_addr),
145 /* Look up the MAC address of the next-hop IP address. */
146 retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
149 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
150 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
153 /* If we don't have a MAC address, then refresh quickly, since we probably
154 * will get a MAC address soon (via ARP). Otherwise, we can afford to wait
156 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
160 refresh_remotes(struct in_band *ib)
162 struct in_band_remote *r;
165 if (time_now() < ib->next_remote_refresh) {
170 ib->next_remote_refresh = TIME_MAX;
171 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
172 uint8_t old_remote_mac[ETH_ADDR_LEN];
176 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
178 /* Refresh remote information. */
179 next_refresh = refresh_remote(ib, r) + time_now();
180 ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
182 /* If the MAC changed, log the changes. */
183 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
185 if (!eth_addr_is_zero(r->remote_mac)
186 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
187 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
189 ETH_ADDR_ARGS(r->last_remote_mac),
190 ETH_ADDR_ARGS(r->remote_mac));
191 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
199 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
200 * for a refresh. Returns true if anything changed, otherwise false. */
202 refresh_local(struct in_band *ib)
204 uint8_t ea[ETH_ADDR_LEN];
208 if (now < ib->next_local_refresh) {
211 ib->next_local_refresh = now + 1;
213 if (netdev_get_etheraddr(ib->local_netdev, ea)
214 || eth_addr_equals(ea, ib->local_mac)) {
218 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
222 /* Returns true if 'packet' should be sent to the local port regardless
223 * of the flow table. */
225 in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
226 const struct ofpbuf *packet)
228 /* Regardless of how the flow table is configured, we want to be
229 * able to see replies to our DHCP requests. */
230 if (flow->dl_type == htons(ETH_TYPE_IP)
231 && flow->nw_proto == IPPROTO_UDP
232 && flow->tp_src == htons(DHCP_SERVER_PORT)
233 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
235 struct dhcp_header *dhcp;
237 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
243 refresh_local(in_band);
244 if (!eth_addr_is_zero(in_band->local_mac)
245 && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
253 /* Returns true if the rule that would match 'flow' with 'actions' is
254 * allowed to be set up in the datapath. */
256 in_band_rule_check(const struct flow *flow,
257 const struct nlattr *actions, size_t actions_len)
259 /* Don't allow flows that would prevent DHCP replies from being seen
260 * by the local port. */
261 if (flow->dl_type == htons(ETH_TYPE_IP)
262 && flow->nw_proto == IPPROTO_UDP
263 && flow->tp_src == htons(DHCP_SERVER_PORT)
264 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
265 const struct nlattr *a;
268 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
269 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
270 && nl_attr_get_u32(a) == OVSP_LOCAL) {
281 add_rule(struct in_band *ib, const struct cls_rule *cls_rule)
283 uint32_t hash = cls_rule_hash(cls_rule, 0);
284 struct in_band_rule *rule;
286 HMAP_FOR_EACH_WITH_HASH (rule, cls_rule.hmap_node, hash, &ib->rules) {
287 if (cls_rule_equal(&rule->cls_rule, cls_rule)) {
293 rule = xmalloc(sizeof *rule);
294 rule->cls_rule = *cls_rule;
296 hmap_insert(&ib->rules, &rule->cls_rule.hmap_node, hash);
300 update_rules(struct in_band *ib)
302 struct in_band_rule *ib_rule;
303 struct in_band_remote *r;
304 struct cls_rule rule;
306 /* Mark all the existing rules for deletion. (Afterward we will re-add any
307 * rules that are still valid.) */
308 HMAP_FOR_EACH (ib_rule, cls_rule.hmap_node, &ib->rules) {
309 ib_rule->op = DELETE;
312 if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) {
313 /* (a) Allow DHCP requests sent from the local port. */
314 cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_DHCP);
315 cls_rule_set_in_port(&rule, OFPP_LOCAL);
316 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
317 cls_rule_set_dl_src(&rule, ib->local_mac);
318 cls_rule_set_nw_proto(&rule, IPPROTO_UDP);
319 cls_rule_set_tp_src(&rule, htons(DHCP_CLIENT_PORT));
320 cls_rule_set_tp_dst(&rule, htons(DHCP_SERVER_PORT));
323 /* (b) Allow ARP replies to the local port's MAC address. */
324 cls_rule_init_catchall(&rule, IBR_TO_LOCAL_ARP);
325 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
326 cls_rule_set_dl_dst(&rule, ib->local_mac);
327 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
330 /* (c) Allow ARP requests from the local port's MAC address. */
331 cls_rule_init_catchall(&rule, IBR_FROM_LOCAL_ARP);
332 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
333 cls_rule_set_dl_src(&rule, ib->local_mac);
334 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
338 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
339 const uint8_t *remote_mac = r->remote_mac;
341 if (eth_addr_is_zero(remote_mac)) {
345 /* (d) Allow ARP replies to the next hop's MAC address. */
346 cls_rule_init_catchall(&rule, IBR_TO_NEXT_HOP_ARP);
347 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
348 cls_rule_set_dl_dst(&rule, remote_mac);
349 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
352 /* (e) Allow ARP requests from the next hop's MAC address. */
353 cls_rule_init_catchall(&rule, IBR_FROM_NEXT_HOP_ARP);
354 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
355 cls_rule_set_dl_src(&rule, remote_mac);
356 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
360 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
361 const struct sockaddr_in *a = &r->remote_addr;
363 /* (f) Allow ARP replies containing the remote's IP address as a
365 cls_rule_init_catchall(&rule, IBR_TO_REMOTE_ARP);
366 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
367 cls_rule_set_nw_proto(&rule, ARP_OP_REPLY);
368 cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
371 /* (g) Allow ARP requests containing the remote's IP address as a
373 cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_ARP);
374 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_ARP));
375 cls_rule_set_nw_proto(&rule, ARP_OP_REQUEST);
376 cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
379 /* (h) Allow TCP traffic to the remote's IP and port. */
380 cls_rule_init_catchall(&rule, IBR_TO_REMOTE_TCP);
381 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
382 cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
383 cls_rule_set_nw_dst(&rule, a->sin_addr.s_addr);
384 cls_rule_set_tp_dst(&rule, a->sin_port);
387 /* (i) Allow TCP traffic from the remote's IP and port. */
388 cls_rule_init_catchall(&rule, IBR_FROM_REMOTE_TCP);
389 cls_rule_set_dl_type(&rule, htons(ETH_TYPE_IP));
390 cls_rule_set_nw_proto(&rule, IPPROTO_TCP);
391 cls_rule_set_nw_src(&rule, a->sin_addr.s_addr);
392 cls_rule_set_tp_src(&rule, a->sin_port);
397 /* Updates the OpenFlow flow table for the current state of in-band control.
398 * Returns true ordinarily. Returns false if no remotes are configured on 'ib'
399 * and 'ib' doesn't have any rules left to remove from the OpenFlow flow
400 * table. Thus, a false return value means that the caller can destroy 'ib'
401 * without leaving extra flows hanging around in the flow table. */
403 in_band_run(struct in_band *ib)
406 struct nx_action_set_queue nxsq;
412 struct in_band_rule *rule, *next;
414 memset(&actions, 0, sizeof actions);
415 actions.oa.output.type = htons(OFPAT10_OUTPUT);
416 actions.oa.output.len = htons(sizeof actions.oa);
417 actions.oa.output.port = htons(OFPP_NORMAL);
418 actions.oa.output.max_len = htons(0);
419 if (ib->queue_id < 0) {
421 na = sizeof actions.oa / sizeof(union ofp_action);
423 actions.nxsq.type = htons(OFPAT10_VENDOR);
424 actions.nxsq.len = htons(sizeof actions.nxsq);
425 actions.nxsq.vendor = htonl(NX_VENDOR_ID);
426 actions.nxsq.subtype = htons(NXAST_SET_QUEUE);
427 actions.nxsq.queue_id = htonl(ib->queue_id);
429 na = sizeof actions / sizeof(union ofp_action);
437 HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
440 ofproto_add_flow(ib->ofproto, &rule->cls_rule, a, na);
444 if (ofproto_delete_flow(ib->ofproto, &rule->cls_rule)) {
445 /* ofproto doesn't have the rule anymore so there's no reason
446 * for us to track it any longer. */
447 hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
454 return ib->n_remotes || !hmap_is_empty(&ib->rules);
458 in_band_wait(struct in_band *in_band)
461 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
462 poll_timer_wait_until(wakeup * 1000);
466 in_band_create(struct ofproto *ofproto, const char *local_name,
467 struct in_band **in_bandp)
469 struct in_band *in_band;
470 struct netdev *local_netdev;
474 error = netdev_open(local_name, "system", &local_netdev);
476 VLOG_ERR("failed to initialize in-band control: cannot open "
477 "datapath local port %s (%s)", local_name, strerror(error));
481 in_band = xzalloc(sizeof *in_band);
482 in_band->ofproto = ofproto;
483 in_band->queue_id = -1;
484 in_band->next_remote_refresh = TIME_MIN;
485 in_band->next_local_refresh = TIME_MIN;
486 in_band->local_netdev = local_netdev;
487 hmap_init(&in_band->rules);
495 in_band_destroy(struct in_band *ib)
498 struct in_band_rule *rule, *next;
500 HMAP_FOR_EACH_SAFE (rule, next, cls_rule.hmap_node, &ib->rules) {
501 hmap_remove(&ib->rules, &rule->cls_rule.hmap_node);
504 hmap_destroy(&ib->rules);
505 in_band_set_remotes(ib, NULL, 0);
506 netdev_close(ib->local_netdev);
512 any_addresses_changed(struct in_band *ib,
513 const struct sockaddr_in *addresses, size_t n)
517 if (n != ib->n_remotes) {
521 for (i = 0; i < n; i++) {
522 const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
523 const struct sockaddr_in *new = &addresses[i];
525 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
526 old->sin_port != new->sin_port) {
535 in_band_set_remotes(struct in_band *ib,
536 const struct sockaddr_in *addresses, size_t n)
540 if (!any_addresses_changed(ib, addresses, n)) {
544 /* Clear old remotes. */
545 for (i = 0; i < ib->n_remotes; i++) {
546 netdev_close(ib->remotes[i].remote_netdev);
550 /* Set up new remotes. */
551 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
553 for (i = 0; i < n; i++) {
554 ib->remotes[i].remote_addr = addresses[i];
557 /* Force refresh in next call to in_band_run(). */
558 ib->next_remote_refresh = TIME_MIN;
561 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'. If
562 * 'queue_id' is negative, 'ib' will not set any queue (which is also the
565 in_band_set_queue(struct in_band *ib, int queue_id)
567 ib->queue_id = queue_id;