2 * Copyright (c) 2008, 2009, 2010 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.
18 #include "learning-switch.h"
22 #include <netinet/in.h>
27 #include "mac-learning.h"
29 #include "ofp-parse.h"
30 #include "ofp-print.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(learning_switch)
44 /* If nonnegative, the switch sets up flows that expire after the given
45 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
46 * Otherwise, the switch processes every packet. */
49 unsigned long long int datapath_id;
50 time_t last_features_request;
51 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
52 uint32_t wildcards; /* Wildcards to apply to flows. */
53 bool action_normal; /* Use OFPP_NORMAL? */
54 uint32_t queue; /* OpenFlow queue to use, or UINT32_MAX. */
56 /* Number of outgoing queued packets on the rconn. */
57 struct rconn_packet_counter *queued;
60 /* The log messages here could actually be useful in debugging, so keep the
61 * rate limit relatively high. */
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
64 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
65 static void send_features_request(struct lswitch *, struct rconn *);
67 typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
68 static packet_handler_func process_switch_features;
69 static packet_handler_func process_packet_in;
70 static packet_handler_func process_echo_request;
72 /* Creates and returns a new learning switch whose configuration is given by
75 * 'rconn' is used to send out an OpenFlow features request. */
77 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
79 const struct ofpbuf *b;
82 sw = xzalloc(sizeof *sw);
83 sw->max_idle = cfg->max_idle;
85 sw->last_features_request = time_now() - 1;
86 sw->ml = cfg->mode == LSW_LEARN ? mac_learning_create() : NULL;
87 sw->action_normal = cfg->mode == LSW_NORMAL;
88 if (cfg->exact_flows) {
92 /* We cannot wildcard all fields.
93 * We need in_port to detect moves.
94 * We need both SA and DA to do learning. */
95 sw->wildcards = (OFPFW_DL_TYPE | OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK
96 | OFPFW_NW_PROTO | OFPFW_TP_SRC | OFPFW_TP_DST);
98 sw->queue = cfg->queue_id;
99 sw->queued = rconn_packet_counter_create();
100 send_features_request(sw, rconn);
102 for (b = cfg->default_flows; b; b = b->next) {
103 queue_tx(sw, rconn, ofpbuf_clone(b));
111 lswitch_destroy(struct lswitch *sw)
114 mac_learning_destroy(sw->ml);
115 rconn_packet_counter_destroy(sw->queued);
120 /* Takes care of necessary 'sw' activity, except for receiving packets (which
121 * the caller must do). */
123 lswitch_run(struct lswitch *sw)
126 mac_learning_run(sw->ml, NULL);
131 lswitch_wait(struct lswitch *sw)
134 mac_learning_wait(sw->ml);
138 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
139 * to the learning switch state in 'sw'. The most likely result of processing
140 * is that flow-setup and packet-out OpenFlow messages will be sent out on
143 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
144 const struct ofpbuf *msg)
149 packet_handler_func *handler;
151 static const struct processor processors[] = {
154 sizeof(struct ofp_header),
159 sizeof(struct ofp_switch_features),
160 process_switch_features
164 offsetof(struct ofp_packet_in, data),
169 sizeof(struct ofp_flow_removed),
173 const size_t n_processors = ARRAY_SIZE(processors);
174 const struct processor *p;
175 struct ofp_header *oh;
178 if (sw->datapath_id == 0
179 && oh->type != OFPT_ECHO_REQUEST
180 && oh->type != OFPT_FEATURES_REPLY) {
181 send_features_request(sw, rconn);
185 for (p = processors; p < &processors[n_processors]; p++) {
186 if (oh->type == p->type) {
187 if (msg->size < p->min_size) {
188 VLOG_WARN_RL(&rl, "%016llx: %s: too short (%zu bytes) for "
189 "type %"PRIu8" (min %zu)", sw->datapath_id,
190 rconn_get_name(rconn), msg->size, oh->type,
195 (p->handler)(sw, rconn, msg->data);
200 if (VLOG_IS_DBG_ENABLED()) {
201 char *s = ofp_to_string(msg->data, msg->size, 2);
202 VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
209 send_features_request(struct lswitch *sw, struct rconn *rconn)
211 time_t now = time_now();
212 if (now >= sw->last_features_request + 1) {
214 struct ofp_switch_config *osc;
216 /* Send OFPT_FEATURES_REQUEST. */
217 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
218 queue_tx(sw, rconn, b);
220 /* Send OFPT_SET_CONFIG. */
221 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
222 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
223 queue_tx(sw, rconn, b);
225 sw->last_features_request = now;
230 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
232 int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
233 if (retval && retval != ENOTCONN) {
234 if (retval == EAGAIN) {
235 VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
236 sw->datapath_id, rconn_get_name(rconn));
238 VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
239 sw->datapath_id, rconn_get_name(rconn),
246 process_switch_features(struct lswitch *sw, struct rconn *rconn OVS_UNUSED,
249 struct ofp_switch_features *osf = osf_;
251 sw->datapath_id = ntohll(osf->datapath_id);
255 lswitch_choose_destination(struct lswitch *sw, const flow_t *flow)
259 /* Learn the source MAC. */
261 if (mac_learning_learn(sw->ml, flow->dl_src, 0, flow->in_port,
262 GRAT_ARP_LOCK_NONE)) {
263 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
264 "port %"PRIu16, sw->datapath_id,
265 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
269 /* Drop frames for reserved multicast addresses. */
270 if (eth_addr_is_reserved(flow->dl_dst)) {
274 out_port = OFPP_FLOOD;
276 int learned_port = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
277 if (learned_port >= 0) {
278 out_port = learned_port;
279 if (out_port == flow->in_port) {
280 /* Don't send a packet back out its input port. */
286 /* Check if we need to use "NORMAL" action. */
287 if (sw->action_normal && out_port != OFPP_FLOOD) {
295 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
297 struct ofp_packet_in *opi = opi_;
298 uint16_t in_port = ntohs(opi->in_port);
301 struct ofp_action_header actions[2];
304 size_t pkt_ofs, pkt_len;
308 /* Ignore packets sent via output to OFPP_CONTROLLER. This library never
309 * uses such an action. You never know what experiments might be going on,
310 * though, and it seems best not to interfere with them. */
311 if (opi->reason != OFPR_NO_MATCH) {
315 /* Extract flow data from 'opi' into 'flow'. */
316 pkt_ofs = offsetof(struct ofp_packet_in, data);
317 pkt_len = ntohs(opi->header.length) - pkt_ofs;
318 pkt.data = opi->data;
320 flow_extract(&pkt, 0, in_port, &flow);
322 /* Choose output port. */
323 out_port = lswitch_choose_destination(sw, &flow);
326 if (out_port == OFPP_NONE) {
328 } else if (sw->queue == UINT32_MAX || out_port >= OFPP_MAX) {
329 struct ofp_action_output oao;
331 memset(&oao, 0, sizeof oao);
332 oao.type = htons(OFPAT_OUTPUT);
333 oao.len = htons(sizeof oao);
334 oao.port = htons(out_port);
336 memcpy(actions, &oao, sizeof oao);
337 actions_len = sizeof oao;
339 struct ofp_action_enqueue oae;
341 memset(&oae, 0, sizeof oae);
342 oae.type = htons(OFPAT_ENQUEUE);
343 oae.len = htons(sizeof oae);
344 oae.port = htons(out_port);
345 oae.queue_id = htonl(sw->queue);
347 memcpy(actions, &oae, sizeof oae);
348 actions_len = sizeof oae;
350 assert(actions_len <= sizeof actions);
352 /* Send the packet, and possibly the whole flow, to the output port. */
353 if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
354 struct ofpbuf *buffer;
355 struct ofp_flow_mod *ofm;
357 /* The output port is known, or we always flood everything, so add a
359 buffer = make_add_flow(&flow, ntohl(opi->buffer_id),
360 sw->max_idle, actions_len);
361 ofpbuf_put(buffer, actions, actions_len);
363 ofm->match.wildcards = htonl(sw->wildcards);
364 queue_tx(sw, rconn, buffer);
366 /* If the switch didn't buffer the packet, we need to send a copy. */
367 if (ntohl(opi->buffer_id) == UINT32_MAX && actions_len > 0) {
369 make_packet_out(&pkt, UINT32_MAX, in_port,
370 actions, actions_len / sizeof *actions));
373 /* We don't know that MAC, or we don't set up flows. Send along the
374 * packet without setting up a flow. */
375 if (ntohl(opi->buffer_id) != UINT32_MAX || actions_len > 0) {
377 make_packet_out(&pkt, ntohl(opi->buffer_id), in_port,
378 actions, actions_len / sizeof *actions));
384 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
386 struct ofp_header *rq = rq_;
387 queue_tx(sw, rconn, make_echo_reply(rq));