]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-xlate.c
ofproto: Fix wildcard masking with nw_tos.
[mirror_ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
0934ebba 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
9583bc14
EJ
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
14
15#include <config.h>
16
17#include "ofproto/ofproto-dpif-xlate.h"
18
8449c4d6
EJ
19#include <errno.h>
20
db7d4e46 21#include "bfd.h"
9583bc14
EJ
22#include "bitmap.h"
23#include "bond.h"
24#include "bundle.h"
25#include "byte-order.h"
db7d4e46 26#include "cfm.h"
9583bc14
EJ
27#include "connmgr.h"
28#include "coverage.h"
29#include "dpif.h"
30#include "dynamic-string.h"
f7f1ea29 31#include "in-band.h"
db7d4e46 32#include "lacp.h"
9583bc14 33#include "learn.h"
46c88433 34#include "list.h"
9583bc14
EJ
35#include "mac-learning.h"
36#include "meta-flow.h"
37#include "multipath.h"
38#include "netdev-vport.h"
39#include "netlink.h"
40#include "nx-match.h"
41#include "odp-execute.h"
42#include "ofp-actions.h"
43#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 44#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 45#include "ofproto/ofproto-dpif-monitor.h"
9583bc14
EJ
46#include "ofproto/ofproto-dpif-sflow.h"
47#include "ofproto/ofproto-dpif.h"
6f00e29b 48#include "ofproto/ofproto-provider.h"
9583bc14
EJ
49#include "tunnel.h"
50#include "vlog.h"
51
46c88433 52COVERAGE_DEFINE(xlate_actions);
0f032e95 53COVERAGE_DEFINE(xlate_actions_oversize);
8bfd0fda 54COVERAGE_DEFINE(xlate_actions_mpls_overflow);
9583bc14
EJ
55
56VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
57
8a553e9a
EJ
58/* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60#define MAX_RESUBMIT_RECURSION 64
adcf00ba
AZ
61#define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
62 internal table. */
8a553e9a 63
98b07853
BP
64/* Maximum number of resubmit actions in a flow translation, whether they are
65 * recursive or not. */
66#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
67
dc24a00f
EJ
68struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
69
46c88433
EJ
70struct xbridge {
71 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
72 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
73
74 struct list xbundles; /* Owned xbundles. */
75 struct hmap xports; /* Indexed by ofp_port. */
76
77 char *name; /* Name used in log messages. */
89a8a7f0 78 struct dpif *dpif; /* Datapath interface. */
46c88433
EJ
79 struct mac_learning *ml; /* Mac learning handle. */
80 struct mbridge *mbridge; /* Mirroring. */
81 struct dpif_sflow *sflow; /* SFlow handle, or null. */
82 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 83 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 84 struct stp *stp; /* STP or null if disabled. */
46c88433 85
ec89fc6f
EJ
86 /* Special rules installed by ofproto-dpif. */
87 struct rule_dpif *miss_rule;
88 struct rule_dpif *no_packet_in_rule;
89
46c88433 90 enum ofp_config_flags frag; /* Fragmentation handling. */
46c88433
EJ
91 bool has_in_band; /* Bridge has in band control? */
92 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 93
adcf00ba
AZ
94 /* True if the datapath supports recirculation. */
95 bool enable_recirc;
96
4b97b70d
BP
97 /* True if the datapath supports variable-length
98 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
99 * False if the datapath supports only 8-byte (or shorter) userdata. */
100 bool variable_length_userdata;
8bfd0fda
BP
101
102 /* Number of MPLS label stack entries that the datapath supports
103 * in matches. */
104 size_t max_mpls_depth;
46c88433
EJ
105};
106
107struct xbundle {
108 struct hmap_node hmap_node; /* In global 'xbundles' map. */
109 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
110
111 struct list list_node; /* In parent 'xbridges' list. */
112 struct xbridge *xbridge; /* Parent xbridge. */
113
114 struct list xports; /* Contains "struct xport"s. */
115
116 char *name; /* Name used in log messages. */
117 struct bond *bond; /* Nonnull iff more than one port. */
118 struct lacp *lacp; /* LACP handle or null. */
119
120 enum port_vlan_mode vlan_mode; /* VLAN mode. */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
125 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
126};
127
128struct xport {
129 struct hmap_node hmap_node; /* Node in global 'xports' map. */
130 struct ofport_dpif *ofport; /* Key in global 'xports map. */
131
132 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
133 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
134
135 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
136
137 struct list bundle_node; /* In parent xbundle (if it exists). */
138 struct xbundle *xbundle; /* Parent xbundle or null. */
139
140 struct netdev *netdev; /* 'ofport''s netdev. */
141
142 struct xbridge *xbridge; /* Parent bridge. */
143 struct xport *peer; /* Patch port peer or null. */
144
145 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 146 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 147 int stp_port_no; /* STP port number or -1 if not in use. */
46c88433 148
55954f6e
EJ
149 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
150
46c88433
EJ
151 bool may_enable; /* May be enabled in bonds. */
152 bool is_tunnel; /* Is a tunnel port. */
153
154 struct cfm *cfm; /* CFM handle or null. */
155 struct bfd *bfd; /* BFD handle or null. */
156};
157
4d0acc70
EJ
158struct xlate_ctx {
159 struct xlate_in *xin;
160 struct xlate_out *xout;
161
46c88433 162 const struct xbridge *xbridge;
4d0acc70
EJ
163
164 /* Flow at the last commit. */
165 struct flow base_flow;
166
167 /* Tunnel IP destination address as received. This is stored separately
168 * as the base_flow.tunnel is cleared on init to reflect the datapath
169 * behavior. Used to make sure not to send tunneled output to ourselves,
170 * which might lead to an infinite loop. This could happen easily
171 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
172 * actually set the tun_dst field. */
173 ovs_be32 orig_tunnel_ip_dst;
174
175 /* Stack for the push and pop actions. Each stack element is of type
176 * "union mf_subvalue". */
177 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
178 struct ofpbuf stack;
179
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
182
98b07853
BP
183 /* Resubmit statistics, via xlate_table_action(). */
184 int recurse; /* Current resubmit nesting depth. */
185 int resubmits; /* Total number of resubmits. */
5a070238 186 bool in_group; /* Currently translating ofgroup, if true. */
98b07853 187
4d0acc70
EJ
188 uint32_t orig_skb_priority; /* Priority when packet arrived. */
189 uint8_t table_id; /* OpenFlow table ID where flow was found. */
190 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 191 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
4d0acc70
EJ
192 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
193 bool exit; /* No further actions should be processed. */
7fdb60a7
SH
194
195 /* OpenFlow 1.1+ action set.
196 *
197 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
198 * When translation is otherwise complete, ofpacts_execute_action_set()
199 * converts it to a set of "struct ofpact"s that can be translated into
200 * datapath actions. */
201 struct ofpbuf action_set; /* Action set. */
202 uint64_t action_set_stub[1024 / 8];
4d0acc70
EJ
203};
204
9583bc14
EJ
205/* A controller may use OFPP_NONE as the ingress port to indicate that
206 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
207 * when an input bundle is needed for validation (e.g., mirroring or
208 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
209 * any 'port' structs, so care must be taken when dealing with it. */
210static struct xbundle ofpp_none_bundle = {
211 .name = "OFPP_NONE",
212 .vlan_mode = PORT_VLAN_TRUNK
213};
9583bc14 214
55954f6e
EJ
215/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
216 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
217 * traffic egressing the 'ofport' with that priority should be marked with. */
218struct skb_priority_to_dscp {
219 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
220 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
221
222 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
223};
224
46c88433
EJ
225static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
226static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
227static struct hmap xports = HMAP_INITIALIZER(&xports);
228
229static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
230static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
231 struct xlate_ctx *);
91d6cd12
AW
232static void xlate_actions__(struct xlate_in *, struct xlate_out *)
233 OVS_REQ_RDLOCK(xlate_rwlock);
adcf00ba
AZ
234static void xlate_normal(struct xlate_ctx *);
235static void xlate_report(struct xlate_ctx *, const char *);
6d328fa2
SH
236static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
237 uint8_t table_id, bool may_packet_in,
238 bool honor_table_miss);
46c88433
EJ
239static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
240static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
241static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 242 uint16_t vlan);
4e022ec0 243static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
9583bc14 244
46c88433
EJ
245static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
246static struct xbundle *xbundle_lookup(const struct ofbundle *);
5e6af486 247static struct xport *xport_lookup(const struct ofport_dpif *);
46c88433 248static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
249static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
250 uint32_t skb_priority);
251static void clear_skb_priorities(struct xport *);
252static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
253 uint8_t *dscp);
46c88433
EJ
254
255void
256xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
ec89fc6f
EJ
257 struct dpif *dpif, struct rule_dpif *miss_rule,
258 struct rule_dpif *no_packet_in_rule,
259 const struct mac_learning *ml, struct stp *stp,
260 const struct mbridge *mbridge,
46c88433 261 const struct dpif_sflow *sflow,
ce3955be
EJ
262 const struct dpif_ipfix *ipfix,
263 const struct netflow *netflow, enum ofp_config_flags frag,
4b97b70d 264 bool forward_bpdu, bool has_in_band,
adcf00ba 265 bool enable_recirc,
8bfd0fda
BP
266 bool variable_length_userdata,
267 size_t max_mpls_depth)
46c88433
EJ
268{
269 struct xbridge *xbridge = xbridge_lookup(ofproto);
270
271 if (!xbridge) {
272 xbridge = xzalloc(sizeof *xbridge);
273 xbridge->ofproto = ofproto;
274
275 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
276 hmap_init(&xbridge->xports);
277 list_init(&xbridge->xbundles);
278 }
279
280 if (xbridge->ml != ml) {
281 mac_learning_unref(xbridge->ml);
282 xbridge->ml = mac_learning_ref(ml);
283 }
284
285 if (xbridge->mbridge != mbridge) {
286 mbridge_unref(xbridge->mbridge);
287 xbridge->mbridge = mbridge_ref(mbridge);
288 }
289
290 if (xbridge->sflow != sflow) {
291 dpif_sflow_unref(xbridge->sflow);
292 xbridge->sflow = dpif_sflow_ref(sflow);
293 }
294
295 if (xbridge->ipfix != ipfix) {
296 dpif_ipfix_unref(xbridge->ipfix);
297 xbridge->ipfix = dpif_ipfix_ref(ipfix);
298 }
299
9d189a50
EJ
300 if (xbridge->stp != stp) {
301 stp_unref(xbridge->stp);
302 xbridge->stp = stp_ref(stp);
303 }
304
ce3955be
EJ
305 if (xbridge->netflow != netflow) {
306 netflow_unref(xbridge->netflow);
307 xbridge->netflow = netflow_ref(netflow);
308 }
309
46c88433
EJ
310 free(xbridge->name);
311 xbridge->name = xstrdup(name);
312
89a8a7f0 313 xbridge->dpif = dpif;
46c88433
EJ
314 xbridge->forward_bpdu = forward_bpdu;
315 xbridge->has_in_band = has_in_band;
46c88433 316 xbridge->frag = frag;
ec89fc6f
EJ
317 xbridge->miss_rule = miss_rule;
318 xbridge->no_packet_in_rule = no_packet_in_rule;
adcf00ba 319 xbridge->enable_recirc = enable_recirc;
4b97b70d 320 xbridge->variable_length_userdata = variable_length_userdata;
8bfd0fda 321 xbridge->max_mpls_depth = max_mpls_depth;
46c88433
EJ
322}
323
324void
325xlate_remove_ofproto(struct ofproto_dpif *ofproto)
326{
327 struct xbridge *xbridge = xbridge_lookup(ofproto);
328 struct xbundle *xbundle, *next_xbundle;
329 struct xport *xport, *next_xport;
330
331 if (!xbridge) {
332 return;
333 }
334
335 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
336 xlate_ofport_remove(xport->ofport);
337 }
338
339 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
340 xlate_bundle_remove(xbundle->ofbundle);
341 }
342
343 hmap_remove(&xbridges, &xbridge->hmap_node);
795cc5c1
EJ
344 mac_learning_unref(xbridge->ml);
345 mbridge_unref(xbridge->mbridge);
346 dpif_sflow_unref(xbridge->sflow);
347 dpif_ipfix_unref(xbridge->ipfix);
348 stp_unref(xbridge->stp);
349 hmap_destroy(&xbridge->xports);
46c88433
EJ
350 free(xbridge->name);
351 free(xbridge);
352}
353
354void
355xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
356 const char *name, enum port_vlan_mode vlan_mode, int vlan,
357 unsigned long *trunks, bool use_priority_tags,
358 const struct bond *bond, const struct lacp *lacp,
359 bool floodable)
360{
361 struct xbundle *xbundle = xbundle_lookup(ofbundle);
362
363 if (!xbundle) {
364 xbundle = xzalloc(sizeof *xbundle);
365 xbundle->ofbundle = ofbundle;
366 xbundle->xbridge = xbridge_lookup(ofproto);
367
368 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
369 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
370 list_init(&xbundle->xports);
371 }
372
373 ovs_assert(xbundle->xbridge);
374
375 free(xbundle->name);
376 xbundle->name = xstrdup(name);
377
378 xbundle->vlan_mode = vlan_mode;
379 xbundle->vlan = vlan;
380 xbundle->trunks = trunks;
381 xbundle->use_priority_tags = use_priority_tags;
382 xbundle->floodable = floodable;
383
384 if (xbundle->bond != bond) {
385 bond_unref(xbundle->bond);
386 xbundle->bond = bond_ref(bond);
387 }
388
389 if (xbundle->lacp != lacp) {
390 lacp_unref(xbundle->lacp);
391 xbundle->lacp = lacp_ref(lacp);
392 }
393}
394
395void
396xlate_bundle_remove(struct ofbundle *ofbundle)
397{
398 struct xbundle *xbundle = xbundle_lookup(ofbundle);
399 struct xport *xport, *next;
400
401 if (!xbundle) {
402 return;
403 }
404
405 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
406 list_remove(&xport->bundle_node);
407 xport->xbundle = NULL;
408 }
409
410 hmap_remove(&xbundles, &xbundle->hmap_node);
411 list_remove(&xbundle->list_node);
412 bond_unref(xbundle->bond);
413 lacp_unref(xbundle->lacp);
414 free(xbundle->name);
415 free(xbundle);
416}
417
418void
419xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
420 struct ofport_dpif *ofport, ofp_port_t ofp_port,
421 odp_port_t odp_port, const struct netdev *netdev,
422 const struct cfm *cfm, const struct bfd *bfd,
9d189a50 423 struct ofport_dpif *peer, int stp_port_no,
55954f6e 424 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
425 enum ofputil_port_config config,
426 enum ofputil_port_state state, bool is_tunnel,
9d189a50 427 bool may_enable)
46c88433
EJ
428{
429 struct xport *xport = xport_lookup(ofport);
55954f6e 430 size_t i;
46c88433
EJ
431
432 if (!xport) {
433 xport = xzalloc(sizeof *xport);
434 xport->ofport = ofport;
435 xport->xbridge = xbridge_lookup(ofproto);
436 xport->ofp_port = ofp_port;
437
55954f6e 438 hmap_init(&xport->skb_priorities);
46c88433
EJ
439 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
440 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
441 hash_ofp_port(xport->ofp_port));
442 }
443
444 ovs_assert(xport->ofp_port == ofp_port);
445
446 xport->config = config;
dd8cd4b4 447 xport->state = state;
9d189a50 448 xport->stp_port_no = stp_port_no;
46c88433
EJ
449 xport->is_tunnel = is_tunnel;
450 xport->may_enable = may_enable;
451 xport->odp_port = odp_port;
452
453 if (xport->netdev != netdev) {
454 netdev_close(xport->netdev);
455 xport->netdev = netdev_ref(netdev);
456 }
457
458 if (xport->cfm != cfm) {
459 cfm_unref(xport->cfm);
460 xport->cfm = cfm_ref(cfm);
461 }
462
463 if (xport->bfd != bfd) {
464 bfd_unref(xport->bfd);
465 xport->bfd = bfd_ref(bfd);
466 }
467
468 if (xport->peer) {
469 xport->peer->peer = NULL;
470 }
5e6af486 471 xport->peer = xport_lookup(peer);
46c88433
EJ
472 if (xport->peer) {
473 xport->peer->peer = xport;
474 }
475
476 if (xport->xbundle) {
477 list_remove(&xport->bundle_node);
478 }
5e6af486 479 xport->xbundle = xbundle_lookup(ofbundle);
46c88433
EJ
480 if (xport->xbundle) {
481 list_insert(&xport->xbundle->xports, &xport->bundle_node);
482 }
55954f6e
EJ
483
484 clear_skb_priorities(xport);
485 for (i = 0; i < n_qdscp; i++) {
486 struct skb_priority_to_dscp *pdscp;
487 uint32_t skb_priority;
488
89a8a7f0
EJ
489 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
490 &skb_priority)) {
55954f6e
EJ
491 continue;
492 }
493
494 pdscp = xmalloc(sizeof *pdscp);
495 pdscp->skb_priority = skb_priority;
496 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
497 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
498 hash_int(pdscp->skb_priority, 0));
499 }
46c88433
EJ
500}
501
502void
503xlate_ofport_remove(struct ofport_dpif *ofport)
504{
505 struct xport *xport = xport_lookup(ofport);
506
507 if (!xport) {
508 return;
509 }
510
511 if (xport->peer) {
512 xport->peer->peer = NULL;
513 xport->peer = NULL;
514 }
515
e621a12d
EJ
516 if (xport->xbundle) {
517 list_remove(&xport->bundle_node);
518 }
519
55954f6e
EJ
520 clear_skb_priorities(xport);
521 hmap_destroy(&xport->skb_priorities);
522
46c88433
EJ
523 hmap_remove(&xports, &xport->hmap_node);
524 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
525
526 netdev_close(xport->netdev);
527 cfm_unref(xport->cfm);
528 bfd_unref(xport->bfd);
529 free(xport);
530}
531
8449c4d6
EJ
532/* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
533 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
836fbda7
SH
534 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
535 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
536 * 'netflow' with the appropriate handles for those protocols if they're
537 * enabled. Caller is responsible for unrefing them.
8449c4d6
EJ
538 *
539 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
540 * 'flow''s in_port to OFPP_NONE.
541 *
542 * This function does post-processing on data returned from
543 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
544 * of the upcall processing logic. In particular, if the extracted in_port is
545 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
546 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
547 * a VLAN header onto 'packet' (if it is nonnull).
548 *
549 * Similarly, this function also includes some logic to help with tunnels. It
550 * may modify 'flow' as necessary to make the tunneling implementation
551 * transparent to the upcall processing logic.
552 *
553 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
554 * or some other positive errno if there are other problems. */
555int
556xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
836fbda7 557 const struct nlattr *key, size_t key_len, struct flow *flow,
1dfdb9b3
EJ
558 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
559 struct dpif_sflow **sflow, struct netflow **netflow,
560 odp_port_t *odp_in_port)
8449c4d6 561{
8449c4d6
EJ
562 const struct xport *xport;
563 int error = ENODEV;
564
dc24a00f 565 ovs_rwlock_rdlock(&xlate_rwlock);
836fbda7 566 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
8449c4d6
EJ
567 error = EINVAL;
568 goto exit;
569 }
570
571 if (odp_in_port) {
572 *odp_in_port = flow->in_port.odp_port;
573 }
574
575 xport = xport_lookup(tnl_port_should_receive(flow)
d6fc5f57
EJ
576 ? tnl_port_receive(flow)
577 : odp_port_to_ofport(backer, flow->in_port.odp_port));
8449c4d6
EJ
578
579 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
580 if (!xport) {
581 goto exit;
582 }
583
584 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
585 if (packet) {
586 /* Make the packet resemble the flow, so that it gets sent to
587 * an OpenFlow controller properly, so that it looks correct
588 * for sFlow, and so that flow_extract() will get the correct
da546e07 589 * vlan_tci if it is called on 'packet'. */
1bf02876 590 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
8449c4d6 591 }
8449c4d6
EJ
592 }
593 error = 0;
594
595 if (ofproto) {
596 *ofproto = xport->xbridge->ofproto;
597 }
598
1dfdb9b3
EJ
599 if (ipfix) {
600 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
601 }
602
603 if (sflow) {
604 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
605 }
606
607 if (netflow) {
608 *netflow = netflow_ref(xport->xbridge->netflow);
609 }
610
8449c4d6 611exit:
dc24a00f 612 ovs_rwlock_unlock(&xlate_rwlock);
8449c4d6
EJ
613 return error;
614}
615
46c88433
EJ
616static struct xbridge *
617xbridge_lookup(const struct ofproto_dpif *ofproto)
618{
619 struct xbridge *xbridge;
620
5e6af486
EJ
621 if (!ofproto) {
622 return NULL;
623 }
624
46c88433
EJ
625 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
626 &xbridges) {
627 if (xbridge->ofproto == ofproto) {
628 return xbridge;
629 }
630 }
631 return NULL;
632}
633
634static struct xbundle *
635xbundle_lookup(const struct ofbundle *ofbundle)
636{
637 struct xbundle *xbundle;
638
5e6af486
EJ
639 if (!ofbundle) {
640 return NULL;
641 }
642
46c88433
EJ
643 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
644 &xbundles) {
645 if (xbundle->ofbundle == ofbundle) {
646 return xbundle;
647 }
648 }
649 return NULL;
650}
651
652static struct xport *
5e6af486 653xport_lookup(const struct ofport_dpif *ofport)
46c88433
EJ
654{
655 struct xport *xport;
656
5e6af486
EJ
657 if (!ofport) {
658 return NULL;
659 }
660
46c88433
EJ
661 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
662 &xports) {
663 if (xport->ofport == ofport) {
664 return xport;
665 }
666 }
667 return NULL;
668}
669
40085e56
EJ
670static struct stp_port *
671xport_get_stp_port(const struct xport *xport)
672{
92cf817b 673 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
674 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
675 : NULL;
676}
9d189a50 677
0d1cee12 678static bool
9d189a50
EJ
679xport_stp_learn_state(const struct xport *xport)
680{
40085e56
EJ
681 struct stp_port *sp = xport_get_stp_port(xport);
682 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
9d189a50
EJ
683}
684
685static bool
686xport_stp_forward_state(const struct xport *xport)
687{
40085e56
EJ
688 struct stp_port *sp = xport_get_stp_port(xport);
689 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
9d189a50
EJ
690}
691
0d1cee12
K
692static bool
693xport_stp_listen_state(const struct xport *xport)
694{
695 struct stp_port *sp = xport_get_stp_port(xport);
696 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
697}
698
9d189a50
EJ
699/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
700 * were used to make the determination.*/
701static bool
702stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
703{
704 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
705 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
706}
707
708static void
709stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
710{
40085e56 711 struct stp_port *sp = xport_get_stp_port(xport);
9d189a50 712 struct ofpbuf payload = *packet;
1f317cb5 713 struct eth_header *eth = ofpbuf_data(&payload);
9d189a50
EJ
714
715 /* Sink packets on ports that have STP disabled when the bridge has
716 * STP enabled. */
717 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
718 return;
719 }
720
721 /* Trim off padding on payload. */
1f317cb5
PS
722 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
723 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
724 }
725
726 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1f317cb5 727 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
9d189a50
EJ
728 }
729}
730
46c88433
EJ
731static struct xport *
732get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
733{
734 struct xport *xport;
735
736 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
737 &xbridge->xports) {
738 if (xport->ofp_port == ofp_port) {
739 return xport;
740 }
741 }
742 return NULL;
743}
744
745static odp_port_t
746ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
747{
748 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
749 return xport ? xport->odp_port : ODPP_NONE;
750}
751
dd8cd4b4
SH
752static bool
753odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
754{
755 struct xport *xport;
756
757 xport = get_ofp_port(ctx->xbridge, ofp_port);
758 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
759 xport->state & OFPUTIL_PS_LINK_DOWN) {
760 return false;
761 }
762
763 return true;
764}
765
766static const struct ofputil_bucket *
767group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
768 int depth);
769
770static bool
771group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
772{
773 struct group_dpif *group;
774 bool hit;
775
776 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
777 if (!hit) {
778 return false;
779 }
780
781 hit = group_first_live_bucket(ctx, group, depth) != NULL;
782
783 group_dpif_release(group);
784 return hit;
785}
786
787#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
788
789static bool
790bucket_is_alive(const struct xlate_ctx *ctx,
791 const struct ofputil_bucket *bucket, int depth)
792{
793 if (depth >= MAX_LIVENESS_RECURSION) {
794 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
795
796 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
797 MAX_LIVENESS_RECURSION);
798 return false;
799 }
800
fe7e5749 801 return !ofputil_bucket_has_liveness(bucket) ||
d6fc5f57
EJ
802 (bucket->watch_port != OFPP_ANY &&
803 odp_port_is_alive(ctx, bucket->watch_port)) ||
804 (bucket->watch_group != OFPG_ANY &&
805 group_is_alive(ctx, bucket->watch_group, depth + 1));
dd8cd4b4
SH
806}
807
808static const struct ofputil_bucket *
809group_first_live_bucket(const struct xlate_ctx *ctx,
810 const struct group_dpif *group, int depth)
811{
812 struct ofputil_bucket *bucket;
813 const struct list *buckets;
814
815 group_dpif_get_buckets(group, &buckets);
816 LIST_FOR_EACH (bucket, list_node, buckets) {
817 if (bucket_is_alive(ctx, bucket, depth)) {
818 return bucket;
819 }
820 }
821
822 return NULL;
823}
824
fe7e5749
SH
825static const struct ofputil_bucket *
826group_best_live_bucket(const struct xlate_ctx *ctx,
827 const struct group_dpif *group,
828 uint32_t basis)
829{
830 const struct ofputil_bucket *best_bucket = NULL;
831 uint32_t best_score = 0;
832 int i = 0;
833
834 const struct ofputil_bucket *bucket;
835 const struct list *buckets;
836
837 group_dpif_get_buckets(group, &buckets);
838 LIST_FOR_EACH (bucket, list_node, buckets) {
839 if (bucket_is_alive(ctx, bucket, 0)) {
7cb279c2 840 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
841 if (score >= best_score) {
842 best_bucket = bucket;
843 best_score = score;
844 }
845 }
846 i++;
847 }
848
849 return best_bucket;
850}
851
9583bc14 852static bool
46c88433 853xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
854{
855 return (bundle->vlan_mode != PORT_VLAN_ACCESS
856 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
857}
858
859static bool
46c88433
EJ
860xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
861{
862 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
863}
864
865static mirror_mask_t
866xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
867{
868 return xbundle != &ofpp_none_bundle
869 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
870 : 0;
871}
872
873static mirror_mask_t
874xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 875{
46c88433
EJ
876 return xbundle != &ofpp_none_bundle
877 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
878 : 0;
9583bc14
EJ
879}
880
46c88433
EJ
881static mirror_mask_t
882xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 883{
46c88433
EJ
884 return xbundle != &ofpp_none_bundle
885 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
886 : 0;
887}
888
889static struct xbundle *
890lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
891 bool warn, struct xport **in_xportp)
892{
893 struct xport *xport;
9583bc14
EJ
894
895 /* Find the port and bundle for the received packet. */
46c88433
EJ
896 xport = get_ofp_port(xbridge, in_port);
897 if (in_xportp) {
898 *in_xportp = xport;
9583bc14 899 }
46c88433
EJ
900 if (xport && xport->xbundle) {
901 return xport->xbundle;
9583bc14
EJ
902 }
903
904 /* Special-case OFPP_NONE, which a controller may use as the ingress
905 * port for traffic that it is sourcing. */
906 if (in_port == OFPP_NONE) {
907 return &ofpp_none_bundle;
908 }
909
910 /* Odd. A few possible reasons here:
911 *
912 * - We deleted a port but there are still a few packets queued up
913 * from it.
914 *
915 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
916 * we don't know about.
917 *
918 * - The ofproto client didn't configure the port as part of a bundle.
919 * This is particularly likely to happen if a packet was received on the
920 * port after it was created, but before the client had a chance to
921 * configure its bundle.
922 */
923 if (warn) {
924 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
925
926 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
46c88433 927 "port %"PRIu16, xbridge->name, in_port);
9583bc14
EJ
928 }
929 return NULL;
930}
931
932static void
933add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
934{
46c88433 935 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 936 mirror_mask_t mirrors;
46c88433 937 struct xbundle *in_xbundle;
9583bc14
EJ
938 uint16_t vlan;
939 uint16_t vid;
cdf5d3a5
EJ
940
941 mirrors = ctx->xout->mirrors;
942 ctx->xout->mirrors = 0;
9583bc14 943
46c88433
EJ
944 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
945 ctx->xin->packet != NULL, NULL);
946 if (!in_xbundle) {
9583bc14
EJ
947 return;
948 }
46c88433 949 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
9583bc14
EJ
950
951 /* Drop frames on bundles reserved for mirroring. */
46c88433 952 if (xbundle_mirror_out(xbridge, in_xbundle)) {
9583bc14
EJ
953 if (ctx->xin->packet != NULL) {
954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
955 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
956 "%s, which is reserved exclusively for mirroring",
46c88433 957 ctx->xbridge->name, in_xbundle->name);
9583bc14 958 }
aaa0fbae 959 ofpbuf_clear(&ctx->xout->odp_actions);
9583bc14
EJ
960 return;
961 }
962
963 /* Check VLAN. */
964 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
46c88433 965 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
966 return;
967 }
46c88433 968 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14 969
9583bc14
EJ
970 if (!mirrors) {
971 return;
972 }
973
974 /* Restore the original packet before adding the mirror actions. */
975 ctx->xin->flow = *orig_flow;
976
977 while (mirrors) {
ec7ceaed
EJ
978 mirror_mask_t dup_mirrors;
979 struct ofbundle *out;
980 unsigned long *vlans;
981 bool vlan_mirrored;
982 bool has_mirror;
983 int out_vlan;
984
4b6ab2b0 985 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
ec7ceaed
EJ
986 &vlans, &dup_mirrors, &out, &out_vlan);
987 ovs_assert(has_mirror);
988
989 if (vlans) {
9583bc14
EJ
990 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
991 }
ec7ceaed
EJ
992 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
993 free(vlans);
9583bc14 994
ec7ceaed 995 if (!vlan_mirrored) {
9583bc14
EJ
996 mirrors = zero_rightmost_1bit(mirrors);
997 continue;
998 }
999
ec7ceaed
EJ
1000 mirrors &= ~dup_mirrors;
1001 ctx->xout->mirrors |= dup_mirrors;
1002 if (out) {
46c88433
EJ
1003 struct xbundle *out_xbundle = xbundle_lookup(out);
1004 if (out_xbundle) {
1005 output_normal(ctx, out_xbundle, vlan);
1006 }
ec7ceaed 1007 } else if (vlan != out_vlan
9583bc14 1008 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
46c88433 1009 struct xbundle *xbundle;
9583bc14 1010
46c88433
EJ
1011 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1012 if (xbundle_includes_vlan(xbundle, out_vlan)
1013 && !xbundle_mirror_out(xbridge, xbundle)) {
1014 output_normal(ctx, xbundle, out_vlan);
9583bc14
EJ
1015 }
1016 }
1017 }
1018 }
1019}
1020
1021/* Given 'vid', the VID obtained from the 802.1Q header that was received as
46c88433 1022 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
9583bc14
EJ
1023 * the bundle on which the packet was received, returns the VLAN to which the
1024 * packet belongs.
1025 *
1026 * Both 'vid' and the return value are in the range 0...4095. */
1027static uint16_t
46c88433 1028input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
9583bc14 1029{
46c88433 1030 switch (in_xbundle->vlan_mode) {
9583bc14 1031 case PORT_VLAN_ACCESS:
46c88433 1032 return in_xbundle->vlan;
9583bc14
EJ
1033 break;
1034
1035 case PORT_VLAN_TRUNK:
1036 return vid;
1037
1038 case PORT_VLAN_NATIVE_UNTAGGED:
1039 case PORT_VLAN_NATIVE_TAGGED:
46c88433 1040 return vid ? vid : in_xbundle->vlan;
9583bc14
EJ
1041
1042 default:
428b2edd 1043 OVS_NOT_REACHED();
9583bc14
EJ
1044 }
1045}
1046
46c88433 1047/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
9583bc14
EJ
1048 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1049 * a warning.
1050 *
1051 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1052 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1053 * 0...4095. */
1054static bool
46c88433 1055input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
9583bc14
EJ
1056{
1057 /* Allow any VID on the OFPP_NONE port. */
46c88433 1058 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1059 return true;
1060 }
1061
46c88433 1062 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1063 case PORT_VLAN_ACCESS:
1064 if (vid) {
1065 if (warn) {
1066 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1067 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
9583bc14 1068 "packet received on port %s configured as VLAN "
46c88433
EJ
1069 "%"PRIu16" access port", vid, in_xbundle->name,
1070 in_xbundle->vlan);
9583bc14
EJ
1071 }
1072 return false;
1073 }
1074 return true;
1075
1076 case PORT_VLAN_NATIVE_UNTAGGED:
1077 case PORT_VLAN_NATIVE_TAGGED:
1078 if (!vid) {
1079 /* Port must always carry its native VLAN. */
1080 return true;
1081 }
1082 /* Fall through. */
1083 case PORT_VLAN_TRUNK:
46c88433 1084 if (!xbundle_includes_vlan(in_xbundle, vid)) {
9583bc14
EJ
1085 if (warn) {
1086 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1087 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
9583bc14 1088 "received on port %s not configured for trunking "
46c88433 1089 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
9583bc14
EJ
1090 }
1091 return false;
1092 }
1093 return true;
1094
1095 default:
428b2edd 1096 OVS_NOT_REACHED();
9583bc14
EJ
1097 }
1098
1099}
1100
1101/* Given 'vlan', the VLAN that a packet belongs to, and
46c88433 1102 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
9583bc14
EJ
1103 * that should be included in the 802.1Q header. (If the return value is 0,
1104 * then the 802.1Q header should only be included in the packet if there is a
1105 * nonzero PCP.)
1106 *
1107 * Both 'vlan' and the return value are in the range 0...4095. */
1108static uint16_t
46c88433 1109output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
9583bc14 1110{
46c88433 1111 switch (out_xbundle->vlan_mode) {
9583bc14
EJ
1112 case PORT_VLAN_ACCESS:
1113 return 0;
1114
1115 case PORT_VLAN_TRUNK:
1116 case PORT_VLAN_NATIVE_TAGGED:
1117 return vlan;
1118
1119 case PORT_VLAN_NATIVE_UNTAGGED:
46c88433 1120 return vlan == out_xbundle->vlan ? 0 : vlan;
9583bc14
EJ
1121
1122 default:
428b2edd 1123 OVS_NOT_REACHED();
9583bc14
EJ
1124 }
1125}
1126
1127static void
46c88433 1128output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
9583bc14
EJ
1129 uint16_t vlan)
1130{
33bf9176 1131 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
9583bc14
EJ
1132 uint16_t vid;
1133 ovs_be16 tci, old_tci;
46c88433 1134 struct xport *xport;
9583bc14 1135
46c88433
EJ
1136 vid = output_vlan_to_vid(out_xbundle, vlan);
1137 if (list_is_empty(&out_xbundle->xports)) {
1138 /* Partially configured bundle with no slaves. Drop the packet. */
1139 return;
1140 } else if (!out_xbundle->bond) {
adcf00ba 1141 ctx->xout->use_recirc = false;
46c88433
EJ
1142 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1143 bundle_node);
9583bc14 1144 } else {
46c88433 1145 struct ofport_dpif *ofport;
adcf00ba 1146 struct xlate_recirc *xr = &ctx->xout->recirc;
54ecb5a2 1147 struct flow_wildcards *wc = &ctx->xout->wc;
adcf00ba
AZ
1148
1149 if (ctx->xbridge->enable_recirc) {
1150 ctx->xout->use_recirc = bond_may_recirc(
1151 out_xbundle->bond, &xr->recirc_id, &xr->hash_bias);
1152
1153 if (ctx->xout->use_recirc) {
1154 /* Only TCP mode uses recirculation. */
1155 xr->hash_alg = OVS_RECIRC_HASH_ALG_L4;
1156 bond_update_post_recirc_rules(out_xbundle->bond, false);
54ecb5a2
AZ
1157
1158 /* Recirculation does not require unmasking hash fields. */
1159 wc = NULL;
adcf00ba
AZ
1160 }
1161 }
46c88433 1162
54ecb5a2
AZ
1163 ofport = bond_choose_output_slave(out_xbundle->bond,
1164 &ctx->xin->flow, wc, vid);
5e6af486 1165 xport = xport_lookup(ofport);
46c88433
EJ
1166
1167 if (!xport) {
9583bc14
EJ
1168 /* No slaves enabled, so drop packet. */
1169 return;
1170 }
d6fc5f57
EJ
1171
1172 if (ctx->xin->resubmit_stats) {
1173 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1174 ctx->xin->resubmit_stats->n_bytes);
1175 }
9583bc14
EJ
1176 }
1177
33bf9176 1178 old_tci = *flow_tci;
9583bc14 1179 tci = htons(vid);
46c88433 1180 if (tci || out_xbundle->use_priority_tags) {
33bf9176 1181 tci |= *flow_tci & htons(VLAN_PCP_MASK);
9583bc14
EJ
1182 if (tci) {
1183 tci |= htons(VLAN_CFI);
1184 }
1185 }
33bf9176 1186 *flow_tci = tci;
9583bc14 1187
46c88433 1188 compose_output_action(ctx, xport->ofp_port);
33bf9176 1189 *flow_tci = old_tci;
9583bc14
EJ
1190}
1191
1192/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1193 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1194 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1195static bool
1196is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1197{
1198 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1199 return false;
1200 }
1201
1202 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1203 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1204 return false;
1205 }
1206
1207 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1208 if (flow->nw_proto == ARP_OP_REPLY) {
1209 return true;
1210 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1211 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1212 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1213
1214 return flow->nw_src == flow->nw_dst;
1215 } else {
1216 return false;
1217 }
1218}
1219
ee047520
BP
1220/* Checks whether a MAC learning update is necessary for MAC learning table
1221 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1222 * 'vlan'.
1223 *
1224 * Most packets processed through the MAC learning table do not actually
1225 * change it in any way. This function requires only a read lock on the MAC
1226 * learning table, so it is much cheaper in this common case.
1227 *
1228 * Keep the code here synchronized with that in update_learning_table__()
1229 * below. */
1230static bool
1231is_mac_learning_update_needed(const struct mac_learning *ml,
1232 const struct flow *flow,
1233 struct flow_wildcards *wc,
1234 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1235OVS_REQ_RDLOCK(ml->rwlock)
9583bc14
EJ
1236{
1237 struct mac_entry *mac;
1238
ee047520
BP
1239 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1240 return false;
1241 }
1242
1243 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1244 if (!mac || mac_entry_age(ml, mac)) {
1245 return true;
9583bc14
EJ
1246 }
1247
ee047520
BP
1248 if (is_gratuitous_arp(flow, wc)) {
1249 /* We don't want to learn from gratuitous ARP packets that are
1250 * reflected back over bond slaves so we lock the learning table. */
1251 if (!in_xbundle->bond) {
1252 return true;
1253 } else if (mac_entry_is_grat_arp_locked(mac)) {
1254 return false;
1255 }
1256 }
1257
1258 return mac->port.p != in_xbundle->ofbundle;
1259}
1260
1261
1262/* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1263 * received on 'in_xbundle' in 'vlan'.
1264 *
1265 * This code repeats all the checks in is_mac_learning_update_needed() because
1266 * the lock was released between there and here and thus the MAC learning state
1267 * could have changed.
1268 *
1269 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1270 * above. */
1271static void
1272update_learning_table__(const struct xbridge *xbridge,
1273 const struct flow *flow, struct flow_wildcards *wc,
1274 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1275OVS_REQ_WRLOCK(xbridge->ml->rwlock)
ee047520
BP
1276{
1277 struct mac_entry *mac;
1278
46c88433 1279 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
ee047520 1280 return;
9583bc14
EJ
1281 }
1282
46c88433 1283 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
9583bc14
EJ
1284 if (is_gratuitous_arp(flow, wc)) {
1285 /* We don't want to learn from gratuitous ARP packets that are
1286 * reflected back over bond slaves so we lock the learning table. */
46c88433 1287 if (!in_xbundle->bond) {
9583bc14
EJ
1288 mac_entry_set_grat_arp_lock(mac);
1289 } else if (mac_entry_is_grat_arp_locked(mac)) {
ee047520 1290 return;
9583bc14
EJ
1291 }
1292 }
1293
30618594 1294 if (mac->port.p != in_xbundle->ofbundle) {
9583bc14
EJ
1295 /* The log messages here could actually be useful in debugging,
1296 * so keep the rate limit relatively high. */
1297 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
ee047520 1298
9583bc14
EJ
1299 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1300 "on port %s in VLAN %d",
46c88433
EJ
1301 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1302 in_xbundle->name, vlan);
9583bc14 1303
46c88433 1304 mac->port.p = in_xbundle->ofbundle;
30618594 1305 mac_learning_changed(xbridge->ml);
9583bc14 1306 }
ee047520
BP
1307}
1308
1309static void
1310update_learning_table(const struct xbridge *xbridge,
1311 const struct flow *flow, struct flow_wildcards *wc,
1312 int vlan, struct xbundle *in_xbundle)
1313{
1314 bool need_update;
1315
1316 /* Don't learn the OFPP_NONE port. */
1317 if (in_xbundle == &ofpp_none_bundle) {
1318 return;
1319 }
1320
1321 /* First try the common case: no change to MAC learning table. */
1322 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1323 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1324 in_xbundle);
509c0149 1325 ovs_rwlock_unlock(&xbridge->ml->rwlock);
ee047520
BP
1326
1327 if (need_update) {
1328 /* Slow path: MAC learning table might need an update. */
1329 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1330 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1331 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1332 }
9583bc14
EJ
1333}
1334
46c88433 1335/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
9583bc14
EJ
1336 * dropped. Returns true if they may be forwarded, false if they should be
1337 * dropped.
1338 *
46c88433 1339 * 'in_port' must be the xport that corresponds to flow->in_port.
9583bc14
EJ
1340 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1341 *
1342 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1343 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1344 * checked by input_vid_is_valid().
1345 *
1346 * May also add tags to '*tags', although the current implementation only does
1347 * so in one special case.
1348 */
1349static bool
46c88433 1350is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
9583bc14
EJ
1351 uint16_t vlan)
1352{
46c88433
EJ
1353 struct xbundle *in_xbundle = in_port->xbundle;
1354 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1355 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
1356
1357 /* Drop frames for reserved multicast addresses
1358 * only if forward_bpdu option is absent. */
46c88433 1359 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
9583bc14
EJ
1360 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1361 return false;
1362 }
1363
46c88433 1364 if (in_xbundle->bond) {
9583bc14
EJ
1365 struct mac_entry *mac;
1366
46c88433 1367 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
4a1b8f30 1368 flow->dl_dst)) {
9583bc14
EJ
1369 case BV_ACCEPT:
1370 break;
1371
1372 case BV_DROP:
1373 xlate_report(ctx, "bonding refused admissibility, dropping");
1374 return false;
1375
1376 case BV_DROP_IF_MOVED:
509c0149 1377 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
30618594 1378 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
46c88433 1379 if (mac && mac->port.p != in_xbundle->ofbundle &&
9583bc14
EJ
1380 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1381 || mac_entry_is_grat_arp_locked(mac))) {
509c0149 1382 ovs_rwlock_unlock(&xbridge->ml->rwlock);
9583bc14 1383 xlate_report(ctx, "SLB bond thinks this packet looped back, "
d6fc5f57 1384 "dropping");
9583bc14
EJ
1385 return false;
1386 }
509c0149 1387 ovs_rwlock_unlock(&xbridge->ml->rwlock);
9583bc14
EJ
1388 break;
1389 }
1390 }
1391
1392 return true;
1393}
1394
1395static void
1396xlate_normal(struct xlate_ctx *ctx)
1397{
33bf9176
BP
1398 struct flow_wildcards *wc = &ctx->xout->wc;
1399 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
1400 struct xbundle *in_xbundle;
1401 struct xport *in_port;
9583bc14 1402 struct mac_entry *mac;
d6d5bbc9 1403 void *mac_port;
9583bc14
EJ
1404 uint16_t vlan;
1405 uint16_t vid;
1406
1407 ctx->xout->has_normal = true;
1408
33bf9176
BP
1409 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1410 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1dd35f8a 1411 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 1412
46c88433
EJ
1413 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1414 ctx->xin->packet != NULL, &in_port);
1415 if (!in_xbundle) {
9583bc14
EJ
1416 xlate_report(ctx, "no input bundle, dropping");
1417 return;
1418 }
1419
1420 /* Drop malformed frames. */
33bf9176
BP
1421 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1422 !(flow->vlan_tci & htons(VLAN_CFI))) {
9583bc14
EJ
1423 if (ctx->xin->packet != NULL) {
1424 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1425 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1426 "VLAN tag received on port %s",
46c88433 1427 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
1428 }
1429 xlate_report(ctx, "partial VLAN tag, dropping");
1430 return;
1431 }
1432
1433 /* Drop frames on bundles reserved for mirroring. */
46c88433 1434 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14
EJ
1435 if (ctx->xin->packet != NULL) {
1436 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1437 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1438 "%s, which is reserved exclusively for mirroring",
46c88433 1439 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
1440 }
1441 xlate_report(ctx, "input port is mirror output port, dropping");
1442 return;
1443 }
1444
1445 /* Check VLAN. */
33bf9176 1446 vid = vlan_tci_to_vid(flow->vlan_tci);
46c88433 1447 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
1448 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1449 return;
1450 }
46c88433 1451 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14
EJ
1452
1453 /* Check other admissibility requirements. */
1454 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1455 return;
1456 }
1457
1458 /* Learn source MAC. */
1459 if (ctx->xin->may_learn) {
46c88433 1460 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
9583bc14
EJ
1461 }
1462
1463 /* Determine output bundle. */
509c0149 1464 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
30618594 1465 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
d6d5bbc9
EJ
1466 mac_port = mac ? mac->port.p : NULL;
1467 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1468
1469 if (mac_port) {
1470 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
46c88433 1471 if (mac_xbundle && mac_xbundle != in_xbundle) {
9583bc14 1472 xlate_report(ctx, "forwarding to learned port");
46c88433
EJ
1473 output_normal(ctx, mac_xbundle, vlan);
1474 } else if (!mac_xbundle) {
1475 xlate_report(ctx, "learned port is unknown, dropping");
9583bc14
EJ
1476 } else {
1477 xlate_report(ctx, "learned port is input port, dropping");
1478 }
1479 } else {
46c88433 1480 struct xbundle *xbundle;
9583bc14
EJ
1481
1482 xlate_report(ctx, "no learned MAC for destination, flooding");
46c88433
EJ
1483 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1484 if (xbundle != in_xbundle
1485 && xbundle_includes_vlan(xbundle, vlan)
1486 && xbundle->floodable
1487 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1488 output_normal(ctx, xbundle, vlan);
9583bc14
EJ
1489 }
1490 }
1491 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1492 }
1493}
1494
1495/* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1496 * the number of packets out of UINT32_MAX to sample. The given
1497 * cookie is passed back in the callback for each sampled packet.
1498 */
1499static size_t
46c88433 1500compose_sample_action(const struct xbridge *xbridge,
9583bc14
EJ
1501 struct ofpbuf *odp_actions,
1502 const struct flow *flow,
1503 const uint32_t probability,
1504 const union user_action_cookie *cookie,
1505 const size_t cookie_size)
1506{
1507 size_t sample_offset, actions_offset;
89a8a7f0 1508 odp_port_t odp_port;
9583bc14 1509 int cookie_offset;
89a8a7f0 1510 uint32_t pid;
9583bc14
EJ
1511
1512 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1513
1514 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1515
1516 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
89a8a7f0
EJ
1517
1518 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1954e6bb 1519 pid = dpif_port_get_pid(xbridge->dpif, odp_port, 0);
89a8a7f0 1520 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
9583bc14
EJ
1521
1522 nl_msg_end_nested(odp_actions, actions_offset);
1523 nl_msg_end_nested(odp_actions, sample_offset);
1524 return cookie_offset;
1525}
1526
1527static void
46c88433
EJ
1528compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1529 odp_port_t odp_port, unsigned int n_outputs,
1530 union user_action_cookie *cookie)
9583bc14
EJ
1531{
1532 int ifindex;
1533
1534 cookie->type = USER_ACTION_COOKIE_SFLOW;
1535 cookie->sflow.vlan_tci = vlan_tci;
1536
1537 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1538 * port information") for the interpretation of cookie->output. */
1539 switch (n_outputs) {
1540 case 0:
1541 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1542 cookie->sflow.output = 0x40000000 | 256;
1543 break;
1544
1545 case 1:
46c88433 1546 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
9583bc14
EJ
1547 if (ifindex) {
1548 cookie->sflow.output = ifindex;
1549 break;
1550 }
1551 /* Fall through. */
1552 default:
1553 /* 0x80000000 means "multiple output ports. */
1554 cookie->sflow.output = 0x80000000 | n_outputs;
1555 break;
1556 }
1557}
1558
1559/* Compose SAMPLE action for sFlow bridge sampling. */
1560static size_t
46c88433 1561compose_sflow_action(const struct xbridge *xbridge,
9583bc14
EJ
1562 struct ofpbuf *odp_actions,
1563 const struct flow *flow,
4e022ec0 1564 odp_port_t odp_port)
9583bc14
EJ
1565{
1566 uint32_t probability;
1567 union user_action_cookie cookie;
1568
46c88433 1569 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
1570 return 0;
1571 }
1572
46c88433
EJ
1573 probability = dpif_sflow_get_probability(xbridge->sflow);
1574 compose_sflow_cookie(xbridge, htons(0), odp_port,
4e022ec0 1575 odp_port == ODPP_NONE ? 0 : 1, &cookie);
9583bc14 1576
46c88433 1577 return compose_sample_action(xbridge, odp_actions, flow, probability,
9583bc14
EJ
1578 &cookie, sizeof cookie.sflow);
1579}
1580
1581static void
1582compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1583 uint32_t obs_domain_id, uint32_t obs_point_id,
1584 union user_action_cookie *cookie)
1585{
1586 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1587 cookie->flow_sample.probability = probability;
1588 cookie->flow_sample.collector_set_id = collector_set_id;
1589 cookie->flow_sample.obs_domain_id = obs_domain_id;
1590 cookie->flow_sample.obs_point_id = obs_point_id;
1591}
1592
1593static void
1594compose_ipfix_cookie(union user_action_cookie *cookie)
1595{
1596 cookie->type = USER_ACTION_COOKIE_IPFIX;
1597}
1598
1599/* Compose SAMPLE action for IPFIX bridge sampling. */
1600static void
46c88433 1601compose_ipfix_action(const struct xbridge *xbridge,
9583bc14
EJ
1602 struct ofpbuf *odp_actions,
1603 const struct flow *flow)
1604{
1605 uint32_t probability;
1606 union user_action_cookie cookie;
1607
46c88433 1608 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
1609 return;
1610 }
1611
46c88433 1612 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
9583bc14
EJ
1613 compose_ipfix_cookie(&cookie);
1614
46c88433 1615 compose_sample_action(xbridge, odp_actions, flow, probability,
9583bc14
EJ
1616 &cookie, sizeof cookie.ipfix);
1617}
1618
1619/* SAMPLE action for sFlow must be first action in any given list of
1620 * actions. At this point we do not have all information required to
1621 * build it. So try to build sample action as complete as possible. */
1622static void
1623add_sflow_action(struct xlate_ctx *ctx)
1624{
46c88433 1625 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
9583bc14 1626 &ctx->xout->odp_actions,
4e022ec0 1627 &ctx->xin->flow, ODPP_NONE);
9583bc14
EJ
1628 ctx->sflow_odp_port = 0;
1629 ctx->sflow_n_outputs = 0;
1630}
1631
1632/* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1633 * of actions, eventually after the SAMPLE action for sFlow. */
1634static void
1635add_ipfix_action(struct xlate_ctx *ctx)
1636{
46c88433 1637 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
9583bc14
EJ
1638 &ctx->xin->flow);
1639}
1640
1641/* Fix SAMPLE action according to data collected while composing ODP actions.
1642 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1643 * USERSPACE action's user-cookie which is required for sflow. */
1644static void
1645fix_sflow_action(struct xlate_ctx *ctx)
1646{
1647 const struct flow *base = &ctx->base_flow;
1648 union user_action_cookie *cookie;
1649
1650 if (!ctx->user_cookie_offset) {
1651 return;
1652 }
1653
1654 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1655 sizeof cookie->sflow);
1656 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1657
46c88433 1658 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
9583bc14
EJ
1659 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1660}
1661
db7d4e46 1662static enum slow_path_reason
642dc74d 1663process_special(struct xlate_ctx *ctx, const struct flow *flow,
46c88433 1664 const struct xport *xport, const struct ofpbuf *packet)
db7d4e46 1665{
642dc74d 1666 struct flow_wildcards *wc = &ctx->xout->wc;
46c88433 1667 const struct xbridge *xbridge = ctx->xbridge;
642dc74d 1668
46c88433 1669 if (!xport) {
db7d4e46 1670 return 0;
46c88433 1671 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 1672 if (packet) {
46c88433 1673 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46
JP
1674 }
1675 return SLOW_CFM;
fab52e16 1676 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 1677 if (packet) {
46c88433 1678 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
1679 /* If POLL received, immediately sends FINAL back. */
1680 if (bfd_should_send_packet(xport->bfd)) {
1681 if (xport->peer) {
1682 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1683 } else {
1684 ofproto_dpif_monitor_port_send_soon_safe(xport->ofport);
1685 }
1686 }
db7d4e46
JP
1687 }
1688 return SLOW_BFD;
46c88433 1689 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
1690 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1691 if (packet) {
46c88433 1692 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46
JP
1693 }
1694 return SLOW_LACP;
9d189a50 1695 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
db7d4e46 1696 if (packet) {
9d189a50 1697 stp_process_packet(xport, packet);
db7d4e46
JP
1698 }
1699 return SLOW_STP;
1700 } else {
1701 return 0;
1702 }
1703}
1704
9583bc14 1705static void
4e022ec0 1706compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
9583bc14
EJ
1707 bool check_stp)
1708{
46c88433 1709 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1dd35f8a 1710 struct flow_wildcards *wc = &ctx->xout->wc;
33bf9176 1711 struct flow *flow = &ctx->xin->flow;
9583bc14 1712 ovs_be16 flow_vlan_tci;
1362e248 1713 uint32_t flow_pkt_mark;
9583bc14 1714 uint8_t flow_nw_tos;
4e022ec0 1715 odp_port_t out_port, odp_port;
ca077186 1716 uint8_t dscp;
9583bc14
EJ
1717
1718 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1719 * before traversing a patch port. */
a79f29f2 1720 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 25);
9583bc14 1721
46c88433 1722 if (!xport) {
9583bc14
EJ
1723 xlate_report(ctx, "Nonexistent output port");
1724 return;
46c88433 1725 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
9583bc14
EJ
1726 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1727 return;
0d1cee12
K
1728 } else if (check_stp) {
1729 if (eth_addr_equals(ctx->base_flow.dl_dst, eth_addr_stp)) {
1730 if (!xport_stp_listen_state(xport)) {
1731 xlate_report(ctx, "STP not in listening state, "
1732 "skipping bpdu output");
1733 return;
1734 }
1735 } else if (!xport_stp_forward_state(xport)) {
1736 xlate_report(ctx, "STP not in forwarding state, "
1737 "skipping output");
1738 return;
1739 }
9583bc14
EJ
1740 }
1741
46c88433
EJ
1742 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1743 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1744 xport->xbundle);
cdf5d3a5
EJ
1745 }
1746
46c88433
EJ
1747 if (xport->peer) {
1748 const struct xport *peer = xport->peer;
9583bc14 1749 struct flow old_flow = ctx->xin->flow;
9583bc14 1750 enum slow_path_reason special;
9583bc14 1751
46c88433
EJ
1752 ctx->xbridge = peer->xbridge;
1753 flow->in_port.ofp_port = peer->ofp_port;
33bf9176
BP
1754 flow->metadata = htonll(0);
1755 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1756 memset(flow->regs, 0, sizeof flow->regs);
9583bc14 1757
642dc74d 1758 special = process_special(ctx, &ctx->xin->flow, peer,
9583bc14
EJ
1759 ctx->xin->packet);
1760 if (special) {
04594cd5 1761 ctx->xout->slow |= special;
ddd3c975 1762 } else if (may_receive(peer, ctx)) {
9d189a50 1763 if (xport_stp_forward_state(peer)) {
6d328fa2 1764 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
9583bc14
EJ
1765 } else {
1766 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1767 * learning action look at the packet, then drop it. */
1768 struct flow old_base_flow = ctx->base_flow;
1f317cb5 1769 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
cdf5d3a5 1770 mirror_mask_t old_mirrors = ctx->xout->mirrors;
6d328fa2 1771 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
cdf5d3a5 1772 ctx->xout->mirrors = old_mirrors;
9583bc14 1773 ctx->base_flow = old_base_flow;
1f317cb5 1774 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
9583bc14
EJ
1775 }
1776 }
1777
1778 ctx->xin->flow = old_flow;
832554e3 1779 ctx->xbridge = xport->xbridge;
9583bc14
EJ
1780
1781 if (ctx->xin->resubmit_stats) {
46c88433
EJ
1782 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1783 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
a1aeea86
AW
1784 if (peer->bfd) {
1785 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1786 }
9583bc14
EJ
1787 }
1788
1789 return;
1790 }
1791
33bf9176 1792 flow_vlan_tci = flow->vlan_tci;
1362e248 1793 flow_pkt_mark = flow->pkt_mark;
33bf9176 1794 flow_nw_tos = flow->nw_tos;
9583bc14 1795
55954f6e 1796 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1ac9326d 1797 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 1798 flow->nw_tos &= ~IP_DSCP_MASK;
ca077186 1799 flow->nw_tos |= dscp;
9583bc14
EJ
1800 }
1801
46c88433 1802 if (xport->is_tunnel) {
9583bc14
EJ
1803 /* Save tunnel metadata so that changes made due to
1804 * the Logical (tunnel) Port are not visible for any further
1805 * matches, while explicit set actions on tunnel metadata are.
1806 */
33bf9176 1807 struct flow_tnl flow_tnl = flow->tunnel;
46c88433 1808 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
4e022ec0 1809 if (odp_port == ODPP_NONE) {
9583bc14
EJ
1810 xlate_report(ctx, "Tunneling decided against output");
1811 goto out; /* restore flow_nw_tos */
1812 }
33bf9176 1813 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
9583bc14
EJ
1814 xlate_report(ctx, "Not tunneling to our own address");
1815 goto out; /* restore flow_nw_tos */
1816 }
1817 if (ctx->xin->resubmit_stats) {
46c88433 1818 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14
EJ
1819 }
1820 out_port = odp_port;
33bf9176 1821 commit_odp_tunnel_action(flow, &ctx->base_flow,
9583bc14 1822 &ctx->xout->odp_actions);
33bf9176 1823 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
9583bc14 1824 } else {
46c88433 1825 odp_port = xport->odp_port;
7614e5d0 1826 out_port = odp_port;
46c88433 1827 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
7614e5d0
JR
1828 ofp_port_t vlandev_port;
1829
1dd35f8a 1830 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
7614e5d0
JR
1831 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
1832 ofp_port, flow->vlan_tci);
1833 if (vlandev_port != ofp_port) {
1834 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1835 flow->vlan_tci = htons(0);
1836 }
9583bc14 1837 }
9583bc14 1838 }
9583bc14 1839
4e022ec0 1840 if (out_port != ODPP_NONE) {
7fd91025
BP
1841 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1842 &ctx->xout->odp_actions,
8bfd0fda 1843 &ctx->xout->wc);
adcf00ba
AZ
1844
1845 if (ctx->xout->use_recirc) {
1846 struct ovs_action_recirc *act_recirc;
1847 struct xlate_recirc *xr = &ctx->xout->recirc;
1848
1849 act_recirc = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
1850 OVS_ACTION_ATTR_RECIRC, sizeof *act_recirc);
1851 act_recirc->recirc_id = xr->recirc_id;
1852 act_recirc->hash_alg = xr->hash_alg;
1853 act_recirc->hash_bias = xr->hash_bias;
1854 } else {
1855 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1856 out_port);
1857 }
9583bc14 1858
6cbbf4fa
EJ
1859 ctx->sflow_odp_port = odp_port;
1860 ctx->sflow_n_outputs++;
1861 ctx->xout->nf_output_iface = ofp_port;
1862 }
1863
1864 out:
9583bc14 1865 /* Restore flow */
33bf9176 1866 flow->vlan_tci = flow_vlan_tci;
1362e248 1867 flow->pkt_mark = flow_pkt_mark;
33bf9176 1868 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
1869}
1870
1871static void
4e022ec0 1872compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
9583bc14
EJ
1873{
1874 compose_output_action__(ctx, ofp_port, true);
1875}
1876
bb61b33d
BP
1877static void
1878xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
1879{
1880 struct rule_dpif *old_rule = ctx->rule;
6f00e29b 1881 struct rule_actions *actions;
bb61b33d
BP
1882
1883 if (ctx->xin->resubmit_stats) {
70742c7f 1884 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
1885 }
1886
98b07853 1887 ctx->resubmits++;
bb61b33d
BP
1888 ctx->recurse++;
1889 ctx->rule = rule;
6f00e29b
BP
1890 actions = rule_dpif_get_actions(rule);
1891 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
bb61b33d
BP
1892 ctx->rule = old_rule;
1893 ctx->recurse--;
bb61b33d
BP
1894}
1895
bd3240ba
SH
1896static bool
1897xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 1898{
98b07853
BP
1899 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1900
adcf00ba 1901 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
98b07853
BP
1902 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1903 MAX_RESUBMIT_RECURSION);
adcf00ba 1904 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
98b07853 1905 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1f317cb5 1906 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
98b07853 1907 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1f317cb5 1908 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
98b07853
BP
1909 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1910 } else {
bd3240ba
SH
1911 return true;
1912 }
1913
1914 return false;
1915}
1916
1917static void
6d328fa2
SH
1918xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
1919 bool may_packet_in, bool honor_table_miss)
bd3240ba
SH
1920{
1921 if (xlate_resubmit_resource_check(ctx)) {
4e022ec0 1922 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
3f207910 1923 bool skip_wildcards = ctx->xin->skip_wildcards;
9583bc14 1924 uint8_t old_table_id = ctx->table_id;
3f207910 1925 struct rule_dpif *rule;
6d328fa2
SH
1926 enum rule_dpif_lookup_verdict verdict;
1927 enum ofputil_port_config config = 0;
9583bc14
EJ
1928
1929 ctx->table_id = table_id;
1930
bb61b33d
BP
1931 /* Look up a flow with 'in_port' as the input port. Then restore the
1932 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1933 * have surprising behavior). */
4e022ec0 1934 ctx->xin->flow.in_port.ofp_port = in_port;
6d328fa2
SH
1935 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
1936 &ctx->xin->flow,
1937 !skip_wildcards
1938 ? &ctx->xout->wc : NULL,
1939 honor_table_miss,
1940 &ctx->table_id, &rule);
4e022ec0 1941 ctx->xin->flow.in_port.ofp_port = old_in_port;
9583bc14 1942
ad3efdcb
EJ
1943 if (ctx->xin->resubmit_hook) {
1944 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1945 }
1946
6d328fa2
SH
1947 switch (verdict) {
1948 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
1949 goto match;
1950 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
1951 if (may_packet_in) {
1952 struct xport *xport;
1953
1954 xport = get_ofp_port(ctx->xbridge,
1955 ctx->xin->flow.in_port.ofp_port);
1956 config = xport ? xport->config : 0;
1957 break;
1958 }
1959 /* Fall through to drop */
1960 case RULE_DPIF_LOOKUP_VERDICT_DROP:
1961 config = OFPUTIL_PC_NO_PACKET_IN;
1962 break;
6b83a3c5
SH
1963 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
1964 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
1965 config = OFPUTIL_PC_NO_PACKET_IN;
1966 }
1967 break;
6d328fa2
SH
1968 default:
1969 OVS_NOT_REACHED();
a2143702 1970 }
6d328fa2
SH
1971
1972 choose_miss_rule(config, ctx->xbridge->miss_rule,
1973 ctx->xbridge->no_packet_in_rule, &rule);
1974
1975match:
a2143702 1976 if (rule) {
bb61b33d 1977 xlate_recursively(ctx, rule);
a2143702 1978 rule_dpif_unref(rule);
ad3efdcb
EJ
1979 }
1980
9583bc14 1981 ctx->table_id = old_table_id;
98b07853 1982 return;
9583bc14 1983 }
98b07853
BP
1984
1985 ctx->exit = true;
9583bc14
EJ
1986}
1987
f4fb341b
SH
1988static void
1989xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1990{
1991 uint64_t action_list_stub[1024 / 8];
1992 struct ofpbuf action_list, action_set;
1993
1994 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1995 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1996
1997 ofpacts_execute_action_set(&action_list, &action_set);
1998 ctx->recurse++;
1f317cb5 1999 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
f4fb341b
SH
2000 ctx->recurse--;
2001
2002 ofpbuf_uninit(&action_set);
2003 ofpbuf_uninit(&action_list);
2004}
2005
2006static void
2007xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2008{
2009 const struct ofputil_bucket *bucket;
2010 const struct list *buckets;
2011 struct flow old_flow = ctx->xin->flow;
2012
2013 group_dpif_get_buckets(group, &buckets);
2014
2015 LIST_FOR_EACH (bucket, list_node, buckets) {
2016 xlate_group_bucket(ctx, bucket);
2017 /* Roll back flow to previous state.
2018 * This is equivalent to cloning the packet for each bucket.
2019 *
2020 * As a side effect any subsequently applied actions will
2021 * also effectively be applied to a clone of the packet taken
2022 * just before applying the all or indirect group. */
2023 ctx->xin->flow = old_flow;
2024 }
2025}
2026
dd8cd4b4
SH
2027static void
2028xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2029{
2030 const struct ofputil_bucket *bucket;
2031
2032 bucket = group_first_live_bucket(ctx, group, 0);
2033 if (bucket) {
2034 xlate_group_bucket(ctx, bucket);
2035 }
2036}
2037
fe7e5749
SH
2038static void
2039xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2040{
2041 struct flow_wildcards *wc = &ctx->xout->wc;
2042 const struct ofputil_bucket *bucket;
2043 uint32_t basis;
2044
7e36ac42 2045 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
fe7e5749
SH
2046 bucket = group_best_live_bucket(ctx, group, basis);
2047 if (bucket) {
2048 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2049 xlate_group_bucket(ctx, bucket);
2050 }
2051}
2052
f4fb341b
SH
2053static void
2054xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2055{
5a070238
BP
2056 ctx->in_group = true;
2057
f4fb341b
SH
2058 switch (group_dpif_get_type(group)) {
2059 case OFPGT11_ALL:
2060 case OFPGT11_INDIRECT:
2061 xlate_all_group(ctx, group);
2062 break;
2063 case OFPGT11_SELECT:
fe7e5749 2064 xlate_select_group(ctx, group);
f4fb341b 2065 break;
dd8cd4b4
SH
2066 case OFPGT11_FF:
2067 xlate_ff_group(ctx, group);
2068 break;
f4fb341b 2069 default:
428b2edd 2070 OVS_NOT_REACHED();
f4fb341b
SH
2071 }
2072 group_dpif_release(group);
5a070238
BP
2073
2074 ctx->in_group = false;
2075}
2076
2077static bool
2078xlate_group_resource_check(struct xlate_ctx *ctx)
2079{
2080 if (!xlate_resubmit_resource_check(ctx)) {
2081 return false;
2082 } else if (ctx->in_group) {
2083 /* Prevent nested translation of OpenFlow groups.
2084 *
2085 * OpenFlow allows this restriction. We enforce this restriction only
2086 * because, with the current architecture, we would otherwise have to
2087 * take a possibly recursive read lock on the ofgroup rwlock, which is
2088 * unsafe given that POSIX allows taking a read lock to block if there
2089 * is a thread blocked on taking the write lock. Other solutions
2090 * without this restriction are also possible, but seem unwarranted
2091 * given the current limited use of groups. */
2092 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2093
2094 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2095 return false;
2096 } else {
2097 return true;
2098 }
f4fb341b
SH
2099}
2100
2101static bool
2102xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2103{
5a070238 2104 if (xlate_group_resource_check(ctx)) {
f4fb341b
SH
2105 struct group_dpif *group;
2106 bool got_group;
2107
2108 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2109 if (got_group) {
2110 xlate_group_action__(ctx, group);
2111 } else {
2112 return true;
2113 }
2114 }
2115
2116 return false;
2117}
2118
9583bc14
EJ
2119static void
2120xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2121 const struct ofpact_resubmit *resubmit)
2122{
4e022ec0 2123 ofp_port_t in_port;
9583bc14 2124 uint8_t table_id;
adcf00ba
AZ
2125 bool may_packet_in = false;
2126 bool honor_table_miss = false;
2127
2128 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2129 /* Still allow missed packets to be sent to the controller
2130 * if resubmitting from an internal table. */
2131 may_packet_in = true;
2132 honor_table_miss = true;
2133 }
9583bc14
EJ
2134
2135 in_port = resubmit->in_port;
2136 if (in_port == OFPP_IN_PORT) {
4e022ec0 2137 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
2138 }
2139
2140 table_id = resubmit->table_id;
2141 if (table_id == 255) {
2142 table_id = ctx->table_id;
2143 }
2144
adcf00ba
AZ
2145 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2146 honor_table_miss);
9583bc14
EJ
2147}
2148
2149static void
2150flood_packets(struct xlate_ctx *ctx, bool all)
2151{
46c88433 2152 const struct xport *xport;
9583bc14 2153
46c88433
EJ
2154 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2155 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2156 continue;
2157 }
2158
2159 if (all) {
46c88433
EJ
2160 compose_output_action__(ctx, xport->ofp_port, false);
2161 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2162 compose_output_action(ctx, xport->ofp_port);
9583bc14
EJ
2163 }
2164 }
2165
2166 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2167}
2168
2169static void
2170execute_controller_action(struct xlate_ctx *ctx, int len,
2171 enum ofp_packet_in_reason reason,
2172 uint16_t controller_id)
2173{
0fb7792a 2174 struct ofproto_packet_in *pin;
9583bc14 2175 struct ofpbuf *packet;
758c456d 2176 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
9583bc14 2177
04594cd5 2178 ctx->xout->slow |= SLOW_CONTROLLER;
9583bc14
EJ
2179 if (!ctx->xin->packet) {
2180 return;
2181 }
2182
2183 packet = ofpbuf_clone(ctx->xin->packet);
2184
7fd91025
BP
2185 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2186 &ctx->xout->odp_actions,
8bfd0fda 2187 &ctx->xout->wc);
9583bc14 2188
1f317cb5
PS
2189 odp_execute_actions(NULL, packet, false, &md,
2190 ofpbuf_data(&ctx->xout->odp_actions),
2191 ofpbuf_size(&ctx->xout->odp_actions), NULL);
9583bc14 2192
ada3a58d 2193 pin = xmalloc(sizeof *pin);
1f317cb5 2194 pin->up.packet_len = ofpbuf_size(packet);
0fb7792a
BP
2195 pin->up.packet = ofpbuf_steal_data(packet);
2196 pin->up.reason = reason;
0fb7792a 2197 pin->up.table_id = ctx->table_id;
d4fa4e79
BP
2198 pin->up.cookie = (ctx->rule
2199 ? rule_dpif_get_flow_cookie(ctx->rule)
2200 : OVS_BE64_MAX);
0fb7792a 2201
0fb7792a 2202 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
9583bc14 2203
f11c7538 2204 pin->controller_id = controller_id;
d38a3c7b 2205 pin->send_len = len;
32260212
SH
2206 /* If a rule is a table-miss rule then this is
2207 * a table-miss handled by a table-miss rule.
2208 *
2209 * Else, if rule is internal and has a controller action,
2210 * the later being implied by the rule being processed here,
2211 * then this is a table-miss handled without a table-miss rule.
2212 *
2213 * Otherwise this is not a table-miss. */
2214 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2215 if (ctx->rule) {
2216 if (rule_dpif_is_table_miss(ctx->rule)) {
2217 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2218 } else if (rule_dpif_is_internal(ctx->rule)) {
2219 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2220 }
2221 }
ada3a58d 2222 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
9583bc14
EJ
2223 ofpbuf_delete(packet);
2224}
2225
8bfd0fda
BP
2226static void
2227compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 2228{
33bf9176
BP
2229 struct flow_wildcards *wc = &ctx->xout->wc;
2230 struct flow *flow = &ctx->xin->flow;
8bfd0fda 2231 int n;
33bf9176 2232
8bfd0fda 2233 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 2234
8bfd0fda
BP
2235 n = flow_count_mpls_labels(flow, wc);
2236 if (!n) {
8bfd0fda
BP
2237 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2238 &ctx->xout->odp_actions,
2239 &ctx->xout->wc);
2240 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2241 if (ctx->xin->packet != NULL) {
2242 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2243 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2244 "MPLS push action can't be performed as it would "
2245 "have more MPLS LSEs than the %d supported.",
2246 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
9583bc14 2247 }
8bfd0fda
BP
2248 ctx->exit = true;
2249 return;
2250 } else if (n >= ctx->xbridge->max_mpls_depth) {
2251 COVERAGE_INC(xlate_actions_mpls_overflow);
2252 ctx->xout->slow |= SLOW_ACTION;
9583bc14 2253 }
b0a17866 2254
8bfd0fda 2255 flow_push_mpls(flow, n, mpls->ethertype, wc);
9583bc14
EJ
2256}
2257
8bfd0fda 2258static void
9cfef3d0 2259compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 2260{
33bf9176 2261 struct flow_wildcards *wc = &ctx->xout->wc;
8bfd0fda
BP
2262 struct flow *flow = &ctx->xin->flow;
2263 int n = flow_count_mpls_labels(flow, wc);
33bf9176 2264
8bfd0fda
BP
2265 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2266 if (ctx->xin->packet != NULL) {
2267 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2268 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2269 "MPLS pop action can't be performed as it has "
2270 "more MPLS LSEs than the %d supported.",
2271 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2272 }
2273 ctx->exit = true;
2274 ofpbuf_clear(&ctx->xout->odp_actions);
9583bc14
EJ
2275 }
2276}
2277
2278static bool
2279compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2280{
33bf9176
BP
2281 struct flow *flow = &ctx->xin->flow;
2282
2283 if (!is_ip_any(flow)) {
9583bc14
EJ
2284 return false;
2285 }
2286
1dd35f8a 2287 ctx->xout->wc.masks.nw_ttl = 0xff;
33bf9176
BP
2288 if (flow->nw_ttl > 1) {
2289 flow->nw_ttl--;
9583bc14
EJ
2290 return false;
2291 } else {
2292 size_t i;
2293
2294 for (i = 0; i < ids->n_controllers; i++) {
2295 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2296 ids->cnt_ids[i]);
2297 }
2298
2299 /* Stop processing for current table. */
2300 return true;
2301 }
2302}
2303
8bfd0fda 2304static void
097d4939
JR
2305compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2306{
8bfd0fda
BP
2307 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2308 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2309 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 2310 }
097d4939
JR
2311}
2312
8bfd0fda 2313static void
097d4939
JR
2314compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2315{
8bfd0fda
BP
2316 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2317 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2318 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 2319 }
097d4939
JR
2320}
2321
8bfd0fda 2322static void
9cfef3d0 2323compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 2324{
8bfd0fda
BP
2325 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2326 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2327 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 2328 }
9583bc14
EJ
2329}
2330
2331static bool
9cfef3d0 2332compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 2333{
33bf9176 2334 struct flow *flow = &ctx->xin->flow;
8bfd0fda 2335 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
1dd35f8a
JP
2336 struct flow_wildcards *wc = &ctx->xout->wc;
2337
1dd35f8a 2338 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
8bfd0fda
BP
2339 if (eth_type_mpls(flow->dl_type)) {
2340 if (ttl > 1) {
2341 ttl--;
2342 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2343 return false;
2344 } else {
2345 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
9583bc14 2346
8bfd0fda
BP
2347 /* Stop processing for current table. */
2348 return true;
2349 }
9583bc14 2350 } else {
9583bc14
EJ
2351 return true;
2352 }
2353}
2354
2355static void
2356xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 2357 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 2358{
4e022ec0 2359 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
9583bc14
EJ
2360
2361 ctx->xout->nf_output_iface = NF_OUT_DROP;
2362
2363 switch (port) {
2364 case OFPP_IN_PORT:
4e022ec0 2365 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
9583bc14
EJ
2366 break;
2367 case OFPP_TABLE:
4e022ec0 2368 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 2369 0, may_packet_in, true);
9583bc14
EJ
2370 break;
2371 case OFPP_NORMAL:
2372 xlate_normal(ctx);
2373 break;
2374 case OFPP_FLOOD:
2375 flood_packets(ctx, false);
2376 break;
2377 case OFPP_ALL:
2378 flood_packets(ctx, true);
2379 break;
2380 case OFPP_CONTROLLER:
2381 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2382 break;
2383 case OFPP_NONE:
2384 break;
2385 case OFPP_LOCAL:
2386 default:
4e022ec0 2387 if (port != ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2388 compose_output_action(ctx, port);
2389 } else {
2390 xlate_report(ctx, "skipping output to input port");
2391 }
2392 break;
2393 }
2394
2395 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2396 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2397 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2398 ctx->xout->nf_output_iface = prev_nf_output_iface;
2399 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2400 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2401 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2402 }
2403}
2404
2405static void
2406xlate_output_reg_action(struct xlate_ctx *ctx,
2407 const struct ofpact_output_reg *or)
2408{
2409 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2410 if (port <= UINT16_MAX) {
2411 union mf_subvalue value;
2412
2413 memset(&value, 0xff, sizeof value);
2414 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
4e022ec0
AW
2415 xlate_output_action(ctx, u16_to_ofp(port),
2416 or->max_len, false);
9583bc14
EJ
2417 }
2418}
2419
2420static void
2421xlate_enqueue_action(struct xlate_ctx *ctx,
2422 const struct ofpact_enqueue *enqueue)
2423{
4e022ec0 2424 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
2425 uint32_t queue_id = enqueue->queue;
2426 uint32_t flow_priority, priority;
2427 int error;
2428
2429 /* Translate queue to priority. */
89a8a7f0 2430 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
2431 if (error) {
2432 /* Fall back to ordinary output action. */
2433 xlate_output_action(ctx, enqueue->port, 0, false);
2434 return;
2435 }
2436
2437 /* Check output port. */
2438 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
2439 ofp_port = ctx->xin->flow.in_port.ofp_port;
2440 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2441 return;
2442 }
2443
2444 /* Add datapath actions. */
2445 flow_priority = ctx->xin->flow.skb_priority;
2446 ctx->xin->flow.skb_priority = priority;
2447 compose_output_action(ctx, ofp_port);
2448 ctx->xin->flow.skb_priority = flow_priority;
2449
2450 /* Update NetFlow output port. */
2451 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2452 ctx->xout->nf_output_iface = ofp_port;
2453 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2454 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2455 }
2456}
2457
2458static void
2459xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2460{
2461 uint32_t skb_priority;
2462
89a8a7f0 2463 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
2464 ctx->xin->flow.skb_priority = skb_priority;
2465 } else {
2466 /* Couldn't translate queue to a priority. Nothing to do. A warning
2467 * has already been logged. */
2468 }
2469}
2470
2471static bool
46c88433 2472slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 2473{
46c88433
EJ
2474 const struct xbridge *xbridge = xbridge_;
2475 struct xport *port;
9583bc14
EJ
2476
2477 switch (ofp_port) {
2478 case OFPP_IN_PORT:
2479 case OFPP_TABLE:
2480 case OFPP_NORMAL:
2481 case OFPP_FLOOD:
2482 case OFPP_ALL:
2483 case OFPP_NONE:
2484 return true;
2485 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2486 return false;
2487 default:
46c88433 2488 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
2489 return port ? port->may_enable : false;
2490 }
2491}
2492
2493static void
2494xlate_bundle_action(struct xlate_ctx *ctx,
2495 const struct ofpact_bundle *bundle)
2496{
4e022ec0 2497 ofp_port_t port;
9583bc14
EJ
2498
2499 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
46c88433
EJ
2500 slave_enabled_cb,
2501 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 2502 if (bundle->dst.field) {
f74e7df7
JP
2503 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2504 &ctx->xout->wc);
9583bc14
EJ
2505 } else {
2506 xlate_output_action(ctx, port, 0, false);
2507 }
2508}
2509
2510static void
2511xlate_learn_action(struct xlate_ctx *ctx,
2512 const struct ofpact_learn *learn)
2513{
1b63b91e
BP
2514 uint64_t ofpacts_stub[1024 / 8];
2515 struct ofputil_flow_mod fm;
9583bc14 2516 struct ofpbuf ofpacts;
9583bc14
EJ
2517
2518 ctx->xout->has_learn = true;
2519
2520 learn_mask(learn, &ctx->xout->wc);
2521
2522 if (!ctx->xin->may_learn) {
2523 return;
2524 }
2525
1b63b91e
BP
2526 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2527 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2528 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2529 ofpbuf_uninit(&ofpacts);
9583bc14
EJ
2530}
2531
9583bc14
EJ
2532static void
2533xlate_fin_timeout(struct xlate_ctx *ctx,
2534 const struct ofpact_fin_timeout *oft)
2535{
2536 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
70742c7f
EJ
2537 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2538 oft->fin_hard_timeout);
9583bc14
EJ
2539 }
2540}
2541
2542static void
2543xlate_sample_action(struct xlate_ctx *ctx,
2544 const struct ofpact_sample *os)
2545{
2546 union user_action_cookie cookie;
2547 /* Scale the probability from 16-bit to 32-bit while representing
2548 * the same percentage. */
2549 uint32_t probability = (os->probability << 16) | os->probability;
2550
4b97b70d
BP
2551 if (!ctx->xbridge->variable_length_userdata) {
2552 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2553
2554 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2555 "lacks support (needs Linux 3.10+ or kernel module from "
2556 "OVS 1.11+)");
2557 return;
2558 }
2559
7fd91025
BP
2560 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2561 &ctx->xout->odp_actions,
8bfd0fda 2562 &ctx->xout->wc);
9583bc14
EJ
2563
2564 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2565 os->obs_domain_id, os->obs_point_id, &cookie);
46c88433 2566 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
9583bc14
EJ
2567 probability, &cookie, sizeof cookie.flow_sample);
2568}
2569
2570static bool
46c88433 2571may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 2572{
46c88433
EJ
2573 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2574 ? OFPUTIL_PC_NO_RECV_STP
2575 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
2576 return false;
2577 }
2578
2579 /* Only drop packets here if both forwarding and learning are
2580 * disabled. If just learning is enabled, we need to have
2581 * OFPP_NORMAL and the learning action have a look at the packet
2582 * before we can drop it. */
9d189a50 2583 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
9583bc14
EJ
2584 return false;
2585 }
2586
2587 return true;
2588}
2589
7fdb60a7
SH
2590static void
2591xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2592{
2593 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2594 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2595 ofpact_pad(&ctx->action_set);
2596}
2597
2598static void
2599xlate_action_set(struct xlate_ctx *ctx)
2600{
2601 uint64_t action_list_stub[1024 / 64];
2602 struct ofpbuf action_list;
2603
2604 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2605 ofpacts_execute_action_set(&action_list, &ctx->action_set);
1f317cb5 2606 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
7fdb60a7
SH
2607 ofpbuf_uninit(&action_list);
2608}
2609
9583bc14
EJ
2610static void
2611do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2612 struct xlate_ctx *ctx)
2613{
33bf9176
BP
2614 struct flow_wildcards *wc = &ctx->xout->wc;
2615 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
2616 const struct ofpact *a;
2617
f47ea021
JR
2618 /* dl_type already in the mask, not set below. */
2619
9583bc14
EJ
2620 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2621 struct ofpact_controller *controller;
2622 const struct ofpact_metadata *metadata;
b2dd70be
JR
2623 const struct ofpact_set_field *set_field;
2624 const struct mf_field *mf;
9583bc14
EJ
2625
2626 if (ctx->exit) {
2627 break;
2628 }
2629
2630 switch (a->type) {
2631 case OFPACT_OUTPUT:
2632 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2633 ofpact_get_OUTPUT(a)->max_len, true);
2634 break;
2635
7395c052 2636 case OFPACT_GROUP:
f4fb341b
SH
2637 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2638 return;
2639 }
7395c052
NZ
2640 break;
2641
9583bc14
EJ
2642 case OFPACT_CONTROLLER:
2643 controller = ofpact_get_CONTROLLER(a);
2644 execute_controller_action(ctx, controller->max_len,
2645 controller->reason,
2646 controller->controller_id);
2647 break;
2648
2649 case OFPACT_ENQUEUE:
2650 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2651 break;
2652
2653 case OFPACT_SET_VLAN_VID:
f74e7df7 2654 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
ca287d20
JR
2655 if (flow->vlan_tci & htons(VLAN_CFI) ||
2656 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2657 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2658 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2659 | htons(VLAN_CFI));
2660 }
9583bc14
EJ
2661 break;
2662
2663 case OFPACT_SET_VLAN_PCP:
f74e7df7 2664 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
ca287d20
JR
2665 if (flow->vlan_tci & htons(VLAN_CFI) ||
2666 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2667 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2668 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2669 << VLAN_PCP_SHIFT) | VLAN_CFI);
2670 }
9583bc14
EJ
2671 break;
2672
2673 case OFPACT_STRIP_VLAN:
f74e7df7 2674 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 2675 flow->vlan_tci = htons(0);
9583bc14
EJ
2676 break;
2677
2678 case OFPACT_PUSH_VLAN:
2679 /* XXX 802.1AD(QinQ) */
f74e7df7 2680 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 2681 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
2682 break;
2683
2684 case OFPACT_SET_ETH_SRC:
f74e7df7 2685 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
33bf9176 2686 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
2687 break;
2688
2689 case OFPACT_SET_ETH_DST:
f74e7df7 2690 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
33bf9176 2691 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
2692 break;
2693
2694 case OFPACT_SET_IPV4_SRC:
33bf9176 2695 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 2696 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 2697 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
2698 }
2699 break;
2700
2701 case OFPACT_SET_IPV4_DST:
33bf9176 2702 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 2703 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 2704 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
2705 }
2706 break;
2707
04f01c24
BP
2708 case OFPACT_SET_IP_DSCP:
2709 if (is_ip_any(flow)) {
f47ea021 2710 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 2711 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 2712 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
2713 }
2714 break;
2715
ff14eb7a
JR
2716 case OFPACT_SET_IP_ECN:
2717 if (is_ip_any(flow)) {
2718 wc->masks.nw_tos |= IP_ECN_MASK;
2719 flow->nw_tos &= ~IP_ECN_MASK;
2720 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2721 }
2722 break;
2723
0c20dbe4
JR
2724 case OFPACT_SET_IP_TTL:
2725 if (is_ip_any(flow)) {
2726 wc->masks.nw_ttl = 0xff;
2727 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2728 }
2729 break;
2730
9583bc14 2731 case OFPACT_SET_L4_SRC_PORT:
33bf9176 2732 if (is_ip_any(flow)) {
f47ea021
JR
2733 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2734 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 2735 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
2736 }
2737 break;
2738
2739 case OFPACT_SET_L4_DST_PORT:
33bf9176 2740 if (is_ip_any(flow)) {
f47ea021
JR
2741 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2742 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 2743 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
2744 }
2745 break;
2746
2747 case OFPACT_RESUBMIT:
2748 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2749 break;
2750
2751 case OFPACT_SET_TUNNEL:
33bf9176 2752 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
2753 break;
2754
2755 case OFPACT_SET_QUEUE:
2756 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2757 break;
2758
2759 case OFPACT_POP_QUEUE:
33bf9176 2760 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
2761 break;
2762
2763 case OFPACT_REG_MOVE:
33bf9176 2764 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
2765 break;
2766
2767 case OFPACT_REG_LOAD:
f47ea021 2768 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
9583bc14
EJ
2769 break;
2770
b2dd70be
JR
2771 case OFPACT_SET_FIELD:
2772 set_field = ofpact_get_SET_FIELD(a);
2773 mf = set_field->field;
2774 mf_mask_field_and_prereqs(mf, &wc->masks);
2775
2776 /* Set field action only ever overwrites packet's outermost
2777 * applicable header fields. Do nothing if no header exists. */
2778 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2779 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
f8b63eed 2780 || eth_type_mpls(flow->dl_type))) {
b2dd70be
JR
2781 mf_set_flow_value(mf, &set_field->value, flow);
2782 }
2783 break;
2784
9583bc14 2785 case OFPACT_STACK_PUSH:
33bf9176
BP
2786 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2787 &ctx->stack);
9583bc14
EJ
2788 break;
2789
2790 case OFPACT_STACK_POP:
f74e7df7
JP
2791 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2792 &ctx->stack);
9583bc14
EJ
2793 break;
2794
2795 case OFPACT_PUSH_MPLS:
8bfd0fda 2796 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
2797 break;
2798
2799 case OFPACT_POP_MPLS:
8bfd0fda 2800 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
2801 break;
2802
097d4939 2803 case OFPACT_SET_MPLS_LABEL:
8bfd0fda
BP
2804 compose_set_mpls_label_action(
2805 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
2806 break;
097d4939
JR
2807
2808 case OFPACT_SET_MPLS_TC:
8bfd0fda 2809 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
2810 break;
2811
9583bc14 2812 case OFPACT_SET_MPLS_TTL:
8bfd0fda 2813 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
2814 break;
2815
2816 case OFPACT_DEC_MPLS_TTL:
9cfef3d0 2817 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 2818 return;
9583bc14
EJ
2819 }
2820 break;
2821
2822 case OFPACT_DEC_TTL:
f74e7df7 2823 wc->masks.nw_ttl = 0xff;
9583bc14 2824 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 2825 return;
9583bc14
EJ
2826 }
2827 break;
2828
2829 case OFPACT_NOTE:
2830 /* Nothing to do. */
2831 break;
2832
2833 case OFPACT_MULTIPATH:
33bf9176 2834 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
2835 break;
2836
2837 case OFPACT_BUNDLE:
9583bc14
EJ
2838 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2839 break;
2840
2841 case OFPACT_OUTPUT_REG:
2842 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2843 break;
2844
2845 case OFPACT_LEARN:
2846 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2847 break;
2848
2849 case OFPACT_EXIT:
2850 ctx->exit = true;
2851 break;
2852
2853 case OFPACT_FIN_TIMEOUT:
33bf9176 2854 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
2855 ctx->xout->has_fin_timeout = true;
2856 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2857 break;
2858
2859 case OFPACT_CLEAR_ACTIONS:
7fdb60a7
SH
2860 ofpbuf_clear(&ctx->action_set);
2861 break;
2862
2863 case OFPACT_WRITE_ACTIONS:
2864 xlate_write_actions(ctx, a);
9583bc14
EJ
2865 break;
2866
2867 case OFPACT_WRITE_METADATA:
2868 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
2869 flow->metadata &= ~metadata->mask;
2870 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
2871 break;
2872
638a19b0
JR
2873 case OFPACT_METER:
2874 /* Not implemented yet. */
2875 break;
2876
9583bc14 2877 case OFPACT_GOTO_TABLE: {
9583bc14 2878 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14
EJ
2879
2880 ovs_assert(ctx->table_id < ogt->table_id);
4468099e 2881 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 2882 ogt->table_id, true, true);
9583bc14
EJ
2883 break;
2884 }
2885
2886 case OFPACT_SAMPLE:
2887 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2888 break;
2889 }
2890 }
9583bc14
EJ
2891}
2892
2893void
2894xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2895 const struct flow *flow, struct rule_dpif *rule,
a66733a8 2896 uint16_t tcp_flags, const struct ofpbuf *packet)
9583bc14
EJ
2897{
2898 xin->ofproto = ofproto;
2899 xin->flow = *flow;
2900 xin->packet = packet;
2901 xin->may_learn = packet != NULL;
2902 xin->rule = rule;
2903 xin->ofpacts = NULL;
2904 xin->ofpacts_len = 0;
2905 xin->tcp_flags = tcp_flags;
2906 xin->resubmit_hook = NULL;
2907 xin->report_hook = NULL;
2908 xin->resubmit_stats = NULL;
3f207910 2909 xin->skip_wildcards = false;
9583bc14
EJ
2910}
2911
2912void
2913xlate_out_uninit(struct xlate_out *xout)
2914{
2915 if (xout) {
2916 ofpbuf_uninit(&xout->odp_actions);
2917 }
2918}
2919
2920/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2921 * into datapath actions, using 'ctx', and discards the datapath actions. */
2922void
2923xlate_actions_for_side_effects(struct xlate_in *xin)
2924{
2925 struct xlate_out xout;
2926
2927 xlate_actions(xin, &xout);
2928 xlate_out_uninit(&xout);
2929}
2930
2931static void
2932xlate_report(struct xlate_ctx *ctx, const char *s)
2933{
2934 if (ctx->xin->report_hook) {
4d0acc70 2935 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
9583bc14
EJ
2936 }
2937}
2938
2939void
2940xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2941{
2942 dst->wc = src->wc;
9583bc14
EJ
2943 dst->slow = src->slow;
2944 dst->has_learn = src->has_learn;
2945 dst->has_normal = src->has_normal;
2946 dst->has_fin_timeout = src->has_fin_timeout;
2947 dst->nf_output_iface = src->nf_output_iface;
2948 dst->mirrors = src->mirrors;
2949
2950 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2951 sizeof dst->odp_actions_stub);
1f317cb5
PS
2952 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
2953 ofpbuf_size(&src->odp_actions));
9583bc14
EJ
2954}
2955\f
55954f6e
EJ
2956static struct skb_priority_to_dscp *
2957get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2958{
2959 struct skb_priority_to_dscp *pdscp;
2960 uint32_t hash;
2961
2962 hash = hash_int(skb_priority, 0);
2963 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2964 if (pdscp->skb_priority == skb_priority) {
2965 return pdscp;
2966 }
2967 }
2968 return NULL;
2969}
2970
2971static bool
2972dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2973 uint8_t *dscp)
2974{
2975 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2976 *dscp = pdscp ? pdscp->dscp : 0;
2977 return pdscp != NULL;
2978}
2979
2980static void
2981clear_skb_priorities(struct xport *xport)
2982{
2983 struct skb_priority_to_dscp *pdscp, *next;
2984
2985 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2986 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2987 free(pdscp);
2988 }
2989}
2990
ce4a6b76
BP
2991static bool
2992actions_output_to_local_port(const struct xlate_ctx *ctx)
2993{
46c88433 2994 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
2995 const struct nlattr *a;
2996 unsigned int left;
2997
1f317cb5
PS
2998 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
2999 ofpbuf_size(&ctx->xout->odp_actions)) {
ce4a6b76
BP
3000 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3001 && nl_attr_get_odp_port(a) == local_odp_port) {
3002 return true;
3003 }
3004 }
3005 return false;
3006}
9583bc14 3007
91d6cd12
AW
3008/* Thread safe call to xlate_actions__(). */
3009void
3010xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
6a4d3ab5 3011 OVS_EXCLUDED(xlate_rwlock)
91d6cd12
AW
3012{
3013 ovs_rwlock_rdlock(&xlate_rwlock);
3014 xlate_actions__(xin, xout);
3015 ovs_rwlock_unlock(&xlate_rwlock);
3016}
3017
9583bc14 3018/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
56450a41
BP
3019 * into datapath actions in 'odp_actions', using 'ctx'.
3020 *
3021 * The caller must take responsibility for eventually freeing 'xout', with
3022 * xlate_out_uninit(). */
91d6cd12
AW
3023static void
3024xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3025 OVS_REQ_RDLOCK(xlate_rwlock)
9583bc14 3026{
33bf9176
BP
3027 struct flow_wildcards *wc = &xout->wc;
3028 struct flow *flow = &xin->flow;
10c44245 3029 struct rule_dpif *rule = NULL;
33bf9176 3030
6f00e29b 3031 struct rule_actions *actions = NULL;
9583bc14
EJ
3032 enum slow_path_reason special;
3033 const struct ofpact *ofpacts;
46c88433 3034 struct xport *in_port;
9583bc14
EJ
3035 struct flow orig_flow;
3036 struct xlate_ctx ctx;
3037 size_t ofpacts_len;
62a7cc71 3038 bool tnl_may_send;
c56fac1b 3039 bool is_icmp;
9583bc14 3040
46c88433 3041 COVERAGE_INC(xlate_actions);
9583bc14
EJ
3042
3043 /* Flow initialization rules:
3044 * - 'base_flow' must match the kernel's view of the packet at the
3045 * time that action processing starts. 'flow' represents any
3046 * transformations we wish to make through actions.
3047 * - By default 'base_flow' and 'flow' are the same since the input
3048 * packet matches the output before any actions are applied.
3049 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3050 * of the received packet as seen by the kernel. If we later output
3051 * to another device without any modifications this will cause us to
3052 * insert a new tag since the original one was stripped off by the
3053 * VLAN device.
3054 * - Tunnel metadata as received is retained in 'flow'. This allows
3055 * tunnel metadata matching also in later tables.
3056 * Since a kernel action for setting the tunnel metadata will only be
3057 * generated with actual tunnel output, changing the tunnel metadata
3058 * values in 'flow' (such as tun_id) will only have effect with a later
3059 * tunnel output action.
3060 * - Tunnel 'base_flow' is completely cleared since that is what the
3061 * kernel does. If we wish to maintain the original values an action
3062 * needs to be generated. */
3063
3064 ctx.xin = xin;
3065 ctx.xout = xout;
46c88433
EJ
3066 ctx.xout->slow = 0;
3067 ctx.xout->has_learn = false;
3068 ctx.xout->has_normal = false;
3069 ctx.xout->has_fin_timeout = false;
3070 ctx.xout->nf_output_iface = NF_OUT_DROP;
3071 ctx.xout->mirrors = 0;
3072 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3073 sizeof ctx.xout->odp_actions_stub);
3074 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3075
3076 ctx.xbridge = xbridge_lookup(xin->ofproto);
3077 if (!ctx.xbridge) {
dc24a00f 3078 goto out;
46c88433 3079 }
9583bc14 3080
9583bc14
EJ
3081 ctx.rule = xin->rule;
3082
33bf9176 3083 ctx.base_flow = *flow;
9583bc14 3084 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
33bf9176 3085 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
9583bc14 3086
33bf9176
BP
3087 flow_wildcards_init_catchall(wc);
3088 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1dd35f8a 3089 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
7431e171 3090 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
0934ebba
BP
3091 if (is_ip_any(flow)) {
3092 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3093 }
c56fac1b 3094 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
9583bc14 3095
62a7cc71 3096 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
ce3955be 3097 if (ctx.xbridge->netflow) {
9b658910 3098 netflow_mask_wc(flow, wc);
9583bc14
EJ
3099 }
3100
9583bc14 3101 ctx.recurse = 0;
98b07853 3102 ctx.resubmits = 0;
5a070238 3103 ctx.in_group = false;
33bf9176 3104 ctx.orig_skb_priority = flow->skb_priority;
9583bc14
EJ
3105 ctx.table_id = 0;
3106 ctx.exit = false;
3107
10c44245 3108 if (!xin->ofpacts && !ctx.rule) {
6d328fa2
SH
3109 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3110 !xin->skip_wildcards ? wc : NULL,
3111 &rule);
10c44245
EJ
3112 if (ctx.xin->resubmit_stats) {
3113 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3114 }
3115 ctx.rule = rule;
3116 }
ee1afdd5 3117 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
adcf00ba 3118 xout->use_recirc = false;
10c44245 3119
9583bc14
EJ
3120 if (xin->ofpacts) {
3121 ofpacts = xin->ofpacts;
3122 ofpacts_len = xin->ofpacts_len;
10c44245
EJ
3123 } else if (ctx.rule) {
3124 actions = rule_dpif_get_actions(ctx.rule);
6f00e29b
BP
3125 ofpacts = actions->ofpacts;
3126 ofpacts_len = actions->ofpacts_len;
9583bc14 3127 } else {
428b2edd 3128 OVS_NOT_REACHED();
9583bc14
EJ
3129 }
3130
3131 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
7fdb60a7
SH
3132 ofpbuf_use_stub(&ctx.action_set,
3133 ctx.action_set_stub, sizeof ctx.action_set_stub);
9583bc14 3134
ade6ad9c 3135 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
3136 /* Do this conditionally because the copy is expensive enough that it
3137 * shows up in profiles. */
33bf9176 3138 orig_flow = *flow;
9583bc14
EJ
3139 }
3140
33bf9176 3141 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
46c88433 3142 switch (ctx.xbridge->frag) {
9583bc14
EJ
3143 case OFPC_FRAG_NORMAL:
3144 /* We must pretend that transport ports are unavailable. */
33bf9176
BP
3145 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3146 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
9583bc14
EJ
3147 break;
3148
3149 case OFPC_FRAG_DROP:
dc24a00f 3150 goto out;
9583bc14
EJ
3151
3152 case OFPC_FRAG_REASM:
428b2edd 3153 OVS_NOT_REACHED();
9583bc14
EJ
3154
3155 case OFPC_FRAG_NX_MATCH:
3156 /* Nothing to do. */
3157 break;
3158
3159 case OFPC_INVALID_TTL_TO_CONTROLLER:
428b2edd 3160 OVS_NOT_REACHED();
9583bc14
EJ
3161 }
3162 }
3163
46c88433 3164 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
d6fc5f57
EJ
3165 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3166 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3167 if (in_port->bfd) {
3168 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3169 }
3170 }
3171
642dc74d 3172 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
9583bc14 3173 if (special) {
04594cd5 3174 ctx.xout->slow |= special;
9583bc14 3175 } else {
9583bc14 3176 size_t sample_actions_len;
9583bc14 3177
4e022ec0 3178 if (flow->in_port.ofp_port
46c88433
EJ
3179 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3180 flow->in_port.ofp_port,
33bf9176 3181 flow->vlan_tci)) {
9583bc14
EJ
3182 ctx.base_flow.vlan_tci = 0;
3183 }
3184
3185 add_sflow_action(&ctx);
3186 add_ipfix_action(&ctx);
1f317cb5 3187 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
9583bc14 3188
62a7cc71 3189 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
9583bc14
EJ
3190 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3191
3192 /* We've let OFPP_NORMAL and the learning action look at the
3193 * packet, so drop it now if forwarding is disabled. */
9d189a50 3194 if (in_port && !xport_stp_forward_state(in_port)) {
1f317cb5 3195 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
9583bc14
EJ
3196 }
3197 }
3198
1f317cb5 3199 if (ofpbuf_size(&ctx.action_set)) {
7fdb60a7
SH
3200 xlate_action_set(&ctx);
3201 }
3202
46c88433 3203 if (ctx.xbridge->has_in_band
ce4a6b76
BP
3204 && in_band_must_output_to_local_port(flow)
3205 && !actions_output_to_local_port(&ctx)) {
9583bc14
EJ
3206 compose_output_action(&ctx, OFPP_LOCAL);
3207 }
aaa0fbae
EJ
3208
3209 fix_sflow_action(&ctx);
3210
46c88433 3211 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
3212 add_mirror_actions(&ctx, &orig_flow);
3213 }
9583bc14
EJ
3214 }
3215
1f317cb5 3216 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
542024c4 3217 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
3218 * can't hand them to the kernel directly. dpif_execute() can execute
3219 * them one by one with help, so just mark the result as SLOW_ACTION to
3220 * prevent the flow from being installed. */
3221 COVERAGE_INC(xlate_actions_oversize);
3222 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
3223 }
3224
d6fc5f57
EJ
3225 if (ctx.xin->resubmit_stats) {
3226 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3227 ctx.xin->resubmit_stats->n_packets,
3228 ctx.xin->resubmit_stats->n_bytes);
3229
3230 if (ctx.xbridge->netflow) {
3231 const struct ofpact *ofpacts;
3232 size_t ofpacts_len;
3233
3234 ofpacts_len = actions->ofpacts_len;
3235 ofpacts = actions->ofpacts;
3236 if (ofpacts_len == 0
3237 || ofpacts->type != OFPACT_CONTROLLER
3238 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3239 /* Only update netflow if we don't have controller flow. We don't
3240 * report NetFlow expiration messages for such facets because they
3241 * are just part of the control logic for the network, not real
3242 * traffic. */
3243 netflow_flow_update(ctx.xbridge->netflow, flow,
3244 xout->nf_output_iface,
3245 ctx.xin->resubmit_stats);
3246 }
3247 }
3248 }
3249
9583bc14 3250 ofpbuf_uninit(&ctx.stack);
7fdb60a7 3251 ofpbuf_uninit(&ctx.action_set);
9583bc14
EJ
3252
3253 /* Clear the metadata and register wildcard masks, because we won't
3254 * use non-header fields as part of the cache. */
c11c6faa 3255 flow_wildcards_clear_non_packet_fields(wc);
dc24a00f 3256
c56fac1b
BP
3257 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3258 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3259 * these fields. The datapath interface, on the other hand, represents
3260 * them with just 8 bits each. This means that if the high 8 bits of the
3261 * masks for these fields somehow become set, then they will get chopped
3262 * off by a round trip through the datapath, and revalidation will spot
3263 * that as an inconsistency and delete the flow. Avoid the problem here by
3264 * making sure that only the low 8 bits of either field can be unwildcarded
3265 * for ICMP.
3266 */
3267 if (is_icmp) {
3268 wc->masks.tp_src &= htons(UINT8_MAX);
3269 wc->masks.tp_dst &= htons(UINT8_MAX);
3270 }
3271
dc24a00f 3272out:
10c44245 3273 rule_dpif_unref(rule);
91d6cd12
AW
3274}
3275
3276/* Sends 'packet' out 'ofport'.
3277 * May modify 'packet'.
3278 * Returns 0 if successful, otherwise a positive errno value. */
3279int
3280xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3281{
91d6cd12 3282 struct xport *xport;
91d6cd12 3283 struct ofpact_output output;
91d6cd12 3284 struct flow flow;
91d6cd12 3285
91d6cd12
AW
3286 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3287 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
b5e7e61a
AZ
3288 flow_extract(packet, NULL, &flow);
3289 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12
AW
3290
3291 ovs_rwlock_rdlock(&xlate_rwlock);
3292 xport = xport_lookup(ofport);
3293 if (!xport) {
91d6cd12 3294 ovs_rwlock_unlock(&xlate_rwlock);
02ea2703 3295 return EINVAL;
91d6cd12 3296 }
91d6cd12
AW
3297 output.port = xport->ofp_port;
3298 output.max_len = 0;
dc24a00f 3299 ovs_rwlock_unlock(&xlate_rwlock);
e491a67a
YT
3300
3301 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3302 &output.ofpact, sizeof output,
3303 packet);
9583bc14 3304}