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