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>
26 #include "mac-learning.h"
29 #include "ofp-print.h"
32 #include "openflow/openflow.h"
34 #include "poll-loop.h"
40 #define THIS_MODULE VLM_in_band
43 #define IB_BASE_PRIORITY 18181800
46 IBR_FROM_LOCAL_PORT, /* Sent by the local port. */
47 IBR_OFP_TO_LOCAL, /* Sent to secure channel on local port. */
48 IBR_ARP_FROM_LOCAL, /* ARP from the local port. */
49 IBR_ARP_FROM_CTL, /* ARP from the controller. */
50 IBR_TO_CTL_OFP_SRC, /* To controller, OpenFlow source port. */
51 IBR_TO_CTL_OFP_DST, /* To controller, OpenFlow dest port. */
52 IBR_FROM_CTL_OFP_SRC, /* From controller, OpenFlow source port. */
53 IBR_FROM_CTL_OFP_DST, /* From controller, OpenFlow dest port. */
54 #if OFP_TCP_PORT != OFP_SSL_PORT
55 #error Need to support separate TCP and SSL flows.
64 unsigned int priority;
68 struct ofproto *ofproto;
69 struct rconn *controller;
70 struct status_category *ss_cat;
72 /* Keeping track of controller's MAC address. */
73 uint32_t ip; /* Current IP, 0 if unknown. */
74 uint32_t last_ip; /* Last known IP, 0 if never known. */
75 uint8_t mac[ETH_ADDR_LEN]; /* Current MAC, 0 if unknown. */
76 uint8_t last_mac[ETH_ADDR_LEN]; /* Last known MAC, 0 if never known */
78 time_t next_refresh; /* Next time to refresh MAC address. */
80 /* Keeping track of the local port's MAC address. */
81 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
82 time_t next_local_refresh; /* Next time to refresh MAC address. */
84 /* Rules that we set up. */
85 struct ib_rule rules[N_IB_RULES];
88 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
90 static const uint8_t *
91 get_controller_mac(struct in_band *ib)
93 time_t now = time_now();
94 uint32_t controller_ip;
96 controller_ip = rconn_get_remote_ip(ib->controller);
97 if (controller_ip != ib->ip || now >= ib->next_refresh) {
100 ib->ip = controller_ip;
102 /* Look up MAC address. */
103 memset(ib->mac, 0, sizeof ib->mac);
105 uint32_t local_ip = rconn_get_local_ip(ib->controller);
109 in4.s_addr = local_ip;
110 if (netdev_find_dev_by_in4(&in4, &ib->dev_name)) {
111 retval = netdev_nodev_arp_lookup(ib->dev_name, ib->ip,
114 VLOG_DBG_RL(&rl, "cannot look up controller MAC address "
116 IP_ARGS(&ib->ip), strerror(retval));
119 VLOG_DBG_RL(&rl, "cannot find device with IP address "IP_FMT,
123 have_mac = !eth_addr_is_zero(ib->mac);
125 /* Log changes in IP, MAC addresses. */
126 if (ib->ip && ib->ip != ib->last_ip) {
127 VLOG_DBG("controller IP address changed from "IP_FMT
128 " to "IP_FMT, IP_ARGS(&ib->last_ip), IP_ARGS(&ib->ip));
129 ib->last_ip = ib->ip;
131 if (have_mac && memcmp(ib->last_mac, ib->mac, ETH_ADDR_LEN)) {
132 VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
134 ETH_ADDR_ARGS(ib->last_mac), ETH_ADDR_ARGS(ib->mac));
135 memcpy(ib->last_mac, ib->mac, ETH_ADDR_LEN);
138 /* Schedule next refresh.
140 * If we have an IP address but not a MAC address, then refresh
141 * quickly, since we probably will get a MAC address soon (via ARP).
142 * Otherwise, we can afford to wait a little while. */
143 ib->next_refresh = now + (!ib->ip || have_mac ? 10 : 1);
145 return !eth_addr_is_zero(ib->mac) ? ib->mac : NULL;
148 static const uint8_t *
149 get_local_mac(struct in_band *ib)
151 time_t now = time_now();
152 if (now >= ib->next_local_refresh) {
153 uint8_t ea[ETH_ADDR_LEN];
154 if (ib->dev_name && (!netdev_nodev_get_etheraddr(ib->dev_name, ea))) {
155 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
157 ib->next_local_refresh = now + 1;
159 return !eth_addr_is_zero(ib->local_mac) ? ib->local_mac : NULL;
163 in_band_status_cb(struct status_reply *sr, void *in_band_)
165 struct in_band *in_band = in_band_;
166 const uint8_t *local_mac;
167 const uint8_t *controller_mac;
169 local_mac = get_local_mac(in_band);
171 status_reply_put(sr, "local-mac="ETH_ADDR_FMT,
172 ETH_ADDR_ARGS(local_mac));
175 controller_mac = get_controller_mac(in_band);
176 if (controller_mac) {
177 status_reply_put(sr, "controller-mac="ETH_ADDR_FMT,
178 ETH_ADDR_ARGS(controller_mac));
183 drop_flow(struct in_band *in_band, int rule_idx)
185 struct ib_rule *rule = &in_band->rules[rule_idx];
187 if (rule->installed) {
188 rule->installed = false;
189 ofproto_delete_flow(in_band->ofproto, &rule->flow, rule->wildcards,
194 /* out_port and fixed_fields are assumed never to change. */
196 setup_flow(struct in_band *in_band, int rule_idx, const flow_t *flow,
197 uint32_t fixed_fields, uint16_t out_port)
199 struct ib_rule *rule = &in_band->rules[rule_idx];
201 if (!rule->installed || memcmp(flow, &rule->flow, sizeof *flow)) {
202 union ofp_action action;
204 drop_flow(in_band, rule_idx);
206 rule->installed = true;
208 rule->wildcards = OFPFW_ALL & ~fixed_fields;
209 rule->priority = IB_BASE_PRIORITY + (N_IB_RULES - rule_idx);
211 action.type = htons(OFPAT_OUTPUT);
212 action.output.len = htons(sizeof action);
213 action.output.port = htons(out_port);
214 action.output.max_len = htons(0);
215 ofproto_add_flow(in_band->ofproto, &rule->flow, rule->wildcards,
216 rule->priority, &action, 1, 0);
221 in_band_run(struct in_band *in_band)
223 const uint8_t *controller_mac;
224 const uint8_t *local_mac;
227 if (time_now() < MIN(in_band->next_refresh, in_band->next_local_refresh)) {
230 controller_mac = get_controller_mac(in_band);
231 local_mac = get_local_mac(in_band);
233 /* Switch traffic sent by the local port. */
234 memset(&flow, 0, sizeof flow);
235 flow.in_port = ODPP_LOCAL;
236 setup_flow(in_band, IBR_FROM_LOCAL_PORT, &flow, OFPFW_IN_PORT,
240 /* Deliver traffic sent to the connection's interface. */
241 memset(&flow, 0, sizeof flow);
242 memcpy(flow.dl_dst, local_mac, ETH_ADDR_LEN);
243 setup_flow(in_band, IBR_OFP_TO_LOCAL, &flow, OFPFW_DL_DST,
246 /* Allow the connection's interface to be the source of ARP traffic. */
247 memset(&flow, 0, sizeof flow);
248 flow.dl_type = htons(ETH_TYPE_ARP);
249 memcpy(flow.dl_src, local_mac, ETH_ADDR_LEN);
250 setup_flow(in_band, IBR_ARP_FROM_LOCAL, &flow,
251 OFPFW_DL_TYPE | OFPFW_DL_SRC, OFPP_NORMAL);
253 drop_flow(in_band, IBR_OFP_TO_LOCAL);
254 drop_flow(in_band, IBR_ARP_FROM_LOCAL);
257 if (controller_mac) {
258 /* Switch ARP requests sent by the controller. (OFPP_NORMAL will "do
259 * the right thing" regarding VLANs here.) */
260 memset(&flow, 0, sizeof flow);
261 flow.dl_type = htons(ETH_TYPE_ARP);
262 memcpy(flow.dl_dst, eth_addr_broadcast, ETH_ADDR_LEN);
263 memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN);
264 setup_flow(in_band, IBR_ARP_FROM_CTL, &flow,
265 OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_DL_SRC,
268 /* OpenFlow traffic to or from the controller.
270 * (A given field's value is completely ignored if it is wildcarded,
271 * which is why we can get away with using a single 'flow' in each
273 memset(&flow, 0, sizeof flow);
274 flow.dl_type = htons(ETH_TYPE_IP);
275 memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN);
276 memcpy(flow.dl_dst, controller_mac, ETH_ADDR_LEN);
277 flow.nw_proto = IP_TYPE_TCP;
278 flow.tp_src = htons(OFP_TCP_PORT);
279 flow.tp_dst = htons(OFP_TCP_PORT);
280 setup_flow(in_band, IBR_TO_CTL_OFP_SRC, &flow,
281 (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO
282 | OFPFW_TP_SRC), OFPP_NORMAL);
283 setup_flow(in_band, IBR_TO_CTL_OFP_DST, &flow,
284 (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO
285 | OFPFW_TP_DST), OFPP_NORMAL);
286 setup_flow(in_band, IBR_FROM_CTL_OFP_SRC, &flow,
287 (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO
288 | OFPFW_TP_SRC), OFPP_NORMAL);
289 setup_flow(in_band, IBR_FROM_CTL_OFP_DST, &flow,
290 (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO
291 | OFPFW_TP_DST), OFPP_NORMAL);
293 drop_flow(in_band, IBR_ARP_FROM_CTL);
294 drop_flow(in_band, IBR_TO_CTL_OFP_DST);
295 drop_flow(in_band, IBR_TO_CTL_OFP_SRC);
296 drop_flow(in_band, IBR_FROM_CTL_OFP_DST);
297 drop_flow(in_band, IBR_FROM_CTL_OFP_SRC);
302 in_band_wait(struct in_band *in_band)
304 time_t now = time_now();
305 time_t wakeup = MIN(in_band->next_refresh, in_band->next_local_refresh);
307 poll_timer_wait((wakeup - now) * 1000);
309 poll_immediate_wake();
314 in_band_flushed(struct in_band *in_band)
318 for (i = 0; i < N_IB_RULES; i++) {
319 in_band->rules[i].installed = false;
324 in_band_create(struct ofproto *ofproto, struct switch_status *ss,
325 struct rconn *controller, struct in_band **in_bandp)
327 struct in_band *in_band;
329 in_band = xcalloc(1, sizeof *in_band);
330 in_band->ofproto = ofproto;
331 in_band->controller = controller;
332 in_band->ss_cat = switch_status_register(ss, "in-band",
333 in_band_status_cb, in_band);
334 in_band->next_refresh = TIME_MIN;
335 in_band->next_local_refresh = TIME_MIN;
336 in_band->dev_name = NULL;
342 in_band_destroy(struct in_band *in_band)
345 switch_status_unregister(in_band->ss_cat);
346 /* We don't own the rconn. */