]> git.proxmox.com Git - mirror_ovs.git/blame - lib/learning-switch.c
netdev-offload-tc: Use single 'once' variable for probing tc features
[mirror_ovs.git] / lib / learning-switch.c
CommitLineData
064af421 1/*
50f96b10 2 * Copyright (c) 2008-2017 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "learning-switch.h"
19
20#include <errno.h>
21#include <inttypes.h>
b2befd5b 22#include <sys/types.h>
064af421
BP
23#include <netinet/in.h>
24#include <stdlib.h>
25#include <time.h>
26
10a24935 27#include "byte-order.h"
daa68e9f 28#include "classifier.h"
cf62fa4c 29#include "dp-packet.h"
064af421 30#include "flow.h"
ee89ea7b 31#include "openvswitch/hmap.h"
064af421 32#include "mac-learning.h"
064af421 33#include "openflow/openflow.h"
b598f214 34#include "openvswitch/ofp-actions.h"
0d71302e 35#include "openvswitch/ofp-connection.h"
d271907f 36#include "openvswitch/ofp-errors.h"
0d71302e
BP
37#include "openvswitch/ofp-flow.h"
38#include "openvswitch/ofp-match.h"
d271907f 39#include "openvswitch/ofp-msgs.h"
25d436fb 40#include "openvswitch/ofp-print.h"
d271907f 41#include "openvswitch/ofp-util.h"
0d71302e
BP
42#include "openvswitch/ofp-packet.h"
43#include "openvswitch/ofp-port.h"
44#include "openvswitch/ofp-switch.h"
d271907f
BW
45#include "openvswitch/ofpbuf.h"
46#include "openvswitch/vconn.h"
47#include "openvswitch/vlog.h"
fd016ae3 48#include "openvswitch/poll-loop.h"
dc02e1eb 49#include "openvswitch/rconn.h"
ee89ea7b 50#include "openvswitch/shash.h"
44bac24b 51#include "simap.h"
064af421 52#include "timeval.h"
064af421 53
d98e6007 54VLOG_DEFINE_THIS_MODULE(learning_switch);
064af421 55
d4cdc6b4
BP
56struct lswitch_port {
57 struct hmap_node hmap_node; /* Hash node for port number. */
4e022ec0 58 ofp_port_t port_no; /* OpenFlow port number. */
d4cdc6b4
BP
59 uint32_t queue_id; /* OpenFlow queue number. */
60};
61
f702893a
BP
62enum lswitch_state {
63 S_CONNECTING, /* Waiting for connection to complete. */
64 S_FEATURES_REPLY, /* Waiting for features reply. */
65 S_SWITCHING, /* Switching flows. */
66};
67
064af421 68struct lswitch {
002c3f17 69 struct rconn *rconn;
f702893a 70 enum lswitch_state state;
002c3f17 71
064af421
BP
72 /* If nonnegative, the switch sets up flows that expire after the given
73 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
74 * Otherwise, the switch processes every packet. */
75 int max_idle;
76
f25d0cf3 77 enum ofputil_protocol protocol;
064af421 78 unsigned long long int datapath_id;
064af421 79 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
daa68e9f 80 struct flow_wildcards wc; /* Wildcards to apply to flows. */
9af9e2e8 81 bool action_normal; /* Use OFPP_NORMAL? */
d4cdc6b4
BP
82
83 /* Queue distribution. */
84 uint32_t default_queue; /* Default OpenFlow queue, or UINT32_MAX. */
85 struct hmap queue_numbers; /* Map from port number to lswitch_port. */
86 struct shash queue_names; /* Map from port name to lswitch_port. */
064af421
BP
87
88 /* Number of outgoing queued packets on the rconn. */
89 struct rconn_packet_counter *queued;
002c3f17
BP
90
91 /* If true, do not reply to any messages from the switch (for debugging
92 * fail-open mode). */
93 bool mute;
f702893a
BP
94
95 /* Optional "flow mod" requests to send to the switch at connection time,
96 * to set up the flow table. */
97 const struct ofputil_flow_mod *default_flows;
98 size_t n_default_flows;
db0b6c29 99 enum ofputil_protocol usable_protocols;
064af421
BP
100};
101
102/* The log messages here could actually be useful in debugging, so keep the
103 * rate limit relatively high. */
104static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
105
002c3f17
BP
106static void queue_tx(struct lswitch *, struct ofpbuf *);
107static void send_features_request(struct lswitch *);
064af421 108
002c3f17 109static void lswitch_process_packet(struct lswitch *, const struct ofpbuf *);
9e1fd49b 110static enum ofperr process_switch_features(struct lswitch *,
982697a4 111 struct ofp_header *);
002c3f17
BP
112static void process_packet_in(struct lswitch *, const struct ofp_header *);
113static void process_echo_request(struct lswitch *, const struct ofp_header *);
064af421 114
9d078ec2
BP
115static ofp_port_t get_mac_entry_ofp_port(const struct mac_learning *ml,
116 const struct mac_entry *)
117 OVS_REQ_RDLOCK(ml->rwlock);
118static void set_mac_entry_ofp_port(struct mac_learning *ml,
119 struct mac_entry *, ofp_port_t)
120 OVS_REQ_WRLOCK(ml->rwlock);
121
ad67e568
BP
122/* Creates and returns a new learning switch whose configuration is given by
123 * 'cfg'.
aaaa7553 124 *
064af421
BP
125 * 'rconn' is used to send out an OpenFlow features request. */
126struct lswitch *
ad67e568 127lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
064af421
BP
128{
129 struct lswitch *sw;
3cbd9931 130 uint32_t ofpfw;
064af421 131
ec6fde61 132 sw = xzalloc(sizeof *sw);
002c3f17 133 sw->rconn = rconn;
f702893a 134 sw->state = S_CONNECTING;
ad67e568 135 sw->max_idle = cfg->max_idle;
064af421 136 sw->datapath_id = 0;
e764773c
BP
137 sw->ml = (cfg->mode == LSW_LEARN
138 ? mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME)
139 : NULL);
ad67e568 140 sw->action_normal = cfg->mode == LSW_NORMAL;
daa68e9f 141
3cbd9931
BP
142 switch (cfg->wildcards) {
143 case 0:
144 ofpfw = 0;
145 break;
146
147 case UINT32_MAX:
148 /* Try to wildcard as many fields as possible, but we cannot
149 * wildcard all fields. We need in_port to detect moves. We need
150 * Ethernet source and dest and VLAN VID to do L2 learning. */
151 ofpfw = (OFPFW10_DL_TYPE | OFPFW10_DL_VLAN_PCP
152 | OFPFW10_NW_SRC_ALL | OFPFW10_NW_DST_ALL
153 | OFPFW10_NW_TOS | OFPFW10_NW_PROTO
154 | OFPFW10_TP_SRC | OFPFW10_TP_DST);
155 break;
156
157 default:
158 ofpfw = cfg->wildcards;
159 break;
52f4c6fa 160 }
3cbd9931 161 ofputil_wildcard_from_ofpfw10(ofpfw, &sw->wc);
d4cdc6b4
BP
162
163 sw->default_queue = cfg->default_queue;
164 hmap_init(&sw->queue_numbers);
165 shash_init(&sw->queue_names);
166 if (cfg->port_queues) {
44bac24b 167 struct simap_node *node;
d4cdc6b4 168
44bac24b 169 SIMAP_FOR_EACH (node, cfg->port_queues) {
d4cdc6b4
BP
170 struct lswitch_port *port = xmalloc(sizeof *port);
171 hmap_node_nullify(&port->hmap_node);
44bac24b 172 port->queue_id = node->data;
d4cdc6b4
BP
173 shash_add(&sw->queue_names, node->name, port);
174 }
175 }
176
f702893a
BP
177 sw->default_flows = cfg->default_flows;
178 sw->n_default_flows = cfg->n_default_flows;
db0b6c29 179 sw->usable_protocols = cfg->usable_protocols;
f702893a 180
064af421 181 sw->queued = rconn_packet_counter_create();
f702893a
BP
182
183 return sw;
184}
185
186static void
187lswitch_handshake(struct lswitch *sw)
188{
189 enum ofputil_protocol protocol;
76fb6ae4 190 enum ofp_version version;
f702893a 191
002c3f17 192 send_features_request(sw);
09913dfd 193
76fb6ae4
BP
194 version = rconn_get_version(sw->rconn);
195 protocol = ofputil_protocol_from_ofp_version(version);
196 if (version >= OFP13_VERSION) {
197 /* OpenFlow 1.3 and later by default drop packets that miss in the flow
198 * table. Set up a flow to send packets to the controller by
199 * default. */
76fb6ae4
BP
200 struct ofpact_output output;
201 struct ofpbuf *msg;
202 int error;
203
204 ofpact_init_OUTPUT(&output);
205 output.port = OFPP_CONTROLLER;
206 output.max_len = OFP_DEFAULT_MISS_SEND_LEN;
207
39cc5c4a 208 struct ofputil_flow_mod fm = {
39cc5c4a
BP
209 .priority = 0,
210 .table_id = 0,
211 .command = OFPFC_ADD,
212 .buffer_id = UINT32_MAX,
213 .out_port = OFPP_NONE,
214 .out_group = OFPG_ANY,
215 .ofpacts = &output.ofpact,
216 .ofpacts_len = sizeof output,
217 };
6a6b7060 218 minimatch_init_catchall(&fm.match);
76fb6ae4 219 msg = ofputil_encode_flow_mod(&fm, protocol);
6a6b7060
BP
220 minimatch_destroy(&fm.match);
221
76fb6ae4
BP
222 error = rconn_send(sw->rconn, msg, NULL);
223 if (error) {
224 VLOG_INFO_RL(&rl, "%s: failed to add default flow (%s)",
225 rconn_get_name(sw->rconn), ovs_strerror(error));
226 }
227 }
f702893a 228 if (sw->default_flows) {
27527aa0 229 struct ofpbuf *msg = NULL;
27527aa0
BP
230 int error = 0;
231 size_t i;
232
27527aa0
BP
233 /* If the initial protocol isn't good enough for default_flows, then
234 * pick one that will work and encode messages to set up that
235 * protocol.
236 *
237 * This could be improved by actually negotiating a mutually acceptable
238 * flow format with the switch, but that would require an asynchronous
239 * state machine. This version ought to work fine in practice. */
db0b6c29
JR
240 if (!(protocol & sw->usable_protocols)) {
241 enum ofputil_protocol want = rightmost_1bit(sw->usable_protocols);
27527aa0
BP
242 while (!error) {
243 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
244 if (!msg) {
245 break;
246 }
f702893a 247 error = rconn_send(sw->rconn, msg, NULL);
b85145b5 248 }
b3907fbc 249 }
db0b6c29 250 if (protocol & sw->usable_protocols) {
e43928f2
BP
251 for (i = 0; !error && i < sw->n_default_flows; i++) {
252 msg = ofputil_encode_flow_mod(&sw->default_flows[i], protocol);
253 error = rconn_send(sw->rconn, msg, NULL);
254 }
27527aa0 255
e43928f2
BP
256 if (error) {
257 VLOG_INFO_RL(&rl, "%s: failed to queue default flows (%s)",
10a89ef0 258 rconn_get_name(sw->rconn), ovs_strerror(error));
e43928f2
BP
259 }
260 } else {
261 VLOG_INFO_RL(&rl, "%s: failed to set usable protocol",
262 rconn_get_name(sw->rconn));
27527aa0 263 }
b3907fbc 264 }
f25d0cf3 265 sw->protocol = protocol;
064af421
BP
266}
267
002c3f17
BP
268bool
269lswitch_is_alive(const struct lswitch *sw)
270{
271 return rconn_is_alive(sw->rconn);
272}
273
064af421
BP
274/* Destroys 'sw'. */
275void
276lswitch_destroy(struct lswitch *sw)
277{
278 if (sw) {
4ec3d7c7 279 struct lswitch_port *node;
d4cdc6b4 280
002c3f17 281 rconn_destroy(sw->rconn);
4ec3d7c7 282 HMAP_FOR_EACH_POP (node, hmap_node, &sw->queue_numbers) {
d4cdc6b4
BP
283 free(node);
284 }
285 shash_destroy(&sw->queue_names);
5d989517 286 mac_learning_unref(sw->ml);
064af421
BP
287 rconn_packet_counter_destroy(sw->queued);
288 free(sw);
289 }
290}
291
292/* Takes care of necessary 'sw' activity, except for receiving packets (which
293 * the caller must do). */
294void
ba186119 295lswitch_run(struct lswitch *sw)
064af421 296{
002c3f17
BP
297 int i;
298
064af421 299 if (sw->ml) {
509c0149 300 ovs_rwlock_wrlock(&sw->ml->rwlock);
30618594 301 mac_learning_run(sw->ml);
509c0149 302 ovs_rwlock_unlock(&sw->ml->rwlock);
064af421 303 }
002c3f17
BP
304
305 rconn_run(sw->rconn);
306
f702893a 307 if (sw->state == S_CONNECTING) {
476d2551 308 if (rconn_is_connected(sw->rconn)) {
f702893a
BP
309 lswitch_handshake(sw);
310 sw->state = S_FEATURES_REPLY;
311 }
312 return;
313 }
314
002c3f17
BP
315 for (i = 0; i < 50; i++) {
316 struct ofpbuf *msg;
317
318 msg = rconn_recv(sw->rconn);
319 if (!msg) {
320 break;
321 }
322
323 if (!sw->mute) {
324 lswitch_process_packet(sw, msg);
325 }
326 ofpbuf_delete(msg);
327 }
064af421
BP
328}
329
330void
331lswitch_wait(struct lswitch *sw)
332{
333 if (sw->ml) {
509c0149 334 ovs_rwlock_rdlock(&sw->ml->rwlock);
064af421 335 mac_learning_wait(sw->ml);
509c0149 336 ovs_rwlock_unlock(&sw->ml->rwlock);
064af421 337 }
f702893a 338 rconn_run_wait(sw->rconn);
002c3f17 339 rconn_recv_wait(sw->rconn);
064af421
BP
340}
341
342/* Processes 'msg', which should be an OpenFlow received on 'rconn', according
343 * to the learning switch state in 'sw'. The most likely result of processing
344 * is that flow-setup and packet-out OpenFlow messages will be sent out on
345 * 'rconn'. */
002c3f17
BP
346static void
347lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
064af421 348{
982697a4
BP
349 enum ofptype type;
350 struct ofpbuf b;
351
352 b = *msg;
353 if (ofptype_pull(&type, &b)) {
354 return;
355 }
d1e2cf21 356
f702893a 357 if (sw->state == S_FEATURES_REPLY
982697a4
BP
358 && type != OFPTYPE_ECHO_REQUEST
359 && type != OFPTYPE_FEATURES_REPLY) {
064af421
BP
360 return;
361 }
362
33fa5fe5 363 if (type == OFPTYPE_ECHO_REQUEST) {
6fd6ed71 364 process_echo_request(sw, msg->data);
33fa5fe5 365 } else if (type == OFPTYPE_FEATURES_REPLY) {
f702893a 366 if (sw->state == S_FEATURES_REPLY) {
6fd6ed71 367 if (!process_switch_features(sw, msg->data)) {
f702893a
BP
368 sw->state = S_SWITCHING;
369 } else {
370 rconn_disconnect(sw->rconn);
371 }
372 }
33fa5fe5 373 } else if (type == OFPTYPE_PACKET_IN) {
6fd6ed71 374 process_packet_in(sw, msg->data);
33fa5fe5 375 } else if (type == OFPTYPE_FLOW_REMOVED) {
d1e2cf21 376 /* Nothing to do. */
33fa5fe5 377 } else if (VLOG_IS_DBG_ENABLED()) {
4bc938cc 378 char *s = ofp_to_string(msg->data, msg->size, NULL, NULL, 2);
33fa5fe5
BP
379 VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
380 sw->datapath_id, s);
381 free(s);
064af421 382 }
064af421
BP
383}
384\f
385static void
002c3f17 386send_features_request(struct lswitch *sw)
064af421 387{
f702893a 388 struct ofpbuf *b;
f702893a 389 int ofp_version = rconn_get_version(sw->rconn);
064af421 390
cb22974d 391 ovs_assert(ofp_version > 0 && ofp_version < 0xff);
064af421 392
f702893a
BP
393 /* Send OFPT_FEATURES_REQUEST. */
394 b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
395 queue_tx(sw, b);
064af421 396
f702893a 397 /* Send OFPT_SET_CONFIG. */
ad99e2ed
BP
398 struct ofputil_switch_config config = {
399 .miss_send_len = OFP_DEFAULT_MISS_SEND_LEN
400 };
401 queue_tx(sw, ofputil_encode_set_config(&config, ofp_version));
064af421
BP
402}
403
404static void
002c3f17 405queue_tx(struct lswitch *sw, struct ofpbuf *b)
064af421 406{
002c3f17 407 int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
064af421
BP
408 if (retval && retval != ENOTCONN) {
409 if (retval == EAGAIN) {
b123cc3c 410 VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
002c3f17 411 sw->datapath_id, rconn_get_name(sw->rconn));
064af421 412 } else {
b123cc3c 413 VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
002c3f17 414 sw->datapath_id, rconn_get_name(sw->rconn),
10a89ef0 415 ovs_strerror(retval));
064af421
BP
416 }
417 }
418}
419
9e1fd49b 420static enum ofperr
982697a4 421process_switch_features(struct lswitch *sw, struct ofp_header *oh)
064af421 422{
9e1fd49b
BP
423 struct ofputil_switch_features features;
424 struct ofputil_phy_port port;
9e1fd49b 425
0a2869d5
BP
426 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
427 enum ofperr error = ofputil_pull_switch_features(&b, &features);
9e1fd49b
BP
428 if (error) {
429 VLOG_ERR("received invalid switch feature reply (%s)",
430 ofperr_to_string(error));
431 return error;
432 }
d4cdc6b4 433
9e1fd49b 434 sw->datapath_id = features.datapath_id;
d4cdc6b4 435
982697a4 436 while (!ofputil_pull_phy_port(oh->version, &b, &port)) {
9e1fd49b 437 struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
d4cdc6b4 438 if (lp && hmap_node_is_null(&lp->hmap_node)) {
9e1fd49b 439 lp->port_no = port.port_no;
d4cdc6b4 440 hmap_insert(&sw->queue_numbers, &lp->hmap_node,
f9c0c3ec 441 hash_ofp_port(lp->port_no));
d4cdc6b4
BP
442 }
443 }
9e1fd49b 444 return 0;
064af421
BP
445}
446
4e022ec0 447static ofp_port_t
ae412e7d 448lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
064af421 449{
4e022ec0 450 ofp_port_t out_port;
064af421 451
81f3cad4 452 /* Learn the source MAC. */
cbd577d6
Z
453 if (sw->ml) {
454 ovs_rwlock_wrlock(&sw->ml->rwlock);
455 if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
456 struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src,
457 0);
9d078ec2
BP
458 if (get_mac_entry_ofp_port(sw->ml, mac)
459 != flow->in_port.ofp_port) {
cbd577d6 460 VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
94783c7c 461 "port %"PRIu32, sw->datapath_id,
cbd577d6
Z
462 ETH_ADDR_ARGS(flow->dl_src),
463 flow->in_port.ofp_port);
464
9d078ec2 465 set_mac_entry_ofp_port(sw->ml, mac, flow->in_port.ofp_port);
cbd577d6 466 }
064af421 467 }
cbd577d6 468 ovs_rwlock_unlock(&sw->ml->rwlock);
064af421
BP
469 }
470
5a003f60 471 /* Drop frames for reserved multicast addresses. */
81f3cad4
BP
472 if (eth_addr_is_reserved(flow->dl_dst)) {
473 return OFPP_NONE;
064af421
BP
474 }
475
81f3cad4 476 out_port = OFPP_FLOOD;
064af421 477 if (sw->ml) {
db8077c3
BP
478 struct mac_entry *mac;
479
509c0149 480 ovs_rwlock_rdlock(&sw->ml->rwlock);
30618594 481 mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0);
db8077c3 482 if (mac) {
9d078ec2 483 out_port = get_mac_entry_ofp_port(sw->ml, mac);
4e022ec0 484 if (out_port == flow->in_port.ofp_port) {
81f3cad4 485 /* Don't send a packet back out its input port. */
509c0149 486 ovs_rwlock_unlock(&sw->ml->rwlock);
81f3cad4
BP
487 return OFPP_NONE;
488 }
064af421 489 }
509c0149 490 ovs_rwlock_unlock(&sw->ml->rwlock);
064af421
BP
491 }
492
81f3cad4
BP
493 /* Check if we need to use "NORMAL" action. */
494 if (sw->action_normal && out_port != OFPP_FLOOD) {
495 return OFPP_NORMAL;
496 }
497
498 return out_port;
499}
500
d4cdc6b4 501static uint32_t
4e022ec0 502get_queue_id(const struct lswitch *sw, ofp_port_t in_port)
d4cdc6b4
BP
503{
504 const struct lswitch_port *port;
505
f9c0c3ec 506 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_ofp_port(in_port),
d4cdc6b4
BP
507 &sw->queue_numbers) {
508 if (port->port_no == in_port) {
509 return port->queue_id;
510 }
511 }
512
513 return sw->default_queue;
514}
515
81f3cad4 516static void
002c3f17 517process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
81f3cad4 518{
f25d0cf3 519 struct ofputil_packet_in pi;
9bfe9334 520 uint32_t buffer_id;
d4cdc6b4 521 uint32_t queue_id;
4e022ec0 522 ofp_port_t out_port;
81f3cad4 523
f25d0cf3
BP
524 uint64_t ofpacts_stub[64 / 8];
525 struct ofpbuf ofpacts;
c71270b7 526
c6a93eb7 527 struct ofputil_packet_out po;
f25d0cf3 528 enum ofperr error;
c6a93eb7 529
cf62fa4c 530 struct dp_packet pkt;
ae412e7d 531 struct flow flow;
81f3cad4 532
3cddeff0 533 error = ofputil_decode_packet_in(oh, true, NULL, NULL, &pi, NULL,
8d8ab6c2 534 &buffer_id, NULL);
f25d0cf3
BP
535 if (error) {
536 VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
537 ofperr_to_string(error));
538 return;
539 }
540
6699af68
BP
541 /* Ignore packets sent via output to OFPP_CONTROLLER. This library never
542 * uses such an action. You never know what experiments might be going on,
543 * though, and it seems best not to interfere with them. */
f25d0cf3 544 if (pi.reason != OFPR_NO_MATCH) {
6699af68
BP
545 return;
546 }
547
9bfe9334 548 /* Extract flow data from 'pi' into 'flow'. */
4adaf182 549 dp_packet_use_const(&pkt, pi.packet, pi.packet_len);
cf62fa4c 550 flow_extract(&pkt, &flow);
50dcbd8e
JG
551 flow.in_port.ofp_port = pi.flow_metadata.flow.in_port.ofp_port;
552 flow.tunnel.tun_id = pi.flow_metadata.flow.tunnel.tun_id;
81f3cad4
BP
553
554 /* Choose output port. */
555 out_port = lswitch_choose_destination(sw, &flow);
556
c71270b7 557 /* Make actions. */
50dcbd8e 558 queue_id = get_queue_id(sw, pi.flow_metadata.flow.in_port.ofp_port);
f25d0cf3 559 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
c71270b7 560 if (out_port == OFPP_NONE) {
f25d0cf3 561 /* No actions. */
4e022ec0
AW
562 } else if (queue_id == UINT32_MAX
563 || ofp_to_u16(out_port) >= ofp_to_u16(OFPP_MAX)) {
f25d0cf3 564 ofpact_put_OUTPUT(&ofpacts)->port = out_port;
c71270b7 565 } else {
f25d0cf3
BP
566 struct ofpact_enqueue *enqueue = ofpact_put_ENQUEUE(&ofpacts);
567 enqueue->port = out_port;
568 enqueue->queue = queue_id;
c71270b7 569 }
c71270b7 570
c6a93eb7 571 /* Prepare packet_out in case we need one. */
9bfe9334
BP
572 po.buffer_id = buffer_id;
573 if (buffer_id == UINT32_MAX) {
cf62fa4c
PS
574 po.packet = dp_packet_data(&pkt);
575 po.packet_len = dp_packet_size(&pkt);
c6a93eb7
BP
576 } else {
577 po.packet = NULL;
578 po.packet_len = 0;
579 }
35eb6326
YHW
580 match_set_in_port(&po.flow_metadata,
581 pi.flow_metadata.flow.in_port.ofp_port);
6fd6ed71
PS
582 po.ofpacts = ofpacts.data;
583 po.ofpacts_len = ofpacts.size;
c6a93eb7 584
81f3cad4
BP
585 /* Send the packet, and possibly the whole flow, to the output port. */
586 if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
064af421
BP
587 /* The output port is known, or we always flood everything, so add a
588 * new flow. */
39cc5c4a
BP
589 struct ofputil_flow_mod fm = {
590 .priority = 1, /* Must be > 0 because of table-miss flow entry. */
591 .table_id = 0xff,
592 .command = OFPFC_ADD,
593 .idle_timeout = sw->max_idle,
9bfe9334 594 .buffer_id = buffer_id,
39cc5c4a
BP
595 .out_port = OFPP_NONE,
596 .ofpacts = ofpacts.data,
597 .ofpacts_len = ofpacts.size,
598 };
6a6b7060
BP
599
600 struct match match;
601 match_init(&match, &flow, &sw->wc);
602 ofputil_normalize_match_quiet(&match);
603 minimatch_init(&fm.match, &match);
39cc5c4a
BP
604
605 struct ofpbuf *buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
f25d0cf3 606
6a6b7060
BP
607 minimatch_destroy(&fm.match);
608
002c3f17 609 queue_tx(sw, buffer);
064af421
BP
610
611 /* If the switch didn't buffer the packet, we need to send a copy. */
9bfe9334 612 if (buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
de0f3156 613 queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
064af421
BP
614 }
615 } else {
616 /* We don't know that MAC, or we don't set up flows. Send along the
617 * packet without setting up a flow. */
9bfe9334 618 if (buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
de0f3156 619 queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
064af421 620 }
064af421 621 }
064af421
BP
622}
623
624static void
002c3f17 625process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
064af421 626{
d9cea8f5 627 queue_tx(sw, ofputil_encode_echo_reply(rq));
064af421 628}
9d078ec2
BP
629
630static ofp_port_t
631get_mac_entry_ofp_port(const struct mac_learning *ml,
632 const struct mac_entry *e)
633 OVS_REQ_RDLOCK(ml->rwlock)
634{
635 void *port = mac_entry_get_port(ml, e);
636 return (OVS_FORCE ofp_port_t) (uintptr_t) port;
637}
638
639static void
640set_mac_entry_ofp_port(struct mac_learning *ml,
641 struct mac_entry *e, ofp_port_t ofp_port)
642 OVS_REQ_WRLOCK(ml->rwlock)
643{
644 mac_entry_set_port(ml, e, (void *) (OVS_FORCE uintptr_t) ofp_port);
645}