2 * Copyright (c) 2008, 2009 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>
28 #include "mac-learning.h"
31 #include "ofp-print.h"
34 #include "openflow/openflow.h"
35 #include "openvswitch/datapath-protocol.h"
37 #include "poll-loop.h"
43 #define THIS_MODULE VLM_in_band
46 #define IB_BASE_PRIORITY 18181800
49 IBR_FROM_LOCAL_DHCP, /* From local port, DHCP. */
50 IBR_TO_LOCAL_ARP, /* To local port, ARP. */
51 IBR_FROM_LOCAL_ARP, /* From local port, ARP. */
52 IBR_TO_REMOTE_ARP, /* To remote MAC, ARP. */
53 IBR_FROM_REMOTE_ARP, /* From remote MAC, ARP. */
54 IBR_TO_CTL_ARP, /* To controller IP, ARP. */
55 IBR_FROM_CTL_ARP, /* From controller IP, ARP. */
56 IBR_TO_CTL_OFP, /* To controller, OpenFlow port. */
57 IBR_FROM_CTL_OFP, /* From controller, OpenFlow port. */
58 #if OFP_TCP_PORT != OFP_SSL_PORT
59 #error Need to support separate TCP and SSL flows.
68 unsigned int priority;
72 struct ofproto *ofproto;
73 struct rconn *controller;
74 struct status_category *ss_cat;
76 /* Keep track of local port's information. */
77 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
78 char local_name[IF_NAMESIZE]; /* Local device name. */
79 time_t next_local_refresh;
81 /* Keep track of controller and next hop's information. */
82 uint32_t controller_ip; /* Controller IP, 0 if unknown. */
83 uint8_t remote_mac[ETH_ADDR_LEN]; /* Remote MAC. */
84 uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous remote MAC. */
85 time_t next_remote_refresh;
87 /* Rules that we set up. */
88 struct ib_rule rules[N_IB_RULES];
91 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
93 static const uint8_t *
94 get_remote_mac(struct in_band *ib)
98 struct in_addr c_in4, r_in4;
100 time_t now = time_now();
102 if (now >= ib->next_remote_refresh) {
103 c_in4.s_addr = ib->controller_ip;
104 memset(ib->remote_mac, 0, sizeof ib->remote_mac);
105 retval = netdev_get_next_hop(&c_in4, &r_in4, &dev_name);
107 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
108 IP_ARGS(&ib->controller_ip), strerror(retval));
109 ib->next_remote_refresh = now + 1;
113 r_in4.s_addr = c_in4.s_addr;
116 retval = netdev_nodev_arp_lookup(dev_name, r_in4.s_addr,
119 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
120 IP_ARGS(&r_in4.s_addr), strerror(retval));
122 have_mac = !eth_addr_is_zero(ib->remote_mac);
126 && !eth_addr_equals(ib->last_remote_mac, ib->remote_mac)) {
127 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT" to "
129 ETH_ADDR_ARGS(ib->last_remote_mac),
130 ETH_ADDR_ARGS(ib->remote_mac));
131 memcpy(ib->last_remote_mac, ib->remote_mac, ETH_ADDR_LEN);
134 /* Schedule next refresh.
136 * If we have an IP address but not a MAC address, then refresh
137 * quickly, since we probably will get a MAC address soon (via ARP).
138 * Otherwise, we can afford to wait a little while. */
139 ib->next_remote_refresh
140 = now + (!ib->controller_ip || have_mac ? 10 : 1);
143 return !eth_addr_is_zero(ib->remote_mac) ? ib->remote_mac : NULL;
146 static const uint8_t *
147 get_local_mac(struct in_band *ib)
149 time_t now = time_now();
150 if (now >= ib->next_local_refresh) {
151 uint8_t ea[ETH_ADDR_LEN];
152 if (!netdev_nodev_get_etheraddr(ib->local_name, ea)) {
153 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
155 ib->next_local_refresh = now + 1;
157 return !eth_addr_is_zero(ib->local_mac) ? ib->local_mac : NULL;
161 in_band_status_cb(struct status_reply *sr, void *in_band_)
163 struct in_band *in_band = in_band_;
165 if (!eth_addr_is_zero(in_band->local_mac)) {
166 status_reply_put(sr, "local-mac="ETH_ADDR_FMT,
167 ETH_ADDR_ARGS(in_band->local_mac));
170 if (!eth_addr_is_zero(in_band->remote_mac)) {
171 status_reply_put(sr, "remote-mac="ETH_ADDR_FMT,
172 ETH_ADDR_ARGS(in_band->remote_mac));
177 drop_flow(struct in_band *in_band, int rule_idx)
179 struct ib_rule *rule = &in_band->rules[rule_idx];
181 if (rule->installed) {
182 rule->installed = false;
183 ofproto_delete_flow(in_band->ofproto, &rule->flow, rule->wildcards,
188 /* out_port and fixed_fields are assumed never to change. */
190 setup_flow(struct in_band *in_band, int rule_idx, const flow_t *flow,
191 uint32_t fixed_fields, uint16_t out_port)
193 struct ib_rule *rule = &in_band->rules[rule_idx];
195 if (!rule->installed || memcmp(flow, &rule->flow, sizeof *flow)) {
196 union ofp_action action;
198 drop_flow(in_band, rule_idx);
200 rule->installed = true;
202 rule->wildcards = OFPFW_ALL & ~fixed_fields;
203 rule->priority = IB_BASE_PRIORITY + (N_IB_RULES - rule_idx);
205 action.type = htons(OFPAT_OUTPUT);
206 action.output.len = htons(sizeof action);
207 action.output.port = htons(out_port);
208 action.output.max_len = htons(0);
209 ofproto_add_flow(in_band->ofproto, &rule->flow, rule->wildcards,
210 rule->priority, &action, 1, 0);
214 /* Returns true if 'packet' should be sent to the local port regardless
215 * of the flow table. */
217 in_band_msg_in_hook(struct in_band *in_band, const flow_t *flow,
218 const struct ofpbuf *packet)
224 /* Regardless of how the flow table is configured, we want to be
225 * able to see replies to our DHCP requests. */
226 if (flow->dl_type == htons(ETH_TYPE_IP)
227 && flow->nw_proto == IP_TYPE_UDP
228 && flow->tp_src == htons(DHCP_SERVER_PORT)
229 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
231 struct dhcp_header *dhcp;
232 const uint8_t *local_mac;
234 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
240 local_mac = get_local_mac(in_band);
241 if (eth_addr_equals(dhcp->chaddr, local_mac)) {
249 /* Returns true if the rule that would match 'flow' with 'actions' is
250 * allowed to be set up in the datapath. */
252 in_band_rule_check(struct in_band *in_band, const flow_t *flow,
253 const struct odp_actions *actions)
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 == IP_TYPE_UDP
263 && flow->tp_src == htons(DHCP_SERVER_PORT)
264 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
267 for (i=0; i<actions->n_actions; i++) {
268 if (actions->actions[i].output.type == ODPAT_OUTPUT
269 && actions->actions[i].output.port == ODPP_LOCAL) {
280 in_band_run(struct in_band *in_band)
282 time_t now = time_now();
283 uint32_t controller_ip;
284 const uint8_t *remote_mac;
285 const uint8_t *local_mac;
288 if (now < in_band->next_remote_refresh
289 && now < in_band->next_local_refresh) {
293 controller_ip = rconn_get_remote_ip(in_band->controller);
294 if (in_band->controller_ip && controller_ip != in_band->controller_ip) {
295 VLOG_DBG("controller IP address changed from "IP_FMT" to "IP_FMT,
296 IP_ARGS(&in_band->controller_ip),
297 IP_ARGS(&controller_ip));
299 in_band->controller_ip = controller_ip;
301 remote_mac = get_remote_mac(in_band);
302 local_mac = get_local_mac(in_band);
305 /* Allow DHCP requests to be sent from the local port. */
306 memset(&flow, 0, sizeof flow);
307 flow.in_port = ODPP_LOCAL;
308 flow.dl_type = htons(ETH_TYPE_IP);
309 memcpy(flow.dl_src, local_mac, ETH_ADDR_LEN);
310 flow.nw_proto = IP_TYPE_UDP;
311 flow.tp_src = htons(DHCP_CLIENT_PORT);
312 flow.tp_dst = htons(DHCP_SERVER_PORT);
313 setup_flow(in_band, IBR_FROM_LOCAL_DHCP, &flow,
314 (OFPFW_IN_PORT | OFPFW_DL_TYPE | OFPFW_DL_SRC
315 | OFPFW_NW_PROTO | OFPFW_TP_SRC | OFPFW_TP_DST),
318 /* Allow the connection's interface to receive directed ARP traffic. */
319 memset(&flow, 0, sizeof flow);
320 flow.dl_type = htons(ETH_TYPE_ARP);
321 memcpy(flow.dl_dst, local_mac, ETH_ADDR_LEN);
322 flow.nw_proto = ARP_OP_REPLY;
323 setup_flow(in_band, IBR_TO_LOCAL_ARP, &flow,
324 (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO),
327 /* Allow the connection's interface to be the source of ARP traffic. */
328 memset(&flow, 0, sizeof flow);
329 flow.dl_type = htons(ETH_TYPE_ARP);
330 memcpy(flow.dl_src, local_mac, ETH_ADDR_LEN);
331 flow.nw_proto = ARP_OP_REQUEST;
332 setup_flow(in_band, IBR_FROM_LOCAL_ARP, &flow,
333 (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO),
336 drop_flow(in_band, IBR_TO_LOCAL_ARP);
337 drop_flow(in_band, IBR_FROM_LOCAL_ARP);
341 /* Allow ARP replies to the remote side's MAC. */
342 memset(&flow, 0, sizeof flow);
343 flow.dl_type = htons(ETH_TYPE_ARP);
344 memcpy(flow.dl_dst, remote_mac, ETH_ADDR_LEN);
345 flow.nw_proto = ARP_OP_REPLY;
346 setup_flow(in_band, IBR_TO_REMOTE_ARP, &flow,
347 (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO),
350 /* Allow ARP requests from the remote side's MAC. */
351 memset(&flow, 0, sizeof flow);
352 flow.dl_type = htons(ETH_TYPE_ARP);
353 memcpy(flow.dl_src, remote_mac, ETH_ADDR_LEN);
354 flow.nw_proto = ARP_OP_REQUEST;
355 setup_flow(in_band, IBR_FROM_REMOTE_ARP, &flow,
356 (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO),
359 drop_flow(in_band, IBR_TO_REMOTE_ARP);
360 drop_flow(in_band, IBR_FROM_REMOTE_ARP);
364 /* Allow ARP replies to the controller's IP. */
365 memset(&flow, 0, sizeof flow);
366 flow.dl_type = htons(ETH_TYPE_ARP);
367 flow.nw_proto = ARP_OP_REPLY;
368 flow.nw_dst = controller_ip;
369 setup_flow(in_band, IBR_TO_CTL_ARP, &flow,
370 (OFPFW_DL_TYPE | OFPFW_NW_PROTO | OFPFW_NW_DST_MASK),
373 /* Allow ARP requests from the controller's IP. */
374 memset(&flow, 0, sizeof flow);
375 flow.dl_type = htons(ETH_TYPE_ARP);
376 flow.nw_proto = ARP_OP_REQUEST;
377 flow.nw_src = controller_ip;
378 setup_flow(in_band, IBR_FROM_CTL_ARP, &flow,
379 (OFPFW_DL_TYPE | OFPFW_NW_PROTO | OFPFW_NW_SRC_MASK),
382 /* OpenFlow traffic to or from the controller.
384 * (A given field's value is completely ignored if it is wildcarded,
385 * which is why we can get away with using a single 'flow' in each
387 memset(&flow, 0, sizeof flow);
388 flow.dl_type = htons(ETH_TYPE_IP);
389 flow.nw_proto = IP_TYPE_TCP;
390 flow.nw_src = controller_ip;
391 flow.nw_dst = controller_ip;
392 flow.tp_src = htons(OFP_TCP_PORT);
393 flow.tp_dst = htons(OFP_TCP_PORT);
394 setup_flow(in_band, IBR_TO_CTL_OFP, &flow,
395 (OFPFW_DL_TYPE | OFPFW_NW_PROTO | OFPFW_NW_DST_MASK
396 | OFPFW_TP_DST), OFPP_NORMAL);
397 setup_flow(in_band, IBR_FROM_CTL_OFP, &flow,
398 (OFPFW_DL_TYPE | OFPFW_NW_PROTO | OFPFW_NW_SRC_MASK
399 | OFPFW_TP_SRC), OFPP_NORMAL);
401 drop_flow(in_band, IBR_TO_CTL_ARP);
402 drop_flow(in_band, IBR_FROM_CTL_ARP);
403 drop_flow(in_band, IBR_TO_CTL_OFP);
404 drop_flow(in_band, IBR_FROM_CTL_OFP);
409 in_band_wait(struct in_band *in_band)
411 time_t now = time_now();
413 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
415 poll_timer_wait((wakeup - now) * 1000);
417 poll_immediate_wake();
422 in_band_flushed(struct in_band *in_band)
426 for (i = 0; i < N_IB_RULES; i++) {
427 in_band->rules[i].installed = false;
432 in_band_create(struct ofproto *ofproto, struct dpif *dpif,
433 struct switch_status *ss, struct rconn *controller,
434 struct in_band **in_bandp)
436 struct in_band *in_band;
439 in_band = xcalloc(1, sizeof *in_band);
440 error = dpif_port_get_name(dpif, ODPP_LOCAL, in_band->local_name,
441 sizeof in_band->local_name);
447 in_band->ofproto = ofproto;
448 in_band->controller = controller;
449 in_band->ss_cat = switch_status_register(ss, "in-band",
450 in_band_status_cb, in_band);
451 in_band->next_remote_refresh = TIME_MIN;
452 in_band->next_local_refresh = TIME_MIN;
458 in_band_destroy(struct in_band *in_band)
461 switch_status_unregister(in_band->ss_cat);
462 /* We don't own the rconn. */