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