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