]> git.proxmox.com Git - ovs.git/blob - lib/learning-switch.c
learning-switch: Reserved addresses are destinations, not sources.
[ovs.git] / lib / learning-switch.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 "flow.h"
27 #include "mac-learning.h"
28 #include "ofpbuf.h"
29 #include "ofp-print.h"
30 #include "ofp-util.h"
31 #include "openflow/openflow.h"
32 #include "poll-loop.h"
33 #include "queue.h"
34 #include "rconn.h"
35 #include "stp.h"
36 #include "timeval.h"
37 #include "vconn.h"
38 #include "xtoxll.h"
39
40 #define THIS_MODULE VLM_learning_switch
41 #include "vlog.h"
42
43 enum port_state {
44 P_DISABLED = 1 << 0,
45 P_LISTENING = 1 << 1,
46 P_LEARNING = 1 << 2,
47 P_FORWARDING = 1 << 3,
48 P_BLOCKING = 1 << 4
49 };
50
51 struct lswitch {
52 /* If nonnegative, the switch sets up flows that expire after the given
53 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
54 * Otherwise, the switch processes every packet. */
55 int max_idle;
56
57 unsigned long long int datapath_id;
58 uint32_t capabilities;
59 time_t last_features_request;
60 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
61 bool exact_flows; /* Use exact-match flows? */
62 bool action_normal; /* Use OFPP_NORMAL? */
63
64 /* Number of outgoing queued packets on the rconn. */
65 struct rconn_packet_counter *queued;
66
67 /* Spanning tree protocol implementation.
68 *
69 * We implement STP states by, whenever a port's STP state changes,
70 * querying all the flows on the switch and then deleting any of them that
71 * are inappropriate for a port's STP state. */
72 long long int next_query; /* Next time at which to query all flows. */
73 long long int last_query; /* Last time we sent a query. */
74 long long int last_reply; /* Last time we received a query reply. */
75 unsigned int port_states[STP_MAX_PORTS];
76 uint32_t query_xid; /* XID used for query. */
77 int n_flows, n_no_recv, n_no_send;
78 };
79
80 /* The log messages here could actually be useful in debugging, so keep the
81 * rate limit relatively high. */
82 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
83
84 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
85 static void send_features_request(struct lswitch *, struct rconn *);
86 static void schedule_query(struct lswitch *, long long int delay);
87 static bool may_learn(const struct lswitch *, uint16_t port_no);
88 static bool may_recv(const struct lswitch *, uint16_t port_no,
89 bool any_actions);
90 static bool may_send(const struct lswitch *, uint16_t port_no);
91
92 typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
93 static packet_handler_func process_switch_features;
94 static packet_handler_func process_packet_in;
95 static packet_handler_func process_echo_request;
96 static packet_handler_func process_port_status;
97 static packet_handler_func process_phy_port;
98 static packet_handler_func process_stats_reply;
99
100 /* Creates and returns a new learning switch.
101 *
102 * If 'learn_macs' is true, the new switch will learn the ports on which MAC
103 * addresses appear. Otherwise, the new switch will flood all packets.
104 *
105 * If 'max_idle' is nonnegative, the new switch will set up flows that expire
106 * after the given number of seconds (or never expire, if 'max_idle' is
107 * OFP_FLOW_PERMANENT). Otherwise, the new switch will process every packet.
108 *
109 * 'rconn' is used to send out an OpenFlow features request. */
110 struct lswitch *
111 lswitch_create(struct rconn *rconn, bool learn_macs,
112 bool exact_flows, int max_idle, bool action_normal)
113 {
114 struct lswitch *sw;
115 size_t i;
116
117 sw = xzalloc(sizeof *sw);
118 sw->max_idle = max_idle;
119 sw->datapath_id = 0;
120 sw->last_features_request = time_now() - 1;
121 sw->ml = learn_macs ? mac_learning_create() : NULL;
122 sw->action_normal = action_normal;
123 sw->exact_flows = exact_flows;
124 sw->queued = rconn_packet_counter_create();
125 sw->next_query = LLONG_MIN;
126 sw->last_query = LLONG_MIN;
127 sw->last_reply = LLONG_MIN;
128 for (i = 0; i < STP_MAX_PORTS; i++) {
129 sw->port_states[i] = P_DISABLED;
130 }
131 send_features_request(sw, rconn);
132 return sw;
133 }
134
135 /* Destroys 'sw'. */
136 void
137 lswitch_destroy(struct lswitch *sw)
138 {
139 if (sw) {
140 mac_learning_destroy(sw->ml);
141 rconn_packet_counter_destroy(sw->queued);
142 free(sw);
143 }
144 }
145
146 /* Takes care of necessary 'sw' activity, except for receiving packets (which
147 * the caller must do). */
148 void
149 lswitch_run(struct lswitch *sw, struct rconn *rconn)
150 {
151 long long int now = time_msec();
152
153 if (sw->ml) {
154 mac_learning_run(sw->ml, NULL);
155 }
156
157 /* If we're waiting for more replies, keeping waiting for up to 10 s. */
158 if (sw->last_reply != LLONG_MIN) {
159 if (now - sw->last_reply > 10000) {
160 VLOG_ERR_RL(&rl, "%016llx: No more flow stat replies last 10 s",
161 sw->datapath_id);
162 sw->last_reply = LLONG_MIN;
163 sw->last_query = LLONG_MIN;
164 schedule_query(sw, 0);
165 } else {
166 return;
167 }
168 }
169
170 /* If we're waiting for any reply at all, keep waiting for up to 10 s. */
171 if (sw->last_query != LLONG_MIN) {
172 if (now - sw->last_query > 10000) {
173 VLOG_ERR_RL(&rl, "%016llx: No flow stat replies in last 10 s",
174 sw->datapath_id);
175 sw->last_query = LLONG_MIN;
176 schedule_query(sw, 0);
177 } else {
178 return;
179 }
180 }
181
182 /* If it's time to send another query, do so. */
183 if (sw->next_query != LLONG_MIN && now >= sw->next_query) {
184 sw->next_query = LLONG_MIN;
185 if (!rconn_is_connected(rconn)) {
186 schedule_query(sw, 1000);
187 } else {
188 struct ofp_stats_request *osr;
189 struct ofp_flow_stats_request *ofsr;
190 struct ofpbuf *b;
191 int error;
192
193 VLOG_DBG("%016llx: Sending flow stats request to implement STP",
194 sw->datapath_id);
195
196 sw->last_query = now;
197 sw->query_xid = random_uint32();
198 sw->n_flows = 0;
199 sw->n_no_recv = 0;
200 sw->n_no_send = 0;
201 osr = make_openflow_xid(sizeof *osr + sizeof *ofsr,
202 OFPT_STATS_REQUEST, sw->query_xid, &b);
203 osr->type = htons(OFPST_FLOW);
204 osr->flags = htons(0);
205 ofsr = (struct ofp_flow_stats_request *) osr->body;
206 ofsr->match.wildcards = htonl(OFPFW_ALL);
207 ofsr->table_id = 0xff;
208 ofsr->out_port = htons(OFPP_NONE);
209
210 error = rconn_send(rconn, b, NULL);
211 if (error) {
212 VLOG_WARN_RL(&rl, "%016llx: sending flow stats request "
213 "failed: %s", sw->datapath_id, strerror(error));
214 ofpbuf_delete(b);
215 schedule_query(sw, 1000);
216 }
217 }
218 }
219 }
220
221 static void
222 wait_timeout(long long int started)
223 {
224 poll_timer_wait_until(started + 10000);
225 }
226
227 void
228 lswitch_wait(struct lswitch *sw)
229 {
230 if (sw->ml) {
231 mac_learning_wait(sw->ml);
232 }
233
234 if (sw->last_reply != LLONG_MIN) {
235 wait_timeout(sw->last_reply);
236 } else if (sw->last_query != LLONG_MIN) {
237 wait_timeout(sw->last_query);
238 }
239 }
240
241 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
242 * to the learning switch state in 'sw'. The most likely result of processing
243 * is that flow-setup and packet-out OpenFlow messages will be sent out on
244 * 'rconn'. */
245 void
246 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
247 const struct ofpbuf *msg)
248 {
249 struct processor {
250 uint8_t type;
251 size_t min_size;
252 packet_handler_func *handler;
253 };
254 static const struct processor processors[] = {
255 {
256 OFPT_ECHO_REQUEST,
257 sizeof(struct ofp_header),
258 process_echo_request
259 },
260 {
261 OFPT_FEATURES_REPLY,
262 sizeof(struct ofp_switch_features),
263 process_switch_features
264 },
265 {
266 OFPT_PACKET_IN,
267 offsetof(struct ofp_packet_in, data),
268 process_packet_in
269 },
270 {
271 OFPT_PORT_STATUS,
272 sizeof(struct ofp_port_status),
273 process_port_status
274 },
275 {
276 OFPT_STATS_REPLY,
277 offsetof(struct ofp_stats_reply, body),
278 process_stats_reply
279 },
280 {
281 OFPT_FLOW_REMOVED,
282 sizeof(struct ofp_flow_removed),
283 NULL
284 },
285 };
286 const size_t n_processors = ARRAY_SIZE(processors);
287 const struct processor *p;
288 struct ofp_header *oh;
289
290 oh = msg->data;
291 if (sw->datapath_id == 0
292 && oh->type != OFPT_ECHO_REQUEST
293 && oh->type != OFPT_FEATURES_REPLY) {
294 send_features_request(sw, rconn);
295 return;
296 }
297
298 for (p = processors; p < &processors[n_processors]; p++) {
299 if (oh->type == p->type) {
300 if (msg->size < p->min_size) {
301 VLOG_WARN_RL(&rl, "%016llx: %s: too short (%zu bytes) for "
302 "type %"PRIu8" (min %zu)", sw->datapath_id,
303 rconn_get_name(rconn), msg->size, oh->type,
304 p->min_size);
305 return;
306 }
307 if (p->handler) {
308 (p->handler)(sw, rconn, msg->data);
309 }
310 return;
311 }
312 }
313 if (VLOG_IS_DBG_ENABLED()) {
314 char *p = ofp_to_string(msg->data, msg->size, 2);
315 VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
316 sw->datapath_id, p);
317 free(p);
318 }
319 }
320 \f
321 static void
322 send_features_request(struct lswitch *sw, struct rconn *rconn)
323 {
324 time_t now = time_now();
325 if (now >= sw->last_features_request + 1) {
326 struct ofpbuf *b;
327 struct ofp_switch_config *osc;
328
329 /* Send OFPT_FEATURES_REQUEST. */
330 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
331 queue_tx(sw, rconn, b);
332
333 /* Send OFPT_SET_CONFIG. */
334 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
335 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
336 queue_tx(sw, rconn, b);
337
338 sw->last_features_request = now;
339 }
340 }
341
342 static void
343 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
344 {
345 int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
346 if (retval && retval != ENOTCONN) {
347 if (retval == EAGAIN) {
348 VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
349 sw->datapath_id, rconn_get_name(rconn));
350 } else {
351 VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
352 sw->datapath_id, rconn_get_name(rconn),
353 strerror(retval));
354 }
355 }
356 }
357
358 static void
359 schedule_query(struct lswitch *sw, long long int delay)
360 {
361 long long int now = time_msec();
362 if (sw->next_query == LLONG_MIN || sw->next_query > now + delay) {
363 sw->next_query = now + delay;
364 }
365 }
366
367 static void
368 process_switch_features(struct lswitch *sw, struct rconn *rconn, void *osf_)
369 {
370 struct ofp_switch_features *osf = osf_;
371 size_t n_ports = ((ntohs(osf->header.length)
372 - offsetof(struct ofp_switch_features, ports))
373 / sizeof *osf->ports);
374 size_t i;
375
376 sw->datapath_id = ntohll(osf->datapath_id);
377 sw->capabilities = ntohl(osf->capabilities);
378 for (i = 0; i < n_ports; i++) {
379 process_phy_port(sw, rconn, &osf->ports[i]);
380 }
381 if (sw->capabilities & OFPC_STP) {
382 schedule_query(sw, 1000);
383 }
384 }
385
386 static void
387 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
388 {
389 struct ofp_packet_in *opi = opi_;
390 uint16_t in_port = ntohs(opi->in_port);
391 uint16_t out_port = OFPP_FLOOD;
392
393 size_t pkt_ofs, pkt_len;
394 struct ofpbuf pkt;
395 flow_t flow;
396
397 /* Extract flow data from 'opi' into 'flow'. */
398 pkt_ofs = offsetof(struct ofp_packet_in, data);
399 pkt_len = ntohs(opi->header.length) - pkt_ofs;
400 pkt.data = opi->data;
401 pkt.size = pkt_len;
402 flow_extract(&pkt, 0, in_port, &flow);
403
404 if (may_learn(sw, in_port) && sw->ml) {
405 if (mac_learning_learn(sw->ml, flow.dl_src, 0, in_port,
406 GRAT_ARP_LOCK_NONE)) {
407 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
408 "port %"PRIu16, sw->datapath_id,
409 ETH_ADDR_ARGS(flow.dl_src), in_port);
410 }
411 }
412
413 /* Drop frames for reserved multicast addresses. */
414 if (eth_addr_is_reserved(flow.dl_dst)) {
415 goto drop_it;
416 }
417
418 if (!may_recv(sw, in_port, false)) {
419 /* STP prevents receiving anything on this port. */
420 goto drop_it;
421 }
422
423 if (sw->ml) {
424 int learned_port = mac_learning_lookup(sw->ml, flow.dl_dst, 0, NULL);
425 if (learned_port >= 0 && may_send(sw, learned_port)) {
426 out_port = learned_port;
427 }
428 }
429
430 if (in_port == out_port) {
431 /* Don't send out packets on their input ports. */
432 goto drop_it;
433 } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
434 struct ofpbuf *buffer;
435 struct ofp_flow_mod *ofm;
436 uint32_t wildcards;
437
438 /* Check if we need to wildcard the flows. */
439 if (!sw->exact_flows) {
440 /* We can not wildcard all fields.
441 * We need in_port to detect moves.
442 * We need both SA and DA to do learning. */
443 wildcards = (OFPFW_DL_TYPE | OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK
444 | OFPFW_NW_PROTO | OFPFW_TP_SRC | OFPFW_TP_DST);
445 } else {
446 /* Exact match */
447 wildcards = 0;
448 }
449
450 /* Check if we need to use "NORMAL" action. */
451 if (sw->action_normal && out_port != OFPP_FLOOD) {
452 out_port = OFPP_NORMAL;
453 }
454
455 /* The output port is known, or we always flood everything, so add a
456 * new flow. */
457 buffer = make_add_simple_flow(&flow, ntohl(opi->buffer_id),
458 out_port, sw->max_idle);
459 ofm = buffer->data;
460 ofm->match.wildcards = htonl(wildcards);
461 queue_tx(sw, rconn, buffer);
462
463 /* If the switch didn't buffer the packet, we need to send a copy. */
464 if (ntohl(opi->buffer_id) == UINT32_MAX) {
465 queue_tx(sw, rconn,
466 make_unbuffered_packet_out(&pkt, in_port, out_port));
467 }
468 } else {
469 struct ofpbuf *b;
470
471 /* Check if we need to use "NORMAL" action. */
472 if (sw->action_normal && out_port != OFPP_FLOOD) {
473 out_port = OFPP_NORMAL;
474 }
475
476 /* We don't know that MAC, or we don't set up flows. Send along the
477 * packet without setting up a flow. */
478 if (ntohl(opi->buffer_id) == UINT32_MAX) {
479 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
480 } else {
481 b = make_buffered_packet_out(ntohl(opi->buffer_id),
482 in_port, out_port);
483 }
484 queue_tx(sw, rconn, b);
485 }
486 return;
487
488 drop_it:
489 if (sw->max_idle >= 0) {
490 /* Set up a flow to drop packets. */
491 queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
492 sw->max_idle, 0));
493 } else {
494 /* Just drop the packet, since we don't set up flows at all.
495 * XXX we should send a packet_out with no actions if buffer_id !=
496 * UINT32_MAX, to avoid clogging the kernel buffers. */
497 }
498 return;
499 }
500
501 static void
502 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
503 {
504 struct ofp_header *rq = rq_;
505 queue_tx(sw, rconn, make_echo_reply(rq));
506 }
507
508 static void
509 process_port_status(struct lswitch *sw, struct rconn *rconn, void *ops_)
510 {
511 struct ofp_port_status *ops = ops_;
512 process_phy_port(sw, rconn, &ops->desc);
513 }
514
515 static void
516 process_phy_port(struct lswitch *sw, struct rconn *rconn OVS_UNUSED,
517 void *opp_)
518 {
519 const struct ofp_phy_port *opp = opp_;
520 uint16_t port_no = ntohs(opp->port_no);
521 if (sw->capabilities & OFPC_STP && port_no < STP_MAX_PORTS) {
522 uint32_t config = ntohl(opp->config);
523 uint32_t state = ntohl(opp->state);
524 unsigned int *port_state = &sw->port_states[port_no];
525 unsigned int new_port_state;
526
527 if (!(config & (OFPPC_NO_STP | OFPPC_PORT_DOWN))
528 && !(state & OFPPS_LINK_DOWN))
529 {
530 switch (state & OFPPS_STP_MASK) {
531 case OFPPS_STP_LISTEN:
532 new_port_state = P_LISTENING;
533 break;
534 case OFPPS_STP_LEARN:
535 new_port_state = P_LEARNING;
536 break;
537 case OFPPS_STP_FORWARD:
538 new_port_state = P_FORWARDING;
539 break;
540 case OFPPS_STP_BLOCK:
541 new_port_state = P_BLOCKING;
542 break;
543 default:
544 new_port_state = P_DISABLED;
545 break;
546 }
547 } else {
548 new_port_state = P_FORWARDING;
549 }
550 if (*port_state != new_port_state) {
551 *port_state = new_port_state;
552 schedule_query(sw, 1000);
553 }
554 }
555 }
556
557 static unsigned int
558 get_port_state(const struct lswitch *sw, uint16_t port_no)
559 {
560 return (port_no >= STP_MAX_PORTS || !(sw->capabilities & OFPC_STP)
561 ? P_FORWARDING
562 : sw->port_states[port_no]);
563 }
564
565 static bool
566 may_learn(const struct lswitch *sw, uint16_t port_no)
567 {
568 return get_port_state(sw, port_no) & (P_LEARNING | P_FORWARDING);
569 }
570
571 static bool
572 may_recv(const struct lswitch *sw, uint16_t port_no, bool any_actions)
573 {
574 unsigned int state = get_port_state(sw, port_no);
575 return !(any_actions
576 ? state & (P_DISABLED | P_LISTENING | P_BLOCKING)
577 : state & (P_DISABLED | P_LISTENING | P_BLOCKING | P_LEARNING));
578 }
579
580 static bool
581 may_send(const struct lswitch *sw, uint16_t port_no)
582 {
583 return get_port_state(sw, port_no) & P_FORWARDING;
584 }
585
586 static void
587 process_flow_stats(struct lswitch *sw, struct rconn *rconn,
588 const struct ofp_flow_stats *ofs)
589 {
590 const char *end = (char *) ofs + ntohs(ofs->length);
591 bool delete = false;
592
593 /* Decide to delete the flow if it matches on an STP-disabled physical
594 * port. But don't delete it if the flow just drops all received packets,
595 * because that's a perfectly reasonable thing to do for disabled physical
596 * ports. */
597 if (!(ofs->match.wildcards & htonl(OFPFW_IN_PORT))) {
598 if (!may_recv(sw, ntohs(ofs->match.in_port),
599 end > (char *) ofs->actions)) {
600 delete = true;
601 sw->n_no_recv++;
602 }
603 }
604
605 /* Decide to delete the flow if it forwards to an STP-disabled physical
606 * port. */
607 if (!delete) {
608 const struct ofp_action_header *a;
609 size_t len;
610
611 for (a = ofs->actions; (char *) a < end; a += len / 8) {
612 len = ntohs(a->len);
613 if (len > end - (char *) a) {
614 VLOG_DBG_RL(&rl, "%016llx: action exceeds available space "
615 "(%zu > %td)",
616 sw->datapath_id, len, end - (char *) a);
617 break;
618 } else if (len % 8) {
619 VLOG_DBG_RL(&rl, "%016llx: action length (%zu) not multiple "
620 "of 8 bytes", sw->datapath_id, len);
621 break;
622 }
623
624 if (a->type == htons(OFPAT_OUTPUT)) {
625 struct ofp_action_output *oao = (struct ofp_action_output *) a;
626 if (!may_send(sw, ntohs(oao->port))) {
627 delete = true;
628 sw->n_no_send++;
629 break;
630 }
631 }
632 }
633 }
634
635 /* Delete the flow. */
636 if (delete) {
637 struct ofp_flow_mod *ofm;
638 struct ofpbuf *b;
639
640 ofm = make_openflow(offsetof(struct ofp_flow_mod, actions),
641 OFPT_FLOW_MOD, &b);
642 ofm->match = ofs->match;
643 ofm->command = OFPFC_DELETE_STRICT;
644 rconn_send(rconn, b, NULL);
645 }
646 }
647
648 static void
649 process_stats_reply(struct lswitch *sw, struct rconn *rconn, void *osr_)
650 {
651 struct ofp_stats_reply *osr = osr_;
652 struct flow_stats_iterator i;
653 const struct ofp_flow_stats *fs;
654
655 if (sw->last_query == LLONG_MIN
656 || osr->type != htons(OFPST_FLOW)
657 || osr->header.xid != sw->query_xid) {
658 return;
659 }
660 for (fs = flow_stats_first(&i, osr); fs; fs = flow_stats_next(&i)) {
661 sw->n_flows++;
662 process_flow_stats(sw, rconn, fs);
663 }
664 if (!(osr->flags & htons(OFPSF_REPLY_MORE))) {
665 VLOG_DBG("%016llx: Deleted %d of %d received flows to "
666 "implement STP, %d because of no-recv, %d because of "
667 "no-send", sw->datapath_id,
668 sw->n_no_recv + sw->n_no_send, sw->n_flows,
669 sw->n_no_recv, sw->n_no_send);
670 sw->last_query = LLONG_MIN;
671 sw->last_reply = LLONG_MIN;
672 } else {
673 sw->last_reply = time_msec();
674 }
675 }
676