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