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