2b7495cad4ce0190495d95f39c6b6a529c5abc63
[cascardo/ovs.git] / lib / learning-switch.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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 "byte-order.h"
27 #include "classifier.h"
28 #include "flow.h"
29 #include "hmap.h"
30 #include "mac-learning.h"
31 #include "ofpbuf.h"
32 #include "ofp-actions.h"
33 #include "ofp-errors.h"
34 #include "ofp-msgs.h"
35 #include "ofp-parse.h"
36 #include "ofp-print.h"
37 #include "ofp-util.h"
38 #include "openflow/openflow.h"
39 #include "poll-loop.h"
40 #include "rconn.h"
41 #include "shash.h"
42 #include "simap.h"
43 #include "timeval.h"
44 #include "vconn.h"
45 #include "vlog.h"
46
47 VLOG_DEFINE_THIS_MODULE(learning_switch);
48
49 struct lswitch_port {
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. */
53 };
54
55 enum lswitch_state {
56     S_CONNECTING,               /* Waiting for connection to complete. */
57     S_FEATURES_REPLY,           /* Waiting for features reply. */
58     S_SWITCHING,                /* Switching flows. */
59 };
60
61 struct lswitch {
62     struct rconn *rconn;
63     enum lswitch_state state;
64
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. */
68     int max_idle;
69
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? */
75
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. */
80
81     /* Number of outgoing queued packets on the rconn. */
82     struct rconn_packet_counter *queued;
83
84     /* If true, do not reply to any messages from the switch (for debugging
85      * fail-open mode). */
86     bool mute;
87
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;
93 };
94
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);
98
99 static void queue_tx(struct lswitch *, struct ofpbuf *);
100 static void send_features_request(struct lswitch *);
101
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 *);
107
108 /* Creates and returns a new learning switch whose configuration is given by
109  * 'cfg'.
110  *
111  * 'rconn' is used to send out an OpenFlow features request. */
112 struct lswitch *
113 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
114 {
115     struct lswitch *sw;
116     uint32_t ofpfw;
117
118     sw = xzalloc(sizeof *sw);
119     sw->rconn = rconn;
120     sw->state = S_CONNECTING;
121     sw->max_idle = cfg->max_idle;
122     sw->datapath_id = 0;
123     sw->ml = (cfg->mode == LSW_LEARN
124               ? mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME)
125               : NULL);
126     sw->action_normal = cfg->mode == LSW_NORMAL;
127
128     switch (cfg->wildcards) {
129     case 0:
130         ofpfw = 0;
131         break;
132
133     case UINT32_MAX:
134         /* Try to wildcard as many fields as possible, but we cannot
135          * wildcard all fields.  We need in_port to detect moves.  We need
136          * Ethernet source and dest and VLAN VID to do L2 learning. */
137         ofpfw = (OFPFW10_DL_TYPE | OFPFW10_DL_VLAN_PCP
138                  | OFPFW10_NW_SRC_ALL | OFPFW10_NW_DST_ALL
139                  | OFPFW10_NW_TOS | OFPFW10_NW_PROTO
140                  | OFPFW10_TP_SRC | OFPFW10_TP_DST);
141         break;
142
143     default:
144         ofpfw = cfg->wildcards;
145         break;
146     }
147     ofputil_wildcard_from_ofpfw10(ofpfw, &sw->wc);
148
149     sw->default_queue = cfg->default_queue;
150     hmap_init(&sw->queue_numbers);
151     shash_init(&sw->queue_names);
152     if (cfg->port_queues) {
153         struct simap_node *node;
154
155         SIMAP_FOR_EACH (node, cfg->port_queues) {
156             struct lswitch_port *port = xmalloc(sizeof *port);
157             hmap_node_nullify(&port->hmap_node);
158             port->queue_id = node->data;
159             shash_add(&sw->queue_names, node->name, port);
160         }
161     }
162
163     sw->default_flows = cfg->default_flows;
164     sw->n_default_flows = cfg->n_default_flows;
165     sw->usable_protocols = cfg->usable_protocols;
166
167     sw->queued = rconn_packet_counter_create();
168
169     return sw;
170 }
171
172 static void
173 lswitch_handshake(struct lswitch *sw)
174 {
175     enum ofputil_protocol protocol;
176     enum ofp_version version;
177
178     send_features_request(sw);
179
180     version = rconn_get_version(sw->rconn);
181     protocol = ofputil_protocol_from_ofp_version(version);
182     if (version >= OFP13_VERSION) {
183         /* OpenFlow 1.3 and later by default drop packets that miss in the flow
184          * table.  Set up a flow to send packets to the controller by
185          * default. */
186         struct ofputil_flow_mod fm;
187         struct ofpact_output output;
188         struct ofpbuf *msg;
189         int error;
190
191         ofpact_init_OUTPUT(&output);
192         output.port = OFPP_CONTROLLER;
193         output.max_len = OFP_DEFAULT_MISS_SEND_LEN;
194
195         match_init_catchall(&fm.match);
196         fm.priority = 0;
197         fm.cookie = 0;
198         fm.cookie_mask = 0;
199         fm.new_cookie = 0;
200         fm.modify_cookie = false;
201         fm.table_id = 0;
202         fm.command = OFPFC_ADD;
203         fm.idle_timeout = 0;
204         fm.hard_timeout = 0;
205         fm.buffer_id = UINT32_MAX;
206         fm.out_port = OFPP_NONE;
207         fm.out_group = OFPG_ANY;
208         fm.flags = 0;
209         fm.ofpacts = &output.ofpact;
210         fm.ofpacts_len = sizeof output;
211         fm.delete_reason = 0;
212
213         msg = ofputil_encode_flow_mod(&fm, protocol);
214         error = rconn_send(sw->rconn, msg, NULL);
215         if (error) {
216             VLOG_INFO_RL(&rl, "%s: failed to add default flow (%s)",
217                          rconn_get_name(sw->rconn), ovs_strerror(error));
218         }
219     }
220     if (sw->default_flows) {
221         struct ofpbuf *msg = NULL;
222         int error = 0;
223         size_t i;
224
225         /* If the initial protocol isn't good enough for default_flows, then
226          * pick one that will work and encode messages to set up that
227          * protocol.
228          *
229          * This could be improved by actually negotiating a mutually acceptable
230          * flow format with the switch, but that would require an asynchronous
231          * state machine.  This version ought to work fine in practice. */
232         if (!(protocol & sw->usable_protocols)) {
233             enum ofputil_protocol want = rightmost_1bit(sw->usable_protocols);
234             while (!error) {
235                 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
236                 if (!msg) {
237                     break;
238                 }
239                 error = rconn_send(sw->rconn, msg, NULL);
240             }
241         }
242         if (protocol & sw->usable_protocols) {
243             for (i = 0; !error && i < sw->n_default_flows; i++) {
244                 msg = ofputil_encode_flow_mod(&sw->default_flows[i], protocol);
245                 error = rconn_send(sw->rconn, msg, NULL);
246             }
247
248             if (error) {
249                 VLOG_INFO_RL(&rl, "%s: failed to queue default flows (%s)",
250                              rconn_get_name(sw->rconn), ovs_strerror(error));
251             }
252         } else {
253             VLOG_INFO_RL(&rl, "%s: failed to set usable protocol",
254                          rconn_get_name(sw->rconn));
255         }
256     }
257     sw->protocol = protocol;
258 }
259
260 bool
261 lswitch_is_alive(const struct lswitch *sw)
262 {
263     return rconn_is_alive(sw->rconn);
264 }
265
266 /* Destroys 'sw'. */
267 void
268 lswitch_destroy(struct lswitch *sw)
269 {
270     if (sw) {
271         struct lswitch_port *node, *next;
272
273         rconn_destroy(sw->rconn);
274         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
275             hmap_remove(&sw->queue_numbers, &node->hmap_node);
276             free(node);
277         }
278         shash_destroy(&sw->queue_names);
279         mac_learning_unref(sw->ml);
280         rconn_packet_counter_destroy(sw->queued);
281         free(sw);
282     }
283 }
284
285 /* Takes care of necessary 'sw' activity, except for receiving packets (which
286  * the caller must do). */
287 void
288 lswitch_run(struct lswitch *sw)
289 {
290     int i;
291
292     if (sw->ml) {
293         ovs_rwlock_wrlock(&sw->ml->rwlock);
294         mac_learning_run(sw->ml);
295         ovs_rwlock_unlock(&sw->ml->rwlock);
296     }
297
298     rconn_run(sw->rconn);
299
300     if (sw->state == S_CONNECTING) {
301         if (rconn_get_version(sw->rconn) != -1) {
302             lswitch_handshake(sw);
303             sw->state = S_FEATURES_REPLY;
304         }
305         return;
306     }
307
308     for (i = 0; i < 50; i++) {
309         struct ofpbuf *msg;
310
311         msg = rconn_recv(sw->rconn);
312         if (!msg) {
313             break;
314         }
315
316         if (!sw->mute) {
317             lswitch_process_packet(sw, msg);
318         }
319         ofpbuf_delete(msg);
320     }
321 }
322
323 void
324 lswitch_wait(struct lswitch *sw)
325 {
326     if (sw->ml) {
327         ovs_rwlock_rdlock(&sw->ml->rwlock);
328         mac_learning_wait(sw->ml);
329         ovs_rwlock_unlock(&sw->ml->rwlock);
330     }
331     rconn_run_wait(sw->rconn);
332     rconn_recv_wait(sw->rconn);
333 }
334
335 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
336  * to the learning switch state in 'sw'.  The most likely result of processing
337  * is that flow-setup and packet-out OpenFlow messages will be sent out on
338  * 'rconn'.  */
339 static void
340 lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
341 {
342     enum ofptype type;
343     struct ofpbuf b;
344
345     b = *msg;
346     if (ofptype_pull(&type, &b)) {
347         return;
348     }
349
350     if (sw->state == S_FEATURES_REPLY
351         && type != OFPTYPE_ECHO_REQUEST
352         && type != OFPTYPE_FEATURES_REPLY) {
353         return;
354     }
355
356     switch (type) {
357     case OFPTYPE_ECHO_REQUEST:
358         process_echo_request(sw, ofpbuf_data(msg));
359         break;
360
361     case OFPTYPE_FEATURES_REPLY:
362         if (sw->state == S_FEATURES_REPLY) {
363             if (!process_switch_features(sw, ofpbuf_data(msg))) {
364                 sw->state = S_SWITCHING;
365             } else {
366                 rconn_disconnect(sw->rconn);
367             }
368         }
369         break;
370
371     case OFPTYPE_PACKET_IN:
372         process_packet_in(sw, ofpbuf_data(msg));
373         break;
374
375     case OFPTYPE_FLOW_REMOVED:
376         /* Nothing to do. */
377         break;
378
379     case OFPTYPE_HELLO:
380     case OFPTYPE_ERROR:
381     case OFPTYPE_ECHO_REPLY:
382     case OFPTYPE_FEATURES_REQUEST:
383     case OFPTYPE_GET_CONFIG_REQUEST:
384     case OFPTYPE_GET_CONFIG_REPLY:
385     case OFPTYPE_SET_CONFIG:
386     case OFPTYPE_PORT_STATUS:
387     case OFPTYPE_PACKET_OUT:
388     case OFPTYPE_FLOW_MOD:
389     case OFPTYPE_GROUP_MOD:
390     case OFPTYPE_PORT_MOD:
391     case OFPTYPE_TABLE_MOD:
392     case OFPTYPE_BARRIER_REQUEST:
393     case OFPTYPE_BARRIER_REPLY:
394     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
395     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
396     case OFPTYPE_DESC_STATS_REQUEST:
397     case OFPTYPE_DESC_STATS_REPLY:
398     case OFPTYPE_FLOW_STATS_REQUEST:
399     case OFPTYPE_FLOW_STATS_REPLY:
400     case OFPTYPE_AGGREGATE_STATS_REQUEST:
401     case OFPTYPE_AGGREGATE_STATS_REPLY:
402     case OFPTYPE_TABLE_STATS_REQUEST:
403     case OFPTYPE_TABLE_STATS_REPLY:
404     case OFPTYPE_PORT_STATS_REQUEST:
405     case OFPTYPE_PORT_STATS_REPLY:
406     case OFPTYPE_QUEUE_STATS_REQUEST:
407     case OFPTYPE_QUEUE_STATS_REPLY:
408     case OFPTYPE_PORT_DESC_STATS_REQUEST:
409     case OFPTYPE_PORT_DESC_STATS_REPLY:
410     case OFPTYPE_ROLE_REQUEST:
411     case OFPTYPE_ROLE_REPLY:
412     case OFPTYPE_ROLE_STATUS:
413     case OFPTYPE_SET_FLOW_FORMAT:
414     case OFPTYPE_FLOW_MOD_TABLE_ID:
415     case OFPTYPE_SET_PACKET_IN_FORMAT:
416     case OFPTYPE_FLOW_AGE:
417     case OFPTYPE_SET_CONTROLLER_ID:
418     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
419     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
420     case OFPTYPE_FLOW_MONITOR_CANCEL:
421     case OFPTYPE_FLOW_MONITOR_PAUSED:
422     case OFPTYPE_FLOW_MONITOR_RESUMED:
423     case OFPTYPE_GET_ASYNC_REQUEST:
424     case OFPTYPE_GET_ASYNC_REPLY:
425     case OFPTYPE_SET_ASYNC_CONFIG:
426     case OFPTYPE_METER_MOD:
427     case OFPTYPE_GROUP_STATS_REQUEST:
428     case OFPTYPE_GROUP_STATS_REPLY:
429     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
430     case OFPTYPE_GROUP_DESC_STATS_REPLY:
431     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
432     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
433     case OFPTYPE_METER_STATS_REQUEST:
434     case OFPTYPE_METER_STATS_REPLY:
435     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
436     case OFPTYPE_METER_CONFIG_STATS_REPLY:
437     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
438     case OFPTYPE_METER_FEATURES_STATS_REPLY:
439     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
440     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
441     case OFPTYPE_BUNDLE_CONTROL:
442     case OFPTYPE_BUNDLE_ADD_MESSAGE:
443     default:
444         if (VLOG_IS_DBG_ENABLED()) {
445             char *s = ofp_to_string(ofpbuf_data(msg), ofpbuf_size(msg), 2);
446             VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
447                         sw->datapath_id, s);
448             free(s);
449         }
450     }
451 }
452 \f
453 static void
454 send_features_request(struct lswitch *sw)
455 {
456     struct ofpbuf *b;
457     struct ofp_switch_config *osc;
458     int ofp_version = rconn_get_version(sw->rconn);
459
460     ovs_assert(ofp_version > 0 && ofp_version < 0xff);
461
462     /* Send OFPT_FEATURES_REQUEST. */
463     b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
464     queue_tx(sw, b);
465
466     /* Send OFPT_SET_CONFIG. */
467     b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, ofp_version, sizeof *osc);
468     osc = ofpbuf_put_zeros(b, sizeof *osc);
469     osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
470     queue_tx(sw, b);
471 }
472
473 static void
474 queue_tx(struct lswitch *sw, struct ofpbuf *b)
475 {
476     int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
477     if (retval && retval != ENOTCONN) {
478         if (retval == EAGAIN) {
479             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
480                          sw->datapath_id, rconn_get_name(sw->rconn));
481         } else {
482             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
483                          sw->datapath_id, rconn_get_name(sw->rconn),
484                          ovs_strerror(retval));
485         }
486     }
487 }
488
489 static enum ofperr
490 process_switch_features(struct lswitch *sw, struct ofp_header *oh)
491 {
492     struct ofputil_switch_features features;
493     struct ofputil_phy_port port;
494     enum ofperr error;
495     struct ofpbuf b;
496
497     error = ofputil_decode_switch_features(oh, &features, &b);
498     if (error) {
499         VLOG_ERR("received invalid switch feature reply (%s)",
500                  ofperr_to_string(error));
501         return error;
502     }
503
504     sw->datapath_id = features.datapath_id;
505
506     while (!ofputil_pull_phy_port(oh->version, &b, &port)) {
507         struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
508         if (lp && hmap_node_is_null(&lp->hmap_node)) {
509             lp->port_no = port.port_no;
510             hmap_insert(&sw->queue_numbers, &lp->hmap_node,
511                         hash_ofp_port(lp->port_no));
512         }
513     }
514     return 0;
515 }
516
517 static ofp_port_t
518 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
519 {
520     ofp_port_t out_port;
521
522     /* Learn the source MAC. */
523     if (sw->ml) {
524         ovs_rwlock_wrlock(&sw->ml->rwlock);
525         if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
526             struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src,
527                                                         0);
528             if (mac->port.ofp_port != flow->in_port.ofp_port) {
529                 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
530                             "port %"PRIu16, sw->datapath_id,
531                             ETH_ADDR_ARGS(flow->dl_src),
532                             flow->in_port.ofp_port);
533
534                 mac->port.ofp_port = flow->in_port.ofp_port;
535                 mac_learning_changed(sw->ml);
536             }
537         }
538         ovs_rwlock_unlock(&sw->ml->rwlock);
539     }
540
541     /* Drop frames for reserved multicast addresses. */
542     if (eth_addr_is_reserved(flow->dl_dst)) {
543         return OFPP_NONE;
544     }
545
546     out_port = OFPP_FLOOD;
547     if (sw->ml) {
548         struct mac_entry *mac;
549
550         ovs_rwlock_rdlock(&sw->ml->rwlock);
551         mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0);
552         if (mac) {
553             out_port = mac->port.ofp_port;
554             if (out_port == flow->in_port.ofp_port) {
555                 /* Don't send a packet back out its input port. */
556                 ovs_rwlock_unlock(&sw->ml->rwlock);
557                 return OFPP_NONE;
558             }
559         }
560         ovs_rwlock_unlock(&sw->ml->rwlock);
561     }
562
563     /* Check if we need to use "NORMAL" action. */
564     if (sw->action_normal && out_port != OFPP_FLOOD) {
565         return OFPP_NORMAL;
566     }
567
568     return out_port;
569 }
570
571 static uint32_t
572 get_queue_id(const struct lswitch *sw, ofp_port_t in_port)
573 {
574     const struct lswitch_port *port;
575
576     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_ofp_port(in_port),
577                              &sw->queue_numbers) {
578         if (port->port_no == in_port) {
579             return port->queue_id;
580         }
581     }
582
583     return sw->default_queue;
584 }
585
586 static void
587 process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
588 {
589     struct ofputil_packet_in pi;
590     uint32_t queue_id;
591     ofp_port_t out_port;
592
593     uint64_t ofpacts_stub[64 / 8];
594     struct ofpbuf ofpacts;
595
596     struct ofputil_packet_out po;
597     enum ofperr error;
598
599     struct ofpbuf pkt;
600     struct flow flow;
601
602     error = ofputil_decode_packet_in(&pi, oh);
603     if (error) {
604         VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
605                      ofperr_to_string(error));
606         return;
607     }
608
609     /* Ignore packets sent via output to OFPP_CONTROLLER.  This library never
610      * uses such an action.  You never know what experiments might be going on,
611      * though, and it seems best not to interfere with them. */
612     if (pi.reason != OFPR_NO_MATCH) {
613         return;
614     }
615
616     /* Extract flow data from 'opi' into 'flow'. */
617     ofpbuf_use_const(&pkt, pi.packet, pi.packet_len);
618     flow_extract(&pkt, NULL, &flow);
619     flow.in_port.ofp_port = pi.fmd.in_port;
620     flow.tunnel.tun_id = pi.fmd.tun_id;
621
622     /* Choose output port. */
623     out_port = lswitch_choose_destination(sw, &flow);
624
625     /* Make actions. */
626     queue_id = get_queue_id(sw, pi.fmd.in_port);
627     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
628     if (out_port == OFPP_NONE) {
629         /* No actions. */
630     } else if (queue_id == UINT32_MAX
631                || ofp_to_u16(out_port) >= ofp_to_u16(OFPP_MAX)) {
632         ofpact_put_OUTPUT(&ofpacts)->port = out_port;
633     } else {
634         struct ofpact_enqueue *enqueue = ofpact_put_ENQUEUE(&ofpacts);
635         enqueue->port = out_port;
636         enqueue->queue = queue_id;
637     }
638     ofpact_pad(&ofpacts);
639
640     /* Prepare packet_out in case we need one. */
641     po.buffer_id = pi.buffer_id;
642     if (po.buffer_id == UINT32_MAX) {
643         po.packet = ofpbuf_data(&pkt);
644         po.packet_len = ofpbuf_size(&pkt);
645     } else {
646         po.packet = NULL;
647         po.packet_len = 0;
648     }
649     po.in_port = pi.fmd.in_port;
650     po.ofpacts = ofpbuf_data(&ofpacts);
651     po.ofpacts_len = ofpbuf_size(&ofpacts);
652
653     /* Send the packet, and possibly the whole flow, to the output port. */
654     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
655         struct ofputil_flow_mod fm;
656         struct ofpbuf *buffer;
657
658         /* The output port is known, or we always flood everything, so add a
659          * new flow. */
660         memset(&fm, 0, sizeof fm);
661         match_init(&fm.match, &flow, &sw->wc);
662         ofputil_normalize_match_quiet(&fm.match);
663         fm.priority = 1; /* Must be > 0 because of table-miss flow entry. */
664         fm.table_id = 0xff;
665         fm.command = OFPFC_ADD;
666         fm.idle_timeout = sw->max_idle;
667         fm.buffer_id = pi.buffer_id;
668         fm.out_port = OFPP_NONE;
669         fm.ofpacts = ofpbuf_data(&ofpacts);
670         fm.ofpacts_len = ofpbuf_size(&ofpacts);
671         buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
672
673         queue_tx(sw, buffer);
674
675         /* If the switch didn't buffer the packet, we need to send a copy. */
676         if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
677             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
678         }
679     } else {
680         /* We don't know that MAC, or we don't set up flows.  Send along the
681          * packet without setting up a flow. */
682         if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
683             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
684         }
685     }
686 }
687
688 static void
689 process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
690 {
691     queue_tx(sw, make_echo_reply(rq));
692 }