2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 hmap_node hmap_node; /* In struct in_band's "rules" hmap. */
86 unsigned int priority;
91 struct ofproto *ofproto;
94 /* Remote information. */
95 time_t next_remote_refresh; /* Refresh timer. */
96 struct in_band_remote *remotes;
99 /* Local information. */
100 time_t next_local_refresh; /* Refresh timer. */
101 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
102 struct netdev *local_netdev; /* Local port's network device. */
105 struct hmap rules; /* Contains "struct in_band_rule"s. */
108 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
111 refresh_remote(struct in_band *ib, struct in_band_remote *r)
113 struct in_addr next_hop_inaddr;
117 /* Find the next-hop IP address. */
118 memset(r->remote_mac, 0, sizeof r->remote_mac);
119 retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
120 &next_hop_inaddr, &next_hop_dev);
122 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
123 IP_ARGS(r->remote_addr.sin_addr.s_addr),
124 ovs_strerror(retval));
127 if (!next_hop_inaddr.s_addr) {
128 next_hop_inaddr = r->remote_addr.sin_addr;
131 /* Open the next-hop network device. */
132 if (!r->remote_netdev
133 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
135 netdev_close(r->remote_netdev);
137 retval = netdev_open(next_hop_dev, "system", &r->remote_netdev);
139 VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
140 "to controller "IP_FMT"): %s",
141 next_hop_dev, IP_ARGS(r->remote_addr.sin_addr.s_addr),
142 ovs_strerror(retval));
149 /* Look up the MAC address of the next-hop IP address. */
150 retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
153 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
154 IP_ARGS(next_hop_inaddr.s_addr), ovs_strerror(retval));
157 /* If we don't have a MAC address, then refresh quickly, since we probably
158 * will get a MAC address soon (via ARP). Otherwise, we can afford to wait
160 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
164 refresh_remotes(struct in_band *ib)
166 struct in_band_remote *r;
169 if (time_now() < ib->next_remote_refresh) {
174 ib->next_remote_refresh = TIME_MAX;
175 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
176 uint8_t old_remote_mac[ETH_ADDR_LEN];
180 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
182 /* Refresh remote information. */
183 next_refresh = refresh_remote(ib, r) + time_now();
184 ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
186 /* If the MAC changed, log the changes. */
187 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
189 if (!eth_addr_is_zero(r->remote_mac)
190 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
191 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
193 ETH_ADDR_ARGS(r->last_remote_mac),
194 ETH_ADDR_ARGS(r->remote_mac));
195 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
203 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
204 * for a refresh. Returns true if anything changed, otherwise false. */
206 refresh_local(struct in_band *ib)
208 uint8_t ea[ETH_ADDR_LEN];
212 if (now < ib->next_local_refresh) {
215 ib->next_local_refresh = now + 1;
217 if (netdev_get_etheraddr(ib->local_netdev, ea)
218 || eth_addr_equals(ea, ib->local_mac)) {
222 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
226 /* Returns true if packets in 'flow' should be directed to the local port.
227 * (This keeps the flow table from preventing DHCP replies from being seen by
228 * the local port.) */
230 in_band_must_output_to_local_port(const struct flow *flow)
232 return (flow->dl_type == htons(ETH_TYPE_IP)
233 && flow->nw_proto == IPPROTO_UDP
234 && flow->tp_src == htons(DHCP_SERVER_PORT)
235 && flow->tp_dst == htons(DHCP_CLIENT_PORT));
239 add_rule(struct in_band *ib, const struct match *match, unsigned int priority)
241 uint32_t hash = match_hash(match, 0);
242 struct in_band_rule *rule;
244 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &ib->rules) {
245 if (match_equal(&rule->match, match)) {
251 rule = xmalloc(sizeof *rule);
252 rule->match = *match;
253 rule->priority = priority;
255 hmap_insert(&ib->rules, &rule->hmap_node, hash);
259 update_rules(struct in_band *ib)
261 struct in_band_rule *ib_rule;
262 struct in_band_remote *r;
265 /* Mark all the existing rules for deletion. (Afterward we will re-add any
266 * rules that are still valid.) */
267 HMAP_FOR_EACH (ib_rule, hmap_node, &ib->rules) {
268 ib_rule->op = DELETE;
271 if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) {
272 /* (a) Allow DHCP requests sent from the local port. */
273 match_init_catchall(&match);
274 match_set_in_port(&match, OFPP_LOCAL);
275 match_set_dl_type(&match, htons(ETH_TYPE_IP));
276 match_set_dl_src(&match, ib->local_mac);
277 match_set_nw_proto(&match, IPPROTO_UDP);
278 match_set_tp_src(&match, htons(DHCP_CLIENT_PORT));
279 match_set_tp_dst(&match, htons(DHCP_SERVER_PORT));
280 add_rule(ib, &match, IBR_FROM_LOCAL_DHCP);
282 /* (b) Allow ARP replies to the local port's MAC address. */
283 match_init_catchall(&match);
284 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
285 match_set_dl_dst(&match, ib->local_mac);
286 match_set_nw_proto(&match, ARP_OP_REPLY);
287 add_rule(ib, &match, IBR_TO_LOCAL_ARP);
289 /* (c) Allow ARP requests from the local port's MAC address. */
290 match_init_catchall(&match);
291 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
292 match_set_dl_src(&match, ib->local_mac);
293 match_set_nw_proto(&match, ARP_OP_REQUEST);
294 add_rule(ib, &match, IBR_FROM_LOCAL_ARP);
297 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
298 const uint8_t *remote_mac = r->remote_mac;
300 if (eth_addr_is_zero(remote_mac)) {
304 /* (d) Allow ARP replies to the next hop's MAC address. */
305 match_init_catchall(&match);
306 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
307 match_set_dl_dst(&match, remote_mac);
308 match_set_nw_proto(&match, ARP_OP_REPLY);
309 add_rule(ib, &match, IBR_TO_NEXT_HOP_ARP);
311 /* (e) Allow ARP requests from the next hop's MAC address. */
312 match_init_catchall(&match);
313 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
314 match_set_dl_src(&match, remote_mac);
315 match_set_nw_proto(&match, ARP_OP_REQUEST);
316 add_rule(ib, &match, IBR_FROM_NEXT_HOP_ARP);
319 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
320 const struct sockaddr_in *a = &r->remote_addr;
322 /* (f) Allow ARP replies containing the remote's IP address as a
324 match_init_catchall(&match);
325 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
326 match_set_nw_proto(&match, ARP_OP_REPLY);
327 match_set_nw_dst(&match, a->sin_addr.s_addr);
328 add_rule(ib, &match, IBR_TO_REMOTE_ARP);
330 /* (g) Allow ARP requests containing the remote's IP address as a
332 match_init_catchall(&match);
333 match_set_dl_type(&match, htons(ETH_TYPE_ARP));
334 match_set_nw_proto(&match, ARP_OP_REQUEST);
335 match_set_nw_src(&match, a->sin_addr.s_addr);
336 add_rule(ib, &match, IBR_FROM_REMOTE_ARP);
338 /* (h) Allow TCP traffic to the remote's IP and port. */
339 match_init_catchall(&match);
340 match_set_dl_type(&match, htons(ETH_TYPE_IP));
341 match_set_nw_proto(&match, IPPROTO_TCP);
342 match_set_nw_dst(&match, a->sin_addr.s_addr);
343 match_set_tp_dst(&match, a->sin_port);
344 add_rule(ib, &match, IBR_TO_REMOTE_TCP);
346 /* (i) Allow TCP traffic from the remote's IP and port. */
347 match_init_catchall(&match);
348 match_set_dl_type(&match, htons(ETH_TYPE_IP));
349 match_set_nw_proto(&match, IPPROTO_TCP);
350 match_set_nw_src(&match, a->sin_addr.s_addr);
351 match_set_tp_src(&match, a->sin_port);
352 add_rule(ib, &match, IBR_FROM_REMOTE_TCP);
356 /* Updates the OpenFlow flow table for the current state of in-band control.
357 * Returns true ordinarily. Returns false if no remotes are configured on 'ib'
358 * and 'ib' doesn't have any rules left to remove from the OpenFlow flow
359 * table. Thus, a false return value means that the caller can destroy 'ib'
360 * without leaving extra flows hanging around in the flow table. */
362 in_band_run(struct in_band *ib)
364 uint64_t ofpacts_stub[128 / 8];
365 struct ofpbuf ofpacts;
367 struct in_band_rule *rule, *next;
369 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
371 if (ib->queue_id >= 0) {
372 ofpact_put_SET_QUEUE(&ofpacts)->queue_id = ib->queue_id;
374 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
381 HMAP_FOR_EACH_SAFE (rule, next, hmap_node, &ib->rules) {
384 ofproto_add_flow(ib->ofproto, &rule->match, rule->priority,
385 ofpacts.data, ofpacts.size);
389 if (ofproto_delete_flow(ib->ofproto,
390 &rule->match, rule->priority)) {
391 /* ofproto doesn't have the rule anymore so there's no reason
392 * for us to track it any longer. */
393 hmap_remove(&ib->rules, &rule->hmap_node);
400 ofpbuf_uninit(&ofpacts);
402 return ib->n_remotes || !hmap_is_empty(&ib->rules);
406 in_band_wait(struct in_band *in_band)
409 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
410 poll_timer_wait_until(wakeup * 1000);
414 in_band_create(struct ofproto *ofproto, const char *local_name,
415 struct in_band **in_bandp)
417 struct in_band *in_band;
418 struct netdev *local_netdev;
422 error = netdev_open(local_name, "internal", &local_netdev);
424 VLOG_ERR("failed to initialize in-band control: cannot open "
425 "datapath local port %s (%s)",
426 local_name, ovs_strerror(error));
430 in_band = xzalloc(sizeof *in_band);
431 in_band->ofproto = ofproto;
432 in_band->queue_id = -1;
433 in_band->next_remote_refresh = TIME_MIN;
434 in_band->next_local_refresh = TIME_MIN;
435 in_band->local_netdev = local_netdev;
436 hmap_init(&in_band->rules);
444 in_band_destroy(struct in_band *ib)
447 struct in_band_rule *rule, *next;
449 HMAP_FOR_EACH_SAFE (rule, next, hmap_node, &ib->rules) {
450 hmap_remove(&ib->rules, &rule->hmap_node);
453 hmap_destroy(&ib->rules);
454 in_band_set_remotes(ib, NULL, 0);
455 netdev_close(ib->local_netdev);
461 any_addresses_changed(struct in_band *ib,
462 const struct sockaddr_in *addresses, size_t n)
466 if (n != ib->n_remotes) {
470 for (i = 0; i < n; i++) {
471 const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
472 const struct sockaddr_in *new = &addresses[i];
474 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
475 old->sin_port != new->sin_port) {
484 in_band_set_remotes(struct in_band *ib,
485 const struct sockaddr_in *addresses, size_t n)
489 if (!any_addresses_changed(ib, addresses, n)) {
493 /* Clear old remotes. */
494 for (i = 0; i < ib->n_remotes; i++) {
495 netdev_close(ib->remotes[i].remote_netdev);
499 /* Set up new remotes. */
500 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
502 for (i = 0; i < n; i++) {
503 ib->remotes[i].remote_addr = addresses[i];
506 /* Force refresh in next call to in_band_run(). */
507 ib->next_remote_refresh = TIME_MIN;
510 /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'. If
511 * 'queue_id' is negative, 'ib' will not set any queue (which is also the
514 in_band_set_queue(struct in_band *ib, int queue_id)
516 ib->queue_id = queue_id;