]> git.proxmox.com Git - mirror_ovs.git/blob - lib/learning-switch.c
ofproto: New feature to notify controllers of flow table changes.
[mirror_ovs.git] / lib / learning-switch.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "learning-switch.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "byte-order.h"
27 #include "classifier.h"
28 #include "flow.h"
29 #include "hmap.h"
30 #include "mac-learning.h"
31 #include "ofpbuf.h"
32 #include "ofp-actions.h"
33 #include "ofp-errors.h"
34 #include "ofp-parse.h"
35 #include "ofp-print.h"
36 #include "ofp-util.h"
37 #include "openflow/openflow.h"
38 #include "poll-loop.h"
39 #include "rconn.h"
40 #include "shash.h"
41 #include "simap.h"
42 #include "timeval.h"
43 #include "vconn.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(learning_switch);
47
48 struct lswitch_port {
49 struct hmap_node hmap_node; /* Hash node for port number. */
50 uint16_t port_no; /* OpenFlow port number, in host byte order. */
51 uint32_t queue_id; /* OpenFlow queue number. */
52 };
53
54 struct lswitch {
55 /* If nonnegative, the switch sets up flows that expire after the given
56 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
57 * Otherwise, the switch processes every packet. */
58 int max_idle;
59
60 enum ofputil_protocol protocol;
61 unsigned long long int datapath_id;
62 time_t last_features_request;
63 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
64 struct flow_wildcards wc; /* Wildcards to apply to flows. */
65 bool action_normal; /* Use OFPP_NORMAL? */
66
67 /* Queue distribution. */
68 uint32_t default_queue; /* Default OpenFlow queue, or UINT32_MAX. */
69 struct hmap queue_numbers; /* Map from port number to lswitch_port. */
70 struct shash queue_names; /* Map from port name to lswitch_port. */
71
72 /* Number of outgoing queued packets on the rconn. */
73 struct rconn_packet_counter *queued;
74 };
75
76 /* The log messages here could actually be useful in debugging, so keep the
77 * rate limit relatively high. */
78 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
79
80 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
81 static void send_features_request(struct lswitch *, struct rconn *);
82
83 static enum ofperr process_switch_features(struct lswitch *,
84 struct ofp_switch_features *);
85 static void process_packet_in(struct lswitch *, struct rconn *,
86 const struct ofp_header *);
87 static void process_echo_request(struct lswitch *, struct rconn *,
88 const struct ofp_header *);
89
90 /* Creates and returns a new learning switch whose configuration is given by
91 * 'cfg'.
92 *
93 * 'rconn' is used to send out an OpenFlow features request. */
94 struct lswitch *
95 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
96 {
97 enum ofputil_protocol protocol;
98 struct lswitch *sw;
99
100 sw = xzalloc(sizeof *sw);
101 sw->max_idle = cfg->max_idle;
102 sw->datapath_id = 0;
103 sw->last_features_request = time_now() - 1;
104 sw->ml = (cfg->mode == LSW_LEARN
105 ? mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME)
106 : NULL);
107 sw->action_normal = cfg->mode == LSW_NORMAL;
108
109 flow_wildcards_init_exact(&sw->wc);
110 if (cfg->wildcards) {
111 uint32_t ofpfw;
112
113 if (cfg->wildcards == UINT32_MAX) {
114 /* Try to wildcard as many fields as possible, but we cannot
115 * wildcard all fields. We need in_port to detect moves. We need
116 * Ethernet source and dest and VLAN VID to do L2 learning. */
117 ofpfw = (OFPFW10_DL_TYPE | OFPFW10_DL_VLAN_PCP
118 | OFPFW10_NW_SRC_ALL | OFPFW10_NW_DST_ALL
119 | OFPFW10_NW_TOS | OFPFW10_NW_PROTO
120 | OFPFW10_TP_SRC | OFPFW10_TP_DST);
121 } else {
122 ofpfw = cfg->wildcards;
123 }
124
125 ofputil_wildcard_from_ofpfw10(ofpfw, &sw->wc);
126 }
127
128 sw->default_queue = cfg->default_queue;
129 hmap_init(&sw->queue_numbers);
130 shash_init(&sw->queue_names);
131 if (cfg->port_queues) {
132 struct simap_node *node;
133
134 SIMAP_FOR_EACH (node, cfg->port_queues) {
135 struct lswitch_port *port = xmalloc(sizeof *port);
136 hmap_node_nullify(&port->hmap_node);
137 port->queue_id = node->data;
138 shash_add(&sw->queue_names, node->name, port);
139 }
140 }
141
142 sw->queued = rconn_packet_counter_create();
143 send_features_request(sw, rconn);
144
145 protocol = ofputil_protocol_from_ofp_version(rconn_get_version(rconn));
146 if (cfg->default_flows) {
147 enum ofputil_protocol usable_protocols;
148 struct ofpbuf *msg = NULL;
149 int error = 0;
150 size_t i;
151
152 /* If the initial protocol isn't good enough for default_flows, then
153 * pick one that will work and encode messages to set up that
154 * protocol.
155 *
156 * This could be improved by actually negotiating a mutually acceptable
157 * flow format with the switch, but that would require an asynchronous
158 * state machine. This version ought to work fine in practice. */
159 usable_protocols = ofputil_flow_mod_usable_protocols(
160 cfg->default_flows, cfg->n_default_flows);
161 if (!(protocol & usable_protocols)) {
162 enum ofputil_protocol want = rightmost_1bit(usable_protocols);
163 while (!error) {
164 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
165 if (!msg) {
166 break;
167 }
168 error = rconn_send(rconn, msg, NULL);
169 }
170 }
171
172 for (i = 0; !error && i < cfg->n_default_flows; i++) {
173 msg = ofputil_encode_flow_mod(&cfg->default_flows[i], protocol);
174 error = rconn_send(rconn, msg, NULL);
175 }
176
177 if (error) {
178 VLOG_INFO_RL(&rl, "%s: failed to queue default flows (%s)",
179 rconn_get_name(rconn), strerror(error));
180 }
181 }
182 sw->protocol = protocol;
183
184 return sw;
185 }
186
187 /* Destroys 'sw'. */
188 void
189 lswitch_destroy(struct lswitch *sw)
190 {
191 if (sw) {
192 struct lswitch_port *node, *next;
193
194 HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
195 hmap_remove(&sw->queue_numbers, &node->hmap_node);
196 free(node);
197 }
198 shash_destroy(&sw->queue_names);
199 mac_learning_destroy(sw->ml);
200 rconn_packet_counter_destroy(sw->queued);
201 free(sw);
202 }
203 }
204
205 /* Takes care of necessary 'sw' activity, except for receiving packets (which
206 * the caller must do). */
207 void
208 lswitch_run(struct lswitch *sw)
209 {
210 if (sw->ml) {
211 mac_learning_run(sw->ml, NULL);
212 }
213 }
214
215 void
216 lswitch_wait(struct lswitch *sw)
217 {
218 if (sw->ml) {
219 mac_learning_wait(sw->ml);
220 }
221 }
222
223 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
224 * to the learning switch state in 'sw'. The most likely result of processing
225 * is that flow-setup and packet-out OpenFlow messages will be sent out on
226 * 'rconn'. */
227 void
228 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
229 const struct ofpbuf *msg)
230 {
231 const struct ofp_header *oh = msg->data;
232 const struct ofputil_msg_type *type;
233
234 if (sw->datapath_id == 0
235 && oh->type != OFPT_ECHO_REQUEST
236 && oh->type != OFPT_FEATURES_REPLY) {
237 send_features_request(sw, rconn);
238 return;
239 }
240
241 ofputil_decode_msg_type(oh, &type);
242 switch (ofputil_msg_type_code(type)) {
243 case OFPUTIL_OFPT_ECHO_REQUEST:
244 process_echo_request(sw, rconn, msg->data);
245 break;
246
247 case OFPUTIL_OFPT_FEATURES_REPLY:
248 process_switch_features(sw, msg->data);
249 break;
250
251 case OFPUTIL_OFPT_PACKET_IN:
252 case OFPUTIL_NXT_PACKET_IN:
253 process_packet_in(sw, rconn, msg->data);
254 break;
255
256 case OFPUTIL_OFPT_FLOW_REMOVED:
257 /* Nothing to do. */
258 break;
259
260 case OFPUTIL_MSG_INVALID:
261 case OFPUTIL_OFPT_HELLO:
262 case OFPUTIL_OFPT_ERROR:
263 case OFPUTIL_OFPT_ECHO_REPLY:
264 case OFPUTIL_OFPT_FEATURES_REQUEST:
265 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
266 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
267 case OFPUTIL_OFPT_SET_CONFIG:
268 case OFPUTIL_OFPT_PORT_STATUS:
269 case OFPUTIL_OFPT_PACKET_OUT:
270 case OFPUTIL_OFPT_FLOW_MOD:
271 case OFPUTIL_OFPT_PORT_MOD:
272 case OFPUTIL_OFPT_BARRIER_REQUEST:
273 case OFPUTIL_OFPT_BARRIER_REPLY:
274 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
275 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
276 case OFPUTIL_OFPST_DESC_REQUEST:
277 case OFPUTIL_OFPST_FLOW_REQUEST:
278 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
279 case OFPUTIL_OFPST_TABLE_REQUEST:
280 case OFPUTIL_OFPST_PORT_REQUEST:
281 case OFPUTIL_OFPST_QUEUE_REQUEST:
282 case OFPUTIL_OFPST_PORT_DESC_REQUEST:
283 case OFPUTIL_OFPST_DESC_REPLY:
284 case OFPUTIL_OFPST_FLOW_REPLY:
285 case OFPUTIL_OFPST_QUEUE_REPLY:
286 case OFPUTIL_OFPST_PORT_REPLY:
287 case OFPUTIL_OFPST_TABLE_REPLY:
288 case OFPUTIL_OFPST_AGGREGATE_REPLY:
289 case OFPUTIL_OFPST_PORT_DESC_REPLY:
290 case OFPUTIL_NXT_ROLE_REQUEST:
291 case OFPUTIL_NXT_ROLE_REPLY:
292 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
293 case OFPUTIL_NXT_SET_FLOW_FORMAT:
294 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
295 case OFPUTIL_NXT_FLOW_MOD:
296 case OFPUTIL_NXT_FLOW_REMOVED:
297 case OFPUTIL_NXT_FLOW_AGE:
298 case OFPUTIL_NXT_FLOW_MONITOR_CANCEL:
299 case OFPUTIL_NXT_FLOW_MONITOR_PAUSED:
300 case OFPUTIL_NXT_FLOW_MONITOR_RESUMED:
301 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
302 case OFPUTIL_NXT_SET_CONTROLLER_ID:
303 case OFPUTIL_NXST_FLOW_REQUEST:
304 case OFPUTIL_NXST_AGGREGATE_REQUEST:
305 case OFPUTIL_NXST_FLOW_MONITOR_REQUEST:
306 case OFPUTIL_NXST_FLOW_REPLY:
307 case OFPUTIL_NXST_AGGREGATE_REPLY:
308 case OFPUTIL_NXST_FLOW_MONITOR_REPLY:
309 default:
310 if (VLOG_IS_DBG_ENABLED()) {
311 char *s = ofp_to_string(msg->data, msg->size, 2);
312 VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
313 sw->datapath_id, s);
314 free(s);
315 }
316 }
317 }
318 \f
319 static void
320 send_features_request(struct lswitch *sw, struct rconn *rconn)
321 {
322 time_t now = time_now();
323 if (now >= sw->last_features_request + 1) {
324 struct ofpbuf *b;
325 struct ofp_switch_config *osc;
326
327 /* Send OFPT_FEATURES_REQUEST. */
328 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
329 queue_tx(sw, rconn, b);
330
331 /* Send OFPT_SET_CONFIG. */
332 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
333 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
334 queue_tx(sw, rconn, b);
335
336 sw->last_features_request = now;
337 }
338 }
339
340 static void
341 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
342 {
343 int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
344 if (retval && retval != ENOTCONN) {
345 if (retval == EAGAIN) {
346 VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
347 sw->datapath_id, rconn_get_name(rconn));
348 } else {
349 VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
350 sw->datapath_id, rconn_get_name(rconn),
351 strerror(retval));
352 }
353 }
354 }
355
356 static enum ofperr
357 process_switch_features(struct lswitch *sw, struct ofp_switch_features *osf)
358 {
359 struct ofputil_switch_features features;
360 struct ofputil_phy_port port;
361 enum ofperr error;
362 struct ofpbuf b;
363
364 error = ofputil_decode_switch_features(osf, &features, &b);
365 if (error) {
366 VLOG_ERR("received invalid switch feature reply (%s)",
367 ofperr_to_string(error));
368 return error;
369 }
370
371 sw->datapath_id = features.datapath_id;
372
373 while (!ofputil_pull_phy_port(osf->header.version, &b, &port)) {
374 struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
375 if (lp && hmap_node_is_null(&lp->hmap_node)) {
376 lp->port_no = port.port_no;
377 hmap_insert(&sw->queue_numbers, &lp->hmap_node,
378 hash_int(lp->port_no, 0));
379 }
380 }
381 return 0;
382 }
383
384 static uint16_t
385 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
386 {
387 uint16_t out_port;
388
389 /* Learn the source MAC. */
390 if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
391 struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src, 0);
392 if (mac_entry_is_new(mac) || mac->port.i != flow->in_port) {
393 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
394 "port %"PRIu16, sw->datapath_id,
395 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
396
397 mac->port.i = flow->in_port;
398 mac_learning_changed(sw->ml, mac);
399 }
400 }
401
402 /* Drop frames for reserved multicast addresses. */
403 if (eth_addr_is_reserved(flow->dl_dst)) {
404 return OFPP_NONE;
405 }
406
407 out_port = OFPP_FLOOD;
408 if (sw->ml) {
409 struct mac_entry *mac;
410
411 mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
412 if (mac) {
413 out_port = mac->port.i;
414 if (out_port == flow->in_port) {
415 /* Don't send a packet back out its input port. */
416 return OFPP_NONE;
417 }
418 }
419 }
420
421 /* Check if we need to use "NORMAL" action. */
422 if (sw->action_normal && out_port != OFPP_FLOOD) {
423 return OFPP_NORMAL;
424 }
425
426 return out_port;
427 }
428
429 static uint32_t
430 get_queue_id(const struct lswitch *sw, uint16_t in_port)
431 {
432 const struct lswitch_port *port;
433
434 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_int(in_port, 0),
435 &sw->queue_numbers) {
436 if (port->port_no == in_port) {
437 return port->queue_id;
438 }
439 }
440
441 return sw->default_queue;
442 }
443
444 static void
445 process_packet_in(struct lswitch *sw, struct rconn *rconn,
446 const struct ofp_header *oh)
447 {
448 struct ofputil_packet_in pi;
449 uint32_t queue_id;
450 uint16_t out_port;
451
452 uint64_t ofpacts_stub[64 / 8];
453 struct ofpbuf ofpacts;
454
455 struct ofputil_packet_out po;
456 enum ofperr error;
457
458 struct ofpbuf pkt;
459 struct flow flow;
460
461 error = ofputil_decode_packet_in(&pi, oh);
462 if (error) {
463 VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
464 ofperr_to_string(error));
465 return;
466 }
467
468 /* Ignore packets sent via output to OFPP_CONTROLLER. This library never
469 * uses such an action. You never know what experiments might be going on,
470 * though, and it seems best not to interfere with them. */
471 if (pi.reason != OFPR_NO_MATCH) {
472 return;
473 }
474
475 /* Extract flow data from 'opi' into 'flow'. */
476 ofpbuf_use_const(&pkt, pi.packet, pi.packet_len);
477 flow_extract(&pkt, 0, pi.fmd.tun_id, pi.fmd.in_port, &flow);
478
479 /* Choose output port. */
480 out_port = lswitch_choose_destination(sw, &flow);
481
482 /* Make actions. */
483 queue_id = get_queue_id(sw, pi.fmd.in_port);
484 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
485 if (out_port == OFPP_NONE) {
486 /* No actions. */
487 } else if (queue_id == UINT32_MAX || out_port >= OFPP_MAX) {
488 ofpact_put_OUTPUT(&ofpacts)->port = out_port;
489 } else {
490 struct ofpact_enqueue *enqueue = ofpact_put_ENQUEUE(&ofpacts);
491 enqueue->port = out_port;
492 enqueue->queue = queue_id;
493 }
494 ofpact_pad(&ofpacts);
495
496 /* Prepare packet_out in case we need one. */
497 po.buffer_id = pi.buffer_id;
498 if (po.buffer_id == UINT32_MAX) {
499 po.packet = pkt.data;
500 po.packet_len = pkt.size;
501 } else {
502 po.packet = NULL;
503 po.packet_len = 0;
504 }
505 po.in_port = pi.fmd.in_port;
506 po.ofpacts = ofpacts.data;
507 po.ofpacts_len = ofpacts.size;
508
509 /* Send the packet, and possibly the whole flow, to the output port. */
510 if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
511 struct ofputil_flow_mod fm;
512 struct ofpbuf *buffer;
513
514 /* The output port is known, or we always flood everything, so add a
515 * new flow. */
516 memset(&fm, 0, sizeof fm);
517 cls_rule_init(&flow, &sw->wc, 0, &fm.cr);
518 fm.table_id = 0xff;
519 fm.command = OFPFC_ADD;
520 fm.idle_timeout = sw->max_idle;
521 fm.buffer_id = pi.buffer_id;
522 fm.out_port = OFPP_NONE;
523 fm.ofpacts = ofpacts.data;
524 fm.ofpacts_len = ofpacts.size;
525 buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
526
527 queue_tx(sw, rconn, buffer);
528
529 /* If the switch didn't buffer the packet, we need to send a copy. */
530 if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
531 queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
532 }
533 } else {
534 /* We don't know that MAC, or we don't set up flows. Send along the
535 * packet without setting up a flow. */
536 if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
537 queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
538 }
539 }
540 }
541
542 static void
543 process_echo_request(struct lswitch *sw, struct rconn *rconn,
544 const struct ofp_header *rq)
545 {
546 queue_tx(sw, rconn, make_echo_reply(rq));
547 }