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