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
*);
66 static void send_default_flows(struct lswitch
*sw
, struct rconn
*rconn
,
69 typedef void packet_handler_func(struct lswitch
*, struct rconn
*, void *);
70 static packet_handler_func process_switch_features
;
71 static packet_handler_func process_packet_in
;
72 static packet_handler_func process_echo_request
;
74 /* Creates and returns a new learning switch.
76 * If 'learn_macs' is true, the new switch will learn the ports on which MAC
77 * addresses appear. Otherwise, the new switch will flood all packets.
79 * If 'max_idle' is nonnegative, the new switch will set up flows that expire
80 * after the given number of seconds (or never expire, if 'max_idle' is
81 * OFP_FLOW_PERMANENT). Otherwise, the new switch will process every packet.
83 * The caller may provide the file stream 'default_flows' that defines
84 * default flows that should be pushed when a switch connects. Each
85 * line is a flow entry in the format described for "add-flows" command
86 * in the Flow Syntax section of the ovs-ofct(8) man page. The caller
87 * is responsible for closing the stream.
89 * 'rconn' is used to send out an OpenFlow features request. */
91 lswitch_create(struct rconn
*rconn
, bool learn_macs
,
92 bool exact_flows
, int max_idle
, bool action_normal
,
97 sw
= xzalloc(sizeof *sw
);
98 sw
->max_idle
= max_idle
;
100 sw
->last_features_request
= time_now() - 1;
101 sw
->ml
= learn_macs
? mac_learning_create() : NULL
;
102 sw
->action_normal
= action_normal
;
107 /* We cannot wildcard all fields.
108 * We need in_port to detect moves.
109 * We need both SA and DA to do learning. */
110 sw
->wildcards
= (OFPFW_DL_TYPE
| OFPFW_NW_SRC_MASK
| OFPFW_NW_DST_MASK
111 | OFPFW_NW_PROTO
| OFPFW_TP_SRC
| OFPFW_TP_DST
);
113 sw
->queue
= UINT32_MAX
;
114 sw
->queued
= rconn_packet_counter_create();
115 send_features_request(sw
, rconn
);
117 send_default_flows(sw
, rconn
, default_flows
);
124 lswitch_destroy(struct lswitch
*sw
)
127 mac_learning_destroy(sw
->ml
);
128 rconn_packet_counter_destroy(sw
->queued
);
133 /* Sets 'queue' as the OpenFlow queue used by packets and flows set up by 'sw'.
134 * Specify UINT32_MAX to avoid specifying a particular queue, which is also the
135 * default if this function is never called for 'sw'. */
137 lswitch_set_queue(struct lswitch
*sw
, uint32_t queue
)
142 /* Takes care of necessary 'sw' activity, except for receiving packets (which
143 * the caller must do). */
145 lswitch_run(struct lswitch
*sw
)
148 mac_learning_run(sw
->ml
, NULL
);
153 lswitch_wait(struct lswitch
*sw
)
156 mac_learning_wait(sw
->ml
);
160 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
161 * to the learning switch state in 'sw'. The most likely result of processing
162 * is that flow-setup and packet-out OpenFlow messages will be sent out on
165 lswitch_process_packet(struct lswitch
*sw
, struct rconn
*rconn
,
166 const struct ofpbuf
*msg
)
171 packet_handler_func
*handler
;
173 static const struct processor processors
[] = {
176 sizeof(struct ofp_header
),
181 sizeof(struct ofp_switch_features
),
182 process_switch_features
186 offsetof(struct ofp_packet_in
, data
),
191 sizeof(struct ofp_flow_removed
),
195 const size_t n_processors
= ARRAY_SIZE(processors
);
196 const struct processor
*p
;
197 struct ofp_header
*oh
;
200 if (sw
->datapath_id
== 0
201 && oh
->type
!= OFPT_ECHO_REQUEST
202 && oh
->type
!= OFPT_FEATURES_REPLY
) {
203 send_features_request(sw
, rconn
);
207 for (p
= processors
; p
< &processors
[n_processors
]; p
++) {
208 if (oh
->type
== p
->type
) {
209 if (msg
->size
< p
->min_size
) {
210 VLOG_WARN_RL(&rl
, "%016llx: %s: too short (%zu bytes) for "
211 "type %"PRIu8
" (min %zu)", sw
->datapath_id
,
212 rconn_get_name(rconn
), msg
->size
, oh
->type
,
217 (p
->handler
)(sw
, rconn
, msg
->data
);
222 if (VLOG_IS_DBG_ENABLED()) {
223 char *s
= ofp_to_string(msg
->data
, msg
->size
, 2);
224 VLOG_DBG_RL(&rl
, "%016llx: OpenFlow packet ignored: %s",
231 send_features_request(struct lswitch
*sw
, struct rconn
*rconn
)
233 time_t now
= time_now();
234 if (now
>= sw
->last_features_request
+ 1) {
236 struct ofp_switch_config
*osc
;
238 /* Send OFPT_FEATURES_REQUEST. */
239 make_openflow(sizeof(struct ofp_header
), OFPT_FEATURES_REQUEST
, &b
);
240 queue_tx(sw
, rconn
, b
);
242 /* Send OFPT_SET_CONFIG. */
243 osc
= make_openflow(sizeof *osc
, OFPT_SET_CONFIG
, &b
);
244 osc
->miss_send_len
= htons(OFP_DEFAULT_MISS_SEND_LEN
);
245 queue_tx(sw
, rconn
, b
);
247 sw
->last_features_request
= now
;
252 send_default_flows(struct lswitch
*sw
, struct rconn
*rconn
,
257 while (fgets(line
, sizeof line
, default_flows
)) {
259 struct ofp_flow_mod
*ofm
;
260 uint16_t priority
, idle_timeout
, hard_timeout
;
262 struct ofp_match match
;
266 /* Delete comments. */
267 comment
= strchr(line
, '#');
272 /* Drop empty lines. */
273 if (line
[strspn(line
, " \t\n")] == '\0') {
277 /* Parse and send. str_to_flow() will expand and reallocate the data
278 * in 'buffer', so we can't keep pointers to across the str_to_flow()
280 make_openflow(sizeof *ofm
, OFPT_FLOW_MOD
, &b
);
281 parse_ofp_str(line
, &match
, b
,
282 NULL
, NULL
, &priority
, &idle_timeout
, &hard_timeout
,
286 ofm
->command
= htons(OFPFC_ADD
);
287 ofm
->cookie
= htonll(cookie
);
288 ofm
->idle_timeout
= htons(idle_timeout
);
289 ofm
->hard_timeout
= htons(hard_timeout
);
290 ofm
->buffer_id
= htonl(UINT32_MAX
);
291 ofm
->priority
= htons(priority
);
293 update_openflow_length(b
);
294 queue_tx(sw
, rconn
, b
);
299 queue_tx(struct lswitch
*sw
, struct rconn
*rconn
, struct ofpbuf
*b
)
301 int retval
= rconn_send_with_limit(rconn
, b
, sw
->queued
, 10);
302 if (retval
&& retval
!= ENOTCONN
) {
303 if (retval
== EAGAIN
) {
304 VLOG_INFO_RL(&rl
, "%016llx: %s: tx queue overflow",
305 sw
->datapath_id
, rconn_get_name(rconn
));
307 VLOG_WARN_RL(&rl
, "%016llx: %s: send: %s",
308 sw
->datapath_id
, rconn_get_name(rconn
),
315 process_switch_features(struct lswitch
*sw
, struct rconn
*rconn OVS_UNUSED
,
318 struct ofp_switch_features
*osf
= osf_
;
320 sw
->datapath_id
= ntohll(osf
->datapath_id
);
324 lswitch_choose_destination(struct lswitch
*sw
, const flow_t
*flow
)
328 /* Learn the source MAC. */
330 if (mac_learning_learn(sw
->ml
, flow
->dl_src
, 0, flow
->in_port
,
331 GRAT_ARP_LOCK_NONE
)) {
332 VLOG_DBG_RL(&rl
, "%016llx: learned that "ETH_ADDR_FMT
" is on "
333 "port %"PRIu16
, sw
->datapath_id
,
334 ETH_ADDR_ARGS(flow
->dl_src
), flow
->in_port
);
338 /* Drop frames for reserved multicast addresses. */
339 if (eth_addr_is_reserved(flow
->dl_dst
)) {
343 out_port
= OFPP_FLOOD
;
345 int learned_port
= mac_learning_lookup(sw
->ml
, flow
->dl_dst
, 0, NULL
);
346 if (learned_port
>= 0) {
347 out_port
= learned_port
;
348 if (out_port
== flow
->in_port
) {
349 /* Don't send a packet back out its input port. */
355 /* Check if we need to use "NORMAL" action. */
356 if (sw
->action_normal
&& out_port
!= OFPP_FLOOD
) {
364 process_packet_in(struct lswitch
*sw
, struct rconn
*rconn
, void *opi_
)
366 struct ofp_packet_in
*opi
= opi_
;
367 uint16_t in_port
= ntohs(opi
->in_port
);
370 struct ofp_action_header actions
[2];
373 size_t pkt_ofs
, pkt_len
;
377 /* Ignore packets sent via output to OFPP_CONTROLLER. This library never
378 * uses such an action. You never know what experiments might be going on,
379 * though, and it seems best not to interfere with them. */
380 if (opi
->reason
!= OFPR_NO_MATCH
) {
384 /* Extract flow data from 'opi' into 'flow'. */
385 pkt_ofs
= offsetof(struct ofp_packet_in
, data
);
386 pkt_len
= ntohs(opi
->header
.length
) - pkt_ofs
;
387 pkt
.data
= opi
->data
;
389 flow_extract(&pkt
, 0, in_port
, &flow
);
391 /* Choose output port. */
392 out_port
= lswitch_choose_destination(sw
, &flow
);
395 if (out_port
== OFPP_NONE
) {
397 } else if (sw
->queue
== UINT32_MAX
|| out_port
>= OFPP_MAX
) {
398 struct ofp_action_output oao
;
400 memset(&oao
, 0, sizeof oao
);
401 oao
.type
= htons(OFPAT_OUTPUT
);
402 oao
.len
= htons(sizeof oao
);
403 oao
.port
= htons(out_port
);
405 memcpy(actions
, &oao
, sizeof oao
);
406 actions_len
= sizeof oao
;
408 struct ofp_action_enqueue oae
;
410 memset(&oae
, 0, sizeof oae
);
411 oae
.type
= htons(OFPAT_ENQUEUE
);
412 oae
.len
= htons(sizeof oae
);
413 oae
.port
= htons(out_port
);
414 oae
.queue_id
= htonl(sw
->queue
);
416 memcpy(actions
, &oae
, sizeof oae
);
417 actions_len
= sizeof oae
;
419 assert(actions_len
<= sizeof actions
);
421 /* Send the packet, and possibly the whole flow, to the output port. */
422 if (sw
->max_idle
>= 0 && (!sw
->ml
|| out_port
!= OFPP_FLOOD
)) {
423 struct ofpbuf
*buffer
;
424 struct ofp_flow_mod
*ofm
;
426 /* The output port is known, or we always flood everything, so add a
428 buffer
= make_add_flow(&flow
, ntohl(opi
->buffer_id
),
429 sw
->max_idle
, actions_len
);
430 ofpbuf_put(buffer
, actions
, actions_len
);
432 ofm
->match
.wildcards
= htonl(sw
->wildcards
);
433 queue_tx(sw
, rconn
, buffer
);
435 /* If the switch didn't buffer the packet, we need to send a copy. */
436 if (ntohl(opi
->buffer_id
) == UINT32_MAX
&& actions_len
> 0) {
438 make_packet_out(&pkt
, UINT32_MAX
, in_port
,
439 actions
, actions_len
/ sizeof *actions
));
442 /* We don't know that MAC, or we don't set up flows. Send along the
443 * packet without setting up a flow. */
444 if (ntohl(opi
->buffer_id
) != UINT32_MAX
|| actions_len
> 0) {
446 make_packet_out(&pkt
, ntohl(opi
->buffer_id
), in_port
,
447 actions
, actions_len
/ sizeof *actions
));
453 process_echo_request(struct lswitch
*sw
, struct rconn
*rconn
, void *rq_
)
455 struct ofp_header
*rq
= rq_
;
456 queue_tx(sw
, rconn
, make_echo_reply(rq
));