]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif-xlate.c
ofproto-dpif-xlate: Don't translate action set when dropping.
[ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
18080541 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 19#include <errno.h>
a36de779
PS
20#include <arpa/inet.h>
21#include <net/if.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
8449c4d6 24
a36de779 25#include "tnl-arp-cache.h"
db7d4e46 26#include "bfd.h"
9583bc14
EJ
27#include "bitmap.h"
28#include "bond.h"
29#include "bundle.h"
30#include "byte-order.h"
db7d4e46 31#include "cfm.h"
9583bc14
EJ
32#include "connmgr.h"
33#include "coverage.h"
e14deea0 34#include "dp-packet.h"
9583bc14
EJ
35#include "dpif.h"
36#include "dynamic-string.h"
f7f1ea29 37#include "in-band.h"
db7d4e46 38#include "lacp.h"
9583bc14 39#include "learn.h"
46c88433 40#include "list.h"
0477baa9 41#include "ovs-lldp.h"
9583bc14 42#include "mac-learning.h"
6d95c4e8 43#include "mcast-snooping.h"
9583bc14
EJ
44#include "meta-flow.h"
45#include "multipath.h"
46#include "netdev-vport.h"
47#include "netlink.h"
48#include "nx-match.h"
49#include "odp-execute.h"
50#include "ofp-actions.h"
51#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 52#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 53#include "ofproto/ofproto-dpif-monitor.h"
9583bc14
EJ
54#include "ofproto/ofproto-dpif-sflow.h"
55#include "ofproto/ofproto-dpif.h"
6f00e29b 56#include "ofproto/ofproto-provider.h"
a36de779
PS
57#include "ovs-router.h"
58#include "tnl-ports.h"
9583bc14 59#include "tunnel.h"
e6211adc 60#include "openvswitch/vlog.h"
9583bc14 61
46c88433 62COVERAGE_DEFINE(xlate_actions);
0f032e95 63COVERAGE_DEFINE(xlate_actions_oversize);
7d031d7e 64COVERAGE_DEFINE(xlate_actions_too_many_output);
9583bc14
EJ
65
66VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
67
8a553e9a
EJ
68/* Maximum depth of flow table recursion (due to resubmit actions) in a
69 * flow translation. */
70#define MAX_RESUBMIT_RECURSION 64
adcf00ba
AZ
71#define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
72 internal table. */
8a553e9a 73
7bbdd84f
SH
74/* Timeout for internal rules created to handle recirculation */
75#define RECIRC_TIMEOUT 60
76
98b07853
BP
77/* Maximum number of resubmit actions in a flow translation, whether they are
78 * recursive or not. */
79#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
80
46c88433
EJ
81struct xbridge {
82 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
83 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
84
ca6ba700 85 struct ovs_list xbundles; /* Owned xbundles. */
46c88433
EJ
86 struct hmap xports; /* Indexed by ofp_port. */
87
88 char *name; /* Name used in log messages. */
89a8a7f0 89 struct dpif *dpif; /* Datapath interface. */
46c88433 90 struct mac_learning *ml; /* Mac learning handle. */
6d95c4e8 91 struct mcast_snooping *ms; /* Multicast Snooping handle. */
46c88433
EJ
92 struct mbridge *mbridge; /* Mirroring. */
93 struct dpif_sflow *sflow; /* SFlow handle, or null. */
94 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 95 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 96 struct stp *stp; /* STP or null if disabled. */
9efd308e 97 struct rstp *rstp; /* RSTP or null if disabled. */
46c88433 98
46c88433
EJ
99 bool has_in_band; /* Bridge has in band control? */
100 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 101
adcf00ba
AZ
102 /* True if the datapath supports recirculation. */
103 bool enable_recirc;
104
4b97b70d
BP
105 /* True if the datapath supports variable-length
106 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
107 * False if the datapath supports only 8-byte (or shorter) userdata. */
108 bool variable_length_userdata;
8bfd0fda
BP
109
110 /* Number of MPLS label stack entries that the datapath supports
111 * in matches. */
112 size_t max_mpls_depth;
53477c2c
JR
113
114 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
115 * actions. */
116 bool masked_set_action;
46c88433
EJ
117};
118
119struct xbundle {
120 struct hmap_node hmap_node; /* In global 'xbundles' map. */
121 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
122
ca6ba700 123 struct ovs_list list_node; /* In parent 'xbridges' list. */
46c88433
EJ
124 struct xbridge *xbridge; /* Parent xbridge. */
125
ca6ba700 126 struct ovs_list xports; /* Contains "struct xport"s. */
46c88433
EJ
127
128 char *name; /* Name used in log messages. */
129 struct bond *bond; /* Nonnull iff more than one port. */
130 struct lacp *lacp; /* LACP handle or null. */
131
132 enum port_vlan_mode vlan_mode; /* VLAN mode. */
133 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
134 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
135 * NULL if all VLANs are trunked. */
136 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
137 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
138};
139
140struct xport {
141 struct hmap_node hmap_node; /* Node in global 'xports' map. */
142 struct ofport_dpif *ofport; /* Key in global 'xports map. */
143
144 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
145 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
146
147 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
148
ca6ba700 149 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
46c88433
EJ
150 struct xbundle *xbundle; /* Parent xbundle or null. */
151
152 struct netdev *netdev; /* 'ofport''s netdev. */
153
154 struct xbridge *xbridge; /* Parent bridge. */
155 struct xport *peer; /* Patch port peer or null. */
156
157 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 158 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 159 int stp_port_no; /* STP port number or -1 if not in use. */
f025bcb7 160 struct rstp_port *rstp_port; /* RSTP port or null. */
46c88433 161
55954f6e
EJ
162 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
163
46c88433
EJ
164 bool may_enable; /* May be enabled in bonds. */
165 bool is_tunnel; /* Is a tunnel port. */
166
167 struct cfm *cfm; /* CFM handle or null. */
168 struct bfd *bfd; /* BFD handle or null. */
0477baa9 169 struct lldp *lldp; /* LLDP handle or null. */
46c88433
EJ
170};
171
4d0acc70
EJ
172struct xlate_ctx {
173 struct xlate_in *xin;
174 struct xlate_out *xout;
175
46c88433 176 const struct xbridge *xbridge;
4d0acc70
EJ
177
178 /* Flow at the last commit. */
179 struct flow base_flow;
180
181 /* Tunnel IP destination address as received. This is stored separately
182 * as the base_flow.tunnel is cleared on init to reflect the datapath
183 * behavior. Used to make sure not to send tunneled output to ourselves,
184 * which might lead to an infinite loop. This could happen easily
185 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
186 * actually set the tun_dst field. */
187 ovs_be32 orig_tunnel_ip_dst;
188
189 /* Stack for the push and pop actions. Each stack element is of type
190 * "union mf_subvalue". */
191 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
192 struct ofpbuf stack;
193
194 /* The rule that we are currently translating, or NULL. */
195 struct rule_dpif *rule;
196
98b07853
BP
197 /* Resubmit statistics, via xlate_table_action(). */
198 int recurse; /* Current resubmit nesting depth. */
199 int resubmits; /* Total number of resubmits. */
5a070238 200 bool in_group; /* Currently translating ofgroup, if true. */
029ca940 201 bool in_action_set; /* Currently translating action_set, if true. */
98b07853 202
4d0acc70
EJ
203 uint32_t orig_skb_priority; /* Priority when packet arrived. */
204 uint8_t table_id; /* OpenFlow table ID where flow was found. */
205 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 206 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
4d0acc70
EJ
207 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
208 bool exit; /* No further actions should be processed. */
7fdb60a7 209
92c08f09
SH
210 bool use_recirc; /* Should generate recirc? */
211 struct xlate_recirc recirc; /* Information used for generating
212 * recirculation actions */
213
7bbdd84f
SH
214 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
215 * This is a trigger for recirculation in cases where translating an action
216 * or looking up a flow requires access to the fields of the packet after
217 * the MPLS label stack that was originally present. */
218 bool was_mpls;
219
7fdb60a7
SH
220 /* OpenFlow 1.1+ action set.
221 *
222 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
223 * When translation is otherwise complete, ofpacts_execute_action_set()
224 * converts it to a set of "struct ofpact"s that can be translated into
225 * datapath actions. */
c61f3870 226 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
7fdb60a7
SH
227 struct ofpbuf action_set; /* Action set. */
228 uint64_t action_set_stub[1024 / 8];
4d0acc70
EJ
229};
230
9583bc14
EJ
231/* A controller may use OFPP_NONE as the ingress port to indicate that
232 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
233 * when an input bundle is needed for validation (e.g., mirroring or
234 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
235 * any 'port' structs, so care must be taken when dealing with it. */
236static struct xbundle ofpp_none_bundle = {
237 .name = "OFPP_NONE",
238 .vlan_mode = PORT_VLAN_TRUNK
239};
9583bc14 240
55954f6e
EJ
241/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
242 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
243 * traffic egressing the 'ofport' with that priority should be marked with. */
244struct skb_priority_to_dscp {
245 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
246 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
247
248 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
249};
250
b256dc52
JS
251enum xc_type {
252 XC_RULE,
253 XC_BOND,
254 XC_NETDEV,
255 XC_NETFLOW,
256 XC_MIRROR,
257 XC_LEARN,
258 XC_NORMAL,
259 XC_FIN_TIMEOUT,
1e684d7d 260 XC_GROUP,
a36de779 261 XC_TNL_ARP,
b256dc52
JS
262};
263
264/* xlate_cache entries hold enough information to perform the side effects of
265 * xlate_actions() for a rule, without needing to perform rule translation
266 * from scratch. The primary usage of these is to submit statistics to objects
267 * that a flow relates to, although they may be used for other effects as well
268 * (for instance, refreshing hard timeouts for learned flows). */
269struct xc_entry {
270 enum xc_type type;
271 union {
272 struct rule_dpif *rule;
273 struct {
274 struct netdev *tx;
275 struct netdev *rx;
276 struct bfd *bfd;
277 } dev;
278 struct {
279 struct netflow *netflow;
280 struct flow *flow;
281 ofp_port_t iface;
282 } nf;
283 struct {
284 struct mbridge *mbridge;
285 mirror_mask_t mirrors;
286 } mirror;
287 struct {
288 struct bond *bond;
289 struct flow *flow;
290 uint16_t vid;
291 } bond;
292 struct {
4165b5e0
JS
293 struct ofproto_dpif *ofproto;
294 struct ofputil_flow_mod *fm;
295 struct ofpbuf *ofpacts;
b256dc52
JS
296 } learn;
297 struct {
298 struct ofproto_dpif *ofproto;
299 struct flow *flow;
300 int vlan;
301 } normal;
302 struct {
303 struct rule_dpif *rule;
304 uint16_t idle;
305 uint16_t hard;
306 } fin;
1e684d7d
RW
307 struct {
308 struct group_dpif *group;
309 struct ofputil_bucket *bucket;
310 } group;
a36de779
PS
311 struct {
312 char br_name[IFNAMSIZ];
313 ovs_be32 d_ip;
314 } tnl_arp_cache;
b256dc52
JS
315 } u;
316};
317
318#define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
319 entries = xcache->entries; \
320 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
321 entry; \
322 entry = ofpbuf_try_pull(&entries, sizeof *entry))
323
324struct xlate_cache {
325 struct ofpbuf entries;
326};
327
84f0f298
RW
328/* Xlate config contains hash maps of all bridges, bundles and ports.
329 * Xcfgp contains the pointer to the current xlate configuration.
330 * When the main thread needs to change the configuration, it copies xcfgp to
331 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
332 * does not block handler and revalidator threads. */
333struct xlate_cfg {
334 struct hmap xbridges;
335 struct hmap xbundles;
336 struct hmap xports;
337};
b1b72f2d 338static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
f439f23b 339static struct xlate_cfg *new_xcfg = NULL;
46c88433
EJ
340
341static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
342static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
343 struct xlate_ctx *);
adcf00ba 344static void xlate_normal(struct xlate_ctx *);
34dd0d78 345static inline void xlate_report(struct xlate_ctx *, const char *);
6d328fa2
SH
346static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
347 uint8_t table_id, bool may_packet_in,
348 bool honor_table_miss);
46c88433
EJ
349static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
350static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
351static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 352 uint16_t vlan);
4e022ec0 353static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
9583bc14 354
84f0f298
RW
355static struct xbridge *xbridge_lookup(struct xlate_cfg *,
356 const struct ofproto_dpif *);
357static struct xbundle *xbundle_lookup(struct xlate_cfg *,
358 const struct ofbundle *);
359static struct xport *xport_lookup(struct xlate_cfg *,
360 const struct ofport_dpif *);
46c88433 361static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
362static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
363 uint32_t skb_priority);
364static void clear_skb_priorities(struct xport *);
16194afd 365static size_t count_skb_priorities(const struct xport *);
55954f6e
EJ
366static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
367 uint8_t *dscp);
46c88433 368
b256dc52
JS
369static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
370 enum xc_type type);
84f0f298
RW
371static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
372static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
373static void xlate_xport_init(struct xlate_cfg *, struct xport *);
9efd308e 374static void xlate_xbridge_set(struct xbridge *, struct dpif *,
9efd308e
DV
375 const struct mac_learning *, struct stp *,
376 struct rstp *, const struct mcast_snooping *,
377 const struct mbridge *,
378 const struct dpif_sflow *,
379 const struct dpif_ipfix *,
2f47cdf4 380 const struct netflow *,
84f0f298
RW
381 bool forward_bpdu, bool has_in_band,
382 bool enable_recirc,
383 bool variable_length_userdata,
53477c2c
JR
384 size_t max_mpls_depth,
385 bool masked_set_action);
84f0f298
RW
386static void xlate_xbundle_set(struct xbundle *xbundle,
387 enum port_vlan_mode vlan_mode, int vlan,
388 unsigned long *trunks, bool use_priority_tags,
389 const struct bond *bond, const struct lacp *lacp,
390 bool floodable);
391static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
392 const struct netdev *netdev, const struct cfm *cfm,
0477baa9
DF
393 const struct bfd *bfd, const struct lldp *lldp,
394 int stp_port_no, const struct rstp_port *rstp_port,
84f0f298
RW
395 enum ofputil_port_config config,
396 enum ofputil_port_state state, bool is_tunnel,
397 bool may_enable);
398static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
399static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
400static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
401static void xlate_xbridge_copy(struct xbridge *);
402static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
403static void xlate_xport_copy(struct xbridge *, struct xbundle *,
404 struct xport *);
405static void xlate_xcfg_free(struct xlate_cfg *);
b256dc52 406
34dd0d78
JR
407static inline void
408xlate_report(struct xlate_ctx *ctx, const char *s)
409{
410 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
411 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
412 }
413}
84f0f298
RW
414
415static void
416xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
417{
418 list_init(&xbridge->xbundles);
419 hmap_init(&xbridge->xports);
420 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
421 hash_pointer(xbridge->ofproto, 0));
422}
423
424static void
425xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
426{
427 list_init(&xbundle->xports);
428 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
429 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
430 hash_pointer(xbundle->ofbundle, 0));
431}
432
433static void
434xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
435{
436 hmap_init(&xport->skb_priorities);
437 hmap_insert(&xcfg->xports, &xport->hmap_node,
438 hash_pointer(xport->ofport, 0));
439 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
440 hash_ofp_port(xport->ofp_port));
441}
442
443static void
444xlate_xbridge_set(struct xbridge *xbridge,
445 struct dpif *dpif,
ec89fc6f 446 const struct mac_learning *ml, struct stp *stp,
9efd308e 447 struct rstp *rstp, const struct mcast_snooping *ms,
ec89fc6f 448 const struct mbridge *mbridge,
46c88433 449 const struct dpif_sflow *sflow,
ce3955be 450 const struct dpif_ipfix *ipfix,
2f47cdf4 451 const struct netflow *netflow,
4b97b70d 452 bool forward_bpdu, bool has_in_band,
adcf00ba 453 bool enable_recirc,
8bfd0fda 454 bool variable_length_userdata,
53477c2c
JR
455 size_t max_mpls_depth,
456 bool masked_set_action)
46c88433 457{
46c88433
EJ
458 if (xbridge->ml != ml) {
459 mac_learning_unref(xbridge->ml);
460 xbridge->ml = mac_learning_ref(ml);
461 }
462
6d95c4e8
FL
463 if (xbridge->ms != ms) {
464 mcast_snooping_unref(xbridge->ms);
465 xbridge->ms = mcast_snooping_ref(ms);
466 }
467
46c88433
EJ
468 if (xbridge->mbridge != mbridge) {
469 mbridge_unref(xbridge->mbridge);
470 xbridge->mbridge = mbridge_ref(mbridge);
471 }
472
473 if (xbridge->sflow != sflow) {
474 dpif_sflow_unref(xbridge->sflow);
475 xbridge->sflow = dpif_sflow_ref(sflow);
476 }
477
478 if (xbridge->ipfix != ipfix) {
479 dpif_ipfix_unref(xbridge->ipfix);
480 xbridge->ipfix = dpif_ipfix_ref(ipfix);
481 }
482
9d189a50
EJ
483 if (xbridge->stp != stp) {
484 stp_unref(xbridge->stp);
485 xbridge->stp = stp_ref(stp);
486 }
487
9efd308e
DV
488 if (xbridge->rstp != rstp) {
489 rstp_unref(xbridge->rstp);
490 xbridge->rstp = rstp_ref(rstp);
491 }
492
ce3955be
EJ
493 if (xbridge->netflow != netflow) {
494 netflow_unref(xbridge->netflow);
495 xbridge->netflow = netflow_ref(netflow);
496 }
497
89a8a7f0 498 xbridge->dpif = dpif;
46c88433
EJ
499 xbridge->forward_bpdu = forward_bpdu;
500 xbridge->has_in_band = has_in_band;
adcf00ba 501 xbridge->enable_recirc = enable_recirc;
4b97b70d 502 xbridge->variable_length_userdata = variable_length_userdata;
8bfd0fda 503 xbridge->max_mpls_depth = max_mpls_depth;
53477c2c 504 xbridge->masked_set_action = masked_set_action;
46c88433
EJ
505}
506
84f0f298
RW
507static void
508xlate_xbundle_set(struct xbundle *xbundle,
509 enum port_vlan_mode vlan_mode, int vlan,
510 unsigned long *trunks, bool use_priority_tags,
511 const struct bond *bond, const struct lacp *lacp,
512 bool floodable)
513{
514 ovs_assert(xbundle->xbridge);
515
516 xbundle->vlan_mode = vlan_mode;
517 xbundle->vlan = vlan;
518 xbundle->trunks = trunks;
519 xbundle->use_priority_tags = use_priority_tags;
520 xbundle->floodable = floodable;
521
522 if (xbundle->bond != bond) {
523 bond_unref(xbundle->bond);
524 xbundle->bond = bond_ref(bond);
525 }
526
527 if (xbundle->lacp != lacp) {
528 lacp_unref(xbundle->lacp);
529 xbundle->lacp = lacp_ref(lacp);
530 }
531}
532
533static void
534xlate_xport_set(struct xport *xport, odp_port_t odp_port,
535 const struct netdev *netdev, const struct cfm *cfm,
0477baa9 536 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
f025bcb7 537 const struct rstp_port* rstp_port,
84f0f298
RW
538 enum ofputil_port_config config, enum ofputil_port_state state,
539 bool is_tunnel, bool may_enable)
540{
541 xport->config = config;
542 xport->state = state;
543 xport->stp_port_no = stp_port_no;
544 xport->is_tunnel = is_tunnel;
545 xport->may_enable = may_enable;
546 xport->odp_port = odp_port;
547
f025bcb7
JR
548 if (xport->rstp_port != rstp_port) {
549 rstp_port_unref(xport->rstp_port);
550 xport->rstp_port = rstp_port_ref(rstp_port);
551 }
552
84f0f298
RW
553 if (xport->cfm != cfm) {
554 cfm_unref(xport->cfm);
555 xport->cfm = cfm_ref(cfm);
556 }
557
558 if (xport->bfd != bfd) {
559 bfd_unref(xport->bfd);
560 xport->bfd = bfd_ref(bfd);
561 }
562
0477baa9
DF
563 if (xport->lldp != lldp) {
564 lldp_unref(xport->lldp);
565 xport->lldp = lldp_ref(lldp);
566 }
567
84f0f298
RW
568 if (xport->netdev != netdev) {
569 netdev_close(xport->netdev);
570 xport->netdev = netdev_ref(netdev);
571 }
572}
573
574static void
575xlate_xbridge_copy(struct xbridge *xbridge)
576{
577 struct xbundle *xbundle;
578 struct xport *xport;
579 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
580 new_xbridge->ofproto = xbridge->ofproto;
581 new_xbridge->name = xstrdup(xbridge->name);
582 xlate_xbridge_init(new_xcfg, new_xbridge);
583
584 xlate_xbridge_set(new_xbridge,
34dd0d78 585 xbridge->dpif, xbridge->ml, xbridge->stp,
9efd308e
DV
586 xbridge->rstp, xbridge->ms, xbridge->mbridge,
587 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
2f47cdf4 588 xbridge->forward_bpdu,
9efd308e
DV
589 xbridge->has_in_band, xbridge->enable_recirc,
590 xbridge->variable_length_userdata,
53477c2c 591 xbridge->max_mpls_depth, xbridge->masked_set_action);
84f0f298
RW
592 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
593 xlate_xbundle_copy(new_xbridge, xbundle);
594 }
595
596 /* Copy xports which are not part of a xbundle */
597 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
598 if (!xport->xbundle) {
599 xlate_xport_copy(new_xbridge, NULL, xport);
600 }
601 }
602}
603
604static void
605xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
606{
607 struct xport *xport;
608 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
609 new_xbundle->ofbundle = xbundle->ofbundle;
610 new_xbundle->xbridge = xbridge;
611 new_xbundle->name = xstrdup(xbundle->name);
612 xlate_xbundle_init(new_xcfg, new_xbundle);
613
614 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
615 xbundle->vlan, xbundle->trunks,
616 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
617 xbundle->floodable);
618 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
619 xlate_xport_copy(xbridge, new_xbundle, xport);
620 }
621}
622
623static void
624xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
625 struct xport *xport)
626{
627 struct skb_priority_to_dscp *pdscp, *new_pdscp;
628 struct xport *new_xport = xzalloc(sizeof *xport);
629 new_xport->ofport = xport->ofport;
630 new_xport->ofp_port = xport->ofp_port;
631 new_xport->xbridge = xbridge;
632 xlate_xport_init(new_xcfg, new_xport);
633
634 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
0477baa9
DF
635 xport->bfd, xport->lldp, xport->stp_port_no,
636 xport->rstp_port, xport->config, xport->state,
637 xport->is_tunnel, xport->may_enable);
84f0f298
RW
638
639 if (xport->peer) {
640 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
641 if (peer) {
642 new_xport->peer = peer;
643 new_xport->peer->peer = new_xport;
644 }
645 }
646
647 if (xbundle) {
648 new_xport->xbundle = xbundle;
649 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
650 }
651
652 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
653 new_pdscp = xmalloc(sizeof *pdscp);
654 new_pdscp->skb_priority = pdscp->skb_priority;
655 new_pdscp->dscp = pdscp->dscp;
656 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
657 hash_int(new_pdscp->skb_priority, 0));
658 }
659}
660
661/* Sets the current xlate configuration to new_xcfg and frees the old xlate
662 * configuration in xcfgp.
663 *
664 * This needs to be called after editing the xlate configuration.
665 *
666 * Functions that edit the new xlate configuration are
667 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
668 *
669 * A sample workflow:
670 *
671 * xlate_txn_start();
672 * ...
673 * edit_xlate_configuration();
674 * ...
675 * xlate_txn_commit(); */
46c88433 676void
84f0f298
RW
677xlate_txn_commit(void)
678{
679 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
680
681 ovsrcu_set(&xcfgp, new_xcfg);
40a9c4c2
AW
682 ovsrcu_synchronize();
683 xlate_xcfg_free(xcfg);
84f0f298
RW
684 new_xcfg = NULL;
685}
686
687/* Copies the current xlate configuration in xcfgp to new_xcfg.
688 *
689 * This needs to be called prior to editing the xlate configuration. */
690void
691xlate_txn_start(void)
692{
693 struct xbridge *xbridge;
694 struct xlate_cfg *xcfg;
695
696 ovs_assert(!new_xcfg);
697
698 new_xcfg = xmalloc(sizeof *new_xcfg);
699 hmap_init(&new_xcfg->xbridges);
700 hmap_init(&new_xcfg->xbundles);
701 hmap_init(&new_xcfg->xports);
702
703 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
704 if (!xcfg) {
705 return;
706 }
707
708 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
709 xlate_xbridge_copy(xbridge);
710 }
711}
712
713
714static void
715xlate_xcfg_free(struct xlate_cfg *xcfg)
716{
717 struct xbridge *xbridge, *next_xbridge;
718
719 if (!xcfg) {
720 return;
721 }
722
723 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
724 xlate_xbridge_remove(xcfg, xbridge);
725 }
726
727 hmap_destroy(&xcfg->xbridges);
728 hmap_destroy(&xcfg->xbundles);
729 hmap_destroy(&xcfg->xports);
730 free(xcfg);
731}
732
733void
734xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
34dd0d78 735 struct dpif *dpif,
84f0f298 736 const struct mac_learning *ml, struct stp *stp,
9efd308e 737 struct rstp *rstp, const struct mcast_snooping *ms,
84f0f298
RW
738 const struct mbridge *mbridge,
739 const struct dpif_sflow *sflow,
740 const struct dpif_ipfix *ipfix,
2f47cdf4 741 const struct netflow *netflow,
9efd308e 742 bool forward_bpdu, bool has_in_band, bool enable_recirc,
53477c2c
JR
743 bool variable_length_userdata, size_t max_mpls_depth,
744 bool masked_set_action)
84f0f298
RW
745{
746 struct xbridge *xbridge;
747
748 ovs_assert(new_xcfg);
749
750 xbridge = xbridge_lookup(new_xcfg, ofproto);
751 if (!xbridge) {
752 xbridge = xzalloc(sizeof *xbridge);
753 xbridge->ofproto = ofproto;
754
755 xlate_xbridge_init(new_xcfg, xbridge);
756 }
757
758 free(xbridge->name);
759 xbridge->name = xstrdup(name);
760
34dd0d78
JR
761 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
762 netflow, forward_bpdu, has_in_band, enable_recirc,
53477c2c
JR
763 variable_length_userdata, max_mpls_depth,
764 masked_set_action);
84f0f298
RW
765}
766
767static void
768xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
46c88433 769{
46c88433
EJ
770 struct xbundle *xbundle, *next_xbundle;
771 struct xport *xport, *next_xport;
772
773 if (!xbridge) {
774 return;
775 }
776
777 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
84f0f298 778 xlate_xport_remove(xcfg, xport);
46c88433
EJ
779 }
780
781 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
84f0f298 782 xlate_xbundle_remove(xcfg, xbundle);
46c88433
EJ
783 }
784
84f0f298 785 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
795cc5c1 786 mac_learning_unref(xbridge->ml);
6d95c4e8 787 mcast_snooping_unref(xbridge->ms);
795cc5c1
EJ
788 mbridge_unref(xbridge->mbridge);
789 dpif_sflow_unref(xbridge->sflow);
790 dpif_ipfix_unref(xbridge->ipfix);
791 stp_unref(xbridge->stp);
9efd308e 792 rstp_unref(xbridge->rstp);
795cc5c1 793 hmap_destroy(&xbridge->xports);
46c88433
EJ
794 free(xbridge->name);
795 free(xbridge);
796}
797
84f0f298
RW
798void
799xlate_remove_ofproto(struct ofproto_dpif *ofproto)
800{
801 struct xbridge *xbridge;
802
803 ovs_assert(new_xcfg);
804
805 xbridge = xbridge_lookup(new_xcfg, ofproto);
806 xlate_xbridge_remove(new_xcfg, xbridge);
807}
808
46c88433
EJ
809void
810xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
811 const char *name, enum port_vlan_mode vlan_mode, int vlan,
812 unsigned long *trunks, bool use_priority_tags,
813 const struct bond *bond, const struct lacp *lacp,
814 bool floodable)
815{
84f0f298 816 struct xbundle *xbundle;
46c88433 817
84f0f298
RW
818 ovs_assert(new_xcfg);
819
820 xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
821 if (!xbundle) {
822 xbundle = xzalloc(sizeof *xbundle);
823 xbundle->ofbundle = ofbundle;
84f0f298 824 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433 825
84f0f298 826 xlate_xbundle_init(new_xcfg, xbundle);
46c88433
EJ
827 }
828
46c88433
EJ
829 free(xbundle->name);
830 xbundle->name = xstrdup(name);
831
84f0f298
RW
832 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
833 use_priority_tags, bond, lacp, floodable);
46c88433
EJ
834}
835
84f0f298
RW
836static void
837xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
46c88433 838{
46c88433
EJ
839 struct xport *xport, *next;
840
841 if (!xbundle) {
842 return;
843 }
844
845 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
846 list_remove(&xport->bundle_node);
847 xport->xbundle = NULL;
848 }
849
84f0f298 850 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
46c88433
EJ
851 list_remove(&xbundle->list_node);
852 bond_unref(xbundle->bond);
853 lacp_unref(xbundle->lacp);
854 free(xbundle->name);
855 free(xbundle);
856}
857
84f0f298
RW
858void
859xlate_bundle_remove(struct ofbundle *ofbundle)
860{
861 struct xbundle *xbundle;
862
863 ovs_assert(new_xcfg);
864
865 xbundle = xbundle_lookup(new_xcfg, ofbundle);
866 xlate_xbundle_remove(new_xcfg, xbundle);
867}
868
46c88433
EJ
869void
870xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
871 struct ofport_dpif *ofport, ofp_port_t ofp_port,
872 odp_port_t odp_port, const struct netdev *netdev,
873 const struct cfm *cfm, const struct bfd *bfd,
0477baa9
DF
874 const struct lldp *lldp, struct ofport_dpif *peer,
875 int stp_port_no, const struct rstp_port *rstp_port,
55954f6e 876 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
877 enum ofputil_port_config config,
878 enum ofputil_port_state state, bool is_tunnel,
9d189a50 879 bool may_enable)
46c88433 880{
55954f6e 881 size_t i;
84f0f298
RW
882 struct xport *xport;
883
884 ovs_assert(new_xcfg);
46c88433 885
84f0f298 886 xport = xport_lookup(new_xcfg, ofport);
46c88433
EJ
887 if (!xport) {
888 xport = xzalloc(sizeof *xport);
889 xport->ofport = ofport;
84f0f298 890 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433
EJ
891 xport->ofp_port = ofp_port;
892
84f0f298 893 xlate_xport_init(new_xcfg, xport);
46c88433
EJ
894 }
895
896 ovs_assert(xport->ofp_port == ofp_port);
897
0477baa9
DF
898 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
899 stp_port_no, rstp_port, config, state, is_tunnel,
900 may_enable);
46c88433
EJ
901
902 if (xport->peer) {
903 xport->peer->peer = NULL;
904 }
84f0f298 905 xport->peer = xport_lookup(new_xcfg, peer);
46c88433
EJ
906 if (xport->peer) {
907 xport->peer->peer = xport;
908 }
909
910 if (xport->xbundle) {
911 list_remove(&xport->bundle_node);
912 }
84f0f298 913 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
914 if (xport->xbundle) {
915 list_insert(&xport->xbundle->xports, &xport->bundle_node);
916 }
55954f6e
EJ
917
918 clear_skb_priorities(xport);
919 for (i = 0; i < n_qdscp; i++) {
920 struct skb_priority_to_dscp *pdscp;
921 uint32_t skb_priority;
922
89a8a7f0
EJ
923 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
924 &skb_priority)) {
55954f6e
EJ
925 continue;
926 }
927
928 pdscp = xmalloc(sizeof *pdscp);
929 pdscp->skb_priority = skb_priority;
930 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
931 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
932 hash_int(pdscp->skb_priority, 0));
933 }
46c88433
EJ
934}
935
84f0f298
RW
936static void
937xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
46c88433 938{
46c88433
EJ
939 if (!xport) {
940 return;
941 }
942
943 if (xport->peer) {
944 xport->peer->peer = NULL;
945 xport->peer = NULL;
946 }
947
e621a12d
EJ
948 if (xport->xbundle) {
949 list_remove(&xport->bundle_node);
950 }
951
55954f6e
EJ
952 clear_skb_priorities(xport);
953 hmap_destroy(&xport->skb_priorities);
954
84f0f298 955 hmap_remove(&xcfg->xports, &xport->hmap_node);
46c88433
EJ
956 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
957
958 netdev_close(xport->netdev);
f025bcb7 959 rstp_port_unref(xport->rstp_port);
46c88433
EJ
960 cfm_unref(xport->cfm);
961 bfd_unref(xport->bfd);
0477baa9 962 lldp_unref(xport->lldp);
46c88433
EJ
963 free(xport);
964}
965
84f0f298
RW
966void
967xlate_ofport_remove(struct ofport_dpif *ofport)
968{
969 struct xport *xport;
970
971 ovs_assert(new_xcfg);
972
973 xport = xport_lookup(new_xcfg, ofport);
974 xlate_xport_remove(new_xcfg, xport);
975}
976
ef377a58
JR
977/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
978 * returns the corresponding struct xport, or NULL if none is found. */
979static struct xport *
980xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
981{
982 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
983
984 return xport_lookup(xcfg, tnl_port_should_receive(flow)
985 ? tnl_port_receive(flow)
986 : odp_port_to_ofport(backer, flow->in_port.odp_port));
987}
988
989static struct ofproto_dpif *
990xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
991 ofp_port_t *ofp_in_port, const struct xport **xportp)
992{
f9038ef6
AW
993 struct ofproto_dpif *recv_ofproto = NULL;
994 struct ofproto_dpif *recirc_ofproto = NULL;
ef377a58 995 const struct xport *xport;
f9038ef6 996 ofp_port_t in_port = OFPP_NONE;
ef377a58
JR
997
998 *xportp = xport = xlate_lookup_xport(backer, flow);
999
1000 if (xport) {
f9038ef6
AW
1001 recv_ofproto = xport->xbridge->ofproto;
1002 in_port = xport->ofp_port;
1003 }
1004
1005 /* When recirc_id is set in 'flow', checks whether the ofproto_dpif that
1006 * corresponds to the recirc_id is same as the receiving bridge. If they
1007 * are the same, uses the 'recv_ofproto' and keeps the 'ofp_in_port' as
1008 * assigned. Otherwise, uses the 'recirc_ofproto' that owns recirc_id and
1009 * assigns OFPP_NONE to 'ofp_in_port'. Doing this is in that, the
1010 * recirculated flow must be processced by the ofproto which originates
1011 * the recirculation, and as bridges can only see their own ports, the
1012 * in_port of the 'recv_ofproto' should not be passed to the
1013 * 'recirc_ofproto'.
1014 *
1015 * Admittedly, setting the 'ofp_in_port' to OFPP_NONE limits the
1016 * 'recirc_ofproto' from meaningfully matching on in_port of recirculated
1017 * flow, and should be fixed in the near future.
1018 *
1019 * TODO: Restore the original patch port.
1020 */
1021 if (recv_ofproto && flow->recirc_id) {
1022 recirc_ofproto = ofproto_dpif_recirc_get_ofproto(backer,
1023 flow->recirc_id);
1024 if (recv_ofproto != recirc_ofproto) {
1025 *xportp = xport = NULL;
1026 in_port = OFPP_NONE;
ef377a58 1027 }
ef377a58
JR
1028 }
1029
f9038ef6
AW
1030 if (ofp_in_port) {
1031 *ofp_in_port = in_port;
1032 }
1033
1034 return xport ? recv_ofproto : recirc_ofproto;
ef377a58
JR
1035}
1036
1037/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1038 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1039struct ofproto_dpif *
1040xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1041 ofp_port_t *ofp_in_port)
1042{
1043 const struct xport *xport;
1044
1045 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1046}
1047
cc377352 1048/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
ef377a58 1049 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
cc377352 1050 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
dcc2c6cd
JR
1051 * handles for those protocols if they're enabled. Caller may use the returned
1052 * pointers until quiescing, for longer term use additional references must
1053 * be taken.
8449c4d6 1054 *
f9038ef6 1055 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
ef377a58 1056 */
8449c4d6 1057int
5c476ea3
JR
1058xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1059 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1060 struct dpif_sflow **sflow, struct netflow **netflow,
1061 ofp_port_t *ofp_in_port)
8449c4d6 1062{
ef377a58 1063 struct ofproto_dpif *ofproto;
84f0f298 1064 const struct xport *xport;
8449c4d6 1065
ef377a58 1066 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
8449c4d6 1067
f9038ef6 1068 if (!ofproto) {
cc377352 1069 return ENODEV;
8449c4d6 1070 }
8449c4d6 1071
ef377a58
JR
1072 if (ofprotop) {
1073 *ofprotop = ofproto;
8449c4d6
EJ
1074 }
1075
1dfdb9b3 1076 if (ipfix) {
f9038ef6 1077 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1dfdb9b3
EJ
1078 }
1079
1080 if (sflow) {
f9038ef6 1081 *sflow = xport ? xport->xbridge->sflow : NULL;
1dfdb9b3
EJ
1082 }
1083
1084 if (netflow) {
f9038ef6 1085 *netflow = xport ? xport->xbridge->netflow : NULL;
1dfdb9b3 1086 }
f9038ef6 1087
cc377352 1088 return 0;
8449c4d6
EJ
1089}
1090
46c88433 1091static struct xbridge *
84f0f298 1092xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
46c88433 1093{
84f0f298 1094 struct hmap *xbridges;
46c88433
EJ
1095 struct xbridge *xbridge;
1096
84f0f298 1097 if (!ofproto || !xcfg) {
5e6af486
EJ
1098 return NULL;
1099 }
1100
84f0f298
RW
1101 xbridges = &xcfg->xbridges;
1102
46c88433 1103 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
84f0f298 1104 xbridges) {
46c88433
EJ
1105 if (xbridge->ofproto == ofproto) {
1106 return xbridge;
1107 }
1108 }
1109 return NULL;
1110}
1111
1112static struct xbundle *
84f0f298 1113xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
46c88433 1114{
84f0f298 1115 struct hmap *xbundles;
46c88433
EJ
1116 struct xbundle *xbundle;
1117
84f0f298 1118 if (!ofbundle || !xcfg) {
5e6af486
EJ
1119 return NULL;
1120 }
1121
84f0f298
RW
1122 xbundles = &xcfg->xbundles;
1123
46c88433 1124 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
84f0f298 1125 xbundles) {
46c88433
EJ
1126 if (xbundle->ofbundle == ofbundle) {
1127 return xbundle;
1128 }
1129 }
1130 return NULL;
1131}
1132
1133static struct xport *
84f0f298 1134xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
46c88433 1135{
84f0f298 1136 struct hmap *xports;
46c88433
EJ
1137 struct xport *xport;
1138
84f0f298 1139 if (!ofport || !xcfg) {
5e6af486
EJ
1140 return NULL;
1141 }
1142
84f0f298
RW
1143 xports = &xcfg->xports;
1144
46c88433 1145 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
84f0f298 1146 xports) {
46c88433
EJ
1147 if (xport->ofport == ofport) {
1148 return xport;
1149 }
1150 }
1151 return NULL;
1152}
1153
40085e56
EJ
1154static struct stp_port *
1155xport_get_stp_port(const struct xport *xport)
1156{
92cf817b 1157 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
1158 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1159 : NULL;
1160}
9d189a50 1161
0d1cee12 1162static bool
9d189a50
EJ
1163xport_stp_learn_state(const struct xport *xport)
1164{
40085e56 1165 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1166 return sp
1167 ? stp_learn_in_state(stp_port_get_state(sp))
1168 : true;
9d189a50
EJ
1169}
1170
1171static bool
1172xport_stp_forward_state(const struct xport *xport)
1173{
40085e56 1174 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1175 return sp
1176 ? stp_forward_in_state(stp_port_get_state(sp))
1177 : true;
9d189a50
EJ
1178}
1179
0d1cee12 1180static bool
bacdb85a 1181xport_stp_should_forward_bpdu(const struct xport *xport)
0d1cee12
K
1182{
1183 struct stp_port *sp = xport_get_stp_port(xport);
bacdb85a 1184 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
0d1cee12
K
1185}
1186
9d189a50
EJ
1187/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1188 * were used to make the determination.*/
1189static bool
1190stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1191{
bbbca389 1192 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
9d189a50 1193 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
bbbca389 1194 return is_stp(flow);
9d189a50
EJ
1195}
1196
1197static void
cf62fa4c 1198stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9d189a50 1199{
40085e56 1200 struct stp_port *sp = xport_get_stp_port(xport);
cf62fa4c
PS
1201 struct dp_packet payload = *packet;
1202 struct eth_header *eth = dp_packet_data(&payload);
9d189a50
EJ
1203
1204 /* Sink packets on ports that have STP disabled when the bridge has
1205 * STP enabled. */
1206 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1207 return;
1208 }
1209
1210 /* Trim off padding on payload. */
cf62fa4c
PS
1211 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1212 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
1213 }
1214
cf62fa4c
PS
1215 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1216 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
9d189a50
EJ
1217 }
1218}
1219
f025bcb7
JR
1220static enum rstp_state
1221xport_get_rstp_port_state(const struct xport *xport)
9efd308e 1222{
f025bcb7
JR
1223 return xport->rstp_port
1224 ? rstp_port_get_state(xport->rstp_port)
1225 : RSTP_DISABLED;
9efd308e
DV
1226}
1227
1228static bool
1229xport_rstp_learn_state(const struct xport *xport)
1230{
4b5f1996
DV
1231 return xport->xbridge->rstp && xport->rstp_port
1232 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1233 : true;
9efd308e
DV
1234}
1235
1236static bool
1237xport_rstp_forward_state(const struct xport *xport)
1238{
4b5f1996
DV
1239 return xport->xbridge->rstp && xport->rstp_port
1240 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1241 : true;
9efd308e
DV
1242}
1243
1244static bool
1245xport_rstp_should_manage_bpdu(const struct xport *xport)
1246{
f025bcb7 1247 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
9efd308e
DV
1248}
1249
1250static void
cf62fa4c 1251rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9efd308e 1252{
cf62fa4c
PS
1253 struct dp_packet payload = *packet;
1254 struct eth_header *eth = dp_packet_data(&payload);
9efd308e 1255
f025bcb7
JR
1256 /* Sink packets on ports that have no RSTP. */
1257 if (!xport->rstp_port) {
9efd308e
DV
1258 return;
1259 }
1260
1261 /* Trim off padding on payload. */
cf62fa4c
PS
1262 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1263 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9efd308e
DV
1264 }
1265
cf62fa4c
PS
1266 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1267 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1268 dp_packet_size(&payload));
9efd308e
DV
1269 }
1270}
1271
46c88433
EJ
1272static struct xport *
1273get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1274{
1275 struct xport *xport;
1276
1277 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1278 &xbridge->xports) {
1279 if (xport->ofp_port == ofp_port) {
1280 return xport;
1281 }
1282 }
1283 return NULL;
1284}
1285
1286static odp_port_t
1287ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1288{
1289 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1290 return xport ? xport->odp_port : ODPP_NONE;
1291}
1292
dd8cd4b4
SH
1293static bool
1294odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1295{
086fa873
BP
1296 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1297 return xport && xport->may_enable;
dd8cd4b4
SH
1298}
1299
1e684d7d 1300static struct ofputil_bucket *
dd8cd4b4
SH
1301group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1302 int depth);
1303
1304static bool
1305group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1306{
1307 struct group_dpif *group;
dd8cd4b4 1308
dc25893e
AZ
1309 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1310 struct ofputil_bucket *bucket;
dd8cd4b4 1311
dc25893e
AZ
1312 bucket = group_first_live_bucket(ctx, group, depth);
1313 group_dpif_unref(group);
1314 return bucket == NULL;
1315 }
dd8cd4b4 1316
dc25893e 1317 return false;
dd8cd4b4
SH
1318}
1319
1320#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1321
1322static bool
1323bucket_is_alive(const struct xlate_ctx *ctx,
1e684d7d 1324 struct ofputil_bucket *bucket, int depth)
dd8cd4b4
SH
1325{
1326 if (depth >= MAX_LIVENESS_RECURSION) {
1327 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1328
1329 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1330 MAX_LIVENESS_RECURSION);
1331 return false;
1332 }
1333
fdb1999b
AZ
1334 return (!ofputil_bucket_has_liveness(bucket)
1335 || (bucket->watch_port != OFPP_ANY
1336 && odp_port_is_alive(ctx, bucket->watch_port))
1337 || (bucket->watch_group != OFPG_ANY
1338 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
dd8cd4b4
SH
1339}
1340
1e684d7d 1341static struct ofputil_bucket *
dd8cd4b4
SH
1342group_first_live_bucket(const struct xlate_ctx *ctx,
1343 const struct group_dpif *group, int depth)
1344{
1345 struct ofputil_bucket *bucket;
ca6ba700 1346 const struct ovs_list *buckets;
dd8cd4b4
SH
1347
1348 group_dpif_get_buckets(group, &buckets);
1349 LIST_FOR_EACH (bucket, list_node, buckets) {
1350 if (bucket_is_alive(ctx, bucket, depth)) {
1351 return bucket;
1352 }
1353 }
1354
1355 return NULL;
1356}
1357
1e684d7d 1358static struct ofputil_bucket *
fe7e5749
SH
1359group_best_live_bucket(const struct xlate_ctx *ctx,
1360 const struct group_dpif *group,
1361 uint32_t basis)
1362{
1e684d7d 1363 struct ofputil_bucket *best_bucket = NULL;
fe7e5749
SH
1364 uint32_t best_score = 0;
1365 int i = 0;
1366
1e684d7d 1367 struct ofputil_bucket *bucket;
ca6ba700 1368 const struct ovs_list *buckets;
fe7e5749
SH
1369
1370 group_dpif_get_buckets(group, &buckets);
1371 LIST_FOR_EACH (bucket, list_node, buckets) {
1372 if (bucket_is_alive(ctx, bucket, 0)) {
7cb279c2 1373 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
1374 if (score >= best_score) {
1375 best_bucket = bucket;
1376 best_score = score;
1377 }
1378 }
1379 i++;
1380 }
1381
1382 return best_bucket;
1383}
1384
9583bc14 1385static bool
46c88433 1386xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
1387{
1388 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1389 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1390}
1391
1392static bool
46c88433
EJ
1393xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1394{
1395 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1396}
1397
1398static mirror_mask_t
1399xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1400{
1401 return xbundle != &ofpp_none_bundle
1402 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1403 : 0;
1404}
1405
1406static mirror_mask_t
1407xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1408{
46c88433
EJ
1409 return xbundle != &ofpp_none_bundle
1410 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1411 : 0;
9583bc14
EJ
1412}
1413
46c88433
EJ
1414static mirror_mask_t
1415xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1416{
46c88433
EJ
1417 return xbundle != &ofpp_none_bundle
1418 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1419 : 0;
1420}
1421
1422static struct xbundle *
1423lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1424 bool warn, struct xport **in_xportp)
1425{
1426 struct xport *xport;
9583bc14
EJ
1427
1428 /* Find the port and bundle for the received packet. */
46c88433
EJ
1429 xport = get_ofp_port(xbridge, in_port);
1430 if (in_xportp) {
1431 *in_xportp = xport;
9583bc14 1432 }
46c88433
EJ
1433 if (xport && xport->xbundle) {
1434 return xport->xbundle;
9583bc14
EJ
1435 }
1436
6362203b
YT
1437 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1438 * which a controller may use as the ingress port for traffic that
1439 * it is sourcing. */
1440 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
9583bc14
EJ
1441 return &ofpp_none_bundle;
1442 }
1443
1444 /* Odd. A few possible reasons here:
1445 *
1446 * - We deleted a port but there are still a few packets queued up
1447 * from it.
1448 *
1449 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1450 * we don't know about.
1451 *
1452 * - The ofproto client didn't configure the port as part of a bundle.
1453 * This is particularly likely to happen if a packet was received on the
1454 * port after it was created, but before the client had a chance to
1455 * configure its bundle.
1456 */
1457 if (warn) {
1458 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1459
1460 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
46c88433 1461 "port %"PRIu16, xbridge->name, in_port);
9583bc14
EJ
1462 }
1463 return NULL;
1464}
1465
1466static void
1467add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1468{
46c88433 1469 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1470 mirror_mask_t mirrors;
46c88433 1471 struct xbundle *in_xbundle;
9583bc14
EJ
1472 uint16_t vlan;
1473 uint16_t vid;
cdf5d3a5
EJ
1474
1475 mirrors = ctx->xout->mirrors;
1476 ctx->xout->mirrors = 0;
9583bc14 1477
46c88433
EJ
1478 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1479 ctx->xin->packet != NULL, NULL);
1480 if (!in_xbundle) {
9583bc14
EJ
1481 return;
1482 }
46c88433 1483 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
9583bc14
EJ
1484
1485 /* Drop frames on bundles reserved for mirroring. */
46c88433 1486 if (xbundle_mirror_out(xbridge, in_xbundle)) {
9583bc14
EJ
1487 if (ctx->xin->packet != NULL) {
1488 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1489 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1490 "%s, which is reserved exclusively for mirroring",
46c88433 1491 ctx->xbridge->name, in_xbundle->name);
9583bc14 1492 }
cc377352 1493 ofpbuf_clear(ctx->xout->odp_actions);
9583bc14
EJ
1494 return;
1495 }
1496
1497 /* Check VLAN. */
1498 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
46c88433 1499 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
1500 return;
1501 }
46c88433 1502 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14 1503
9583bc14
EJ
1504 if (!mirrors) {
1505 return;
1506 }
1507
1508 /* Restore the original packet before adding the mirror actions. */
1509 ctx->xin->flow = *orig_flow;
1510
1511 while (mirrors) {
ec7ceaed
EJ
1512 mirror_mask_t dup_mirrors;
1513 struct ofbundle *out;
1514 unsigned long *vlans;
1515 bool vlan_mirrored;
1516 bool has_mirror;
1517 int out_vlan;
1518
4b6ab2b0 1519 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
ec7ceaed
EJ
1520 &vlans, &dup_mirrors, &out, &out_vlan);
1521 ovs_assert(has_mirror);
1522
1523 if (vlans) {
9583bc14
EJ
1524 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1525 }
ec7ceaed
EJ
1526 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1527 free(vlans);
9583bc14 1528
ec7ceaed 1529 if (!vlan_mirrored) {
9583bc14
EJ
1530 mirrors = zero_rightmost_1bit(mirrors);
1531 continue;
1532 }
1533
ec7ceaed
EJ
1534 mirrors &= ~dup_mirrors;
1535 ctx->xout->mirrors |= dup_mirrors;
1536 if (out) {
84f0f298
RW
1537 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1538 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
46c88433
EJ
1539 if (out_xbundle) {
1540 output_normal(ctx, out_xbundle, vlan);
1541 }
ec7ceaed 1542 } else if (vlan != out_vlan
9583bc14 1543 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
46c88433 1544 struct xbundle *xbundle;
9583bc14 1545
46c88433
EJ
1546 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1547 if (xbundle_includes_vlan(xbundle, out_vlan)
1548 && !xbundle_mirror_out(xbridge, xbundle)) {
1549 output_normal(ctx, xbundle, out_vlan);
9583bc14
EJ
1550 }
1551 }
1552 }
1553 }
1554}
1555
1556/* Given 'vid', the VID obtained from the 802.1Q header that was received as
46c88433 1557 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
9583bc14
EJ
1558 * the bundle on which the packet was received, returns the VLAN to which the
1559 * packet belongs.
1560 *
1561 * Both 'vid' and the return value are in the range 0...4095. */
1562static uint16_t
46c88433 1563input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
9583bc14 1564{
46c88433 1565 switch (in_xbundle->vlan_mode) {
9583bc14 1566 case PORT_VLAN_ACCESS:
46c88433 1567 return in_xbundle->vlan;
9583bc14
EJ
1568 break;
1569
1570 case PORT_VLAN_TRUNK:
1571 return vid;
1572
1573 case PORT_VLAN_NATIVE_UNTAGGED:
1574 case PORT_VLAN_NATIVE_TAGGED:
46c88433 1575 return vid ? vid : in_xbundle->vlan;
9583bc14
EJ
1576
1577 default:
428b2edd 1578 OVS_NOT_REACHED();
9583bc14
EJ
1579 }
1580}
1581
46c88433 1582/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
9583bc14
EJ
1583 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1584 * a warning.
1585 *
1586 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1587 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1588 * 0...4095. */
1589static bool
46c88433 1590input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
9583bc14
EJ
1591{
1592 /* Allow any VID on the OFPP_NONE port. */
46c88433 1593 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1594 return true;
1595 }
1596
46c88433 1597 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1598 case PORT_VLAN_ACCESS:
1599 if (vid) {
1600 if (warn) {
1601 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1602 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
9583bc14 1603 "packet received on port %s configured as VLAN "
46c88433
EJ
1604 "%"PRIu16" access port", vid, in_xbundle->name,
1605 in_xbundle->vlan);
9583bc14
EJ
1606 }
1607 return false;
1608 }
1609 return true;
1610
1611 case PORT_VLAN_NATIVE_UNTAGGED:
1612 case PORT_VLAN_NATIVE_TAGGED:
1613 if (!vid) {
1614 /* Port must always carry its native VLAN. */
1615 return true;
1616 }
1617 /* Fall through. */
1618 case PORT_VLAN_TRUNK:
46c88433 1619 if (!xbundle_includes_vlan(in_xbundle, vid)) {
9583bc14
EJ
1620 if (warn) {
1621 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1622 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
9583bc14 1623 "received on port %s not configured for trunking "
46c88433 1624 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
9583bc14
EJ
1625 }
1626 return false;
1627 }
1628 return true;
1629
1630 default:
428b2edd 1631 OVS_NOT_REACHED();
9583bc14
EJ
1632 }
1633
1634}
1635
1636/* Given 'vlan', the VLAN that a packet belongs to, and
46c88433 1637 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
9583bc14
EJ
1638 * that should be included in the 802.1Q header. (If the return value is 0,
1639 * then the 802.1Q header should only be included in the packet if there is a
1640 * nonzero PCP.)
1641 *
1642 * Both 'vlan' and the return value are in the range 0...4095. */
1643static uint16_t
46c88433 1644output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
9583bc14 1645{
46c88433 1646 switch (out_xbundle->vlan_mode) {
9583bc14
EJ
1647 case PORT_VLAN_ACCESS:
1648 return 0;
1649
1650 case PORT_VLAN_TRUNK:
1651 case PORT_VLAN_NATIVE_TAGGED:
1652 return vlan;
1653
1654 case PORT_VLAN_NATIVE_UNTAGGED:
46c88433 1655 return vlan == out_xbundle->vlan ? 0 : vlan;
9583bc14
EJ
1656
1657 default:
428b2edd 1658 OVS_NOT_REACHED();
9583bc14
EJ
1659 }
1660}
1661
1662static void
46c88433 1663output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
9583bc14
EJ
1664 uint16_t vlan)
1665{
33bf9176 1666 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
9583bc14
EJ
1667 uint16_t vid;
1668 ovs_be16 tci, old_tci;
46c88433 1669 struct xport *xport;
9583bc14 1670
46c88433
EJ
1671 vid = output_vlan_to_vid(out_xbundle, vlan);
1672 if (list_is_empty(&out_xbundle->xports)) {
1673 /* Partially configured bundle with no slaves. Drop the packet. */
1674 return;
1675 } else if (!out_xbundle->bond) {
92c08f09 1676 ctx->use_recirc = false;
46c88433
EJ
1677 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1678 bundle_node);
9583bc14 1679 } else {
84f0f298 1680 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
54ecb5a2 1681 struct flow_wildcards *wc = &ctx->xout->wc;
84f0f298
RW
1682 struct xlate_recirc *xr = &ctx->recirc;
1683 struct ofport_dpif *ofport;
adcf00ba
AZ
1684
1685 if (ctx->xbridge->enable_recirc) {
92c08f09 1686 ctx->use_recirc = bond_may_recirc(
62ac1f20 1687 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
adcf00ba 1688
92c08f09 1689 if (ctx->use_recirc) {
adcf00ba 1690 /* Only TCP mode uses recirculation. */
347bf289 1691 xr->hash_alg = OVS_HASH_ALG_L4;
adcf00ba 1692 bond_update_post_recirc_rules(out_xbundle->bond, false);
54ecb5a2
AZ
1693
1694 /* Recirculation does not require unmasking hash fields. */
1695 wc = NULL;
adcf00ba
AZ
1696 }
1697 }
46c88433 1698
54ecb5a2
AZ
1699 ofport = bond_choose_output_slave(out_xbundle->bond,
1700 &ctx->xin->flow, wc, vid);
84f0f298 1701 xport = xport_lookup(xcfg, ofport);
46c88433
EJ
1702
1703 if (!xport) {
9583bc14
EJ
1704 /* No slaves enabled, so drop packet. */
1705 return;
1706 }
d6fc5f57 1707
b256dc52
JS
1708 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1709 * accounting for this bond. */
92c08f09 1710 if (!ctx->use_recirc) {
b256dc52
JS
1711 if (ctx->xin->resubmit_stats) {
1712 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1713 ctx->xin->resubmit_stats->n_bytes);
1714 }
1715 if (ctx->xin->xcache) {
1716 struct xc_entry *entry;
1717 struct flow *flow;
1718
1719 flow = &ctx->xin->flow;
1720 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1721 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1722 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1723 entry->u.bond.vid = vid;
1724 }
d6fc5f57 1725 }
9583bc14
EJ
1726 }
1727
33bf9176 1728 old_tci = *flow_tci;
9583bc14 1729 tci = htons(vid);
46c88433 1730 if (tci || out_xbundle->use_priority_tags) {
33bf9176 1731 tci |= *flow_tci & htons(VLAN_PCP_MASK);
9583bc14
EJ
1732 if (tci) {
1733 tci |= htons(VLAN_CFI);
1734 }
1735 }
33bf9176 1736 *flow_tci = tci;
9583bc14 1737
46c88433 1738 compose_output_action(ctx, xport->ofp_port);
33bf9176 1739 *flow_tci = old_tci;
9583bc14
EJ
1740}
1741
1742/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1743 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1744 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1745static bool
1746is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1747{
1748 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1749 return false;
1750 }
1751
1752 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1753 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1754 return false;
1755 }
1756
1757 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1758 if (flow->nw_proto == ARP_OP_REPLY) {
1759 return true;
1760 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1761 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1762 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1763
1764 return flow->nw_src == flow->nw_dst;
1765 } else {
1766 return false;
1767 }
1768}
1769
ff69c24a
FL
1770/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1771 * dropped. Returns true if they may be forwarded, false if they should be
1772 * dropped.
1773 *
1774 * 'in_port' must be the xport that corresponds to flow->in_port.
1775 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1776 *
1777 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1778 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1779 * checked by input_vid_is_valid().
1780 *
1781 * May also add tags to '*tags', although the current implementation only does
1782 * so in one special case.
1783 */
1784static bool
1785is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1786 uint16_t vlan)
1787{
1788 struct xbundle *in_xbundle = in_port->xbundle;
1789 const struct xbridge *xbridge = ctx->xbridge;
1790 struct flow *flow = &ctx->xin->flow;
1791
1792 /* Drop frames for reserved multicast addresses
1793 * only if forward_bpdu option is absent. */
1794 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1795 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1796 return false;
1797 }
1798
1799 if (in_xbundle->bond) {
1800 struct mac_entry *mac;
1801
1802 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1803 flow->dl_dst)) {
1804 case BV_ACCEPT:
1805 break;
1806
1807 case BV_DROP:
1808 xlate_report(ctx, "bonding refused admissibility, dropping");
1809 return false;
1810
1811 case BV_DROP_IF_MOVED:
1812 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1813 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
9d078ec2
BP
1814 if (mac
1815 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
1816 && (!is_gratuitous_arp(flow, &ctx->xout->wc)
1817 || mac_entry_is_grat_arp_locked(mac))) {
ff69c24a
FL
1818 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1819 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1820 "dropping");
1821 return false;
1822 }
1823 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1824 break;
1825 }
1826 }
1827
1828 return true;
1829}
1830
ee047520
BP
1831/* Checks whether a MAC learning update is necessary for MAC learning table
1832 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1833 * 'vlan'.
1834 *
1835 * Most packets processed through the MAC learning table do not actually
1836 * change it in any way. This function requires only a read lock on the MAC
1837 * learning table, so it is much cheaper in this common case.
1838 *
1839 * Keep the code here synchronized with that in update_learning_table__()
1840 * below. */
1841static bool
1842is_mac_learning_update_needed(const struct mac_learning *ml,
1843 const struct flow *flow,
1844 struct flow_wildcards *wc,
1845 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1846OVS_REQ_RDLOCK(ml->rwlock)
9583bc14
EJ
1847{
1848 struct mac_entry *mac;
1849
ee047520
BP
1850 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1851 return false;
1852 }
1853
1854 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1855 if (!mac || mac_entry_age(ml, mac)) {
1856 return true;
9583bc14
EJ
1857 }
1858
ee047520
BP
1859 if (is_gratuitous_arp(flow, wc)) {
1860 /* We don't want to learn from gratuitous ARP packets that are
1861 * reflected back over bond slaves so we lock the learning table. */
1862 if (!in_xbundle->bond) {
1863 return true;
1864 } else if (mac_entry_is_grat_arp_locked(mac)) {
1865 return false;
1866 }
1867 }
1868
9d078ec2 1869 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
ee047520
BP
1870}
1871
1872
1873/* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1874 * received on 'in_xbundle' in 'vlan'.
1875 *
1876 * This code repeats all the checks in is_mac_learning_update_needed() because
1877 * the lock was released between there and here and thus the MAC learning state
1878 * could have changed.
1879 *
1880 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1881 * above. */
1882static void
1883update_learning_table__(const struct xbridge *xbridge,
1884 const struct flow *flow, struct flow_wildcards *wc,
1885 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1886OVS_REQ_WRLOCK(xbridge->ml->rwlock)
ee047520
BP
1887{
1888 struct mac_entry *mac;
1889
46c88433 1890 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
ee047520 1891 return;
9583bc14
EJ
1892 }
1893
46c88433 1894 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
9583bc14
EJ
1895 if (is_gratuitous_arp(flow, wc)) {
1896 /* We don't want to learn from gratuitous ARP packets that are
1897 * reflected back over bond slaves so we lock the learning table. */
46c88433 1898 if (!in_xbundle->bond) {
9583bc14
EJ
1899 mac_entry_set_grat_arp_lock(mac);
1900 } else if (mac_entry_is_grat_arp_locked(mac)) {
ee047520 1901 return;
9583bc14
EJ
1902 }
1903 }
1904
9d078ec2 1905 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
9583bc14
EJ
1906 /* The log messages here could actually be useful in debugging,
1907 * so keep the rate limit relatively high. */
1908 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
ee047520 1909
9583bc14
EJ
1910 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1911 "on port %s in VLAN %d",
46c88433
EJ
1912 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1913 in_xbundle->name, vlan);
9583bc14 1914
9d078ec2 1915 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
9583bc14 1916 }
ee047520
BP
1917}
1918
1919static void
1920update_learning_table(const struct xbridge *xbridge,
1921 const struct flow *flow, struct flow_wildcards *wc,
1922 int vlan, struct xbundle *in_xbundle)
1923{
1924 bool need_update;
1925
1926 /* Don't learn the OFPP_NONE port. */
1927 if (in_xbundle == &ofpp_none_bundle) {
1928 return;
1929 }
1930
1931 /* First try the common case: no change to MAC learning table. */
1932 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1933 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1934 in_xbundle);
509c0149 1935 ovs_rwlock_unlock(&xbridge->ml->rwlock);
ee047520
BP
1936
1937 if (need_update) {
1938 /* Slow path: MAC learning table might need an update. */
1939 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1940 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1941 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1942 }
9583bc14
EJ
1943}
1944
86e2dcdd
FL
1945/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1946 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1947static void
1948update_mcast_snooping_table__(const struct xbridge *xbridge,
1949 const struct flow *flow,
1950 struct mcast_snooping *ms,
1951 ovs_be32 ip4, int vlan,
1952 struct xbundle *in_xbundle)
1953 OVS_REQ_WRLOCK(ms->rwlock)
1954{
1955 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1956
1957 switch (ntohs(flow->tp_src)) {
1958 case IGMP_HOST_MEMBERSHIP_REPORT:
1959 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1960 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1961 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1962 IP_FMT" is on port %s in VLAN %d",
1963 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1964 }
1965 break;
1966 case IGMP_HOST_LEAVE_MESSAGE:
1967 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1968 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1969 IP_FMT" is on port %s in VLAN %d",
1970 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1971 }
1972 break;
1973 case IGMP_HOST_MEMBERSHIP_QUERY:
1974 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1975 in_xbundle->ofbundle)) {
1976 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1977 IP_FMT" is on port %s in VLAN %d",
1978 xbridge->name, IP_ARGS(flow->nw_src),
1979 in_xbundle->name, vlan);
1980 }
1981 break;
1982 }
1983}
1984
1985/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1986 * was received on 'in_xbundle' in 'vlan'. */
1987static void
1988update_mcast_snooping_table(const struct xbridge *xbridge,
1989 const struct flow *flow, int vlan,
1990 struct xbundle *in_xbundle)
1991{
1992 struct mcast_snooping *ms = xbridge->ms;
1993 struct xlate_cfg *xcfg;
1994 struct xbundle *mcast_xbundle;
f4ae6e23 1995 struct mcast_port_bundle *fport;
86e2dcdd
FL
1996
1997 /* Don't learn the OFPP_NONE port. */
1998 if (in_xbundle == &ofpp_none_bundle) {
1999 return;
2000 }
2001
2002 /* Don't learn from flood ports */
2003 mcast_xbundle = NULL;
2004 ovs_rwlock_wrlock(&ms->rwlock);
2005 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2006 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2007 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2008 if (mcast_xbundle == in_xbundle) {
2009 break;
2010 }
2011 }
2012
2013 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2014 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
2015 vlan, in_xbundle);
2016 }
2017 ovs_rwlock_unlock(&ms->rwlock);
2018}
2019
2020/* send the packet to ports having the multicast group learned */
2021static void
2022xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2023 struct mcast_snooping *ms OVS_UNUSED,
2024 struct mcast_group *grp,
2025 struct xbundle *in_xbundle, uint16_t vlan)
2026 OVS_REQ_RDLOCK(ms->rwlock)
2027{
2028 struct xlate_cfg *xcfg;
2029 struct mcast_group_bundle *b;
2030 struct xbundle *mcast_xbundle;
2031
2032 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2033 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2034 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2035 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2036 xlate_report(ctx, "forwarding to mcast group port");
2037 output_normal(ctx, mcast_xbundle, vlan);
2038 } else if (!mcast_xbundle) {
2039 xlate_report(ctx, "mcast group port is unknown, dropping");
2040 } else {
2041 xlate_report(ctx, "mcast group port is input port, dropping");
2042 }
2043 }
2044}
2045
2046/* send the packet to ports connected to multicast routers */
2047static void
2048xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2049 struct mcast_snooping *ms,
2050 struct xbundle *in_xbundle, uint16_t vlan)
2051 OVS_REQ_RDLOCK(ms->rwlock)
2052{
2053 struct xlate_cfg *xcfg;
2054 struct mcast_mrouter_bundle *mrouter;
2055 struct xbundle *mcast_xbundle;
2056
2057 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2058 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2059 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2060 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2061 xlate_report(ctx, "forwarding to mcast router port");
2062 output_normal(ctx, mcast_xbundle, vlan);
2063 } else if (!mcast_xbundle) {
2064 xlate_report(ctx, "mcast router port is unknown, dropping");
2065 } else {
2066 xlate_report(ctx, "mcast router port is input port, dropping");
2067 }
2068 }
2069}
2070
2071/* send the packet to ports flagged to be flooded */
2072static void
2073xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2074 struct mcast_snooping *ms,
2075 struct xbundle *in_xbundle, uint16_t vlan)
2076 OVS_REQ_RDLOCK(ms->rwlock)
2077{
2078 struct xlate_cfg *xcfg;
f4ae6e23 2079 struct mcast_port_bundle *fport;
86e2dcdd
FL
2080 struct xbundle *mcast_xbundle;
2081
2082 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2083 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2084 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2085 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2086 xlate_report(ctx, "forwarding to mcast flood port");
2087 output_normal(ctx, mcast_xbundle, vlan);
2088 } else if (!mcast_xbundle) {
2089 xlate_report(ctx, "mcast flood port is unknown, dropping");
2090 } else {
2091 xlate_report(ctx, "mcast flood port is input port, dropping");
2092 }
2093 }
2094}
2095
8e04a33f
FL
2096/* forward the Reports to configured ports */
2097static void
2098xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2099 struct mcast_snooping *ms,
2100 struct xbundle *in_xbundle, uint16_t vlan)
2101 OVS_REQ_RDLOCK(ms->rwlock)
2102{
2103 struct xlate_cfg *xcfg;
2104 struct mcast_port_bundle *rport;
2105 struct xbundle *mcast_xbundle;
2106
2107 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2108 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2109 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2110 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2111 xlate_report(ctx, "forwarding Report to mcast flagged port");
2112 output_normal(ctx, mcast_xbundle, vlan);
2113 } else if (!mcast_xbundle) {
2114 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2115 } else {
2116 xlate_report(ctx, "mcast port is input port, dropping the Report");
2117 }
2118 }
2119}
2120
682800a4
FL
2121static void
2122xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2123 uint16_t vlan)
2124{
2125 struct xbundle *xbundle;
2126
2127 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2128 if (xbundle != in_xbundle
2129 && xbundle_includes_vlan(xbundle, vlan)
2130 && xbundle->floodable
2131 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2132 output_normal(ctx, xbundle, vlan);
2133 }
2134 }
2135 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2136}
2137
9583bc14
EJ
2138static void
2139xlate_normal(struct xlate_ctx *ctx)
2140{
33bf9176
BP
2141 struct flow_wildcards *wc = &ctx->xout->wc;
2142 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
2143 struct xbundle *in_xbundle;
2144 struct xport *in_port;
9583bc14 2145 struct mac_entry *mac;
d6d5bbc9 2146 void *mac_port;
9583bc14
EJ
2147 uint16_t vlan;
2148 uint16_t vid;
2149
2150 ctx->xout->has_normal = true;
2151
33bf9176
BP
2152 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2153 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1dd35f8a 2154 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 2155
46c88433
EJ
2156 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2157 ctx->xin->packet != NULL, &in_port);
2158 if (!in_xbundle) {
9583bc14
EJ
2159 xlate_report(ctx, "no input bundle, dropping");
2160 return;
2161 }
2162
2163 /* Drop malformed frames. */
33bf9176
BP
2164 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2165 !(flow->vlan_tci & htons(VLAN_CFI))) {
9583bc14
EJ
2166 if (ctx->xin->packet != NULL) {
2167 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2168 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2169 "VLAN tag received on port %s",
46c88433 2170 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2171 }
2172 xlate_report(ctx, "partial VLAN tag, dropping");
2173 return;
2174 }
2175
2176 /* Drop frames on bundles reserved for mirroring. */
46c88433 2177 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14
EJ
2178 if (ctx->xin->packet != NULL) {
2179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2180 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2181 "%s, which is reserved exclusively for mirroring",
46c88433 2182 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2183 }
2184 xlate_report(ctx, "input port is mirror output port, dropping");
2185 return;
2186 }
2187
2188 /* Check VLAN. */
33bf9176 2189 vid = vlan_tci_to_vid(flow->vlan_tci);
46c88433 2190 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
2191 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2192 return;
2193 }
46c88433 2194 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14
EJ
2195
2196 /* Check other admissibility requirements. */
2197 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2198 return;
2199 }
2200
2201 /* Learn source MAC. */
2202 if (ctx->xin->may_learn) {
46c88433 2203 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
9583bc14 2204 }
b256dc52
JS
2205 if (ctx->xin->xcache) {
2206 struct xc_entry *entry;
2207
2208 /* Save enough info to update mac learning table later. */
2209 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
9edf6b48 2210 entry->u.normal.ofproto = ctx->xbridge->ofproto;
b256dc52
JS
2211 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2212 entry->u.normal.vlan = vlan;
2213 }
9583bc14
EJ
2214
2215 /* Determine output bundle. */
86e2dcdd
FL
2216 if (mcast_snooping_enabled(ctx->xbridge->ms)
2217 && !eth_addr_is_broadcast(flow->dl_dst)
2218 && eth_addr_is_multicast(flow->dl_dst)
2219 && flow->dl_type == htons(ETH_TYPE_IP)) {
2220 struct mcast_snooping *ms = ctx->xbridge->ms;
2221 struct mcast_group *grp;
2222
2223 if (flow->nw_proto == IPPROTO_IGMP) {
2224 if (ctx->xin->may_learn) {
2225 if (mcast_snooping_is_membership(flow->tp_src) ||
2226 mcast_snooping_is_query(flow->tp_src)) {
2227 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2228 in_xbundle);
2229 }
2230 }
d6d5bbc9 2231
86e2dcdd
FL
2232 if (mcast_snooping_is_membership(flow->tp_src)) {
2233 ovs_rwlock_rdlock(&ms->rwlock);
2234 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
8e04a33f
FL
2235 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2236 * forward IGMP Membership Reports only to those ports where
2237 * multicast routers are attached. Alternatively stated: a
2238 * snooping switch should not forward IGMP Membership Reports
2239 * to ports on which only hosts are attached.
2240 * An administrative control may be provided to override this
2241 * restriction, allowing the report messages to be flooded to
2242 * other ports. */
2243 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
86e2dcdd
FL
2244 ovs_rwlock_unlock(&ms->rwlock);
2245 } else {
2246 xlate_report(ctx, "multicast traffic, flooding");
2247 xlate_normal_flood(ctx, in_xbundle, vlan);
2248 }
2249 return;
2250 } else {
2251 if (ip_is_local_multicast(flow->nw_dst)) {
2252 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2253 * address in the 224.0.0.x range which are not IGMP must
2254 * be forwarded on all ports */
2255 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2256 xlate_normal_flood(ctx, in_xbundle, vlan);
2257 return;
2258 }
2259 }
2260
2261 /* forwarding to group base ports */
2262 ovs_rwlock_rdlock(&ms->rwlock);
2263 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2264 if (grp) {
2265 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2266 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2267 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
9583bc14 2268 } else {
86e2dcdd
FL
2269 if (mcast_snooping_flood_unreg(ms)) {
2270 xlate_report(ctx, "unregistered multicast, flooding");
2271 xlate_normal_flood(ctx, in_xbundle, vlan);
2272 } else {
2273 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2274 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2275 }
9583bc14 2276 }
86e2dcdd 2277 ovs_rwlock_unlock(&ms->rwlock);
9583bc14 2278 } else {
86e2dcdd
FL
2279 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2280 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
9d078ec2 2281 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
86e2dcdd
FL
2282 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2283
2284 if (mac_port) {
2285 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2286 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2287 if (mac_xbundle && mac_xbundle != in_xbundle) {
2288 xlate_report(ctx, "forwarding to learned port");
2289 output_normal(ctx, mac_xbundle, vlan);
2290 } else if (!mac_xbundle) {
2291 xlate_report(ctx, "learned port is unknown, dropping");
2292 } else {
2293 xlate_report(ctx, "learned port is input port, dropping");
2294 }
2295 } else {
2296 xlate_report(ctx, "no learned MAC for destination, flooding");
2297 xlate_normal_flood(ctx, in_xbundle, vlan);
2298 }
9583bc14
EJ
2299 }
2300}
2301
2302/* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2303 * the number of packets out of UINT32_MAX to sample. The given
2304 * cookie is passed back in the callback for each sampled packet.
2305 */
2306static size_t
46c88433 2307compose_sample_action(const struct xbridge *xbridge,
9583bc14
EJ
2308 struct ofpbuf *odp_actions,
2309 const struct flow *flow,
2310 const uint32_t probability,
2311 const union user_action_cookie *cookie,
8b7ea2d4
WZ
2312 const size_t cookie_size,
2313 const odp_port_t tunnel_out_port)
9583bc14
EJ
2314{
2315 size_t sample_offset, actions_offset;
89a8a7f0 2316 odp_port_t odp_port;
9583bc14 2317 int cookie_offset;
89a8a7f0 2318 uint32_t pid;
9583bc14
EJ
2319
2320 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2321
2322 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2323
2324 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
89a8a7f0
EJ
2325
2326 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
9a159f74
AW
2327 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2328 flow_hash_5tuple(flow, 0));
2329 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
8b7ea2d4 2330 tunnel_out_port, odp_actions);
9583bc14
EJ
2331
2332 nl_msg_end_nested(odp_actions, actions_offset);
2333 nl_msg_end_nested(odp_actions, sample_offset);
2334 return cookie_offset;
2335}
2336
2337static void
46c88433
EJ
2338compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2339 odp_port_t odp_port, unsigned int n_outputs,
2340 union user_action_cookie *cookie)
9583bc14
EJ
2341{
2342 int ifindex;
2343
2344 cookie->type = USER_ACTION_COOKIE_SFLOW;
2345 cookie->sflow.vlan_tci = vlan_tci;
2346
2347 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2348 * port information") for the interpretation of cookie->output. */
2349 switch (n_outputs) {
2350 case 0:
2351 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2352 cookie->sflow.output = 0x40000000 | 256;
2353 break;
2354
2355 case 1:
46c88433 2356 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
9583bc14
EJ
2357 if (ifindex) {
2358 cookie->sflow.output = ifindex;
2359 break;
2360 }
2361 /* Fall through. */
2362 default:
2363 /* 0x80000000 means "multiple output ports. */
2364 cookie->sflow.output = 0x80000000 | n_outputs;
2365 break;
2366 }
2367}
2368
2369/* Compose SAMPLE action for sFlow bridge sampling. */
2370static size_t
46c88433 2371compose_sflow_action(const struct xbridge *xbridge,
9583bc14
EJ
2372 struct ofpbuf *odp_actions,
2373 const struct flow *flow,
4e022ec0 2374 odp_port_t odp_port)
9583bc14
EJ
2375{
2376 uint32_t probability;
2377 union user_action_cookie cookie;
2378
46c88433 2379 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2380 return 0;
2381 }
2382
46c88433
EJ
2383 probability = dpif_sflow_get_probability(xbridge->sflow);
2384 compose_sflow_cookie(xbridge, htons(0), odp_port,
4e022ec0 2385 odp_port == ODPP_NONE ? 0 : 1, &cookie);
9583bc14 2386
46c88433 2387 return compose_sample_action(xbridge, odp_actions, flow, probability,
8b7ea2d4 2388 &cookie, sizeof cookie.sflow, ODPP_NONE);
9583bc14
EJ
2389}
2390
2391static void
2392compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2393 uint32_t obs_domain_id, uint32_t obs_point_id,
2394 union user_action_cookie *cookie)
2395{
2396 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2397 cookie->flow_sample.probability = probability;
2398 cookie->flow_sample.collector_set_id = collector_set_id;
2399 cookie->flow_sample.obs_domain_id = obs_domain_id;
2400 cookie->flow_sample.obs_point_id = obs_point_id;
2401}
2402
2403static void
8b7ea2d4
WZ
2404compose_ipfix_cookie(union user_action_cookie *cookie,
2405 odp_port_t output_odp_port)
9583bc14
EJ
2406{
2407 cookie->type = USER_ACTION_COOKIE_IPFIX;
8b7ea2d4 2408 cookie->ipfix.output_odp_port = output_odp_port;
9583bc14
EJ
2409}
2410
2411/* Compose SAMPLE action for IPFIX bridge sampling. */
2412static void
46c88433 2413compose_ipfix_action(const struct xbridge *xbridge,
9583bc14 2414 struct ofpbuf *odp_actions,
8b7ea2d4
WZ
2415 const struct flow *flow,
2416 odp_port_t output_odp_port)
9583bc14
EJ
2417{
2418 uint32_t probability;
2419 union user_action_cookie cookie;
8b7ea2d4 2420 odp_port_t tunnel_out_port = ODPP_NONE;
9583bc14 2421
46c88433 2422 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2423 return;
2424 }
2425
8b7ea2d4
WZ
2426 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2427 * number. */
2428 if (output_odp_port == ODPP_NONE &&
2429 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2430 return;
2431 }
2432
2433 /* For output case, output_odp_port is valid*/
2434 if (output_odp_port != ODPP_NONE) {
2435 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2436 return;
2437 }
2438 /* If tunnel sampling is enabled, put an additional option attribute:
2439 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2440 */
2441 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2442 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2443 tunnel_out_port = output_odp_port;
2444 }
2445 }
2446
46c88433 2447 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
8b7ea2d4 2448 compose_ipfix_cookie(&cookie, output_odp_port);
9583bc14 2449
46c88433 2450 compose_sample_action(xbridge, odp_actions, flow, probability,
8b7ea2d4 2451 &cookie, sizeof cookie.ipfix, tunnel_out_port);
9583bc14
EJ
2452}
2453
2454/* SAMPLE action for sFlow must be first action in any given list of
2455 * actions. At this point we do not have all information required to
2456 * build it. So try to build sample action as complete as possible. */
2457static void
2458add_sflow_action(struct xlate_ctx *ctx)
2459{
46c88433 2460 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
cc377352 2461 ctx->xout->odp_actions,
4e022ec0 2462 &ctx->xin->flow, ODPP_NONE);
9583bc14
EJ
2463 ctx->sflow_odp_port = 0;
2464 ctx->sflow_n_outputs = 0;
2465}
2466
2467/* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2468 * of actions, eventually after the SAMPLE action for sFlow. */
2469static void
2470add_ipfix_action(struct xlate_ctx *ctx)
2471{
cc377352 2472 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
8b7ea2d4
WZ
2473 &ctx->xin->flow, ODPP_NONE);
2474}
2475
2476static void
2477add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2478{
2479 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2480 &ctx->xin->flow, port);
9583bc14
EJ
2481}
2482
2483/* Fix SAMPLE action according to data collected while composing ODP actions.
2484 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2485 * USERSPACE action's user-cookie which is required for sflow. */
2486static void
2487fix_sflow_action(struct xlate_ctx *ctx)
2488{
2489 const struct flow *base = &ctx->base_flow;
2490 union user_action_cookie *cookie;
2491
2492 if (!ctx->user_cookie_offset) {
2493 return;
2494 }
2495
cc377352 2496 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
9583bc14
EJ
2497 sizeof cookie->sflow);
2498 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2499
46c88433 2500 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
9583bc14
EJ
2501 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2502}
2503
db7d4e46 2504static enum slow_path_reason
642dc74d 2505process_special(struct xlate_ctx *ctx, const struct flow *flow,
cf62fa4c 2506 const struct xport *xport, const struct dp_packet *packet)
db7d4e46 2507{
642dc74d 2508 struct flow_wildcards *wc = &ctx->xout->wc;
46c88433 2509 const struct xbridge *xbridge = ctx->xbridge;
642dc74d 2510
46c88433 2511 if (!xport) {
db7d4e46 2512 return 0;
46c88433 2513 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 2514 if (packet) {
46c88433 2515 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46
JP
2516 }
2517 return SLOW_CFM;
fab52e16 2518 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 2519 if (packet) {
46c88433 2520 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
2521 /* If POLL received, immediately sends FINAL back. */
2522 if (bfd_should_send_packet(xport->bfd)) {
6d308b28 2523 ofproto_dpif_monitor_port_send_soon(xport->ofport);
60d02c72 2524 }
db7d4e46
JP
2525 }
2526 return SLOW_BFD;
46c88433 2527 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
2528 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2529 if (packet) {
46c88433 2530 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46
JP
2531 }
2532 return SLOW_LACP;
9efd308e
DV
2533 } else if ((xbridge->stp || xbridge->rstp) &&
2534 stp_should_process_flow(flow, wc)) {
db7d4e46 2535 if (packet) {
f025bcb7
JR
2536 xbridge->stp
2537 ? stp_process_packet(xport, packet)
2538 : rstp_process_packet(xport, packet);
db7d4e46
JP
2539 }
2540 return SLOW_STP;
0477baa9
DF
2541 } else if (xport->lldp && lldp_should_process_flow(flow)) {
2542 if (packet) {
2543 lldp_process_packet(xport->lldp, packet);
2544 }
2545 return SLOW_LLDP;
db7d4e46
JP
2546 } else {
2547 return 0;
2548 }
2549}
2550
a36de779
PS
2551static int
2552tnl_route_lookup_flow(const struct flow *oflow,
2553 ovs_be32 *ip, struct xport **out_port)
2554{
2555 char out_dev[IFNAMSIZ];
2556 struct xbridge *xbridge;
2557 struct xlate_cfg *xcfg;
2558 ovs_be32 gw;
2559
2560 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2561 return -ENOENT;
2562 }
2563
2564 if (gw) {
2565 *ip = gw;
2566 } else {
2567 *ip = oflow->tunnel.ip_dst;
2568 }
2569
2570 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2571 ovs_assert(xcfg);
2572
2573 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2574 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2575 struct xport *port;
2576
2577 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2578 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2579 *out_port = port;
2580 return 0;
2581 }
2582 }
2583 }
2584 }
2585 return -ENOENT;
2586}
2587
2588static int
cf62fa4c 2589xlate_flood_packet(struct xbridge *xbridge, struct dp_packet *packet)
a36de779
PS
2590{
2591 struct ofpact_output output;
2592 struct flow flow;
2593
2594 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2595 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
cf62fa4c 2596 flow_extract(packet, &flow);
a36de779
PS
2597 flow.in_port.ofp_port = OFPP_NONE;
2598 output.port = OFPP_FLOOD;
2599 output.max_len = 0;
2600
2601 return ofproto_dpif_execute_actions(xbridge->ofproto, &flow, NULL,
2602 &output.ofpact, sizeof output,
2603 packet);
2604}
2605
2606static void
2607tnl_send_arp_request(const struct xport *out_dev, const uint8_t eth_src[ETH_ADDR_LEN],
2608 ovs_be32 ip_src, ovs_be32 ip_dst)
2609{
2610 struct xbridge *xbridge = out_dev->xbridge;
cf62fa4c 2611 struct dp_packet packet;
a36de779 2612
cf62fa4c 2613 dp_packet_init(&packet, 0);
a36de779
PS
2614 compose_arp(&packet, eth_src, ip_src, ip_dst);
2615
2616 xlate_flood_packet(xbridge, &packet);
cf62fa4c 2617 dp_packet_uninit(&packet);
a36de779
PS
2618}
2619
2620static int
2621build_tunnel_send(const struct xlate_ctx *ctx, const struct xport *xport,
2622 const struct flow *flow, odp_port_t tunnel_odp_port)
2623{
2624 struct ovs_action_push_tnl tnl_push_data;
2625 struct xport *out_dev = NULL;
2626 ovs_be32 s_ip, d_ip = 0;
2627 uint8_t smac[ETH_ADDR_LEN];
2628 uint8_t dmac[ETH_ADDR_LEN];
2629 int err;
2630
2631 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2632 if (err) {
2633 return err;
2634 }
2635
2636 /* Use mac addr of bridge port of the peer. */
2637 err = netdev_get_etheraddr(out_dev->netdev, smac);
2638 if (err) {
2639 return err;
2640 }
2641
2642 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2643 if (err) {
2644 return err;
2645 }
2646
2647 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, dmac);
2648 if (err) {
2649 tnl_send_arp_request(out_dev, smac, s_ip, d_ip);
2650 return err;
2651 }
2652 if (ctx->xin->xcache) {
2653 struct xc_entry *entry;
2654
2655 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
8742957c
BP
2656 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2657 sizeof entry->u.tnl_arp_cache.br_name);
a36de779
PS
2658 entry->u.tnl_arp_cache.d_ip = d_ip;
2659 }
2660 err = tnl_port_build_header(xport->ofport, flow,
2661 dmac, smac, s_ip, &tnl_push_data);
2662 if (err) {
2663 return err;
2664 }
2665 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2666 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
2667 odp_put_tnl_push_action(ctx->xout->odp_actions, &tnl_push_data);
2668 return 0;
2669}
2670
9583bc14 2671static void
4e022ec0 2672compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
9583bc14
EJ
2673 bool check_stp)
2674{
46c88433 2675 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1dd35f8a 2676 struct flow_wildcards *wc = &ctx->xout->wc;
33bf9176 2677 struct flow *flow = &ctx->xin->flow;
a36de779 2678 struct flow_tnl flow_tnl;
9583bc14 2679 ovs_be16 flow_vlan_tci;
1362e248 2680 uint32_t flow_pkt_mark;
9583bc14 2681 uint8_t flow_nw_tos;
4e022ec0 2682 odp_port_t out_port, odp_port;
a36de779 2683 bool tnl_push_pop_send = false;
ca077186 2684 uint8_t dscp;
9583bc14
EJ
2685
2686 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2687 * before traversing a patch port. */
ac6073e3 2688 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
a36de779 2689 memset(&flow_tnl, 0, sizeof flow_tnl);
9583bc14 2690
46c88433 2691 if (!xport) {
9583bc14
EJ
2692 xlate_report(ctx, "Nonexistent output port");
2693 return;
46c88433 2694 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
9583bc14
EJ
2695 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2696 return;
0d1cee12 2697 } else if (check_stp) {
bbbca389 2698 if (is_stp(&ctx->base_flow)) {
9efd308e
DV
2699 if (!xport_stp_should_forward_bpdu(xport) &&
2700 !xport_rstp_should_manage_bpdu(xport)) {
2701 if (ctx->xbridge->stp != NULL) {
2702 xlate_report(ctx, "STP not in listening state, "
2703 "skipping bpdu output");
2704 } else if (ctx->xbridge->rstp != NULL) {
2705 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2706 "skipping bpdu output");
2707 }
0d1cee12
K
2708 return;
2709 }
9efd308e
DV
2710 } else if (!xport_stp_forward_state(xport) ||
2711 !xport_rstp_forward_state(xport)) {
2712 if (ctx->xbridge->stp != NULL) {
2713 xlate_report(ctx, "STP not in forwarding state, "
2714 "skipping output");
2715 } else if (ctx->xbridge->rstp != NULL) {
2716 xlate_report(ctx, "RSTP not in forwarding state, "
2717 "skipping output");
2718 }
0d1cee12
K
2719 return;
2720 }
9583bc14
EJ
2721 }
2722
46c88433
EJ
2723 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2724 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2725 xport->xbundle);
cdf5d3a5
EJ
2726 }
2727
46c88433
EJ
2728 if (xport->peer) {
2729 const struct xport *peer = xport->peer;
9583bc14 2730 struct flow old_flow = ctx->xin->flow;
9583bc14 2731 enum slow_path_reason special;
0c7812e5 2732 uint8_t table_id = rule_dpif_lookup_get_init_table_id(&ctx->xin->flow);
9583bc14 2733
46c88433
EJ
2734 ctx->xbridge = peer->xbridge;
2735 flow->in_port.ofp_port = peer->ofp_port;
33bf9176
BP
2736 flow->metadata = htonll(0);
2737 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2738 memset(flow->regs, 0, sizeof flow->regs);
c61f3870 2739 flow->actset_output = OFPP_UNSET;
9583bc14 2740
642dc74d 2741 special = process_special(ctx, &ctx->xin->flow, peer,
9583bc14
EJ
2742 ctx->xin->packet);
2743 if (special) {
04594cd5 2744 ctx->xout->slow |= special;
ddd3c975 2745 } else if (may_receive(peer, ctx)) {
9efd308e 2746 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
0c7812e5
AW
2747 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2748 true, true);
9583bc14 2749 } else {
9efd308e
DV
2750 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2751 * the learning action look at the packet, then drop it. */
9583bc14 2752 struct flow old_base_flow = ctx->base_flow;
6fd6ed71 2753 size_t old_size = ctx->xout->odp_actions->size;
cdf5d3a5 2754 mirror_mask_t old_mirrors = ctx->xout->mirrors;
0c7812e5
AW
2755 xlate_table_action(ctx, flow->in_port.ofp_port, table_id,
2756 true, true);
cdf5d3a5 2757 ctx->xout->mirrors = old_mirrors;
9583bc14 2758 ctx->base_flow = old_base_flow;
6fd6ed71 2759 ctx->xout->odp_actions->size = old_size;
9583bc14
EJ
2760 }
2761 }
2762
2763 ctx->xin->flow = old_flow;
832554e3 2764 ctx->xbridge = xport->xbridge;
9583bc14
EJ
2765
2766 if (ctx->xin->resubmit_stats) {
46c88433
EJ
2767 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2768 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
a1aeea86
AW
2769 if (peer->bfd) {
2770 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2771 }
9583bc14 2772 }
b256dc52
JS
2773 if (ctx->xin->xcache) {
2774 struct xc_entry *entry;
2775
2776 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2777 entry->u.dev.tx = netdev_ref(xport->netdev);
2778 entry->u.dev.rx = netdev_ref(peer->netdev);
2779 entry->u.dev.bfd = bfd_ref(peer->bfd);
2780 }
9583bc14
EJ
2781 return;
2782 }
2783
33bf9176 2784 flow_vlan_tci = flow->vlan_tci;
1362e248 2785 flow_pkt_mark = flow->pkt_mark;
33bf9176 2786 flow_nw_tos = flow->nw_tos;
9583bc14 2787
16194afd
DDP
2788 if (count_skb_priorities(xport)) {
2789 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2790 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2791 wc->masks.nw_tos |= IP_DSCP_MASK;
2792 flow->nw_tos &= ~IP_DSCP_MASK;
2793 flow->nw_tos |= dscp;
2794 }
9583bc14
EJ
2795 }
2796
46c88433 2797 if (xport->is_tunnel) {
9583bc14
EJ
2798 /* Save tunnel metadata so that changes made due to
2799 * the Logical (tunnel) Port are not visible for any further
2800 * matches, while explicit set actions on tunnel metadata are.
2801 */
a36de779 2802 flow_tnl = flow->tunnel;
46c88433 2803 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
4e022ec0 2804 if (odp_port == ODPP_NONE) {
9583bc14
EJ
2805 xlate_report(ctx, "Tunneling decided against output");
2806 goto out; /* restore flow_nw_tos */
2807 }
33bf9176 2808 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
9583bc14
EJ
2809 xlate_report(ctx, "Not tunneling to our own address");
2810 goto out; /* restore flow_nw_tos */
2811 }
2812 if (ctx->xin->resubmit_stats) {
46c88433 2813 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14 2814 }
b256dc52
JS
2815 if (ctx->xin->xcache) {
2816 struct xc_entry *entry;
2817
2818 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2819 entry->u.dev.tx = netdev_ref(xport->netdev);
2820 }
9583bc14 2821 out_port = odp_port;
a36de779
PS
2822 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2823 tnl_push_pop_send = true;
2824 } else {
2825 commit_odp_tunnel_action(flow, &ctx->base_flow,
2826 ctx->xout->odp_actions);
2827 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2828 }
9583bc14 2829 } else {
46c88433 2830 odp_port = xport->odp_port;
7614e5d0 2831 out_port = odp_port;
46c88433 2832 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
7614e5d0
JR
2833 ofp_port_t vlandev_port;
2834
1dd35f8a 2835 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
7614e5d0
JR
2836 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2837 ofp_port, flow->vlan_tci);
2838 if (vlandev_port != ofp_port) {
2839 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2840 flow->vlan_tci = htons(0);
2841 }
9583bc14 2842 }
9583bc14 2843 }
9583bc14 2844
4e022ec0 2845 if (out_port != ODPP_NONE) {
7fd91025 2846 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
cc377352 2847 ctx->xout->odp_actions,
a36de779 2848 wc,
d23df9a8 2849 ctx->xbridge->masked_set_action);
adcf00ba 2850
92c08f09 2851 if (ctx->use_recirc) {
347bf289 2852 struct ovs_action_hash *act_hash;
92c08f09 2853 struct xlate_recirc *xr = &ctx->recirc;
adcf00ba 2854
347bf289 2855 /* Hash action. */
cc377352 2856 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
347bf289
AZ
2857 OVS_ACTION_ATTR_HASH,
2858 sizeof *act_hash);
2859 act_hash->hash_alg = xr->hash_alg;
62ac1f20 2860 act_hash->hash_basis = xr->hash_basis;
347bf289
AZ
2861
2862 /* Recirc action. */
cc377352 2863 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
347bf289 2864 xr->recirc_id);
adcf00ba 2865 } else {
a36de779
PS
2866
2867 if (tnl_push_pop_send) {
2868 build_tunnel_send(ctx, xport, flow, odp_port);
2869 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2870 } else {
2871 odp_port_t odp_tnl_port = ODPP_NONE;
2872
2873 /* XXX: Write better Filter for tunnel port. We can use inport
2874 * int tunnel-port flow to avoid these checks completely. */
2875 if (ofp_port == OFPP_LOCAL &&
2876 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2877
2878 odp_tnl_port = tnl_port_map_lookup(flow, wc);
2879 }
2880
2881 if (odp_tnl_port != ODPP_NONE) {
2882 nl_msg_put_odp_port(ctx->xout->odp_actions,
2883 OVS_ACTION_ATTR_TUNNEL_POP,
2884 odp_tnl_port);
2885 } else {
2886 /* Tunnel push-pop action is not compatible with
2887 * IPFIX action. */
2888 add_ipfix_output_action(ctx, out_port);
2889 nl_msg_put_odp_port(ctx->xout->odp_actions,
2890 OVS_ACTION_ATTR_OUTPUT,
2891 out_port);
2892 }
2893 }
adcf00ba 2894 }
9583bc14 2895
6cbbf4fa
EJ
2896 ctx->sflow_odp_port = odp_port;
2897 ctx->sflow_n_outputs++;
2898 ctx->xout->nf_output_iface = ofp_port;
2899 }
2900
2901 out:
9583bc14 2902 /* Restore flow */
33bf9176 2903 flow->vlan_tci = flow_vlan_tci;
1362e248 2904 flow->pkt_mark = flow_pkt_mark;
33bf9176 2905 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
2906}
2907
2908static void
4e022ec0 2909compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
9583bc14
EJ
2910{
2911 compose_output_action__(ctx, ofp_port, true);
2912}
2913
bb61b33d
BP
2914static void
2915xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
2916{
2917 struct rule_dpif *old_rule = ctx->rule;
dc723c44 2918 const struct rule_actions *actions;
bb61b33d
BP
2919
2920 if (ctx->xin->resubmit_stats) {
70742c7f 2921 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
2922 }
2923
98b07853 2924 ctx->resubmits++;
bb61b33d
BP
2925 ctx->recurse++;
2926 ctx->rule = rule;
6f00e29b
BP
2927 actions = rule_dpif_get_actions(rule);
2928 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
bb61b33d
BP
2929 ctx->rule = old_rule;
2930 ctx->recurse--;
bb61b33d
BP
2931}
2932
bd3240ba
SH
2933static bool
2934xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 2935{
98b07853
BP
2936 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2937
adcf00ba 2938 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
98b07853
BP
2939 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2940 MAX_RESUBMIT_RECURSION);
adcf00ba 2941 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
98b07853 2942 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
6fd6ed71 2943 } else if (ctx->xout->odp_actions->size > UINT16_MAX) {
98b07853 2944 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
6fd6ed71 2945 } else if (ctx->stack.size >= 65536) {
98b07853
BP
2946 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2947 } else {
bd3240ba
SH
2948 return true;
2949 }
2950
2951 return false;
2952}
2953
2954static void
6d328fa2
SH
2955xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2956 bool may_packet_in, bool honor_table_miss)
bd3240ba
SH
2957{
2958 if (xlate_resubmit_resource_check(ctx)) {
34dd0d78 2959 struct flow_wildcards *wc;
9583bc14 2960 uint8_t old_table_id = ctx->table_id;
3f207910 2961 struct rule_dpif *rule;
9583bc14
EJ
2962
2963 ctx->table_id = table_id;
34dd0d78 2964 wc = (ctx->xin->skip_wildcards) ? NULL : &ctx->xout->wc;
9583bc14 2965
34dd0d78
JR
2966 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2967 &ctx->xin->flow, wc,
2968 ctx->xin->xcache != NULL,
2969 ctx->xin->resubmit_stats,
2970 &ctx->table_id, in_port,
2971 may_packet_in, honor_table_miss);
9583bc14 2972
a8c31348
BP
2973 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
2974 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
ad3efdcb
EJ
2975 }
2976
a2143702 2977 if (rule) {
83709dfa
JR
2978 /* Fill in the cache entry here instead of xlate_recursively
2979 * to make the reference counting more explicit. We take a
2980 * reference in the lookups above if we are going to cache the
2981 * rule. */
2982 if (ctx->xin->xcache) {
2983 struct xc_entry *entry;
2984
2985 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2986 entry->u.rule = rule;
2987 }
bb61b33d 2988 xlate_recursively(ctx, rule);
ad3efdcb
EJ
2989 }
2990
9583bc14 2991 ctx->table_id = old_table_id;
98b07853 2992 return;
9583bc14 2993 }
98b07853
BP
2994
2995 ctx->exit = true;
9583bc14
EJ
2996}
2997
f4fb341b 2998static void
1e684d7d
RW
2999xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3000 struct ofputil_bucket *bucket)
3001{
3002 if (ctx->xin->resubmit_stats) {
3003 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3004 }
3005 if (ctx->xin->xcache) {
3006 struct xc_entry *entry;
3007
3008 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3009 entry->u.group.group = group_dpif_ref(group);
3010 entry->u.group.bucket = bucket;
3011 }
3012}
3013
3014static void
3015xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
f4fb341b
SH
3016{
3017 uint64_t action_list_stub[1024 / 8];
3018 struct ofpbuf action_list, action_set;
3019
3020 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3021 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3022
3023 ofpacts_execute_action_set(&action_list, &action_set);
3024 ctx->recurse++;
6fd6ed71 3025 do_xlate_actions(action_list.data, action_list.size, ctx);
f4fb341b
SH
3026 ctx->recurse--;
3027
3028 ofpbuf_uninit(&action_set);
3029 ofpbuf_uninit(&action_list);
3030}
3031
3032static void
3033xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3034{
1e684d7d 3035 struct ofputil_bucket *bucket;
ca6ba700 3036 const struct ovs_list *buckets;
f4fb341b
SH
3037 struct flow old_flow = ctx->xin->flow;
3038
3039 group_dpif_get_buckets(group, &buckets);
3040
3041 LIST_FOR_EACH (bucket, list_node, buckets) {
3042 xlate_group_bucket(ctx, bucket);
3043 /* Roll back flow to previous state.
3044 * This is equivalent to cloning the packet for each bucket.
3045 *
3046 * As a side effect any subsequently applied actions will
3047 * also effectively be applied to a clone of the packet taken
3048 * just before applying the all or indirect group. */
3049 ctx->xin->flow = old_flow;
3050 }
1e684d7d 3051 xlate_group_stats(ctx, group, NULL);
f4fb341b
SH
3052}
3053
dd8cd4b4
SH
3054static void
3055xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3056{
1e684d7d 3057 struct ofputil_bucket *bucket;
dd8cd4b4
SH
3058
3059 bucket = group_first_live_bucket(ctx, group, 0);
3060 if (bucket) {
3061 xlate_group_bucket(ctx, bucket);
1e684d7d 3062 xlate_group_stats(ctx, group, bucket);
dd8cd4b4
SH
3063 }
3064}
3065
fe7e5749
SH
3066static void
3067xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3068{
3069 struct flow_wildcards *wc = &ctx->xout->wc;
1e684d7d 3070 struct ofputil_bucket *bucket;
fe7e5749
SH
3071 uint32_t basis;
3072
1d1aae0b 3073 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
fe7e5749
SH
3074 bucket = group_best_live_bucket(ctx, group, basis);
3075 if (bucket) {
3076 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1d1aae0b
SS
3077 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3078 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3079 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3080 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3081 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3082 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3083 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3084 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3085
fe7e5749 3086 xlate_group_bucket(ctx, bucket);
1e684d7d 3087 xlate_group_stats(ctx, group, bucket);
fe7e5749
SH
3088 }
3089}
3090
f4fb341b
SH
3091static void
3092xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3093{
5a070238
BP
3094 ctx->in_group = true;
3095
f4fb341b
SH
3096 switch (group_dpif_get_type(group)) {
3097 case OFPGT11_ALL:
3098 case OFPGT11_INDIRECT:
3099 xlate_all_group(ctx, group);
3100 break;
3101 case OFPGT11_SELECT:
fe7e5749 3102 xlate_select_group(ctx, group);
f4fb341b 3103 break;
dd8cd4b4
SH
3104 case OFPGT11_FF:
3105 xlate_ff_group(ctx, group);
3106 break;
f4fb341b 3107 default:
428b2edd 3108 OVS_NOT_REACHED();
f4fb341b 3109 }
809c7548 3110 group_dpif_unref(group);
5a070238
BP
3111
3112 ctx->in_group = false;
3113}
3114
3115static bool
3116xlate_group_resource_check(struct xlate_ctx *ctx)
3117{
3118 if (!xlate_resubmit_resource_check(ctx)) {
3119 return false;
3120 } else if (ctx->in_group) {
3121 /* Prevent nested translation of OpenFlow groups.
3122 *
3123 * OpenFlow allows this restriction. We enforce this restriction only
3124 * because, with the current architecture, we would otherwise have to
3125 * take a possibly recursive read lock on the ofgroup rwlock, which is
3126 * unsafe given that POSIX allows taking a read lock to block if there
3127 * is a thread blocked on taking the write lock. Other solutions
3128 * without this restriction are also possible, but seem unwarranted
3129 * given the current limited use of groups. */
3130 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3131
3132 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
3133 return false;
3134 } else {
3135 return true;
3136 }
f4fb341b
SH
3137}
3138
3139static bool
3140xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3141{
5a070238 3142 if (xlate_group_resource_check(ctx)) {
f4fb341b
SH
3143 struct group_dpif *group;
3144 bool got_group;
3145
3146 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3147 if (got_group) {
3148 xlate_group_action__(ctx, group);
3149 } else {
3150 return true;
3151 }
3152 }
3153
3154 return false;
3155}
3156
9583bc14
EJ
3157static void
3158xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3159 const struct ofpact_resubmit *resubmit)
3160{
4e022ec0 3161 ofp_port_t in_port;
9583bc14 3162 uint8_t table_id;
adcf00ba
AZ
3163 bool may_packet_in = false;
3164 bool honor_table_miss = false;
3165
3166 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3167 /* Still allow missed packets to be sent to the controller
3168 * if resubmitting from an internal table. */
3169 may_packet_in = true;
3170 honor_table_miss = true;
3171 }
9583bc14
EJ
3172
3173 in_port = resubmit->in_port;
3174 if (in_port == OFPP_IN_PORT) {
4e022ec0 3175 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
3176 }
3177
3178 table_id = resubmit->table_id;
3179 if (table_id == 255) {
3180 table_id = ctx->table_id;
3181 }
3182
adcf00ba
AZ
3183 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3184 honor_table_miss);
9583bc14
EJ
3185}
3186
3187static void
3188flood_packets(struct xlate_ctx *ctx, bool all)
3189{
46c88433 3190 const struct xport *xport;
9583bc14 3191
46c88433
EJ
3192 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3193 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3194 continue;
3195 }
3196
3197 if (all) {
46c88433
EJ
3198 compose_output_action__(ctx, xport->ofp_port, false);
3199 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
3200 compose_output_action(ctx, xport->ofp_port);
9583bc14
EJ
3201 }
3202 }
3203
3204 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3205}
3206
3207static void
3208execute_controller_action(struct xlate_ctx *ctx, int len,
3209 enum ofp_packet_in_reason reason,
3210 uint16_t controller_id)
3211{
0fb7792a 3212 struct ofproto_packet_in *pin;
e14deea0 3213 struct dp_packet *packet;
9583bc14 3214
04594cd5 3215 ctx->xout->slow |= SLOW_CONTROLLER;
9583bc14
EJ
3216 if (!ctx->xin->packet) {
3217 return;
3218 }
3219
cf62fa4c 3220 packet = dp_packet_clone(ctx->xin->packet);
9583bc14 3221
7fd91025 3222 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3223 ctx->xout->odp_actions,
d23df9a8
JR
3224 &ctx->xout->wc,
3225 ctx->xbridge->masked_set_action);
9583bc14 3226
41ccaa24 3227 odp_execute_actions(NULL, &packet, 1, false,
6fd6ed71
PS
3228 ctx->xout->odp_actions->data,
3229 ctx->xout->odp_actions->size, NULL);
9583bc14 3230
ada3a58d 3231 pin = xmalloc(sizeof *pin);
cf62fa4c
PS
3232 pin->up.packet_len = dp_packet_size(packet);
3233 pin->up.packet = dp_packet_steal_data(packet);
0fb7792a 3234 pin->up.reason = reason;
0fb7792a 3235 pin->up.table_id = ctx->table_id;
d4fa4e79
BP
3236 pin->up.cookie = (ctx->rule
3237 ? rule_dpif_get_flow_cookie(ctx->rule)
3238 : OVS_BE64_MAX);
0fb7792a 3239
0fb7792a 3240 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
9583bc14 3241
f11c7538 3242 pin->controller_id = controller_id;
d38a3c7b 3243 pin->send_len = len;
32260212
SH
3244 /* If a rule is a table-miss rule then this is
3245 * a table-miss handled by a table-miss rule.
3246 *
3247 * Else, if rule is internal and has a controller action,
3248 * the later being implied by the rule being processed here,
3249 * then this is a table-miss handled without a table-miss rule.
3250 *
3251 * Otherwise this is not a table-miss. */
3252 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3253 if (ctx->rule) {
3254 if (rule_dpif_is_table_miss(ctx->rule)) {
3255 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3256 } else if (rule_dpif_is_internal(ctx->rule)) {
3257 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3258 }
3259 }
ada3a58d 3260 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
e14deea0 3261 dp_packet_delete(packet);
9583bc14
EJ
3262}
3263
7bbdd84f
SH
3264static void
3265compose_recirculate_action(struct xlate_ctx *ctx,
3266 const struct ofpact *ofpacts_base,
3267 const struct ofpact *ofpact_current,
3268 size_t ofpacts_base_len)
3269{
3270 uint32_t id;
3271 int error;
3272 unsigned ofpacts_len;
3273 struct match match;
3274 struct rule *rule;
3275 struct ofpbuf ofpacts;
3276
3277 ctx->exit = true;
3278
3279 ofpacts_len = ofpacts_base_len -
3280 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3281
3282 if (ctx->rule) {
3283 id = rule_dpif_get_recirc_id(ctx->rule);
3284 } else {
3285 /* In the case where ctx has no rule then allocate a recirc id.
3286 * The life-cycle of this recirc id is managed by associating it
3287 * with the internal rule that is created to to handle
3288 * recirculation below.
3289 *
3290 * The known use-case of this is packet_out which
3291 * translates actions without a rule */
3292 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3293 }
3294 if (!id) {
3295 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3296 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3297 return;
3298 }
3299
3300 match_init_catchall(&match);
3301 match_set_recirc_id(&match, id);
3302 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3303 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3304 RECIRC_RULE_PRIORITY,
3305 RECIRC_TIMEOUT, &ofpacts, &rule);
3306 if (error) {
3307 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3308 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3309 match_to_string(&match, 0));
3310 return;
3311 }
3312 /* If ctx has no rule then associate the recirc id, which
3313 * was allocated above, with the internal rule. This allows
3314 * the recirc id to be released when the internal rule times out. */
3315 if (!ctx->rule) {
3316 rule_set_recirc_id(rule, id);
3317 }
3318
3319 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3320 ctx->xout->odp_actions,
d23df9a8
JR
3321 &ctx->xout->wc,
3322 ctx->xbridge->masked_set_action);
cc377352 3323 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
7bbdd84f
SH
3324}
3325
8bfd0fda
BP
3326static void
3327compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 3328{
33bf9176
BP
3329 struct flow_wildcards *wc = &ctx->xout->wc;
3330 struct flow *flow = &ctx->xin->flow;
8bfd0fda 3331 int n;
33bf9176 3332
8bfd0fda 3333 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 3334
8bfd0fda
BP
3335 n = flow_count_mpls_labels(flow, wc);
3336 if (!n) {
8bfd0fda 3337 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
cc377352 3338 ctx->xout->odp_actions,
d23df9a8
JR
3339 &ctx->xout->wc,
3340 ctx->xbridge->masked_set_action);
8bfd0fda
BP
3341 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3342 if (ctx->xin->packet != NULL) {
3343 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3344 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3345 "MPLS push action can't be performed as it would "
3346 "have more MPLS LSEs than the %d supported.",
3347 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
9583bc14 3348 }
8bfd0fda
BP
3349 ctx->exit = true;
3350 return;
9583bc14 3351 }
b0a17866 3352
8bfd0fda 3353 flow_push_mpls(flow, n, mpls->ethertype, wc);
9583bc14
EJ
3354}
3355
8bfd0fda 3356static void
9cfef3d0 3357compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 3358{
33bf9176 3359 struct flow_wildcards *wc = &ctx->xout->wc;
8bfd0fda
BP
3360 struct flow *flow = &ctx->xin->flow;
3361 int n = flow_count_mpls_labels(flow, wc);
33bf9176 3362
7bbdd84f 3363 if (flow_pop_mpls(flow, n, eth_type, wc)) {
5af43325 3364 if (ctx->xbridge->enable_recirc) {
7bbdd84f
SH
3365 ctx->was_mpls = true;
3366 }
3367 } else if (n >= FLOW_MAX_MPLS_LABELS) {
8bfd0fda
BP
3368 if (ctx->xin->packet != NULL) {
3369 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3370 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3371 "MPLS pop action can't be performed as it has "
3372 "more MPLS LSEs than the %d supported.",
3373 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3374 }
3375 ctx->exit = true;
cc377352 3376 ofpbuf_clear(ctx->xout->odp_actions);
9583bc14
EJ
3377 }
3378}
3379
3380static bool
3381compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3382{
33bf9176
BP
3383 struct flow *flow = &ctx->xin->flow;
3384
3385 if (!is_ip_any(flow)) {
9583bc14
EJ
3386 return false;
3387 }
3388
1dd35f8a 3389 ctx->xout->wc.masks.nw_ttl = 0xff;
33bf9176
BP
3390 if (flow->nw_ttl > 1) {
3391 flow->nw_ttl--;
9583bc14
EJ
3392 return false;
3393 } else {
3394 size_t i;
3395
3396 for (i = 0; i < ids->n_controllers; i++) {
3397 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3398 ids->cnt_ids[i]);
3399 }
3400
3401 /* Stop processing for current table. */
3402 return true;
3403 }
3404}
3405
8bfd0fda 3406static void
097d4939
JR
3407compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3408{
8bfd0fda
BP
3409 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3410 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3411 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 3412 }
097d4939
JR
3413}
3414
8bfd0fda 3415static void
097d4939
JR
3416compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3417{
8bfd0fda
BP
3418 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3419 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3420 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 3421 }
097d4939
JR
3422}
3423
8bfd0fda 3424static void
9cfef3d0 3425compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 3426{
8bfd0fda
BP
3427 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3428 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3429 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 3430 }
9583bc14
EJ
3431}
3432
3433static bool
9cfef3d0 3434compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 3435{
33bf9176 3436 struct flow *flow = &ctx->xin->flow;
1dd35f8a
JP
3437 struct flow_wildcards *wc = &ctx->xout->wc;
3438
8bfd0fda 3439 if (eth_type_mpls(flow->dl_type)) {
22d38fca
JR
3440 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3441
3442 wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda
BP
3443 if (ttl > 1) {
3444 ttl--;
3445 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3446 return false;
3447 } else {
3448 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
8bfd0fda 3449 }
9583bc14 3450 }
22d38fca
JR
3451
3452 /* Stop processing for current table. */
3453 return true;
9583bc14
EJ
3454}
3455
3456static void
3457xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 3458 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 3459{
4e022ec0 3460 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
9583bc14
EJ
3461
3462 ctx->xout->nf_output_iface = NF_OUT_DROP;
3463
3464 switch (port) {
3465 case OFPP_IN_PORT:
4e022ec0 3466 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
9583bc14
EJ
3467 break;
3468 case OFPP_TABLE:
4e022ec0 3469 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 3470 0, may_packet_in, true);
9583bc14
EJ
3471 break;
3472 case OFPP_NORMAL:
3473 xlate_normal(ctx);
3474 break;
3475 case OFPP_FLOOD:
3476 flood_packets(ctx, false);
3477 break;
3478 case OFPP_ALL:
3479 flood_packets(ctx, true);
3480 break;
3481 case OFPP_CONTROLLER:
3a11fd5b 3482 execute_controller_action(ctx, max_len,
029ca940
SS
3483 (ctx->in_group ? OFPR_GROUP
3484 : ctx->in_action_set ? OFPR_ACTION_SET
3485 : OFPR_ACTION),
3486 0);
9583bc14
EJ
3487 break;
3488 case OFPP_NONE:
3489 break;
3490 case OFPP_LOCAL:
3491 default:
4e022ec0 3492 if (port != ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3493 compose_output_action(ctx, port);
3494 } else {
3495 xlate_report(ctx, "skipping output to input port");
3496 }
3497 break;
3498 }
3499
3500 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3501 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3502 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3503 ctx->xout->nf_output_iface = prev_nf_output_iface;
3504 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3505 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3506 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3507 }
3508}
3509
3510static void
3511xlate_output_reg_action(struct xlate_ctx *ctx,
3512 const struct ofpact_output_reg *or)
3513{
3514 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3515 if (port <= UINT16_MAX) {
3516 union mf_subvalue value;
3517
3518 memset(&value, 0xff, sizeof value);
3519 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
4e022ec0
AW
3520 xlate_output_action(ctx, u16_to_ofp(port),
3521 or->max_len, false);
9583bc14
EJ
3522 }
3523}
3524
3525static void
3526xlate_enqueue_action(struct xlate_ctx *ctx,
3527 const struct ofpact_enqueue *enqueue)
3528{
4e022ec0 3529 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
3530 uint32_t queue_id = enqueue->queue;
3531 uint32_t flow_priority, priority;
3532 int error;
3533
3534 /* Translate queue to priority. */
89a8a7f0 3535 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
3536 if (error) {
3537 /* Fall back to ordinary output action. */
3538 xlate_output_action(ctx, enqueue->port, 0, false);
3539 return;
3540 }
3541
3542 /* Check output port. */
3543 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
3544 ofp_port = ctx->xin->flow.in_port.ofp_port;
3545 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3546 return;
3547 }
3548
3549 /* Add datapath actions. */
3550 flow_priority = ctx->xin->flow.skb_priority;
3551 ctx->xin->flow.skb_priority = priority;
3552 compose_output_action(ctx, ofp_port);
3553 ctx->xin->flow.skb_priority = flow_priority;
3554
3555 /* Update NetFlow output port. */
3556 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3557 ctx->xout->nf_output_iface = ofp_port;
3558 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3559 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3560 }
3561}
3562
3563static void
3564xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3565{
3566 uint32_t skb_priority;
3567
89a8a7f0 3568 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
3569 ctx->xin->flow.skb_priority = skb_priority;
3570 } else {
3571 /* Couldn't translate queue to a priority. Nothing to do. A warning
3572 * has already been logged. */
3573 }
3574}
3575
3576static bool
46c88433 3577slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 3578{
46c88433
EJ
3579 const struct xbridge *xbridge = xbridge_;
3580 struct xport *port;
9583bc14
EJ
3581
3582 switch (ofp_port) {
3583 case OFPP_IN_PORT:
3584 case OFPP_TABLE:
3585 case OFPP_NORMAL:
3586 case OFPP_FLOOD:
3587 case OFPP_ALL:
3588 case OFPP_NONE:
3589 return true;
3590 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3591 return false;
3592 default:
46c88433 3593 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
3594 return port ? port->may_enable : false;
3595 }
3596}
3597
3598static void
3599xlate_bundle_action(struct xlate_ctx *ctx,
3600 const struct ofpact_bundle *bundle)
3601{
4e022ec0 3602 ofp_port_t port;
9583bc14
EJ
3603
3604 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
46c88433
EJ
3605 slave_enabled_cb,
3606 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 3607 if (bundle->dst.field) {
f74e7df7
JP
3608 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3609 &ctx->xout->wc);
9583bc14
EJ
3610 } else {
3611 xlate_output_action(ctx, port, 0, false);
3612 }
3613}
3614
3615static void
4165b5e0
JS
3616xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3617 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
9583bc14 3618{
4165b5e0
JS
3619 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3620 if (ctx->xin->may_learn) {
3621 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3622 }
3623}
9583bc14 3624
4165b5e0
JS
3625static void
3626xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3627{
9583bc14 3628 ctx->xout->has_learn = true;
9583bc14
EJ
3629 learn_mask(learn, &ctx->xout->wc);
3630
b256dc52
JS
3631 if (ctx->xin->xcache) {
3632 struct xc_entry *entry;
3633
3634 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4165b5e0
JS
3635 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3636 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3637 entry->u.learn.ofpacts = ofpbuf_new(64);
3638 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3639 entry->u.learn.ofpacts);
3640 } else if (ctx->xin->may_learn) {
3641 uint64_t ofpacts_stub[1024 / 8];
3642 struct ofputil_flow_mod fm;
3643 struct ofpbuf ofpacts;
3644
3645 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3646 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3647 ofpbuf_uninit(&ofpacts);
b256dc52
JS
3648 }
3649}
3650
3651static void
3652xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3653 uint16_t idle_timeout, uint16_t hard_timeout)
3654{
3655 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3656 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3657 }
9583bc14
EJ
3658}
3659
9583bc14
EJ
3660static void
3661xlate_fin_timeout(struct xlate_ctx *ctx,
3662 const struct ofpact_fin_timeout *oft)
3663{
b256dc52
JS
3664 if (ctx->rule) {
3665 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3666 oft->fin_idle_timeout, oft->fin_hard_timeout);
3667 if (ctx->xin->xcache) {
3668 struct xc_entry *entry;
3669
3670 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
83709dfa
JR
3671 /* XC_RULE already holds a reference on the rule, none is taken
3672 * here. */
b256dc52
JS
3673 entry->u.fin.rule = ctx->rule;
3674 entry->u.fin.idle = oft->fin_idle_timeout;
3675 entry->u.fin.hard = oft->fin_hard_timeout;
b256dc52 3676 }
9583bc14
EJ
3677 }
3678}
3679
3680static void
3681xlate_sample_action(struct xlate_ctx *ctx,
3682 const struct ofpact_sample *os)
3683{
3684 union user_action_cookie cookie;
3685 /* Scale the probability from 16-bit to 32-bit while representing
3686 * the same percentage. */
3687 uint32_t probability = (os->probability << 16) | os->probability;
3688
4b97b70d
BP
3689 if (!ctx->xbridge->variable_length_userdata) {
3690 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3691
3692 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3693 "lacks support (needs Linux 3.10+ or kernel module from "
3694 "OVS 1.11+)");
3695 return;
3696 }
3697
7fd91025 3698 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3699 ctx->xout->odp_actions,
d23df9a8
JR
3700 &ctx->xout->wc,
3701 ctx->xbridge->masked_set_action);
9583bc14
EJ
3702
3703 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3704 os->obs_domain_id, os->obs_point_id, &cookie);
cc377352 3705 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions, &ctx->xin->flow,
8b7ea2d4
WZ
3706 probability, &cookie, sizeof cookie.flow_sample,
3707 ODPP_NONE);
9583bc14
EJ
3708}
3709
3710static bool
46c88433 3711may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 3712{
bbbca389 3713 if (xport->config & (is_stp(&ctx->xin->flow)
46c88433
EJ
3714 ? OFPUTIL_PC_NO_RECV_STP
3715 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
3716 return false;
3717 }
3718
3719 /* Only drop packets here if both forwarding and learning are
3720 * disabled. If just learning is enabled, we need to have
3721 * OFPP_NORMAL and the learning action have a look at the packet
3722 * before we can drop it. */
9efd308e
DV
3723 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3724 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
9583bc14
EJ
3725 return false;
3726 }
3727
3728 return true;
3729}
3730
7fdb60a7
SH
3731static void
3732xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3733{
c61f3870
BP
3734 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3735 size_t on_len = ofpact_nest_get_action_len(on);
3736 const struct ofpact *inner;
3737
3738 /* Maintain actset_output depending on the contents of the action set:
3739 *
3740 * - OFPP_UNSET, if there is no "output" action.
3741 *
3742 * - The output port, if there is an "output" action and no "group"
3743 * action.
3744 *
3745 * - OFPP_UNSET, if there is a "group" action.
3746 */
3747 if (!ctx->action_set_has_group) {
3748 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3749 if (inner->type == OFPACT_OUTPUT) {
3750 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3751 } else if (inner->type == OFPACT_GROUP) {
3752 ctx->xin->flow.actset_output = OFPP_UNSET;
3753 ctx->action_set_has_group = true;
3754 }
3755 }
3756 }
3757
3758 ofpbuf_put(&ctx->action_set, on->actions, on_len);
7fdb60a7
SH
3759 ofpact_pad(&ctx->action_set);
3760}
3761
3762static void
3763xlate_action_set(struct xlate_ctx *ctx)
3764{
3765 uint64_t action_list_stub[1024 / 64];
3766 struct ofpbuf action_list;
3767
029ca940 3768 ctx->in_action_set = true;
7fdb60a7
SH
3769 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3770 ofpacts_execute_action_set(&action_list, &ctx->action_set);
6fd6ed71 3771 do_xlate_actions(action_list.data, action_list.size, ctx);
029ca940 3772 ctx->in_action_set = false;
7fdb60a7
SH
3773 ofpbuf_uninit(&action_list);
3774}
3775
7bbdd84f 3776static bool
5af43325 3777ofpact_needs_recirculation_after_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
7bbdd84f
SH
3778{
3779 struct flow_wildcards *wc = &ctx->xout->wc;
3780 struct flow *flow = &ctx->xin->flow;
3781
5af43325
PS
3782 if (!ctx->was_mpls) {
3783 return false;
3784 }
3785
7bbdd84f
SH
3786 switch (a->type) {
3787 case OFPACT_OUTPUT:
3788 case OFPACT_GROUP:
3789 case OFPACT_CONTROLLER:
3790 case OFPACT_STRIP_VLAN:
3791 case OFPACT_SET_VLAN_PCP:
3792 case OFPACT_SET_VLAN_VID:
3793 case OFPACT_ENQUEUE:
3794 case OFPACT_PUSH_VLAN:
3795 case OFPACT_SET_ETH_SRC:
3796 case OFPACT_SET_ETH_DST:
3797 case OFPACT_SET_TUNNEL:
3798 case OFPACT_SET_QUEUE:
3799 case OFPACT_POP_QUEUE:
18080541 3800 case OFPACT_CONJUNCTION:
7bbdd84f
SH
3801 case OFPACT_NOTE:
3802 case OFPACT_OUTPUT_REG:
3803 case OFPACT_EXIT:
3804 case OFPACT_METER:
3805 case OFPACT_WRITE_METADATA:
3806 case OFPACT_WRITE_ACTIONS:
3807 case OFPACT_CLEAR_ACTIONS:
3808 case OFPACT_SAMPLE:
3809 return false;
3810
5af43325
PS
3811 case OFPACT_POP_MPLS:
3812 case OFPACT_DEC_MPLS_TTL:
3813 case OFPACT_SET_MPLS_TTL:
3814 case OFPACT_SET_MPLS_TC:
3815 case OFPACT_SET_MPLS_LABEL:
7bbdd84f
SH
3816 case OFPACT_SET_IPV4_SRC:
3817 case OFPACT_SET_IPV4_DST:
3818 case OFPACT_SET_IP_DSCP:
3819 case OFPACT_SET_IP_ECN:
3820 case OFPACT_SET_IP_TTL:
3821 case OFPACT_SET_L4_SRC_PORT:
3822 case OFPACT_SET_L4_DST_PORT:
3823 case OFPACT_RESUBMIT:
3824 case OFPACT_STACK_PUSH:
3825 case OFPACT_STACK_POP:
3826 case OFPACT_DEC_TTL:
3827 case OFPACT_MULTIPATH:
3828 case OFPACT_BUNDLE:
3829 case OFPACT_LEARN:
3830 case OFPACT_FIN_TIMEOUT:
3831 case OFPACT_GOTO_TABLE:
3832 return true;
3833
3834 case OFPACT_REG_MOVE:
3835 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3836 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3837
7bbdd84f
SH
3838 case OFPACT_SET_FIELD:
3839 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3840
3841 case OFPACT_PUSH_MPLS:
3842 /* Recirculate if it is an IP packet with a zero ttl. This may
3843 * indicate that the packet was previously MPLS and an MPLS pop action
3844 * converted it to IP. In this case recirculating should reveal the IP
3845 * TTL which is used as the basis for a new MPLS LSE. */
3846 return (!flow_count_mpls_labels(flow, wc)
3847 && flow->nw_ttl == 0
3848 && is_ip_any(flow));
3849 }
3850
3851 OVS_NOT_REACHED();
3852}
3853
9583bc14
EJ
3854static void
3855do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3856 struct xlate_ctx *ctx)
3857{
33bf9176
BP
3858 struct flow_wildcards *wc = &ctx->xout->wc;
3859 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
3860 const struct ofpact *a;
3861
a36de779
PS
3862 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3863 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
3864 }
f47ea021
JR
3865 /* dl_type already in the mask, not set below. */
3866
9583bc14
EJ
3867 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3868 struct ofpact_controller *controller;
3869 const struct ofpact_metadata *metadata;
b2dd70be
JR
3870 const struct ofpact_set_field *set_field;
3871 const struct mf_field *mf;
9583bc14
EJ
3872
3873 if (ctx->exit) {
3874 break;
3875 }
3876
5af43325 3877 if (ofpact_needs_recirculation_after_mpls(a, ctx)) {
7bbdd84f
SH
3878 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3879 return;
3880 }
3881
9583bc14
EJ
3882 switch (a->type) {
3883 case OFPACT_OUTPUT:
3884 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3885 ofpact_get_OUTPUT(a)->max_len, true);
3886 break;
3887
7395c052 3888 case OFPACT_GROUP:
f4fb341b
SH
3889 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3890 return;
3891 }
7395c052
NZ
3892 break;
3893
9583bc14
EJ
3894 case OFPACT_CONTROLLER:
3895 controller = ofpact_get_CONTROLLER(a);
3896 execute_controller_action(ctx, controller->max_len,
3897 controller->reason,
3898 controller->controller_id);
3899 break;
3900
3901 case OFPACT_ENQUEUE:
16194afd
DDP
3902 memset(&wc->masks.skb_priority, 0xff,
3903 sizeof wc->masks.skb_priority);
9583bc14
EJ
3904 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3905 break;
3906
3907 case OFPACT_SET_VLAN_VID:
f74e7df7 3908 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
ca287d20
JR
3909 if (flow->vlan_tci & htons(VLAN_CFI) ||
3910 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3911 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3912 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3913 | htons(VLAN_CFI));
3914 }
9583bc14
EJ
3915 break;
3916
3917 case OFPACT_SET_VLAN_PCP:
f74e7df7 3918 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
ca287d20
JR
3919 if (flow->vlan_tci & htons(VLAN_CFI) ||
3920 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3921 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3922 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3923 << VLAN_PCP_SHIFT) | VLAN_CFI);
3924 }
9583bc14
EJ
3925 break;
3926
3927 case OFPACT_STRIP_VLAN:
f74e7df7 3928 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 3929 flow->vlan_tci = htons(0);
9583bc14
EJ
3930 break;
3931
3932 case OFPACT_PUSH_VLAN:
3933 /* XXX 802.1AD(QinQ) */
f74e7df7 3934 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 3935 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
3936 break;
3937
3938 case OFPACT_SET_ETH_SRC:
f74e7df7 3939 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
33bf9176 3940 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
3941 break;
3942
3943 case OFPACT_SET_ETH_DST:
f74e7df7 3944 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
33bf9176 3945 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
3946 break;
3947
3948 case OFPACT_SET_IPV4_SRC:
33bf9176 3949 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 3950 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 3951 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
3952 }
3953 break;
3954
3955 case OFPACT_SET_IPV4_DST:
33bf9176 3956 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 3957 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 3958 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
3959 }
3960 break;
3961
04f01c24
BP
3962 case OFPACT_SET_IP_DSCP:
3963 if (is_ip_any(flow)) {
f47ea021 3964 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 3965 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 3966 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
3967 }
3968 break;
3969
ff14eb7a
JR
3970 case OFPACT_SET_IP_ECN:
3971 if (is_ip_any(flow)) {
3972 wc->masks.nw_tos |= IP_ECN_MASK;
3973 flow->nw_tos &= ~IP_ECN_MASK;
3974 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3975 }
3976 break;
3977
0c20dbe4
JR
3978 case OFPACT_SET_IP_TTL:
3979 if (is_ip_any(flow)) {
3980 wc->masks.nw_ttl = 0xff;
3981 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3982 }
3983 break;
3984
9583bc14 3985 case OFPACT_SET_L4_SRC_PORT:
b8778a0d 3986 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
3987 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3988 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 3989 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
3990 }
3991 break;
3992
3993 case OFPACT_SET_L4_DST_PORT:
b8778a0d 3994 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
3995 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3996 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 3997 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
3998 }
3999 break;
4000
4001 case OFPACT_RESUBMIT:
4002 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4003 break;
4004
4005 case OFPACT_SET_TUNNEL:
33bf9176 4006 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
4007 break;
4008
4009 case OFPACT_SET_QUEUE:
16194afd
DDP
4010 memset(&wc->masks.skb_priority, 0xff,
4011 sizeof wc->masks.skb_priority);
9583bc14
EJ
4012 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4013 break;
4014
4015 case OFPACT_POP_QUEUE:
16194afd
DDP
4016 memset(&wc->masks.skb_priority, 0xff,
4017 sizeof wc->masks.skb_priority);
33bf9176 4018 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
4019 break;
4020
4021 case OFPACT_REG_MOVE:
33bf9176 4022 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
4023 break;
4024
b2dd70be
JR
4025 case OFPACT_SET_FIELD:
4026 set_field = ofpact_get_SET_FIELD(a);
4027 mf = set_field->field;
b2dd70be
JR
4028
4029 /* Set field action only ever overwrites packet's outermost
4030 * applicable header fields. Do nothing if no header exists. */
4479846e
JR
4031 if (mf->id == MFF_VLAN_VID) {
4032 wc->masks.vlan_tci |= htons(VLAN_CFI);
4033 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4034 break;
4035 }
4036 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4037 /* 'dl_type' is already unwildcarded. */
4038 && !eth_type_mpls(flow->dl_type)) {
4039 break;
b2dd70be 4040 }
b8778a0d
JR
4041 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4042 * header field on a packet that does not have them. */
4479846e 4043 mf_mask_field_and_prereqs(mf, &wc->masks);
b8778a0d
JR
4044 if (mf_are_prereqs_ok(mf, flow)) {
4045 mf_set_flow_value_masked(mf, &set_field->value,
4046 &set_field->mask, flow);
4047 }
b2dd70be
JR
4048 break;
4049
9583bc14 4050 case OFPACT_STACK_PUSH:
33bf9176
BP
4051 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4052 &ctx->stack);
9583bc14
EJ
4053 break;
4054
4055 case OFPACT_STACK_POP:
f74e7df7
JP
4056 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4057 &ctx->stack);
9583bc14
EJ
4058 break;
4059
4060 case OFPACT_PUSH_MPLS:
8bfd0fda 4061 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
4062 break;
4063
4064 case OFPACT_POP_MPLS:
8bfd0fda 4065 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
4066 break;
4067
097d4939 4068 case OFPACT_SET_MPLS_LABEL:
8bfd0fda
BP
4069 compose_set_mpls_label_action(
4070 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
4071 break;
097d4939
JR
4072
4073 case OFPACT_SET_MPLS_TC:
8bfd0fda 4074 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
4075 break;
4076
9583bc14 4077 case OFPACT_SET_MPLS_TTL:
8bfd0fda 4078 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
4079 break;
4080
4081 case OFPACT_DEC_MPLS_TTL:
9cfef3d0 4082 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 4083 return;
9583bc14
EJ
4084 }
4085 break;
4086
4087 case OFPACT_DEC_TTL:
f74e7df7 4088 wc->masks.nw_ttl = 0xff;
9583bc14 4089 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 4090 return;
9583bc14
EJ
4091 }
4092 break;
4093
4094 case OFPACT_NOTE:
4095 /* Nothing to do. */
4096 break;
4097
4098 case OFPACT_MULTIPATH:
33bf9176 4099 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
4100 break;
4101
4102 case OFPACT_BUNDLE:
9583bc14
EJ
4103 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4104 break;
4105
4106 case OFPACT_OUTPUT_REG:
4107 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4108 break;
4109
4110 case OFPACT_LEARN:
4111 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4112 break;
4113
afc3987b
BP
4114 case OFPACT_CONJUNCTION: {
4115 /* A flow with a "conjunction" action represents part of a special
4116 * kind of "set membership match". Such a flow should not actually
4117 * get executed, but it could via, say, a "packet-out", even though
4118 * that wouldn't be useful. Log it to help debugging. */
4119 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4120 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
18080541 4121 break;
afc3987b 4122 }
18080541 4123
9583bc14
EJ
4124 case OFPACT_EXIT:
4125 ctx->exit = true;
4126 break;
4127
4128 case OFPACT_FIN_TIMEOUT:
33bf9176 4129 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
4130 ctx->xout->has_fin_timeout = true;
4131 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4132 break;
4133
4134 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 4135 ofpbuf_clear(&ctx->action_set);
c61f3870
BP
4136 ctx->xin->flow.actset_output = OFPP_UNSET;
4137 ctx->action_set_has_group = false;
7fdb60a7
SH
4138 break;
4139
4140 case OFPACT_WRITE_ACTIONS:
4141 xlate_write_actions(ctx, a);
9583bc14
EJ
4142 break;
4143
4144 case OFPACT_WRITE_METADATA:
4145 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
4146 flow->metadata &= ~metadata->mask;
4147 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
4148 break;
4149
638a19b0
JR
4150 case OFPACT_METER:
4151 /* Not implemented yet. */
4152 break;
4153
9583bc14 4154 case OFPACT_GOTO_TABLE: {
9583bc14 4155 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14 4156
7bbdd84f
SH
4157 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4158 * than ogt->table_id. This is to allow goto_table actions that
4159 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4160 * after recirculation. */
4161 ovs_assert(ctx->table_id == TBL_INTERNAL
4162 || ctx->table_id < ogt->table_id);
4468099e 4163 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 4164 ogt->table_id, true, true);
9583bc14
EJ
4165 break;
4166 }
4167
4168 case OFPACT_SAMPLE:
4169 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4170 break;
4171 }
4172 }
9583bc14
EJ
4173}
4174
4175void
4176xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
cc377352
EJ
4177 const struct flow *flow, ofp_port_t in_port,
4178 struct rule_dpif *rule, uint16_t tcp_flags,
cf62fa4c 4179 const struct dp_packet *packet)
9583bc14
EJ
4180{
4181 xin->ofproto = ofproto;
4182 xin->flow = *flow;
cc377352 4183 xin->flow.in_port.ofp_port = in_port;
c61f3870 4184 xin->flow.actset_output = OFPP_UNSET;
9583bc14
EJ
4185 xin->packet = packet;
4186 xin->may_learn = packet != NULL;
4187 xin->rule = rule;
b256dc52 4188 xin->xcache = NULL;
9583bc14
EJ
4189 xin->ofpacts = NULL;
4190 xin->ofpacts_len = 0;
4191 xin->tcp_flags = tcp_flags;
4192 xin->resubmit_hook = NULL;
4193 xin->report_hook = NULL;
4194 xin->resubmit_stats = NULL;
3f207910 4195 xin->skip_wildcards = false;
cc377352 4196 xin->odp_actions = NULL;
9583bc14
EJ
4197}
4198
4199void
4200xlate_out_uninit(struct xlate_out *xout)
4201{
cc377352
EJ
4202 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
4203 ofpbuf_uninit(xout->odp_actions);
9583bc14
EJ
4204 }
4205}
4206
4207/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4208 * into datapath actions, using 'ctx', and discards the datapath actions. */
4209void
4210xlate_actions_for_side_effects(struct xlate_in *xin)
4211{
4212 struct xlate_out xout;
4213
4214 xlate_actions(xin, &xout);
4215 xlate_out_uninit(&xout);
4216}
4217
9583bc14
EJ
4218void
4219xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
4220{
4221 dst->wc = src->wc;
9583bc14
EJ
4222 dst->slow = src->slow;
4223 dst->has_learn = src->has_learn;
4224 dst->has_normal = src->has_normal;
4225 dst->has_fin_timeout = src->has_fin_timeout;
4226 dst->nf_output_iface = src->nf_output_iface;
4227 dst->mirrors = src->mirrors;
4228
cc377352
EJ
4229 dst->odp_actions = &dst->odp_actions_buf;
4230 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
9583bc14 4231 sizeof dst->odp_actions_stub);
6fd6ed71 4232 ofpbuf_put(dst->odp_actions, src->odp_actions->data, src->odp_actions->size);
9583bc14
EJ
4233}
4234\f
55954f6e
EJ
4235static struct skb_priority_to_dscp *
4236get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4237{
4238 struct skb_priority_to_dscp *pdscp;
4239 uint32_t hash;
4240
4241 hash = hash_int(skb_priority, 0);
4242 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4243 if (pdscp->skb_priority == skb_priority) {
4244 return pdscp;
4245 }
4246 }
4247 return NULL;
4248}
4249
4250static bool
4251dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4252 uint8_t *dscp)
4253{
4254 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4255 *dscp = pdscp ? pdscp->dscp : 0;
4256 return pdscp != NULL;
4257}
4258
16194afd
DDP
4259static size_t
4260count_skb_priorities(const struct xport *xport)
4261{
4262 return hmap_count(&xport->skb_priorities);
4263}
4264
55954f6e
EJ
4265static void
4266clear_skb_priorities(struct xport *xport)
4267{
4268 struct skb_priority_to_dscp *pdscp, *next;
4269
4270 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4271 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4272 free(pdscp);
4273 }
4274}
4275
ce4a6b76
BP
4276static bool
4277actions_output_to_local_port(const struct xlate_ctx *ctx)
4278{
46c88433 4279 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
4280 const struct nlattr *a;
4281 unsigned int left;
4282
6fd6ed71
PS
4283 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions->data,
4284 ctx->xout->odp_actions->size) {
ce4a6b76
BP
4285 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4286 && nl_attr_get_odp_port(a) == local_odp_port) {
4287 return true;
4288 }
4289 }
4290 return false;
4291}
9583bc14 4292
5e2a6702 4293#if defined(__linux__)
7d031d7e
BP
4294/* Returns the maximum number of packets that the Linux kernel is willing to
4295 * queue up internally to certain kinds of software-implemented ports, or the
4296 * default (and rarely modified) value if it cannot be determined. */
4297static int
4298netdev_max_backlog(void)
4299{
4300 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4301 static int max_backlog = 1000; /* The normal default value. */
4302
4303 if (ovsthread_once_start(&once)) {
4304 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4305 FILE *stream;
4306 int n;
4307
4308 stream = fopen(filename, "r");
4309 if (!stream) {
4310 VLOG_WARN("%s: open failed (%s)", filename, ovs_strerror(errno));
4311 } else {
4312 if (fscanf(stream, "%d", &n) != 1) {
4313 VLOG_WARN("%s: read error", filename);
4314 } else if (n <= 100) {
4315 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4316 } else {
4317 max_backlog = n;
4318 }
4319 fclose(stream);
4320 }
4321 ovsthread_once_done(&once);
4322
4323 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4324 }
4325
4326 return max_backlog;
4327}
4328
4329/* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4330 * 'odp_actions'. */
4331static int
4332count_output_actions(const struct ofpbuf *odp_actions)
4333{
4334 const struct nlattr *a;
4335 size_t left;
4336 int n = 0;
4337
6fd6ed71 4338 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
7d031d7e
BP
4339 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4340 n++;
4341 }
4342 }
4343 return n;
4344}
5e2a6702 4345#endif /* defined(__linux__) */
7d031d7e
BP
4346
4347/* Returns true if 'odp_actions' contains more output actions than the datapath
4348 * can reliably handle in one go. On Linux, this is the value of the
4349 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4350 * packets that the kernel is willing to queue up for processing while the
4351 * datapath is processing a set of actions. */
4352static bool
5e2a6702 4353too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
7d031d7e
BP
4354{
4355#ifdef __linux__
6fd6ed71 4356 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
7d031d7e
BP
4357 && count_output_actions(odp_actions) > netdev_max_backlog());
4358#else
4359 /* OSes other than Linux might have similar limits, but we don't know how
4360 * to determine them.*/
4361 return false;
4362#endif
4363}
4364
9583bc14 4365/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
56450a41
BP
4366 * into datapath actions in 'odp_actions', using 'ctx'.
4367 *
4368 * The caller must take responsibility for eventually freeing 'xout', with
4369 * xlate_out_uninit(). */
84f0f298
RW
4370void
4371xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
9583bc14 4372{
84f0f298 4373 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
33bf9176
BP
4374 struct flow_wildcards *wc = &xout->wc;
4375 struct flow *flow = &xin->flow;
10c44245 4376 struct rule_dpif *rule = NULL;
33bf9176 4377
9583bc14
EJ
4378 enum slow_path_reason special;
4379 const struct ofpact *ofpacts;
46c88433 4380 struct xport *in_port;
9583bc14
EJ
4381 struct flow orig_flow;
4382 struct xlate_ctx ctx;
4383 size_t ofpacts_len;
62a7cc71 4384 bool tnl_may_send;
c56fac1b 4385 bool is_icmp;
9583bc14 4386
46c88433 4387 COVERAGE_INC(xlate_actions);
9583bc14
EJ
4388
4389 /* Flow initialization rules:
4390 * - 'base_flow' must match the kernel's view of the packet at the
4391 * time that action processing starts. 'flow' represents any
4392 * transformations we wish to make through actions.
4393 * - By default 'base_flow' and 'flow' are the same since the input
4394 * packet matches the output before any actions are applied.
4395 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4396 * of the received packet as seen by the kernel. If we later output
4397 * to another device without any modifications this will cause us to
4398 * insert a new tag since the original one was stripped off by the
4399 * VLAN device.
4400 * - Tunnel metadata as received is retained in 'flow'. This allows
4401 * tunnel metadata matching also in later tables.
4402 * Since a kernel action for setting the tunnel metadata will only be
4403 * generated with actual tunnel output, changing the tunnel metadata
4404 * values in 'flow' (such as tun_id) will only have effect with a later
4405 * tunnel output action.
4406 * - Tunnel 'base_flow' is completely cleared since that is what the
4407 * kernel does. If we wish to maintain the original values an action
4408 * needs to be generated. */
4409
4410 ctx.xin = xin;
4411 ctx.xout = xout;
46c88433
EJ
4412 ctx.xout->slow = 0;
4413 ctx.xout->has_learn = false;
4414 ctx.xout->has_normal = false;
4415 ctx.xout->has_fin_timeout = false;
4416 ctx.xout->nf_output_iface = NF_OUT_DROP;
4417 ctx.xout->mirrors = 0;
cc377352
EJ
4418
4419 xout->odp_actions = xin->odp_actions;
4420 if (!xout->odp_actions) {
4421 xout->odp_actions = &xout->odp_actions_buf;
4422 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4423 sizeof xout->odp_actions_stub);
4424 }
4425 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
46c88433 4426
84f0f298 4427 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
46c88433 4428 if (!ctx.xbridge) {
83709dfa 4429 return;
46c88433 4430 }
9583bc14 4431
9583bc14
EJ
4432 ctx.rule = xin->rule;
4433
33bf9176 4434 ctx.base_flow = *flow;
9583bc14 4435 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
33bf9176 4436 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
9583bc14 4437
33bf9176
BP
4438 flow_wildcards_init_catchall(wc);
4439 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
7431e171 4440 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
0934ebba
BP
4441 if (is_ip_any(flow)) {
4442 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4443 }
c56fac1b 4444 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
9583bc14 4445
62a7cc71 4446 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
ce3955be 4447 if (ctx.xbridge->netflow) {
9b658910 4448 netflow_mask_wc(flow, wc);
9583bc14
EJ
4449 }
4450
9583bc14 4451 ctx.recurse = 0;
98b07853 4452 ctx.resubmits = 0;
5a070238 4453 ctx.in_group = false;
029ca940 4454 ctx.in_action_set = false;
33bf9176 4455 ctx.orig_skb_priority = flow->skb_priority;
9583bc14
EJ
4456 ctx.table_id = 0;
4457 ctx.exit = false;
92c08f09 4458 ctx.use_recirc = false;
7bbdd84f 4459 ctx.was_mpls = false;
9583bc14 4460
10c44245 4461 if (!xin->ofpacts && !ctx.rule) {
34dd0d78
JR
4462 rule = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
4463 xin->skip_wildcards ? NULL : wc,
4464 ctx.xin->xcache != NULL,
4465 ctx.xin->resubmit_stats, &ctx.table_id);
10c44245
EJ
4466 if (ctx.xin->resubmit_stats) {
4467 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4468 }
b256dc52
JS
4469 if (ctx.xin->xcache) {
4470 struct xc_entry *entry;
4471
4472 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
b256dc52
JS
4473 entry->u.rule = rule;
4474 }
10c44245 4475 ctx.rule = rule;
a8c31348
BP
4476
4477 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
4478 ctx.xin->resubmit_hook(ctx.xin, rule, 0);
4479 }
10c44245 4480 }
ee1afdd5 4481 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
10c44245 4482
9583bc14
EJ
4483 if (xin->ofpacts) {
4484 ofpacts = xin->ofpacts;
4485 ofpacts_len = xin->ofpacts_len;
10c44245 4486 } else if (ctx.rule) {
d8227eba
JR
4487 const struct rule_actions *actions = rule_dpif_get_actions(ctx.rule);
4488
6f00e29b
BP
4489 ofpacts = actions->ofpacts;
4490 ofpacts_len = actions->ofpacts_len;
9583bc14 4491 } else {
428b2edd 4492 OVS_NOT_REACHED();
9583bc14
EJ
4493 }
4494
4495 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
c61f3870
BP
4496
4497 ctx.action_set_has_group = false;
7fdb60a7
SH
4498 ofpbuf_use_stub(&ctx.action_set,
4499 ctx.action_set_stub, sizeof ctx.action_set_stub);
9583bc14 4500
ade6ad9c 4501 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
4502 /* Do this conditionally because the copy is expensive enough that it
4503 * shows up in profiles. */
33bf9176 4504 orig_flow = *flow;
9583bc14
EJ
4505 }
4506
46c88433 4507 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
b256dc52
JS
4508 if (in_port && in_port->is_tunnel) {
4509 if (ctx.xin->resubmit_stats) {
4510 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4511 if (in_port->bfd) {
4512 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4513 }
4514 }
4515 if (ctx.xin->xcache) {
4516 struct xc_entry *entry;
4517
4518 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4519 entry->u.dev.rx = netdev_ref(in_port->netdev);
4520 entry->u.dev.bfd = bfd_ref(in_port->bfd);
d6fc5f57
EJ
4521 }
4522 }
4523
642dc74d 4524 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
9583bc14 4525 if (special) {
04594cd5 4526 ctx.xout->slow |= special;
9583bc14 4527 } else {
9583bc14 4528 size_t sample_actions_len;
9583bc14 4529
4e022ec0 4530 if (flow->in_port.ofp_port
46c88433
EJ
4531 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4532 flow->in_port.ofp_port,
33bf9176 4533 flow->vlan_tci)) {
9583bc14
EJ
4534 ctx.base_flow.vlan_tci = 0;
4535 }
4536
4537 add_sflow_action(&ctx);
4538 add_ipfix_action(&ctx);
6fd6ed71 4539 sample_actions_len = ctx.xout->odp_actions->size;
9583bc14 4540
62a7cc71 4541 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
9583bc14
EJ
4542 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4543
4544 /* We've let OFPP_NORMAL and the learning action look at the
4545 * packet, so drop it now if forwarding is disabled. */
9efd308e
DV
4546 if (in_port && (!xport_stp_forward_state(in_port) ||
4547 !xport_rstp_forward_state(in_port))) {
8ec90813 4548 /* Drop all actions added by do_xlate_actions() above. */
6fd6ed71 4549 ctx.xout->odp_actions->size = sample_actions_len;
8ec90813
JR
4550 } else if (ctx.action_set.size) {
4551 /* Translate action set only if not dropping the packet. */
4552 xlate_action_set(&ctx);
9583bc14
EJ
4553 }
4554 }
4555
46c88433 4556 if (ctx.xbridge->has_in_band
ce4a6b76
BP
4557 && in_band_must_output_to_local_port(flow)
4558 && !actions_output_to_local_port(&ctx)) {
9583bc14
EJ
4559 compose_output_action(&ctx, OFPP_LOCAL);
4560 }
aaa0fbae
EJ
4561
4562 fix_sflow_action(&ctx);
4563
46c88433 4564 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
4565 add_mirror_actions(&ctx, &orig_flow);
4566 }
9583bc14
EJ
4567 }
4568
6fd6ed71 4569 if (nl_attr_oversized(ctx.xout->odp_actions->size)) {
542024c4 4570 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
4571 * can't hand them to the kernel directly. dpif_execute() can execute
4572 * them one by one with help, so just mark the result as SLOW_ACTION to
4573 * prevent the flow from being installed. */
4574 COVERAGE_INC(xlate_actions_oversize);
4575 ctx.xout->slow |= SLOW_ACTION;
7d031d7e
BP
4576 } else if (too_many_output_actions(ctx.xout->odp_actions)) {
4577 COVERAGE_INC(xlate_actions_too_many_output);
4578 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
4579 }
4580
b256dc52
JS
4581 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4582 if (ctx.xin->resubmit_stats) {
4583 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4584 ctx.xin->resubmit_stats->n_packets,
4585 ctx.xin->resubmit_stats->n_bytes);
4586 }
4587 if (ctx.xin->xcache) {
4588 struct xc_entry *entry;
4589
4590 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4591 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4592 entry->u.mirror.mirrors = xout->mirrors;
4593 }
4594 }
4595
4596 if (ctx.xbridge->netflow) {
b256dc52
JS
4597 /* Only update netflow if we don't have controller flow. We don't
4598 * report NetFlow expiration messages for such facets because they
4599 * are just part of the control logic for the network, not real
4600 * traffic. */
4601 if (ofpacts_len == 0
4602 || ofpacts->type != OFPACT_CONTROLLER
4603 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4604 if (ctx.xin->resubmit_stats) {
d6fc5f57
EJ
4605 netflow_flow_update(ctx.xbridge->netflow, flow,
4606 xout->nf_output_iface,
4607 ctx.xin->resubmit_stats);
4608 }
b256dc52
JS
4609 if (ctx.xin->xcache) {
4610 struct xc_entry *entry;
4611
4612 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4613 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4614 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4615 entry->u.nf.iface = xout->nf_output_iface;
4616 }
d6fc5f57
EJ
4617 }
4618 }
4619
9583bc14 4620 ofpbuf_uninit(&ctx.stack);
7fdb60a7 4621 ofpbuf_uninit(&ctx.action_set);
9583bc14
EJ
4622
4623 /* Clear the metadata and register wildcard masks, because we won't
4624 * use non-header fields as part of the cache. */
c11c6faa 4625 flow_wildcards_clear_non_packet_fields(wc);
dc24a00f 4626
c56fac1b
BP
4627 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4628 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4629 * these fields. The datapath interface, on the other hand, represents
4630 * them with just 8 bits each. This means that if the high 8 bits of the
4631 * masks for these fields somehow become set, then they will get chopped
4632 * off by a round trip through the datapath, and revalidation will spot
4633 * that as an inconsistency and delete the flow. Avoid the problem here by
4634 * making sure that only the low 8 bits of either field can be unwildcarded
4635 * for ICMP.
4636 */
4637 if (is_icmp) {
4638 wc->masks.tp_src &= htons(UINT8_MAX);
4639 wc->masks.tp_dst &= htons(UINT8_MAX);
4640 }
91d6cd12
AW
4641}
4642
4643/* Sends 'packet' out 'ofport'.
4644 * May modify 'packet'.
4645 * Returns 0 if successful, otherwise a positive errno value. */
4646int
cf62fa4c 4647xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
91d6cd12 4648{
84f0f298 4649 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
91d6cd12 4650 struct xport *xport;
91d6cd12 4651 struct ofpact_output output;
91d6cd12 4652 struct flow flow;
91d6cd12 4653
91d6cd12
AW
4654 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4655 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
cf62fa4c 4656 flow_extract(packet, &flow);
b5e7e61a 4657 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12 4658
84f0f298 4659 xport = xport_lookup(xcfg, ofport);
91d6cd12 4660 if (!xport) {
02ea2703 4661 return EINVAL;
91d6cd12 4662 }
91d6cd12
AW
4663 output.port = xport->ofp_port;
4664 output.max_len = 0;
e491a67a
YT
4665
4666 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4667 &output.ofpact, sizeof output,
4668 packet);
9583bc14 4669}
b256dc52
JS
4670
4671struct xlate_cache *
4672xlate_cache_new(void)
4673{
4674 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4675
4676 ofpbuf_init(&xcache->entries, 512);
4677 return xcache;
4678}
4679
4680static struct xc_entry *
4681xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4682{
4683 struct xc_entry *entry;
4684
4685 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4686 entry->type = type;
4687
4688 return entry;
4689}
4690
4691static void
4692xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4693{
4694 if (entry->u.dev.tx) {
4695 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4696 }
4697 if (entry->u.dev.rx) {
4698 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4699 }
4700 if (entry->u.dev.bfd) {
4701 bfd_account_rx(entry->u.dev.bfd, stats);
4702 }
4703}
4704
4705static void
4706xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4707{
84f0f298 4708 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
b256dc52
JS
4709 struct xbridge *xbridge;
4710 struct xbundle *xbundle;
4711 struct flow_wildcards wc;
4712
84f0f298 4713 xbridge = xbridge_lookup(xcfg, ofproto);
b256dc52
JS
4714 if (!xbridge) {
4715 return;
4716 }
4717
4718 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4719 NULL);
4720 if (!xbundle) {
4721 return;
4722 }
4723
4724 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4725}
4726
4727/* Push stats and perform side effects of flow translation. */
4728void
0725e747 4729xlate_push_stats(struct xlate_cache *xcache,
b256dc52
JS
4730 const struct dpif_flow_stats *stats)
4731{
4732 struct xc_entry *entry;
4733 struct ofpbuf entries = xcache->entries;
a36de779 4734 uint8_t dmac[ETH_ADDR_LEN];
b256dc52 4735
bc388583
BP
4736 if (!stats->n_packets) {
4737 return;
4738 }
4739
b256dc52
JS
4740 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4741 switch (entry->type) {
4742 case XC_RULE:
4743 rule_dpif_credit_stats(entry->u.rule, stats);
4744 break;
4745 case XC_BOND:
4746 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4747 entry->u.bond.vid, stats->n_bytes);
4748 break;
4749 case XC_NETDEV:
4750 xlate_cache_netdev(entry, stats);
4751 break;
4752 case XC_NETFLOW:
4753 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4754 entry->u.nf.iface, stats);
4755 break;
4756 case XC_MIRROR:
4757 mirror_update_stats(entry->u.mirror.mbridge,
4758 entry->u.mirror.mirrors,
4759 stats->n_packets, stats->n_bytes);
4760 break;
4761 case XC_LEARN:
0725e747 4762 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
b256dc52
JS
4763 break;
4764 case XC_NORMAL:
0725e747
BP
4765 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4766 entry->u.normal.vlan);
b256dc52
JS
4767 break;
4768 case XC_FIN_TIMEOUT:
4769 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4770 entry->u.fin.idle, entry->u.fin.hard);
4771 break;
1e684d7d
RW
4772 case XC_GROUP:
4773 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4774 stats);
4775 break;
a36de779
PS
4776 case XC_TNL_ARP:
4777 /* Lookup arp to avoid arp timeout. */
4778 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name, entry->u.tnl_arp_cache.d_ip, dmac);
4779 break;
b256dc52
JS
4780 default:
4781 OVS_NOT_REACHED();
4782 }
4783 }
4784}
4785
4786static void
4787xlate_dev_unref(struct xc_entry *entry)
4788{
4789 if (entry->u.dev.tx) {
4790 netdev_close(entry->u.dev.tx);
4791 }
4792 if (entry->u.dev.rx) {
4793 netdev_close(entry->u.dev.rx);
4794 }
4795 if (entry->u.dev.bfd) {
4796 bfd_unref(entry->u.dev.bfd);
4797 }
4798}
4799
4800static void
4801xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4802{
b256dc52
JS
4803 netflow_flow_clear(netflow, flow);
4804 netflow_unref(netflow);
4805 free(flow);
4806}
4807
4808void
4809xlate_cache_clear(struct xlate_cache *xcache)
4810{
4811 struct xc_entry *entry;
4812 struct ofpbuf entries;
4813
4814 if (!xcache) {
4815 return;
4816 }
4817
4818 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4819 switch (entry->type) {
4820 case XC_RULE:
4821 rule_dpif_unref(entry->u.rule);
4822 break;
4823 case XC_BOND:
4824 free(entry->u.bond.flow);
4825 bond_unref(entry->u.bond.bond);
4826 break;
4827 case XC_NETDEV:
4828 xlate_dev_unref(entry);
4829 break;
4830 case XC_NETFLOW:
4831 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4832 break;
4833 case XC_MIRROR:
4834 mbridge_unref(entry->u.mirror.mbridge);
4835 break;
4836 case XC_LEARN:
4165b5e0
JS
4837 free(entry->u.learn.fm);
4838 ofpbuf_delete(entry->u.learn.ofpacts);
b256dc52
JS
4839 break;
4840 case XC_NORMAL:
4841 free(entry->u.normal.flow);
4842 break;
4843 case XC_FIN_TIMEOUT:
83709dfa
JR
4844 /* 'u.fin.rule' is always already held as a XC_RULE, which
4845 * has already released it's reference above. */
b256dc52 4846 break;
1e684d7d
RW
4847 case XC_GROUP:
4848 group_dpif_unref(entry->u.group.group);
4849 break;
a36de779
PS
4850 case XC_TNL_ARP:
4851 break;
b256dc52
JS
4852 default:
4853 OVS_NOT_REACHED();
4854 }
4855 }
4856
4857 ofpbuf_clear(&xcache->entries);
4858}
4859
4860void
4861xlate_cache_delete(struct xlate_cache *xcache)
4862{
4863 xlate_cache_clear(xcache);
4864 ofpbuf_uninit(&xcache->entries);
4865 free(xcache);
4866}