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