2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 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.
18 #include "learning-switch.h"
22 #include <netinet/in.h>
26 #include "byte-order.h"
27 #include "classifier.h"
30 #include "mac-learning.h"
32 #include "ofp-actions.h"
33 #include "ofp-errors.h"
35 #include "ofp-parse.h"
36 #include "ofp-print.h"
38 #include "openflow/openflow.h"
39 #include "poll-loop.h"
47 VLOG_DEFINE_THIS_MODULE(learning_switch);
50 struct hmap_node hmap_node; /* Hash node for port number. */
51 ofp_port_t port_no; /* OpenFlow port number. */
52 uint32_t queue_id; /* OpenFlow queue number. */
56 S_CONNECTING, /* Waiting for connection to complete. */
57 S_FEATURES_REPLY, /* Waiting for features reply. */
58 S_SWITCHING, /* Switching flows. */
63 enum lswitch_state state;
65 /* If nonnegative, the switch sets up flows that expire after the given
66 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
67 * Otherwise, the switch processes every packet. */
70 enum ofputil_protocol protocol;
71 unsigned long long int datapath_id;
72 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
73 struct flow_wildcards wc; /* Wildcards to apply to flows. */
74 bool action_normal; /* Use OFPP_NORMAL? */
76 /* Queue distribution. */
77 uint32_t default_queue; /* Default OpenFlow queue, or UINT32_MAX. */
78 struct hmap queue_numbers; /* Map from port number to lswitch_port. */
79 struct shash queue_names; /* Map from port name to lswitch_port. */
81 /* Number of outgoing queued packets on the rconn. */
82 struct rconn_packet_counter *queued;
84 /* If true, do not reply to any messages from the switch (for debugging
88 /* Optional "flow mod" requests to send to the switch at connection time,
89 * to set up the flow table. */
90 const struct ofputil_flow_mod *default_flows;
91 size_t n_default_flows;
92 enum ofputil_protocol usable_protocols;
95 /* The log messages here could actually be useful in debugging, so keep the
96 * rate limit relatively high. */
97 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
99 static void queue_tx(struct lswitch *, struct ofpbuf *);
100 static void send_features_request(struct lswitch *);
102 static void lswitch_process_packet(struct lswitch *, const struct ofpbuf *);
103 static enum ofperr process_switch_features(struct lswitch *,
104 struct ofp_header *);
105 static void process_packet_in(struct lswitch *, const struct ofp_header *);
106 static void process_echo_request(struct lswitch *, const struct ofp_header *);
108 static ofp_port_t get_mac_entry_ofp_port(const struct mac_learning *ml,
109 const struct mac_entry *)
110 OVS_REQ_RDLOCK(ml->rwlock);
111 static void set_mac_entry_ofp_port(struct mac_learning *ml,
112 struct mac_entry *, ofp_port_t)
113 OVS_REQ_WRLOCK(ml->rwlock);
115 /* Creates and returns a new learning switch whose configuration is given by
118 * 'rconn' is used to send out an OpenFlow features request. */
120 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
125 sw = xzalloc(sizeof *sw);
127 sw->state = S_CONNECTING;
128 sw->max_idle = cfg->max_idle;
130 sw->ml = (cfg->mode == LSW_LEARN
131 ? mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME)
133 sw->action_normal = cfg->mode == LSW_NORMAL;
135 switch (cfg->wildcards) {
141 /* Try to wildcard as many fields as possible, but we cannot
142 * wildcard all fields. We need in_port to detect moves. We need
143 * Ethernet source and dest and VLAN VID to do L2 learning. */
144 ofpfw = (OFPFW10_DL_TYPE | OFPFW10_DL_VLAN_PCP
145 | OFPFW10_NW_SRC_ALL | OFPFW10_NW_DST_ALL
146 | OFPFW10_NW_TOS | OFPFW10_NW_PROTO
147 | OFPFW10_TP_SRC | OFPFW10_TP_DST);
151 ofpfw = cfg->wildcards;
154 ofputil_wildcard_from_ofpfw10(ofpfw, &sw->wc);
156 sw->default_queue = cfg->default_queue;
157 hmap_init(&sw->queue_numbers);
158 shash_init(&sw->queue_names);
159 if (cfg->port_queues) {
160 struct simap_node *node;
162 SIMAP_FOR_EACH (node, cfg->port_queues) {
163 struct lswitch_port *port = xmalloc(sizeof *port);
164 hmap_node_nullify(&port->hmap_node);
165 port->queue_id = node->data;
166 shash_add(&sw->queue_names, node->name, port);
170 sw->default_flows = cfg->default_flows;
171 sw->n_default_flows = cfg->n_default_flows;
172 sw->usable_protocols = cfg->usable_protocols;
174 sw->queued = rconn_packet_counter_create();
180 lswitch_handshake(struct lswitch *sw)
182 enum ofputil_protocol protocol;
184 send_features_request(sw);
186 protocol = ofputil_protocol_from_ofp_version(rconn_get_version(sw->rconn));
187 if (sw->default_flows) {
188 struct ofpbuf *msg = NULL;
192 /* If the initial protocol isn't good enough for default_flows, then
193 * pick one that will work and encode messages to set up that
196 * This could be improved by actually negotiating a mutually acceptable
197 * flow format with the switch, but that would require an asynchronous
198 * state machine. This version ought to work fine in practice. */
199 if (!(protocol & sw->usable_protocols)) {
200 enum ofputil_protocol want = rightmost_1bit(sw->usable_protocols);
202 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
206 error = rconn_send(sw->rconn, msg, NULL);
209 if (protocol & sw->usable_protocols) {
210 for (i = 0; !error && i < sw->n_default_flows; i++) {
211 msg = ofputil_encode_flow_mod(&sw->default_flows[i], protocol);
212 error = rconn_send(sw->rconn, msg, NULL);
216 VLOG_INFO_RL(&rl, "%s: failed to queue default flows (%s)",
217 rconn_get_name(sw->rconn), ovs_strerror(error));
220 VLOG_INFO_RL(&rl, "%s: failed to set usable protocol",
221 rconn_get_name(sw->rconn));
224 sw->protocol = protocol;
228 lswitch_is_alive(const struct lswitch *sw)
230 return rconn_is_alive(sw->rconn);
235 lswitch_destroy(struct lswitch *sw)
238 struct lswitch_port *node, *next;
240 rconn_destroy(sw->rconn);
241 HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
242 hmap_remove(&sw->queue_numbers, &node->hmap_node);
245 shash_destroy(&sw->queue_names);
246 mac_learning_unref(sw->ml);
247 rconn_packet_counter_destroy(sw->queued);
252 /* Takes care of necessary 'sw' activity, except for receiving packets (which
253 * the caller must do). */
255 lswitch_run(struct lswitch *sw)
260 ovs_rwlock_wrlock(&sw->ml->rwlock);
261 mac_learning_run(sw->ml);
262 ovs_rwlock_unlock(&sw->ml->rwlock);
265 rconn_run(sw->rconn);
267 if (sw->state == S_CONNECTING) {
268 if (rconn_get_version(sw->rconn) != -1) {
269 lswitch_handshake(sw);
270 sw->state = S_FEATURES_REPLY;
275 for (i = 0; i < 50; i++) {
278 msg = rconn_recv(sw->rconn);
284 lswitch_process_packet(sw, msg);
291 lswitch_wait(struct lswitch *sw)
294 ovs_rwlock_rdlock(&sw->ml->rwlock);
295 mac_learning_wait(sw->ml);
296 ovs_rwlock_unlock(&sw->ml->rwlock);
298 rconn_run_wait(sw->rconn);
299 rconn_recv_wait(sw->rconn);
302 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
303 * to the learning switch state in 'sw'. The most likely result of processing
304 * is that flow-setup and packet-out OpenFlow messages will be sent out on
307 lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
313 if (ofptype_pull(&type, &b)) {
317 if (sw->state == S_FEATURES_REPLY
318 && type != OFPTYPE_ECHO_REQUEST
319 && type != OFPTYPE_FEATURES_REPLY) {
324 case OFPTYPE_ECHO_REQUEST:
325 process_echo_request(sw, ofpbuf_data(msg));
328 case OFPTYPE_FEATURES_REPLY:
329 if (sw->state == S_FEATURES_REPLY) {
330 if (!process_switch_features(sw, ofpbuf_data(msg))) {
331 sw->state = S_SWITCHING;
333 rconn_disconnect(sw->rconn);
338 case OFPTYPE_PACKET_IN:
339 process_packet_in(sw, ofpbuf_data(msg));
342 case OFPTYPE_FLOW_REMOVED:
348 case OFPTYPE_ECHO_REPLY:
349 case OFPTYPE_FEATURES_REQUEST:
350 case OFPTYPE_GET_CONFIG_REQUEST:
351 case OFPTYPE_GET_CONFIG_REPLY:
352 case OFPTYPE_SET_CONFIG:
353 case OFPTYPE_PORT_STATUS:
354 case OFPTYPE_PACKET_OUT:
355 case OFPTYPE_FLOW_MOD:
356 case OFPTYPE_GROUP_MOD:
357 case OFPTYPE_PORT_MOD:
358 case OFPTYPE_TABLE_MOD:
359 case OFPTYPE_BARRIER_REQUEST:
360 case OFPTYPE_BARRIER_REPLY:
361 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
362 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
363 case OFPTYPE_DESC_STATS_REQUEST:
364 case OFPTYPE_DESC_STATS_REPLY:
365 case OFPTYPE_FLOW_STATS_REQUEST:
366 case OFPTYPE_FLOW_STATS_REPLY:
367 case OFPTYPE_AGGREGATE_STATS_REQUEST:
368 case OFPTYPE_AGGREGATE_STATS_REPLY:
369 case OFPTYPE_TABLE_STATS_REQUEST:
370 case OFPTYPE_TABLE_STATS_REPLY:
371 case OFPTYPE_PORT_STATS_REQUEST:
372 case OFPTYPE_PORT_STATS_REPLY:
373 case OFPTYPE_QUEUE_STATS_REQUEST:
374 case OFPTYPE_QUEUE_STATS_REPLY:
375 case OFPTYPE_PORT_DESC_STATS_REQUEST:
376 case OFPTYPE_PORT_DESC_STATS_REPLY:
377 case OFPTYPE_ROLE_REQUEST:
378 case OFPTYPE_ROLE_REPLY:
379 case OFPTYPE_ROLE_STATUS:
380 case OFPTYPE_SET_FLOW_FORMAT:
381 case OFPTYPE_FLOW_MOD_TABLE_ID:
382 case OFPTYPE_SET_PACKET_IN_FORMAT:
383 case OFPTYPE_FLOW_AGE:
384 case OFPTYPE_SET_CONTROLLER_ID:
385 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
386 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
387 case OFPTYPE_FLOW_MONITOR_CANCEL:
388 case OFPTYPE_FLOW_MONITOR_PAUSED:
389 case OFPTYPE_FLOW_MONITOR_RESUMED:
390 case OFPTYPE_GET_ASYNC_REQUEST:
391 case OFPTYPE_GET_ASYNC_REPLY:
392 case OFPTYPE_SET_ASYNC_CONFIG:
393 case OFPTYPE_METER_MOD:
394 case OFPTYPE_GROUP_STATS_REQUEST:
395 case OFPTYPE_GROUP_STATS_REPLY:
396 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
397 case OFPTYPE_GROUP_DESC_STATS_REPLY:
398 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
399 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
400 case OFPTYPE_METER_STATS_REQUEST:
401 case OFPTYPE_METER_STATS_REPLY:
402 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
403 case OFPTYPE_METER_CONFIG_STATS_REPLY:
404 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
405 case OFPTYPE_METER_FEATURES_STATS_REPLY:
406 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
407 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
408 case OFPTYPE_BUNDLE_CONTROL:
409 case OFPTYPE_BUNDLE_ADD_MESSAGE:
411 if (VLOG_IS_DBG_ENABLED()) {
412 char *s = ofp_to_string(ofpbuf_data(msg), ofpbuf_size(msg), 2);
413 VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
421 send_features_request(struct lswitch *sw)
424 struct ofp_switch_config *osc;
425 int ofp_version = rconn_get_version(sw->rconn);
427 ovs_assert(ofp_version > 0 && ofp_version < 0xff);
429 /* Send OFPT_FEATURES_REQUEST. */
430 b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
433 /* Send OFPT_SET_CONFIG. */
434 b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, ofp_version, sizeof *osc);
435 osc = ofpbuf_put_zeros(b, sizeof *osc);
436 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
441 queue_tx(struct lswitch *sw, struct ofpbuf *b)
443 int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
444 if (retval && retval != ENOTCONN) {
445 if (retval == EAGAIN) {
446 VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
447 sw->datapath_id, rconn_get_name(sw->rconn));
449 VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
450 sw->datapath_id, rconn_get_name(sw->rconn),
451 ovs_strerror(retval));
457 process_switch_features(struct lswitch *sw, struct ofp_header *oh)
459 struct ofputil_switch_features features;
460 struct ofputil_phy_port port;
464 error = ofputil_decode_switch_features(oh, &features, &b);
466 VLOG_ERR("received invalid switch feature reply (%s)",
467 ofperr_to_string(error));
471 sw->datapath_id = features.datapath_id;
473 while (!ofputil_pull_phy_port(oh->version, &b, &port)) {
474 struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
475 if (lp && hmap_node_is_null(&lp->hmap_node)) {
476 lp->port_no = port.port_no;
477 hmap_insert(&sw->queue_numbers, &lp->hmap_node,
478 hash_ofp_port(lp->port_no));
485 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
489 /* Learn the source MAC. */
491 ovs_rwlock_wrlock(&sw->ml->rwlock);
492 if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
493 struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src,
495 if (get_mac_entry_ofp_port(sw->ml, mac)
496 != flow->in_port.ofp_port) {
497 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
498 "port %"PRIu16, sw->datapath_id,
499 ETH_ADDR_ARGS(flow->dl_src),
500 flow->in_port.ofp_port);
502 set_mac_entry_ofp_port(sw->ml, mac, flow->in_port.ofp_port);
505 ovs_rwlock_unlock(&sw->ml->rwlock);
508 /* Drop frames for reserved multicast addresses. */
509 if (eth_addr_is_reserved(flow->dl_dst)) {
513 out_port = OFPP_FLOOD;
515 struct mac_entry *mac;
517 ovs_rwlock_rdlock(&sw->ml->rwlock);
518 mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0);
520 out_port = get_mac_entry_ofp_port(sw->ml, mac);
521 if (out_port == flow->in_port.ofp_port) {
522 /* Don't send a packet back out its input port. */
523 ovs_rwlock_unlock(&sw->ml->rwlock);
527 ovs_rwlock_unlock(&sw->ml->rwlock);
530 /* Check if we need to use "NORMAL" action. */
531 if (sw->action_normal && out_port != OFPP_FLOOD) {
539 get_queue_id(const struct lswitch *sw, ofp_port_t in_port)
541 const struct lswitch_port *port;
543 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_ofp_port(in_port),
544 &sw->queue_numbers) {
545 if (port->port_no == in_port) {
546 return port->queue_id;
550 return sw->default_queue;
554 process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
556 struct ofputil_packet_in pi;
560 uint64_t ofpacts_stub[64 / 8];
561 struct ofpbuf ofpacts;
563 struct ofputil_packet_out po;
569 error = ofputil_decode_packet_in(&pi, oh);
571 VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
572 ofperr_to_string(error));
576 /* Ignore packets sent via output to OFPP_CONTROLLER. This library never
577 * uses such an action. You never know what experiments might be going on,
578 * though, and it seems best not to interfere with them. */
579 if (pi.reason != OFPR_NO_MATCH) {
583 /* Extract flow data from 'opi' into 'flow'. */
584 ofpbuf_use_const(&pkt, pi.packet, pi.packet_len);
585 flow_extract(&pkt, NULL, &flow);
586 flow.in_port.ofp_port = pi.fmd.in_port;
587 flow.tunnel.tun_id = pi.fmd.tun_id;
589 /* Choose output port. */
590 out_port = lswitch_choose_destination(sw, &flow);
593 queue_id = get_queue_id(sw, pi.fmd.in_port);
594 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
595 if (out_port == OFPP_NONE) {
597 } else if (queue_id == UINT32_MAX
598 || ofp_to_u16(out_port) >= ofp_to_u16(OFPP_MAX)) {
599 ofpact_put_OUTPUT(&ofpacts)->port = out_port;
601 struct ofpact_enqueue *enqueue = ofpact_put_ENQUEUE(&ofpacts);
602 enqueue->port = out_port;
603 enqueue->queue = queue_id;
605 ofpact_pad(&ofpacts);
607 /* Prepare packet_out in case we need one. */
608 po.buffer_id = pi.buffer_id;
609 if (po.buffer_id == UINT32_MAX) {
610 po.packet = ofpbuf_data(&pkt);
611 po.packet_len = ofpbuf_size(&pkt);
616 po.in_port = pi.fmd.in_port;
617 po.ofpacts = ofpbuf_data(&ofpacts);
618 po.ofpacts_len = ofpbuf_size(&ofpacts);
620 /* Send the packet, and possibly the whole flow, to the output port. */
621 if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
622 struct ofputil_flow_mod fm;
623 struct ofpbuf *buffer;
625 /* The output port is known, or we always flood everything, so add a
627 memset(&fm, 0, sizeof fm);
628 match_init(&fm.match, &flow, &sw->wc);
629 ofputil_normalize_match_quiet(&fm.match);
632 fm.command = OFPFC_ADD;
633 fm.idle_timeout = sw->max_idle;
634 fm.buffer_id = pi.buffer_id;
635 fm.out_port = OFPP_NONE;
636 fm.ofpacts = ofpbuf_data(&ofpacts);
637 fm.ofpacts_len = ofpbuf_size(&ofpacts);
638 buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
640 queue_tx(sw, buffer);
642 /* If the switch didn't buffer the packet, we need to send a copy. */
643 if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
644 queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
647 /* We don't know that MAC, or we don't set up flows. Send along the
648 * packet without setting up a flow. */
649 if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
650 queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
656 process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
658 queue_tx(sw, make_echo_reply(rq));
662 get_mac_entry_ofp_port(const struct mac_learning *ml,
663 const struct mac_entry *e)
664 OVS_REQ_RDLOCK(ml->rwlock)
666 void *port = mac_entry_get_port(ml, e);
667 return (OVS_FORCE ofp_port_t) (uintptr_t) port;
671 set_mac_entry_ofp_port(struct mac_learning *ml,
672 struct mac_entry *e, ofp_port_t ofp_port)
673 OVS_REQ_WRLOCK(ml->rwlock)
675 mac_entry_set_port(ml, e, (void *) (OVS_FORCE uintptr_t) ofp_port);