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