]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-xlate.c
userspace: Add packet_type in dp_packet and flow
[mirror_ovs.git] / ofproto / ofproto-dpif-xlate.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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
19 #include <errno.h>
20 #include <arpa/inet.h>
21 #include <net/if.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24
25 #include "bfd.h"
26 #include "bitmap.h"
27 #include "bond.h"
28 #include "bundle.h"
29 #include "byte-order.h"
30 #include "cfm.h"
31 #include "connmgr.h"
32 #include "coverage.h"
33 #include "csum.h"
34 #include "dp-packet.h"
35 #include "dpif.h"
36 #include "in-band.h"
37 #include "lacp.h"
38 #include "learn.h"
39 #include "mac-learning.h"
40 #include "mcast-snooping.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
43 #include "netlink.h"
44 #include "nx-match.h"
45 #include "odp-execute.h"
46 #include "ofproto/ofproto-dpif-ipfix.h"
47 #include "ofproto/ofproto-dpif-mirror.h"
48 #include "ofproto/ofproto-dpif-monitor.h"
49 #include "ofproto/ofproto-dpif-sflow.h"
50 #include "ofproto/ofproto-dpif-trace.h"
51 #include "ofproto/ofproto-dpif-xlate-cache.h"
52 #include "ofproto/ofproto-dpif.h"
53 #include "ofproto/ofproto-provider.h"
54 #include "openvswitch/dynamic-string.h"
55 #include "openvswitch/meta-flow.h"
56 #include "openvswitch/list.h"
57 #include "openvswitch/ofp-actions.h"
58 #include "openvswitch/vlog.h"
59 #include "ovs-lldp.h"
60 #include "ovs-router.h"
61 #include "packets.h"
62 #include "tnl-neigh-cache.h"
63 #include "tnl-ports.h"
64 #include "tunnel.h"
65 #include "util.h"
66
67 COVERAGE_DEFINE(xlate_actions);
68 COVERAGE_DEFINE(xlate_actions_oversize);
69 COVERAGE_DEFINE(xlate_actions_too_many_output);
70
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
72
73 /* Maximum depth of flow table recursion (due to resubmit actions) in a
74 * flow translation.
75 *
76 * The goal of limiting the depth of resubmits is to ensure that flow
77 * translation eventually terminates. Only resubmits to the same table or an
78 * earlier table count against the maximum depth. This is because resubmits to
79 * strictly monotonically increasing table IDs will eventually terminate, since
80 * any OpenFlow switch has a finite number of tables. OpenFlow tables are most
81 * commonly traversed in numerically increasing order, so this limit has little
82 * effect on conventionally designed OpenFlow pipelines.
83 *
84 * Outputs to patch ports and to groups also count against the depth limit. */
85 #define MAX_DEPTH 64
86
87 /* Maximum number of resubmit actions in a flow translation, whether they are
88 * recursive or not. */
89 #define MAX_RESUBMITS (MAX_DEPTH * MAX_DEPTH)
90
91 struct xbridge {
92 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
93 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
94
95 struct ovs_list xbundles; /* Owned xbundles. */
96 struct hmap xports; /* Indexed by ofp_port. */
97
98 char *name; /* Name used in log messages. */
99 struct dpif *dpif; /* Datapath interface. */
100 struct mac_learning *ml; /* Mac learning handle. */
101 struct mcast_snooping *ms; /* Multicast Snooping handle. */
102 struct mbridge *mbridge; /* Mirroring. */
103 struct dpif_sflow *sflow; /* SFlow handle, or null. */
104 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
105 struct netflow *netflow; /* Netflow handle, or null. */
106 struct stp *stp; /* STP or null if disabled. */
107 struct rstp *rstp; /* RSTP or null if disabled. */
108
109 bool has_in_band; /* Bridge has in band control? */
110 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
111
112 /* Datapath feature support. */
113 struct dpif_backer_support support;
114 };
115
116 struct xbundle {
117 struct hmap_node hmap_node; /* In global 'xbundles' map. */
118 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
119
120 struct ovs_list list_node; /* In parent 'xbridges' list. */
121 struct xbridge *xbridge; /* Parent xbridge. */
122
123 struct ovs_list xports; /* Contains "struct xport"s. */
124
125 char *name; /* Name used in log messages. */
126 struct bond *bond; /* Nonnull iff more than one port. */
127 struct lacp *lacp; /* LACP handle or null. */
128
129 enum port_vlan_mode vlan_mode; /* VLAN mode. */
130 uint16_t qinq_ethtype; /* Ethertype of dot1q-tunnel interface
131 * either 0x8100 or 0x88a8. */
132 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
133 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
134 * NULL if all VLANs are trunked. */
135 unsigned long *cvlans; /* Bitmap of allowed customer vlans,
136 * NULL if all VLANs are allowed */
137 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
138 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
139 bool protected; /* Protected port mode */
140 };
141
142 struct xport {
143 struct hmap_node hmap_node; /* Node in global 'xports' map. */
144 struct ofport_dpif *ofport; /* Key in global 'xports map. */
145
146 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
147 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
148
149 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
150
151 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
152 struct xbundle *xbundle; /* Parent xbundle or null. */
153
154 struct netdev *netdev; /* 'ofport''s netdev. */
155
156 struct xbridge *xbridge; /* Parent bridge. */
157 struct xport *peer; /* Patch port peer or null. */
158
159 enum ofputil_port_config config; /* OpenFlow port configuration. */
160 enum ofputil_port_state state; /* OpenFlow port state. */
161 int stp_port_no; /* STP port number or -1 if not in use. */
162 struct rstp_port *rstp_port; /* RSTP port or null. */
163
164 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
165
166 bool may_enable; /* May be enabled in bonds. */
167 bool is_tunnel; /* Is a tunnel port. */
168
169 struct cfm *cfm; /* CFM handle or null. */
170 struct bfd *bfd; /* BFD handle or null. */
171 struct lldp *lldp; /* LLDP handle or null. */
172 };
173
174 struct xlate_ctx {
175 struct xlate_in *xin;
176 struct xlate_out *xout;
177
178 const struct xbridge *xbridge;
179
180 /* Flow at the last commit. */
181 struct flow base_flow;
182
183 /* Tunnel IP destination address as received. This is stored separately
184 * as the base_flow.tunnel is cleared on init to reflect the datapath
185 * behavior. Used to make sure not to send tunneled output to ourselves,
186 * which might lead to an infinite loop. This could happen easily
187 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
188 * actually set the tun_dst field. */
189 struct in6_addr orig_tunnel_ipv6_dst;
190
191 /* Stack for the push and pop actions. See comment above nx_stack_push()
192 * in nx-match.c for info on how the stack is stored. */
193 struct ofpbuf stack;
194
195 /* The rule that we are currently translating, or NULL. */
196 struct rule_dpif *rule;
197
198 /* Flow translation populates this with wildcards relevant in translation.
199 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
200 * null, this is a pointer to a temporary buffer. */
201 struct flow_wildcards *wc;
202
203 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
204 * this is the same pointer. When 'xin->odp_actions' is null, this points
205 * to a scratch ofpbuf. This allows code to add actions to
206 * 'ctx->odp_actions' without worrying about whether the caller really
207 * wants actions. */
208 struct ofpbuf *odp_actions;
209
210 /* Statistics maintained by xlate_table_action().
211 *
212 * These statistics limit the amount of work that a single flow
213 * translation can perform. The goal of the first of these, 'depth', is
214 * primarily to prevent translation from performing an infinite amount of
215 * work. It counts the current depth of nested "resubmit"s (and a few
216 * other activities); when a resubmit returns, it decreases. Resubmits to
217 * tables in strictly monotonically increasing order don't contribute to
218 * 'depth' because they cannot cause a flow translation to take an infinite
219 * amount of time (because the number of tables is finite). Translation
220 * aborts when 'depth' exceeds MAX_DEPTH.
221 *
222 * 'resubmits', on the other hand, prevents flow translation from
223 * performing an extraordinarily large while still finite amount of work.
224 * It counts the total number of resubmits (and a few other activities)
225 * that have been executed. Returning from a resubmit does not affect this
226 * counter. Thus, this limits the amount of work that a particular
227 * translation can perform. Translation aborts when 'resubmits' exceeds
228 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
229 */
230 int depth; /* Current resubmit nesting depth. */
231 int resubmits; /* Total number of resubmits. */
232 bool in_group; /* Currently translating ofgroup, if true. */
233 bool in_action_set; /* Currently translating action_set, if true. */
234 bool in_packet_out; /* Currently translating a packet_out msg, if
235 * true. */
236
237 uint8_t table_id; /* OpenFlow table ID where flow was found. */
238 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
239 uint32_t orig_skb_priority; /* Priority when packet arrived. */
240 uint32_t sflow_n_outputs; /* Number of output ports. */
241 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
242 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
243 bool exit; /* No further actions should be processed. */
244 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
245 int mirror_snaplen; /* Max size of a mirror packet in byte. */
246
247 /* Freezing Translation
248 * ====================
249 *
250 * At some point during translation, the code may recognize the need to halt
251 * and checkpoint the translation in a way that it can be restarted again
252 * later. We call the checkpointing process "freezing" and the restarting
253 * process "thawing".
254 *
255 * The use cases for freezing are:
256 *
257 * - "Recirculation", where the translation process discovers that it
258 * doesn't have enough information to complete translation without
259 * actually executing the actions that have already been translated,
260 * which provides the additionally needed information. In these
261 * situations, translation freezes translation and assigns the frozen
262 * data a unique "recirculation ID", which it associates with the data
263 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
264 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
265 * actions. When a packet hits that action, the datapath looks its
266 * flow up again using the ID. If there's a miss, it comes back to
267 * userspace, which find the recirculation table entry for the ID,
268 * thaws the associated frozen data, and continues translation from
269 * that point given the additional information that is now known.
270 *
271 * The archetypal example is MPLS. As MPLS is implemented in
272 * OpenFlow, the protocol that follows the last MPLS label becomes
273 * known only when that label is popped by an OpenFlow action. That
274 * means that Open vSwitch can't extract the headers beyond the MPLS
275 * labels until the pop action is executed. Thus, at that point
276 * translation uses the recirculation process to extract the headers
277 * beyond the MPLS labels.
278 *
279 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
280 * output to bonds. OVS pre-populates all the datapath flows for bond
281 * output in the datapath, though, which means that the elaborate
282 * process of coming back to userspace for a second round of
283 * translation isn't needed, and so bonds don't follow the above
284 * process.)
285 *
286 * - "Continuation". A continuation is a way for an OpenFlow controller
287 * to interpose on a packet's traversal of the OpenFlow tables. When
288 * the translation process encounters a "controller" action with the
289 * "pause" flag, it freezes translation, serializes the frozen data,
290 * and sends it to an OpenFlow controller. The controller then
291 * examines and possibly modifies the frozen data and eventually sends
292 * it back to the switch, which thaws it and continues translation.
293 *
294 * The main problem of freezing translation is preserving state, so that
295 * when the translation is thawed later it resumes from where it left off,
296 * without disruption. In particular, actions must be preserved as follows:
297 *
298 * - If we're freezing because an action needed more information, the
299 * action that prompted it.
300 *
301 * - Any actions remaining to be translated within the current flow.
302 *
303 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
304 * following the resubmit action. Resubmit actions can be nested, so
305 * this has to go all the way up the control stack.
306 *
307 * - The OpenFlow 1.1+ action set.
308 *
309 * State that actions and flow table lookups can depend on, such as the
310 * following, must also be preserved:
311 *
312 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
313 *
314 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
315 *
316 * - The table ID and cookie of the flow being translated at each level
317 * of the control stack, because these can become visible through
318 * OFPAT_CONTROLLER actions (and other ways).
319 *
320 * Translation allows for the control of this state preservation via these
321 * members. When a need to freeze translation is identified, the
322 * translation process:
323 *
324 * 1. Sets 'freezing' to true.
325 *
326 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
327 * translation process.
328 *
329 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
330 * frozen_actions.header to the action to make it easy to find it later.
331 * This action holds the current table ID and cookie so that they can be
332 * restored during a post-recirculation upcall translation.
333 *
334 * 4. Adds the action that prompted recirculation and any actions following
335 * it within the same flow to 'frozen_actions', so that they can be
336 * executed during a post-recirculation upcall translation.
337 *
338 * 5. Returns.
339 *
340 * 6. The action that prompted recirculation might be nested in a stack of
341 * nested "resubmit"s that have actions remaining. Each of these notices
342 * that we're exiting and freezing and responds by adding more
343 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
344 * followed by any actions that were yet unprocessed.
345 *
346 * If we're freezing because of recirculation, the caller generates a
347 * recirculation ID and associates all the state produced by this process
348 * with it. For post-recirculation upcall translation, the caller passes it
349 * back in for the new translation to execute. The process yielded a set of
350 * ofpacts that can be translated directly, so it is not much of a special
351 * case at that point.
352 */
353 bool freezing;
354 bool recirc_update_dp_hash; /* Generated recirculation will be preceded
355 * by datapath HASH action to get an updated
356 * dp_hash after recirculation. */
357 uint32_t dp_hash_alg;
358 uint32_t dp_hash_basis;
359 struct ofpbuf frozen_actions;
360 const struct ofpact_controller *pause;
361
362 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
363 * This is a trigger for recirculation in cases where translating an action
364 * or looking up a flow requires access to the fields of the packet after
365 * the MPLS label stack that was originally present. */
366 bool was_mpls;
367
368 /* True if conntrack has been performed on this packet during processing
369 * on the current bridge. This is used to determine whether conntrack
370 * state from the datapath should be honored after thawing. */
371 bool conntracked;
372
373 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
374 struct ofpact_nat *ct_nat_action;
375
376 /* OpenFlow 1.1+ action set.
377 *
378 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
379 * When translation is otherwise complete, ofpacts_execute_action_set()
380 * converts it to a set of "struct ofpact"s that can be translated into
381 * datapath actions. */
382 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
383 struct ofpbuf action_set; /* Action set. */
384
385 enum xlate_error error; /* Translation failed. */
386 };
387
388 /* Structure to track VLAN manipulation */
389 struct xvlan_single {
390 uint16_t tpid;
391 uint16_t vid;
392 uint16_t pcp;
393 };
394
395 struct xvlan {
396 struct xvlan_single v[FLOW_MAX_VLAN_HEADERS];
397 };
398
399 const char *xlate_strerror(enum xlate_error error)
400 {
401 switch (error) {
402 case XLATE_OK:
403 return "OK";
404 case XLATE_BRIDGE_NOT_FOUND:
405 return "Bridge not found";
406 case XLATE_RECURSION_TOO_DEEP:
407 return "Recursion too deep";
408 case XLATE_TOO_MANY_RESUBMITS:
409 return "Too many resubmits";
410 case XLATE_STACK_TOO_DEEP:
411 return "Stack too deep";
412 case XLATE_NO_RECIRCULATION_CONTEXT:
413 return "No recirculation context";
414 case XLATE_RECIRCULATION_CONFLICT:
415 return "Recirculation conflict";
416 case XLATE_TOO_MANY_MPLS_LABELS:
417 return "Too many MPLS labels";
418 case XLATE_INVALID_TUNNEL_METADATA:
419 return "Invalid tunnel metadata";
420 }
421 return "Unknown error";
422 }
423
424 static void xlate_action_set(struct xlate_ctx *ctx);
425 static void xlate_commit_actions(struct xlate_ctx *ctx);
426
427 static void
428 apply_nested_clone_actions(struct xlate_ctx *ctx, const struct xport *in_dev,
429 struct xport *out_dev);
430
431 static void
432 ctx_trigger_freeze(struct xlate_ctx *ctx)
433 {
434 ctx->exit = true;
435 ctx->freezing = true;
436 }
437
438 static void
439 ctx_trigger_recirculate_with_hash(struct xlate_ctx *ctx, uint32_t type,
440 uint32_t basis)
441 {
442 ctx->exit = true;
443 ctx->freezing = true;
444 ctx->recirc_update_dp_hash = true;
445 ctx->dp_hash_alg = type;
446 ctx->dp_hash_basis = basis;
447 }
448
449 static bool
450 ctx_first_frozen_action(const struct xlate_ctx *ctx)
451 {
452 return !ctx->frozen_actions.size;
453 }
454
455 static void
456 ctx_cancel_freeze(struct xlate_ctx *ctx)
457 {
458 if (ctx->freezing) {
459 ctx->freezing = false;
460 ctx->recirc_update_dp_hash = false;
461 ofpbuf_clear(&ctx->frozen_actions);
462 ctx->frozen_actions.header = NULL;
463 }
464 }
465
466 static void finish_freezing(struct xlate_ctx *ctx);
467
468 /* A controller may use OFPP_NONE as the ingress port to indicate that
469 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
470 * when an input bundle is needed for validation (e.g., mirroring or
471 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
472 * any 'port' structs, so care must be taken when dealing with it. */
473 static struct xbundle ofpp_none_bundle = {
474 .name = "OFPP_NONE",
475 .vlan_mode = PORT_VLAN_TRUNK
476 };
477
478 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
479 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
480 * traffic egressing the 'ofport' with that priority should be marked with. */
481 struct skb_priority_to_dscp {
482 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
483 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
484
485 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
486 };
487
488 /* Xlate config contains hash maps of all bridges, bundles and ports.
489 * Xcfgp contains the pointer to the current xlate configuration.
490 * When the main thread needs to change the configuration, it copies xcfgp to
491 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
492 * does not block handler and revalidator threads. */
493 struct xlate_cfg {
494 struct hmap xbridges;
495 struct hmap xbundles;
496 struct hmap xports;
497 };
498 static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
499 static struct xlate_cfg *new_xcfg = NULL;
500
501 static bool may_receive(const struct xport *, struct xlate_ctx *);
502 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
503 struct xlate_ctx *);
504 static void xlate_normal(struct xlate_ctx *);
505 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
506 uint8_t table_id, bool may_packet_in,
507 bool honor_table_miss, bool with_ct_orig);
508 static bool input_vid_is_valid(const struct xlate_ctx *,
509 uint16_t vid, struct xbundle *);
510 static void xvlan_copy(struct xvlan *dst, const struct xvlan *src);
511 static void xvlan_pop(struct xvlan *src);
512 static void xvlan_push_uninit(struct xvlan *src);
513 static void xvlan_extract(const struct flow *, struct xvlan *);
514 static void xvlan_put(struct flow *, const struct xvlan *);
515 static void xvlan_input_translate(const struct xbundle *,
516 const struct xvlan *in,
517 struct xvlan *xvlan);
518 static void xvlan_output_translate(const struct xbundle *,
519 const struct xvlan *xvlan,
520 struct xvlan *out);
521 static void output_normal(struct xlate_ctx *, const struct xbundle *,
522 const struct xvlan *);
523
524 /* Optional bond recirculation parameter to compose_output_action(). */
525 struct xlate_bond_recirc {
526 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
527 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
528 uint32_t hash_basis; /* Compute hash for recirc before. */
529 };
530
531 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
532 const struct xlate_bond_recirc *xr);
533
534 static struct xbridge *xbridge_lookup(struct xlate_cfg *,
535 const struct ofproto_dpif *);
536 static struct xbridge *xbridge_lookup_by_uuid(struct xlate_cfg *,
537 const struct uuid *);
538 static struct xbundle *xbundle_lookup(struct xlate_cfg *,
539 const struct ofbundle *);
540 static struct xport *xport_lookup(struct xlate_cfg *,
541 const struct ofport_dpif *);
542 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
543 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
544 uint32_t skb_priority);
545 static void clear_skb_priorities(struct xport *);
546 static size_t count_skb_priorities(const struct xport *);
547 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
548 uint8_t *dscp);
549
550 static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
551 static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
552 static void xlate_xport_init(struct xlate_cfg *, struct xport *);
553 static void xlate_xbridge_set(struct xbridge *, struct dpif *,
554 const struct mac_learning *, struct stp *,
555 struct rstp *, const struct mcast_snooping *,
556 const struct mbridge *,
557 const struct dpif_sflow *,
558 const struct dpif_ipfix *,
559 const struct netflow *,
560 bool forward_bpdu, bool has_in_band,
561 const struct dpif_backer_support *);
562 static void xlate_xbundle_set(struct xbundle *xbundle,
563 enum port_vlan_mode vlan_mode,
564 uint16_t qinq_ethtype, int vlan,
565 unsigned long *trunks, unsigned long *cvlans,
566 bool use_priority_tags,
567 const struct bond *bond, const struct lacp *lacp,
568 bool floodable, bool protected);
569 static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
570 const struct netdev *netdev, const struct cfm *cfm,
571 const struct bfd *bfd, const struct lldp *lldp,
572 int stp_port_no, const struct rstp_port *rstp_port,
573 enum ofputil_port_config config,
574 enum ofputil_port_state state, bool is_tunnel,
575 bool may_enable);
576 static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
577 static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
578 static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
579 static void xlate_xbridge_copy(struct xbridge *);
580 static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
581 static void xlate_xport_copy(struct xbridge *, struct xbundle *,
582 struct xport *);
583 static void xlate_xcfg_free(struct xlate_cfg *);
584 \f
585 /* Tracing helpers. */
586
587 /* If tracing is enabled in 'ctx', creates a new trace node and appends it to
588 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
589 * its text is created from 'format' by treating it as a printf format string.
590 * Returns the list of nodes embedded within the new trace node; ordinarily,
591 * the calleer can ignore this, but it is useful if the caller needs to nest
592 * more trace nodes within the new node.
593 *
594 * If tracing is not enabled, does nothing and returns NULL. */
595 static struct ovs_list * OVS_PRINTF_FORMAT(3, 4)
596 xlate_report(const struct xlate_ctx *ctx, enum oftrace_node_type type,
597 const char *format, ...)
598 {
599 struct ovs_list *subtrace = NULL;
600 if (OVS_UNLIKELY(ctx->xin->trace)) {
601 va_list args;
602 va_start(args, format);
603 char *text = xvasprintf(format, args);
604 subtrace = &oftrace_report(ctx->xin->trace, type, text)->subs;
605 va_end(args);
606 free(text);
607 }
608 return subtrace;
609 }
610
611 /* This is like xlate_report() for errors that are serious enough that we
612 * should log them even if we are not tracing. */
613 static void OVS_PRINTF_FORMAT(2, 3)
614 xlate_report_error(const struct xlate_ctx *ctx, const char *format, ...)
615 {
616 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
617 if (!OVS_UNLIKELY(ctx->xin->trace)
618 && (!ctx->xin->packet || VLOG_DROP_WARN(&rl))) {
619 return;
620 }
621
622 struct ds s = DS_EMPTY_INITIALIZER;
623 va_list args;
624 va_start(args, format);
625 ds_put_format_valist(&s, format, args);
626 va_end(args);
627
628 if (ctx->xin->trace) {
629 oftrace_report(ctx->xin->trace, OFT_ERROR, ds_cstr(&s));
630 } else {
631 ds_put_cstr(&s, " while processing ");
632 flow_format(&s, &ctx->base_flow);
633 ds_put_format(&s, " on bridge %s", ctx->xbridge->name);
634 VLOG_WARN("%s", ds_cstr(&s));
635 }
636 ds_destroy(&s);
637 }
638
639 /* This is like xlate_report() for messages that should be logged at debug
640 * level (even if we are not tracing) because they can be valuable for
641 * debugging. */
642 static void OVS_PRINTF_FORMAT(3, 4)
643 xlate_report_debug(const struct xlate_ctx *ctx, enum oftrace_node_type type,
644 const char *format, ...)
645 {
646 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
647 if (!OVS_UNLIKELY(ctx->xin->trace)
648 && (!ctx->xin->packet || VLOG_DROP_DBG(&rl))) {
649 return;
650 }
651
652 struct ds s = DS_EMPTY_INITIALIZER;
653 va_list args;
654 va_start(args, format);
655 ds_put_format_valist(&s, format, args);
656 va_end(args);
657
658 if (ctx->xin->trace) {
659 oftrace_report(ctx->xin->trace, type, ds_cstr(&s));
660 } else {
661 VLOG_DBG("bridge %s: %s", ctx->xbridge->name, ds_cstr(&s));
662 }
663 ds_destroy(&s);
664 }
665
666 /* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
667 * trace, whose text is 'title' followed by a formatted version of the
668 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
669 *
670 * If tracing is not enabled, does nothing. */
671 static void
672 xlate_report_actions(const struct xlate_ctx *ctx, enum oftrace_node_type type,
673 const char *title,
674 const struct ofpact *ofpacts, size_t ofpacts_len)
675 {
676 if (OVS_UNLIKELY(ctx->xin->trace)) {
677 struct ds s = DS_EMPTY_INITIALIZER;
678 ds_put_format(&s, "%s: ", title);
679 ofpacts_format(ofpacts, ofpacts_len, &s);
680 oftrace_report(ctx->xin->trace, type, ds_cstr(&s));
681 ds_destroy(&s);
682 }
683 }
684
685 /* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
686 * trace, whose the message is a formatted version of the OpenFlow action set.
687 * 'verb' should be "was" or "is", depending on whether the action set reported
688 * is the new action set or the old one.
689 *
690 * If tracing is not enabled, does nothing. */
691 static void
692 xlate_report_action_set(const struct xlate_ctx *ctx, const char *verb)
693 {
694 if (OVS_UNLIKELY(ctx->xin->trace)) {
695 struct ofpbuf action_list;
696 ofpbuf_init(&action_list, 0);
697 ofpacts_execute_action_set(&action_list, &ctx->action_set);
698 if (action_list.size) {
699 struct ds s = DS_EMPTY_INITIALIZER;
700 ofpacts_format(action_list.data, action_list.size, &s);
701 xlate_report(ctx, OFT_DETAIL, "action set %s: %s",
702 verb, ds_cstr(&s));
703 ds_destroy(&s);
704 } else {
705 xlate_report(ctx, OFT_DETAIL, "action set %s empty", verb);
706 }
707 ofpbuf_uninit(&action_list);
708 }
709 }
710
711
712 /* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
713 * OpenFlow table 'table_id') to the trace and makes this node the parent for
714 * future trace nodes. The caller should save ctx->xin->trace before calling
715 * this function, then after tracing all of the activities under the table,
716 * restore its previous value.
717 *
718 * If tracing is not enabled, does nothing. */
719 static void
720 xlate_report_table(const struct xlate_ctx *ctx, struct rule_dpif *rule,
721 uint8_t table_id)
722 {
723 if (OVS_LIKELY(!ctx->xin->trace)) {
724 return;
725 }
726
727 struct ds s = DS_EMPTY_INITIALIZER;
728 ds_put_format(&s, "%2d. ", table_id);
729 if (rule == ctx->xin->ofproto->miss_rule) {
730 ds_put_cstr(&s, "No match, and a \"packet-in\" is called for.");
731 } else if (rule == ctx->xin->ofproto->no_packet_in_rule) {
732 ds_put_cstr(&s, "No match.");
733 } else if (rule == ctx->xin->ofproto->drop_frags_rule) {
734 ds_put_cstr(&s, "Packets are IP fragments and "
735 "the fragment handling mode is \"drop\".");
736 } else {
737 minimatch_format(&rule->up.cr.match,
738 ofproto_get_tun_tab(&ctx->xin->ofproto->up),
739 &s, OFP_DEFAULT_PRIORITY);
740 if (ds_last(&s) != ' ') {
741 ds_put_cstr(&s, ", ");
742 }
743 ds_put_format(&s, "priority %d", rule->up.cr.priority);
744 if (rule->up.flow_cookie) {
745 ds_put_format(&s, ", cookie %#"PRIx64,
746 ntohll(rule->up.flow_cookie));
747 }
748 }
749 ctx->xin->trace = &oftrace_report(ctx->xin->trace, OFT_TABLE,
750 ds_cstr(&s))->subs;
751 ds_destroy(&s);
752 }
753
754 /* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
755 * reporting the value of subfield 'sf'.
756 *
757 * If tracing is not enabled, does nothing. */
758 static void
759 xlate_report_subfield(const struct xlate_ctx *ctx,
760 const struct mf_subfield *sf)
761 {
762 if (OVS_UNLIKELY(ctx->xin->trace)) {
763 struct ds s = DS_EMPTY_INITIALIZER;
764 mf_format_subfield(sf, &s);
765 ds_put_cstr(&s, " is now ");
766
767 if (sf->ofs == 0 && sf->n_bits >= sf->field->n_bits) {
768 union mf_value value;
769 mf_get_value(sf->field, &ctx->xin->flow, &value);
770 mf_format(sf->field, &value, NULL, &s);
771 } else {
772 union mf_subvalue cst;
773 mf_read_subfield(sf, &ctx->xin->flow, &cst);
774 ds_put_hex(&s, &cst, sizeof cst);
775 }
776
777 xlate_report(ctx, OFT_DETAIL, "%s", ds_cstr(&s));
778
779 ds_destroy(&s);
780 }
781 }
782 \f
783 static void
784 xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
785 {
786 ovs_list_init(&xbridge->xbundles);
787 hmap_init(&xbridge->xports);
788 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
789 hash_pointer(xbridge->ofproto, 0));
790 }
791
792 static void
793 xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
794 {
795 ovs_list_init(&xbundle->xports);
796 ovs_list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
797 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
798 hash_pointer(xbundle->ofbundle, 0));
799 }
800
801 static void
802 xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
803 {
804 hmap_init(&xport->skb_priorities);
805 hmap_insert(&xcfg->xports, &xport->hmap_node,
806 hash_pointer(xport->ofport, 0));
807 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
808 hash_ofp_port(xport->ofp_port));
809 }
810
811 static void
812 xlate_xbridge_set(struct xbridge *xbridge,
813 struct dpif *dpif,
814 const struct mac_learning *ml, struct stp *stp,
815 struct rstp *rstp, const struct mcast_snooping *ms,
816 const struct mbridge *mbridge,
817 const struct dpif_sflow *sflow,
818 const struct dpif_ipfix *ipfix,
819 const struct netflow *netflow,
820 bool forward_bpdu, bool has_in_band,
821 const struct dpif_backer_support *support)
822 {
823 if (xbridge->ml != ml) {
824 mac_learning_unref(xbridge->ml);
825 xbridge->ml = mac_learning_ref(ml);
826 }
827
828 if (xbridge->ms != ms) {
829 mcast_snooping_unref(xbridge->ms);
830 xbridge->ms = mcast_snooping_ref(ms);
831 }
832
833 if (xbridge->mbridge != mbridge) {
834 mbridge_unref(xbridge->mbridge);
835 xbridge->mbridge = mbridge_ref(mbridge);
836 }
837
838 if (xbridge->sflow != sflow) {
839 dpif_sflow_unref(xbridge->sflow);
840 xbridge->sflow = dpif_sflow_ref(sflow);
841 }
842
843 if (xbridge->ipfix != ipfix) {
844 dpif_ipfix_unref(xbridge->ipfix);
845 xbridge->ipfix = dpif_ipfix_ref(ipfix);
846 }
847
848 if (xbridge->stp != stp) {
849 stp_unref(xbridge->stp);
850 xbridge->stp = stp_ref(stp);
851 }
852
853 if (xbridge->rstp != rstp) {
854 rstp_unref(xbridge->rstp);
855 xbridge->rstp = rstp_ref(rstp);
856 }
857
858 if (xbridge->netflow != netflow) {
859 netflow_unref(xbridge->netflow);
860 xbridge->netflow = netflow_ref(netflow);
861 }
862
863 xbridge->dpif = dpif;
864 xbridge->forward_bpdu = forward_bpdu;
865 xbridge->has_in_band = has_in_band;
866 xbridge->support = *support;
867 }
868
869 static void
870 xlate_xbundle_set(struct xbundle *xbundle,
871 enum port_vlan_mode vlan_mode, uint16_t qinq_ethtype,
872 int vlan, unsigned long *trunks, unsigned long *cvlans,
873 bool use_priority_tags,
874 const struct bond *bond, const struct lacp *lacp,
875 bool floodable, bool protected)
876 {
877 ovs_assert(xbundle->xbridge);
878
879 xbundle->vlan_mode = vlan_mode;
880 xbundle->qinq_ethtype = qinq_ethtype;
881 xbundle->vlan = vlan;
882 xbundle->trunks = trunks;
883 xbundle->cvlans = cvlans;
884 xbundle->use_priority_tags = use_priority_tags;
885 xbundle->floodable = floodable;
886 xbundle->protected = protected;
887
888 if (xbundle->bond != bond) {
889 bond_unref(xbundle->bond);
890 xbundle->bond = bond_ref(bond);
891 }
892
893 if (xbundle->lacp != lacp) {
894 lacp_unref(xbundle->lacp);
895 xbundle->lacp = lacp_ref(lacp);
896 }
897 }
898
899 static void
900 xlate_xport_set(struct xport *xport, odp_port_t odp_port,
901 const struct netdev *netdev, const struct cfm *cfm,
902 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
903 const struct rstp_port* rstp_port,
904 enum ofputil_port_config config, enum ofputil_port_state state,
905 bool is_tunnel, bool may_enable)
906 {
907 xport->config = config;
908 xport->state = state;
909 xport->stp_port_no = stp_port_no;
910 xport->is_tunnel = is_tunnel;
911 xport->may_enable = may_enable;
912 xport->odp_port = odp_port;
913
914 if (xport->rstp_port != rstp_port) {
915 rstp_port_unref(xport->rstp_port);
916 xport->rstp_port = rstp_port_ref(rstp_port);
917 }
918
919 if (xport->cfm != cfm) {
920 cfm_unref(xport->cfm);
921 xport->cfm = cfm_ref(cfm);
922 }
923
924 if (xport->bfd != bfd) {
925 bfd_unref(xport->bfd);
926 xport->bfd = bfd_ref(bfd);
927 }
928
929 if (xport->lldp != lldp) {
930 lldp_unref(xport->lldp);
931 xport->lldp = lldp_ref(lldp);
932 }
933
934 if (xport->netdev != netdev) {
935 netdev_close(xport->netdev);
936 xport->netdev = netdev_ref(netdev);
937 }
938 }
939
940 static void
941 xlate_xbridge_copy(struct xbridge *xbridge)
942 {
943 struct xbundle *xbundle;
944 struct xport *xport;
945 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
946 new_xbridge->ofproto = xbridge->ofproto;
947 new_xbridge->name = xstrdup(xbridge->name);
948 xlate_xbridge_init(new_xcfg, new_xbridge);
949
950 xlate_xbridge_set(new_xbridge,
951 xbridge->dpif, xbridge->ml, xbridge->stp,
952 xbridge->rstp, xbridge->ms, xbridge->mbridge,
953 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
954 xbridge->forward_bpdu, xbridge->has_in_band,
955 &xbridge->support);
956 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
957 xlate_xbundle_copy(new_xbridge, xbundle);
958 }
959
960 /* Copy xports which are not part of a xbundle */
961 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
962 if (!xport->xbundle) {
963 xlate_xport_copy(new_xbridge, NULL, xport);
964 }
965 }
966 }
967
968 static void
969 xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
970 {
971 struct xport *xport;
972 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
973 new_xbundle->ofbundle = xbundle->ofbundle;
974 new_xbundle->xbridge = xbridge;
975 new_xbundle->name = xstrdup(xbundle->name);
976 xlate_xbundle_init(new_xcfg, new_xbundle);
977
978 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode, xbundle->qinq_ethtype,
979 xbundle->vlan, xbundle->trunks, xbundle->cvlans,
980 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
981 xbundle->floodable, xbundle->protected);
982 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
983 xlate_xport_copy(xbridge, new_xbundle, xport);
984 }
985 }
986
987 static void
988 xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
989 struct xport *xport)
990 {
991 struct skb_priority_to_dscp *pdscp, *new_pdscp;
992 struct xport *new_xport = xzalloc(sizeof *xport);
993 new_xport->ofport = xport->ofport;
994 new_xport->ofp_port = xport->ofp_port;
995 new_xport->xbridge = xbridge;
996 xlate_xport_init(new_xcfg, new_xport);
997
998 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
999 xport->bfd, xport->lldp, xport->stp_port_no,
1000 xport->rstp_port, xport->config, xport->state,
1001 xport->is_tunnel, xport->may_enable);
1002
1003 if (xport->peer) {
1004 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
1005 if (peer) {
1006 new_xport->peer = peer;
1007 new_xport->peer->peer = new_xport;
1008 }
1009 }
1010
1011 if (xbundle) {
1012 new_xport->xbundle = xbundle;
1013 ovs_list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
1014 }
1015
1016 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
1017 new_pdscp = xmalloc(sizeof *pdscp);
1018 new_pdscp->skb_priority = pdscp->skb_priority;
1019 new_pdscp->dscp = pdscp->dscp;
1020 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
1021 hash_int(new_pdscp->skb_priority, 0));
1022 }
1023 }
1024
1025 /* Sets the current xlate configuration to new_xcfg and frees the old xlate
1026 * configuration in xcfgp.
1027 *
1028 * This needs to be called after editing the xlate configuration.
1029 *
1030 * Functions that edit the new xlate configuration are
1031 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
1032 *
1033 * A sample workflow:
1034 *
1035 * xlate_txn_start();
1036 * ...
1037 * edit_xlate_configuration();
1038 * ...
1039 * xlate_txn_commit(); */
1040 void
1041 xlate_txn_commit(void)
1042 {
1043 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1044
1045 ovsrcu_set(&xcfgp, new_xcfg);
1046 ovsrcu_synchronize();
1047 xlate_xcfg_free(xcfg);
1048 new_xcfg = NULL;
1049 }
1050
1051 /* Copies the current xlate configuration in xcfgp to new_xcfg.
1052 *
1053 * This needs to be called prior to editing the xlate configuration. */
1054 void
1055 xlate_txn_start(void)
1056 {
1057 struct xbridge *xbridge;
1058 struct xlate_cfg *xcfg;
1059
1060 ovs_assert(!new_xcfg);
1061
1062 new_xcfg = xmalloc(sizeof *new_xcfg);
1063 hmap_init(&new_xcfg->xbridges);
1064 hmap_init(&new_xcfg->xbundles);
1065 hmap_init(&new_xcfg->xports);
1066
1067 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1068 if (!xcfg) {
1069 return;
1070 }
1071
1072 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1073 xlate_xbridge_copy(xbridge);
1074 }
1075 }
1076
1077
1078 static void
1079 xlate_xcfg_free(struct xlate_cfg *xcfg)
1080 {
1081 struct xbridge *xbridge, *next_xbridge;
1082
1083 if (!xcfg) {
1084 return;
1085 }
1086
1087 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
1088 xlate_xbridge_remove(xcfg, xbridge);
1089 }
1090
1091 hmap_destroy(&xcfg->xbridges);
1092 hmap_destroy(&xcfg->xbundles);
1093 hmap_destroy(&xcfg->xports);
1094 free(xcfg);
1095 }
1096
1097 void
1098 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
1099 struct dpif *dpif,
1100 const struct mac_learning *ml, struct stp *stp,
1101 struct rstp *rstp, const struct mcast_snooping *ms,
1102 const struct mbridge *mbridge,
1103 const struct dpif_sflow *sflow,
1104 const struct dpif_ipfix *ipfix,
1105 const struct netflow *netflow,
1106 bool forward_bpdu, bool has_in_band,
1107 const struct dpif_backer_support *support)
1108 {
1109 struct xbridge *xbridge;
1110
1111 ovs_assert(new_xcfg);
1112
1113 xbridge = xbridge_lookup(new_xcfg, ofproto);
1114 if (!xbridge) {
1115 xbridge = xzalloc(sizeof *xbridge);
1116 xbridge->ofproto = ofproto;
1117
1118 xlate_xbridge_init(new_xcfg, xbridge);
1119 }
1120
1121 free(xbridge->name);
1122 xbridge->name = xstrdup(name);
1123
1124 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
1125 netflow, forward_bpdu, has_in_band, support);
1126 }
1127
1128 static void
1129 xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
1130 {
1131 struct xbundle *xbundle, *next_xbundle;
1132 struct xport *xport, *next_xport;
1133
1134 if (!xbridge) {
1135 return;
1136 }
1137
1138 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
1139 xlate_xport_remove(xcfg, xport);
1140 }
1141
1142 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
1143 xlate_xbundle_remove(xcfg, xbundle);
1144 }
1145
1146 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
1147 mac_learning_unref(xbridge->ml);
1148 mcast_snooping_unref(xbridge->ms);
1149 mbridge_unref(xbridge->mbridge);
1150 dpif_sflow_unref(xbridge->sflow);
1151 dpif_ipfix_unref(xbridge->ipfix);
1152 netflow_unref(xbridge->netflow);
1153 stp_unref(xbridge->stp);
1154 rstp_unref(xbridge->rstp);
1155 hmap_destroy(&xbridge->xports);
1156 free(xbridge->name);
1157 free(xbridge);
1158 }
1159
1160 void
1161 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
1162 {
1163 struct xbridge *xbridge;
1164
1165 ovs_assert(new_xcfg);
1166
1167 xbridge = xbridge_lookup(new_xcfg, ofproto);
1168 xlate_xbridge_remove(new_xcfg, xbridge);
1169 }
1170
1171 void
1172 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1173 const char *name, enum port_vlan_mode vlan_mode,
1174 uint16_t qinq_ethtype, int vlan,
1175 unsigned long *trunks, unsigned long *cvlans,
1176 bool use_priority_tags,
1177 const struct bond *bond, const struct lacp *lacp,
1178 bool floodable, bool protected)
1179 {
1180 struct xbundle *xbundle;
1181
1182 ovs_assert(new_xcfg);
1183
1184 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1185 if (!xbundle) {
1186 xbundle = xzalloc(sizeof *xbundle);
1187 xbundle->ofbundle = ofbundle;
1188 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
1189
1190 xlate_xbundle_init(new_xcfg, xbundle);
1191 }
1192
1193 free(xbundle->name);
1194 xbundle->name = xstrdup(name);
1195
1196 xlate_xbundle_set(xbundle, vlan_mode, qinq_ethtype, vlan, trunks, cvlans,
1197 use_priority_tags, bond, lacp, floodable, protected);
1198 }
1199
1200 static void
1201 xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
1202 {
1203 struct xport *xport;
1204
1205 if (!xbundle) {
1206 return;
1207 }
1208
1209 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
1210 xport->xbundle = NULL;
1211 }
1212
1213 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
1214 ovs_list_remove(&xbundle->list_node);
1215 bond_unref(xbundle->bond);
1216 lacp_unref(xbundle->lacp);
1217 free(xbundle->name);
1218 free(xbundle);
1219 }
1220
1221 void
1222 xlate_bundle_remove(struct ofbundle *ofbundle)
1223 {
1224 struct xbundle *xbundle;
1225
1226 ovs_assert(new_xcfg);
1227
1228 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1229 xlate_xbundle_remove(new_xcfg, xbundle);
1230 }
1231
1232 void
1233 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1234 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1235 odp_port_t odp_port, const struct netdev *netdev,
1236 const struct cfm *cfm, const struct bfd *bfd,
1237 const struct lldp *lldp, struct ofport_dpif *peer,
1238 int stp_port_no, const struct rstp_port *rstp_port,
1239 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
1240 enum ofputil_port_config config,
1241 enum ofputil_port_state state, bool is_tunnel,
1242 bool may_enable)
1243 {
1244 size_t i;
1245 struct xport *xport;
1246
1247 ovs_assert(new_xcfg);
1248
1249 xport = xport_lookup(new_xcfg, ofport);
1250 if (!xport) {
1251 xport = xzalloc(sizeof *xport);
1252 xport->ofport = ofport;
1253 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
1254 xport->ofp_port = ofp_port;
1255
1256 xlate_xport_init(new_xcfg, xport);
1257 }
1258
1259 ovs_assert(xport->ofp_port == ofp_port);
1260
1261 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1262 stp_port_no, rstp_port, config, state, is_tunnel,
1263 may_enable);
1264
1265 if (xport->peer) {
1266 xport->peer->peer = NULL;
1267 }
1268 xport->peer = xport_lookup(new_xcfg, peer);
1269 if (xport->peer) {
1270 xport->peer->peer = xport;
1271 }
1272
1273 if (xport->xbundle) {
1274 ovs_list_remove(&xport->bundle_node);
1275 }
1276 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
1277 if (xport->xbundle) {
1278 ovs_list_insert(&xport->xbundle->xports, &xport->bundle_node);
1279 }
1280
1281 clear_skb_priorities(xport);
1282 for (i = 0; i < n_qdscp; i++) {
1283 struct skb_priority_to_dscp *pdscp;
1284 uint32_t skb_priority;
1285
1286 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1287 &skb_priority)) {
1288 continue;
1289 }
1290
1291 pdscp = xmalloc(sizeof *pdscp);
1292 pdscp->skb_priority = skb_priority;
1293 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1294 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1295 hash_int(pdscp->skb_priority, 0));
1296 }
1297 }
1298
1299 static void
1300 xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
1301 {
1302 if (!xport) {
1303 return;
1304 }
1305
1306 if (xport->peer) {
1307 xport->peer->peer = NULL;
1308 xport->peer = NULL;
1309 }
1310
1311 if (xport->xbundle) {
1312 ovs_list_remove(&xport->bundle_node);
1313 }
1314
1315 clear_skb_priorities(xport);
1316 hmap_destroy(&xport->skb_priorities);
1317
1318 hmap_remove(&xcfg->xports, &xport->hmap_node);
1319 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1320
1321 netdev_close(xport->netdev);
1322 rstp_port_unref(xport->rstp_port);
1323 cfm_unref(xport->cfm);
1324 bfd_unref(xport->bfd);
1325 lldp_unref(xport->lldp);
1326 free(xport);
1327 }
1328
1329 void
1330 xlate_ofport_remove(struct ofport_dpif *ofport)
1331 {
1332 struct xport *xport;
1333
1334 ovs_assert(new_xcfg);
1335
1336 xport = xport_lookup(new_xcfg, ofport);
1337 xlate_xport_remove(new_xcfg, xport);
1338 }
1339
1340 static struct ofproto_dpif *
1341 xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1342 ofp_port_t *ofp_in_port, const struct xport **xportp)
1343 {
1344 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1345 const struct xport *xport;
1346
1347 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1348 ? tnl_port_receive(flow)
1349 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1350 if (OVS_UNLIKELY(!xport)) {
1351 return NULL;
1352 }
1353 *xportp = xport;
1354 if (ofp_in_port) {
1355 *ofp_in_port = xport->ofp_port;
1356 }
1357 return xport->xbridge->ofproto;
1358 }
1359
1360 /* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1361 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1362 struct ofproto_dpif *
1363 xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1364 ofp_port_t *ofp_in_port)
1365 {
1366 const struct xport *xport;
1367
1368 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1369 }
1370
1371 /* Given a datapath and flow metadata ('backer', and 'flow' respectively),
1372 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
1373 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
1374 * handles for those protocols if they're enabled. Caller may use the returned
1375 * pointers until quiescing, for longer term use additional references must
1376 * be taken.
1377 *
1378 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
1379 */
1380 int
1381 xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1382 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1383 struct dpif_sflow **sflow, struct netflow **netflow,
1384 ofp_port_t *ofp_in_port)
1385 {
1386 struct ofproto_dpif *ofproto;
1387 const struct xport *xport;
1388
1389 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1390
1391 if (!ofproto) {
1392 return ENODEV;
1393 }
1394
1395 if (ofprotop) {
1396 *ofprotop = ofproto;
1397 }
1398
1399 if (ipfix) {
1400 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1401 }
1402
1403 if (sflow) {
1404 *sflow = xport ? xport->xbridge->sflow : NULL;
1405 }
1406
1407 if (netflow) {
1408 *netflow = xport ? xport->xbridge->netflow : NULL;
1409 }
1410
1411 return 0;
1412 }
1413
1414 static struct xbridge *
1415 xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
1416 {
1417 struct hmap *xbridges;
1418 struct xbridge *xbridge;
1419
1420 if (!ofproto || !xcfg) {
1421 return NULL;
1422 }
1423
1424 xbridges = &xcfg->xbridges;
1425
1426 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
1427 xbridges) {
1428 if (xbridge->ofproto == ofproto) {
1429 return xbridge;
1430 }
1431 }
1432 return NULL;
1433 }
1434
1435 static struct xbridge *
1436 xbridge_lookup_by_uuid(struct xlate_cfg *xcfg, const struct uuid *uuid)
1437 {
1438 struct xbridge *xbridge;
1439
1440 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1441 if (uuid_equals(&xbridge->ofproto->uuid, uuid)) {
1442 return xbridge;
1443 }
1444 }
1445 return NULL;
1446 }
1447
1448 static struct xbundle *
1449 xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
1450 {
1451 struct hmap *xbundles;
1452 struct xbundle *xbundle;
1453
1454 if (!ofbundle || !xcfg) {
1455 return NULL;
1456 }
1457
1458 xbundles = &xcfg->xbundles;
1459
1460 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
1461 xbundles) {
1462 if (xbundle->ofbundle == ofbundle) {
1463 return xbundle;
1464 }
1465 }
1466 return NULL;
1467 }
1468
1469 static struct xport *
1470 xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
1471 {
1472 struct hmap *xports;
1473 struct xport *xport;
1474
1475 if (!ofport || !xcfg) {
1476 return NULL;
1477 }
1478
1479 xports = &xcfg->xports;
1480
1481 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
1482 xports) {
1483 if (xport->ofport == ofport) {
1484 return xport;
1485 }
1486 }
1487 return NULL;
1488 }
1489
1490 static struct stp_port *
1491 xport_get_stp_port(const struct xport *xport)
1492 {
1493 return xport->xbridge->stp && xport->stp_port_no != -1
1494 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1495 : NULL;
1496 }
1497
1498 static bool
1499 xport_stp_learn_state(const struct xport *xport)
1500 {
1501 struct stp_port *sp = xport_get_stp_port(xport);
1502 return sp
1503 ? stp_learn_in_state(stp_port_get_state(sp))
1504 : true;
1505 }
1506
1507 static bool
1508 xport_stp_forward_state(const struct xport *xport)
1509 {
1510 struct stp_port *sp = xport_get_stp_port(xport);
1511 return sp
1512 ? stp_forward_in_state(stp_port_get_state(sp))
1513 : true;
1514 }
1515
1516 static bool
1517 xport_stp_should_forward_bpdu(const struct xport *xport)
1518 {
1519 struct stp_port *sp = xport_get_stp_port(xport);
1520 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
1521 }
1522
1523 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1524 * were used to make the determination.*/
1525 static bool
1526 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1527 {
1528 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
1529 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1530 return is_stp(flow);
1531 }
1532
1533 static void
1534 stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1535 {
1536 struct stp_port *sp = xport_get_stp_port(xport);
1537 struct dp_packet payload = *packet;
1538 struct eth_header *eth = dp_packet_data(&payload);
1539
1540 /* Sink packets on ports that have STP disabled when the bridge has
1541 * STP enabled. */
1542 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1543 return;
1544 }
1545
1546 /* Trim off padding on payload. */
1547 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1548 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1549 }
1550
1551 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1552 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
1553 }
1554 }
1555
1556 static enum rstp_state
1557 xport_get_rstp_port_state(const struct xport *xport)
1558 {
1559 return xport->rstp_port
1560 ? rstp_port_get_state(xport->rstp_port)
1561 : RSTP_DISABLED;
1562 }
1563
1564 static bool
1565 xport_rstp_learn_state(const struct xport *xport)
1566 {
1567 return xport->xbridge->rstp && xport->rstp_port
1568 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1569 : true;
1570 }
1571
1572 static bool
1573 xport_rstp_forward_state(const struct xport *xport)
1574 {
1575 return xport->xbridge->rstp && xport->rstp_port
1576 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1577 : true;
1578 }
1579
1580 static bool
1581 xport_rstp_should_manage_bpdu(const struct xport *xport)
1582 {
1583 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
1584 }
1585
1586 static void
1587 rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
1588 {
1589 struct dp_packet payload = *packet;
1590 struct eth_header *eth = dp_packet_data(&payload);
1591
1592 /* Sink packets on ports that have no RSTP. */
1593 if (!xport->rstp_port) {
1594 return;
1595 }
1596
1597 /* Trim off padding on payload. */
1598 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1599 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1600 }
1601
1602 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1603 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1604 dp_packet_size(&payload));
1605 }
1606 }
1607
1608 static struct xport *
1609 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1610 {
1611 struct xport *xport;
1612
1613 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1614 &xbridge->xports) {
1615 if (xport->ofp_port == ofp_port) {
1616 return xport;
1617 }
1618 }
1619 return NULL;
1620 }
1621
1622 static odp_port_t
1623 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1624 {
1625 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1626 return xport ? xport->odp_port : ODPP_NONE;
1627 }
1628
1629 static bool
1630 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1631 {
1632 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1633 return xport && xport->may_enable;
1634 }
1635
1636 static struct ofputil_bucket *
1637 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1638 int depth);
1639
1640 static bool
1641 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1642 {
1643 struct group_dpif *group;
1644
1645 group = group_dpif_lookup(ctx->xbridge->ofproto, group_id,
1646 ctx->xin->tables_version, false);
1647 if (group) {
1648 return group_first_live_bucket(ctx, group, depth) != NULL;
1649 }
1650
1651 return false;
1652 }
1653
1654 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1655
1656 static bool
1657 bucket_is_alive(const struct xlate_ctx *ctx,
1658 struct ofputil_bucket *bucket, int depth)
1659 {
1660 if (depth >= MAX_LIVENESS_RECURSION) {
1661 xlate_report_error(ctx, "bucket chaining exceeded %d links",
1662 MAX_LIVENESS_RECURSION);
1663 return false;
1664 }
1665
1666 return (!ofputil_bucket_has_liveness(bucket)
1667 || (bucket->watch_port != OFPP_ANY
1668 && odp_port_is_alive(ctx, bucket->watch_port))
1669 || (bucket->watch_group != OFPG_ANY
1670 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
1671 }
1672
1673 static struct ofputil_bucket *
1674 group_first_live_bucket(const struct xlate_ctx *ctx,
1675 const struct group_dpif *group, int depth)
1676 {
1677 struct ofputil_bucket *bucket;
1678 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
1679 if (bucket_is_alive(ctx, bucket, depth)) {
1680 return bucket;
1681 }
1682 }
1683
1684 return NULL;
1685 }
1686
1687 static struct ofputil_bucket *
1688 group_best_live_bucket(const struct xlate_ctx *ctx,
1689 const struct group_dpif *group,
1690 uint32_t basis)
1691 {
1692 struct ofputil_bucket *best_bucket = NULL;
1693 uint32_t best_score = 0;
1694
1695 struct ofputil_bucket *bucket;
1696 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
1697 if (bucket_is_alive(ctx, bucket, 0)) {
1698 uint32_t score =
1699 (hash_int(bucket->bucket_id, basis) & 0xffff) * bucket->weight;
1700 if (score >= best_score) {
1701 best_bucket = bucket;
1702 best_score = score;
1703 }
1704 }
1705 }
1706
1707 return best_bucket;
1708 }
1709
1710 static bool
1711 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
1712 {
1713 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1714 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1715 }
1716
1717 static bool
1718 xbundle_allows_cvlan(const struct xbundle *bundle, uint16_t vlan)
1719 {
1720 return (!bundle->cvlans || bitmap_is_set(bundle->cvlans, vlan));
1721 }
1722
1723 static bool
1724 xbundle_includes_vlan(const struct xbundle *xbundle, const struct xvlan *xvlan)
1725 {
1726 switch (xbundle->vlan_mode) {
1727 case PORT_VLAN_ACCESS:
1728 return xvlan->v[0].vid == xbundle->vlan && xvlan->v[1].vid == 0;
1729
1730 case PORT_VLAN_TRUNK:
1731 case PORT_VLAN_NATIVE_UNTAGGED:
1732 case PORT_VLAN_NATIVE_TAGGED:
1733 return xbundle_trunks_vlan(xbundle, xvlan->v[0].vid);
1734
1735 case PORT_VLAN_DOT1Q_TUNNEL:
1736 return xvlan->v[0].vid == xbundle->vlan &&
1737 xbundle_allows_cvlan(xbundle, xvlan->v[1].vid);
1738
1739 default:
1740 OVS_NOT_REACHED();
1741 }
1742 }
1743
1744 static mirror_mask_t
1745 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1746 {
1747 return xbundle != &ofpp_none_bundle
1748 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1749 : 0;
1750 }
1751
1752 static mirror_mask_t
1753 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
1754 {
1755 return xbundle != &ofpp_none_bundle
1756 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1757 : 0;
1758 }
1759
1760 static mirror_mask_t
1761 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
1762 {
1763 return xbundle != &ofpp_none_bundle
1764 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1765 : 0;
1766 }
1767
1768 static struct xbundle *
1769 lookup_input_bundle__(const struct xbridge *xbridge,
1770 ofp_port_t in_port, struct xport **in_xportp)
1771 {
1772 struct xport *xport;
1773
1774 /* Find the port and bundle for the received packet. */
1775 xport = get_ofp_port(xbridge, in_port);
1776 if (in_xportp) {
1777 *in_xportp = xport;
1778 }
1779 if (xport && xport->xbundle) {
1780 return xport->xbundle;
1781 }
1782
1783 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1784 * which a controller may use as the ingress port for traffic that
1785 * it is sourcing. */
1786 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
1787 return &ofpp_none_bundle;
1788 }
1789 return NULL;
1790 }
1791
1792 static struct xbundle *
1793 lookup_input_bundle(const struct xlate_ctx *ctx,
1794 ofp_port_t in_port, struct xport **in_xportp)
1795 {
1796 struct xbundle *xbundle = lookup_input_bundle__(ctx->xbridge,
1797 in_port, in_xportp);
1798 if (!xbundle) {
1799 /* Odd. A few possible reasons here:
1800 *
1801 * - We deleted a port but there are still a few packets queued up
1802 * from it.
1803 *
1804 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1805 * we don't know about.
1806 *
1807 * - The ofproto client didn't configure the port as part of a bundle.
1808 * This is particularly likely to happen if a packet was received on
1809 * the port after it was created, but before the client had a chance
1810 * to configure its bundle.
1811 */
1812 xlate_report_error(ctx, "received packet on unknown port %"PRIu32,
1813 in_port);
1814 }
1815 return xbundle;
1816 }
1817
1818 /* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1819 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1820 * or egress (as appropriate) mirrors 'mirrors'. */
1821 static void
1822 mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1823 mirror_mask_t mirrors)
1824 {
1825 struct xvlan in_xvlan;
1826 struct xvlan xvlan;
1827
1828 /* Figure out what VLAN the packet is in (because mirrors can select
1829 * packets on basis of VLAN). */
1830 xvlan_extract(&ctx->xin->flow, &in_xvlan);
1831 if (!input_vid_is_valid(ctx, in_xvlan.v[0].vid, xbundle)) {
1832 return;
1833 }
1834 xvlan_input_translate(xbundle, &in_xvlan, &xvlan);
1835
1836 const struct xbridge *xbridge = ctx->xbridge;
1837
1838 /* Don't mirror to destinations that we've already mirrored to. */
1839 mirrors &= ~ctx->mirrors;
1840 if (!mirrors) {
1841 return;
1842 }
1843
1844 if (ctx->xin->resubmit_stats) {
1845 mirror_update_stats(xbridge->mbridge, mirrors,
1846 ctx->xin->resubmit_stats->n_packets,
1847 ctx->xin->resubmit_stats->n_bytes);
1848 }
1849 if (ctx->xin->xcache) {
1850 struct xc_entry *entry;
1851
1852 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1853 entry->mirror.mbridge = mbridge_ref(xbridge->mbridge);
1854 entry->mirror.mirrors = mirrors;
1855 }
1856
1857 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1858 * some candidates remain. */
1859 while (mirrors) {
1860 const unsigned long *vlans;
1861 mirror_mask_t dup_mirrors;
1862 struct ofbundle *out;
1863 int out_vlan;
1864 int snaplen;
1865
1866 /* Get the details of the mirror represented by the rightmost 1-bit. */
1867 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1868 &vlans, &dup_mirrors,
1869 &out, &snaplen, &out_vlan);
1870 ovs_assert(has_mirror);
1871
1872
1873 /* If this mirror selects on the basis of VLAN, and it does not select
1874 * 'vlan', then discard this mirror and go on to the next one. */
1875 if (vlans) {
1876 ctx->wc->masks.vlans[0].tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1877 }
1878 if (vlans && !bitmap_is_set(vlans, xvlan.v[0].vid)) {
1879 mirrors = zero_rightmost_1bit(mirrors);
1880 continue;
1881 }
1882
1883 /* Record the mirror, and the mirrors that output to the same
1884 * destination, so that we don't mirror to them again. This must be
1885 * done now to ensure that output_normal(), below, doesn't recursively
1886 * output to the same mirrors. */
1887 ctx->mirrors |= dup_mirrors;
1888 ctx->mirror_snaplen = snaplen;
1889
1890 /* Send the packet to the mirror. */
1891 if (out) {
1892 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1893 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
1894 if (out_xbundle) {
1895 output_normal(ctx, out_xbundle, &xvlan);
1896 }
1897 } else if (xvlan.v[0].vid != out_vlan
1898 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
1899 struct xbundle *xbundle;
1900 uint16_t old_vid = xvlan.v[0].vid;
1901
1902 xvlan.v[0].vid = out_vlan;
1903 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1904 if (xbundle_includes_vlan(xbundle, &xvlan)
1905 && !xbundle_mirror_out(xbridge, xbundle)) {
1906 output_normal(ctx, xbundle, &xvlan);
1907 }
1908 }
1909 xvlan.v[0].vid = old_vid;
1910 }
1911
1912 /* output_normal() could have recursively output (to different
1913 * mirrors), so make sure that we don't send duplicates. */
1914 mirrors &= ~ctx->mirrors;
1915 ctx->mirror_snaplen = 0;
1916 }
1917 }
1918
1919 static void
1920 mirror_ingress_packet(struct xlate_ctx *ctx)
1921 {
1922 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1923 struct xbundle *xbundle = lookup_input_bundle(
1924 ctx, ctx->xin->flow.in_port.ofp_port, NULL);
1925 if (xbundle) {
1926 mirror_packet(ctx, xbundle,
1927 xbundle_mirror_src(ctx->xbridge, xbundle));
1928 }
1929 }
1930 }
1931
1932 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1933 * If so, returns true. Otherwise, returns false.
1934 *
1935 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1936 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1937 * 0...4095. */
1938 static bool
1939 input_vid_is_valid(const struct xlate_ctx *ctx,
1940 uint16_t vid, struct xbundle *in_xbundle)
1941 {
1942 /* Allow any VID on the OFPP_NONE port. */
1943 if (in_xbundle == &ofpp_none_bundle) {
1944 return true;
1945 }
1946
1947 switch (in_xbundle->vlan_mode) {
1948 case PORT_VLAN_ACCESS:
1949 if (vid) {
1950 xlate_report_error(ctx, "dropping VLAN %"PRIu16" tagged "
1951 "packet received on port %s configured as VLAN "
1952 "%d access port", vid, in_xbundle->name,
1953 in_xbundle->vlan);
1954 return false;
1955 }
1956 return true;
1957
1958 case PORT_VLAN_NATIVE_UNTAGGED:
1959 case PORT_VLAN_NATIVE_TAGGED:
1960 if (!vid) {
1961 /* Port must always carry its native VLAN. */
1962 return true;
1963 }
1964 /* Fall through. */
1965 case PORT_VLAN_TRUNK:
1966 if (!xbundle_trunks_vlan(in_xbundle, vid)) {
1967 xlate_report_error(ctx, "dropping VLAN %"PRIu16" packet "
1968 "received on port %s not configured for "
1969 "trunking VLAN %"PRIu16,
1970 vid, in_xbundle->name, vid);
1971 return false;
1972 }
1973 return true;
1974
1975 case PORT_VLAN_DOT1Q_TUNNEL:
1976 if (!xbundle_allows_cvlan(in_xbundle, vid)) {
1977 xlate_report_error(ctx, "dropping VLAN %"PRIu16" packet received "
1978 "on dot1q-tunnel port %s that excludes this "
1979 "VLAN", vid, in_xbundle->name);
1980 return false;
1981 }
1982 return true;
1983
1984 default:
1985 OVS_NOT_REACHED();
1986 }
1987
1988 }
1989
1990 static void
1991 xvlan_copy(struct xvlan *dst, const struct xvlan *src)
1992 {
1993 *dst = *src;
1994 }
1995
1996 static void
1997 xvlan_pop(struct xvlan *src)
1998 {
1999 memmove(&src->v[0], &src->v[1], sizeof(src->v) - sizeof(src->v[0]));
2000 memset(&src->v[FLOW_MAX_VLAN_HEADERS - 1], 0,
2001 sizeof(src->v[FLOW_MAX_VLAN_HEADERS - 1]));
2002 }
2003
2004 static void
2005 xvlan_push_uninit(struct xvlan *src)
2006 {
2007 memmove(&src->v[1], &src->v[0], sizeof(src->v) - sizeof(src->v[0]));
2008 memset(&src->v[0], 0, sizeof(src->v[0]));
2009 }
2010
2011 /* Extract VLAN information (headers) from flow */
2012 static void
2013 xvlan_extract(const struct flow *flow, struct xvlan *xvlan)
2014 {
2015 int i;
2016 memset(xvlan, 0, sizeof(*xvlan));
2017 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2018 if (!eth_type_vlan(flow->vlans[i].tpid) ||
2019 !(flow->vlans[i].tci & htons(VLAN_CFI))) {
2020 break;
2021 }
2022 xvlan->v[i].tpid = ntohs(flow->vlans[i].tpid);
2023 xvlan->v[i].vid = vlan_tci_to_vid(flow->vlans[i].tci);
2024 xvlan->v[i].pcp = ntohs(flow->vlans[i].tci) & VLAN_PCP_MASK;
2025 }
2026 }
2027
2028 /* Put VLAN information (headers) to flow */
2029 static void
2030 xvlan_put(struct flow *flow, const struct xvlan *xvlan)
2031 {
2032 ovs_be16 tci;
2033 int i;
2034 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2035 tci = htons(xvlan->v[i].vid | (xvlan->v[i].pcp & VLAN_PCP_MASK));
2036 if (tci) {
2037 tci |= htons(VLAN_CFI);
2038 flow->vlans[i].tpid = xvlan->v[i].tpid ?
2039 htons(xvlan->v[i].tpid) :
2040 htons(ETH_TYPE_VLAN_8021Q);
2041 }
2042 flow->vlans[i].tci = tci;
2043 }
2044 }
2045
2046 /* Given 'in_xvlan', extracted from the input 802.1Q headers received as part
2047 * of a packet, and 'in_xbundle', the bundle on which the packet was received,
2048 * returns the VLANs of the packet during bridge internal processing. */
2049 static void
2050 xvlan_input_translate(const struct xbundle *in_xbundle,
2051 const struct xvlan *in_xvlan, struct xvlan *xvlan)
2052 {
2053
2054 switch (in_xbundle->vlan_mode) {
2055 case PORT_VLAN_ACCESS:
2056 memset(xvlan, 0, sizeof(*xvlan));
2057 xvlan->v[0].tpid = in_xvlan->v[0].tpid ? in_xvlan->v[0].tpid :
2058 ETH_TYPE_VLAN_8021Q;
2059 xvlan->v[0].vid = in_xbundle->vlan;
2060 xvlan->v[0].pcp = in_xvlan->v[0].pcp;
2061 break;
2062
2063 case PORT_VLAN_TRUNK:
2064 xvlan_copy(xvlan, in_xvlan);
2065 break;
2066
2067 case PORT_VLAN_NATIVE_UNTAGGED:
2068 case PORT_VLAN_NATIVE_TAGGED:
2069 xvlan_copy(xvlan, in_xvlan);
2070 if (!in_xvlan->v[0].vid) {
2071 xvlan->v[0].tpid = in_xvlan->v[0].tpid ? in_xvlan->v[0].tpid :
2072 ETH_TYPE_VLAN_8021Q;
2073 xvlan->v[0].vid = in_xbundle->vlan;
2074 xvlan->v[0].pcp = in_xvlan->v[0].pcp;
2075 }
2076 break;
2077
2078 case PORT_VLAN_DOT1Q_TUNNEL:
2079 xvlan_copy(xvlan, in_xvlan);
2080 xvlan_push_uninit(xvlan);
2081 xvlan->v[0].tpid = in_xbundle->qinq_ethtype;
2082 xvlan->v[0].vid = in_xbundle->vlan;
2083 xvlan->v[0].pcp = 0;
2084 break;
2085
2086 default:
2087 OVS_NOT_REACHED();
2088 }
2089 }
2090
2091 /* Given 'xvlan', the VLANs of a packet during internal processing, and
2092 * 'out_xbundle', a bundle on which the packet is to be output, returns the
2093 * VLANs that should be included in output packet. */
2094 static void
2095 xvlan_output_translate(const struct xbundle *out_xbundle,
2096 const struct xvlan *xvlan, struct xvlan *out_xvlan)
2097 {
2098 switch (out_xbundle->vlan_mode) {
2099 case PORT_VLAN_ACCESS:
2100 memset(out_xvlan, 0, sizeof(*out_xvlan));
2101 break;
2102
2103 case PORT_VLAN_TRUNK:
2104 case PORT_VLAN_NATIVE_TAGGED:
2105 xvlan_copy(out_xvlan, xvlan);
2106 break;
2107
2108 case PORT_VLAN_NATIVE_UNTAGGED:
2109 xvlan_copy(out_xvlan, xvlan);
2110 if (xvlan->v[0].vid == out_xbundle->vlan) {
2111 xvlan_pop(out_xvlan);
2112 }
2113 break;
2114
2115 case PORT_VLAN_DOT1Q_TUNNEL:
2116 xvlan_copy(out_xvlan, xvlan);
2117 xvlan_pop(out_xvlan);
2118 break;
2119
2120 default:
2121 OVS_NOT_REACHED();
2122 }
2123 }
2124
2125 /* If output xbundle is dot1q-tunnel, set mask bits of cvlan */
2126 static void
2127 check_and_set_cvlan_mask(struct flow_wildcards *wc,
2128 const struct xbundle *xbundle)
2129 {
2130 if (xbundle->vlan_mode == PORT_VLAN_DOT1Q_TUNNEL && xbundle->cvlans) {
2131 wc->masks.vlans[1].tci = htons(0xffff);
2132 }
2133 }
2134
2135 static void
2136 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
2137 const struct xvlan *xvlan)
2138 {
2139 uint16_t vid;
2140 union flow_vlan_hdr old_vlans[FLOW_MAX_VLAN_HEADERS];
2141 struct xport *xport;
2142 struct xlate_bond_recirc xr;
2143 bool use_recirc = false;
2144 struct xvlan out_xvlan;
2145
2146 check_and_set_cvlan_mask(ctx->wc, out_xbundle);
2147
2148 xvlan_output_translate(out_xbundle, xvlan, &out_xvlan);
2149 if (out_xbundle->use_priority_tags) {
2150 out_xvlan.v[0].pcp = ntohs(ctx->xin->flow.vlans[0].tci) &
2151 VLAN_PCP_MASK;
2152 }
2153 vid = out_xvlan.v[0].vid;
2154 if (ovs_list_is_empty(&out_xbundle->xports)) {
2155 /* Partially configured bundle with no slaves. Drop the packet. */
2156 return;
2157 } else if (!out_xbundle->bond) {
2158 xport = CONTAINER_OF(ovs_list_front(&out_xbundle->xports), struct xport,
2159 bundle_node);
2160 } else {
2161 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2162 struct flow_wildcards *wc = ctx->wc;
2163 struct ofport_dpif *ofport;
2164
2165 if (ctx->xbridge->support.odp.recirc) {
2166 /* In case recirculation is not actually in use, 'xr.recirc_id'
2167 * will be set to '0', since a valid 'recirc_id' can
2168 * not be zero. */
2169 bond_update_post_recirc_rules(out_xbundle->bond,
2170 &xr.recirc_id,
2171 &xr.hash_basis);
2172 if (xr.recirc_id) {
2173 /* Use recirculation instead of output. */
2174 use_recirc = true;
2175 xr.hash_alg = OVS_HASH_ALG_L4;
2176 /* Recirculation does not require unmasking hash fields. */
2177 wc = NULL;
2178 }
2179 }
2180
2181 ofport = bond_choose_output_slave(out_xbundle->bond,
2182 &ctx->xin->flow, wc, vid);
2183 xport = xport_lookup(xcfg, ofport);
2184
2185 if (!xport) {
2186 /* No slaves enabled, so drop packet. */
2187 return;
2188 }
2189
2190 /* If use_recirc is set, the main thread will handle stats
2191 * accounting for this bond. */
2192 if (!use_recirc) {
2193 if (ctx->xin->resubmit_stats) {
2194 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
2195 ctx->xin->resubmit_stats->n_bytes);
2196 }
2197 if (ctx->xin->xcache) {
2198 struct xc_entry *entry;
2199 struct flow *flow;
2200
2201 flow = &ctx->xin->flow;
2202 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
2203 entry->bond.bond = bond_ref(out_xbundle->bond);
2204 entry->bond.flow = xmemdup(flow, sizeof *flow);
2205 entry->bond.vid = vid;
2206 }
2207 }
2208 }
2209
2210 memcpy(&old_vlans, &ctx->xin->flow.vlans, sizeof(old_vlans));
2211 xvlan_put(&ctx->xin->flow, &out_xvlan);
2212
2213 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
2214 memcpy(&ctx->xin->flow.vlans, &old_vlans, sizeof(old_vlans));
2215 }
2216
2217 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2218 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2219 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2220 static bool
2221 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
2222 {
2223 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
2224 return false;
2225 }
2226
2227 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2228 if (!eth_addr_is_broadcast(flow->dl_dst)) {
2229 return false;
2230 }
2231
2232 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2233 if (flow->nw_proto == ARP_OP_REPLY) {
2234 return true;
2235 } else if (flow->nw_proto == ARP_OP_REQUEST) {
2236 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2237 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2238
2239 return flow->nw_src == flow->nw_dst;
2240 } else {
2241 return false;
2242 }
2243 }
2244
2245 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2246 * dropped. Returns true if they may be forwarded, false if they should be
2247 * dropped.
2248 *
2249 * 'in_port' must be the xport that corresponds to flow->in_port.
2250 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2251 *
2252 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2253 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2254 * checked by input_vid_is_valid().
2255 *
2256 * May also add tags to '*tags', although the current implementation only does
2257 * so in one special case.
2258 */
2259 static bool
2260 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
2261 uint16_t vlan)
2262 {
2263 struct xbundle *in_xbundle = in_port->xbundle;
2264 const struct xbridge *xbridge = ctx->xbridge;
2265 struct flow *flow = &ctx->xin->flow;
2266
2267 /* Drop frames for reserved multicast addresses
2268 * only if forward_bpdu option is absent. */
2269 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
2270 xlate_report(ctx, OFT_DETAIL,
2271 "packet has reserved destination MAC, dropping");
2272 return false;
2273 }
2274
2275 if (in_xbundle->bond) {
2276 struct mac_entry *mac;
2277
2278 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
2279 flow->dl_dst)) {
2280 case BV_ACCEPT:
2281 break;
2282
2283 case BV_DROP:
2284 xlate_report(ctx, OFT_DETAIL,
2285 "bonding refused admissibility, dropping");
2286 return false;
2287
2288 case BV_DROP_IF_MOVED:
2289 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2290 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
2291 if (mac
2292 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
2293 && (!is_gratuitous_arp(flow, ctx->wc)
2294 || mac_entry_is_grat_arp_locked(mac))) {
2295 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2296 xlate_report(ctx, OFT_DETAIL,
2297 "SLB bond thinks this packet looped back, "
2298 "dropping");
2299 return false;
2300 }
2301 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2302 break;
2303 }
2304 }
2305
2306 return true;
2307 }
2308
2309 static bool
2310 update_learning_table__(const struct xbridge *xbridge,
2311 struct xbundle *in_xbundle, struct eth_addr dl_src,
2312 int vlan, bool is_grat_arp)
2313 {
2314 return (in_xbundle == &ofpp_none_bundle
2315 || !mac_learning_update(xbridge->ml, dl_src, vlan,
2316 is_grat_arp,
2317 in_xbundle->bond != NULL,
2318 in_xbundle->ofbundle));
2319 }
2320
2321 static void
2322 update_learning_table(const struct xlate_ctx *ctx,
2323 struct xbundle *in_xbundle, struct eth_addr dl_src,
2324 int vlan, bool is_grat_arp)
2325 {
2326 if (!update_learning_table__(ctx->xbridge, in_xbundle, dl_src, vlan,
2327 is_grat_arp)) {
2328 xlate_report_debug(ctx, OFT_DETAIL, "learned that "ETH_ADDR_FMT" is "
2329 "on port %s in VLAN %d",
2330 ETH_ADDR_ARGS(dl_src), in_xbundle->name, vlan);
2331 }
2332 }
2333
2334 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2335 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2336 static void
2337 update_mcast_snooping_table4__(const struct xlate_ctx *ctx,
2338 const struct flow *flow,
2339 struct mcast_snooping *ms, int vlan,
2340 struct xbundle *in_xbundle,
2341 const struct dp_packet *packet)
2342 OVS_REQ_WRLOCK(ms->rwlock)
2343 {
2344 const struct igmp_header *igmp;
2345 int count;
2346 size_t offset;
2347 ovs_be32 ip4 = flow->igmp_group_ip4;
2348
2349 offset = (char *) dp_packet_l4(packet) - (char *) dp_packet_data(packet);
2350 igmp = dp_packet_at(packet, offset, IGMP_HEADER_LEN);
2351 if (!igmp || csum(igmp, dp_packet_l4_size(packet)) != 0) {
2352 xlate_report_debug(ctx, OFT_DETAIL,
2353 "multicast snooping received bad IGMP "
2354 "checksum on port %s in VLAN %d",
2355 in_xbundle->name, vlan);
2356 return;
2357 }
2358
2359 switch (ntohs(flow->tp_src)) {
2360 case IGMP_HOST_MEMBERSHIP_REPORT:
2361 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2362 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2363 xlate_report_debug(ctx, OFT_DETAIL,
2364 "multicast snooping learned that "
2365 IP_FMT" is on port %s in VLAN %d",
2366 IP_ARGS(ip4), in_xbundle->name, vlan);
2367 }
2368 break;
2369 case IGMP_HOST_LEAVE_MESSAGE:
2370 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2371 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping leaving "
2372 IP_FMT" is on port %s in VLAN %d",
2373 IP_ARGS(ip4), in_xbundle->name, vlan);
2374 }
2375 break;
2376 case IGMP_HOST_MEMBERSHIP_QUERY:
2377 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2378 in_xbundle->ofbundle)) {
2379 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping query "
2380 "from "IP_FMT" is on port %s in VLAN %d",
2381 IP_ARGS(flow->nw_src), in_xbundle->name, vlan);
2382 }
2383 break;
2384 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2385 count = mcast_snooping_add_report(ms, packet, vlan,
2386 in_xbundle->ofbundle);
2387 if (count) {
2388 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping processed "
2389 "%d addresses on port %s in VLAN %d",
2390 count, in_xbundle->name, vlan);
2391 }
2392 break;
2393 }
2394 }
2395
2396 static void
2397 update_mcast_snooping_table6__(const struct xlate_ctx *ctx,
2398 const struct flow *flow,
2399 struct mcast_snooping *ms, int vlan,
2400 struct xbundle *in_xbundle,
2401 const struct dp_packet *packet)
2402 OVS_REQ_WRLOCK(ms->rwlock)
2403 {
2404 const struct mld_header *mld;
2405 int count;
2406 size_t offset;
2407
2408 offset = (char *) dp_packet_l4(packet) - (char *) dp_packet_data(packet);
2409 mld = dp_packet_at(packet, offset, MLD_HEADER_LEN);
2410
2411 if (!mld ||
2412 packet_csum_upperlayer6(dp_packet_l3(packet),
2413 mld, IPPROTO_ICMPV6,
2414 dp_packet_l4_size(packet)) != 0) {
2415 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping received "
2416 "bad MLD checksum on port %s in VLAN %d",
2417 in_xbundle->name, vlan);
2418 return;
2419 }
2420
2421 switch (ntohs(flow->tp_src)) {
2422 case MLD_QUERY:
2423 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2424 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2425 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping query on "
2426 "port %s in VLAN %d", in_xbundle->name, vlan);
2427 }
2428 break;
2429 case MLD_REPORT:
2430 case MLD_DONE:
2431 case MLD2_REPORT:
2432 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2433 if (count) {
2434 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping processed "
2435 "%d addresses on port %s in VLAN %d",
2436 count, in_xbundle->name, vlan);
2437 }
2438 break;
2439 }
2440 }
2441
2442 /* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2443 * was received on 'in_xbundle' in 'vlan'. */
2444 static void
2445 update_mcast_snooping_table(const struct xlate_ctx *ctx,
2446 const struct flow *flow, int vlan,
2447 struct xbundle *in_xbundle,
2448 const struct dp_packet *packet)
2449 {
2450 struct mcast_snooping *ms = ctx->xbridge->ms;
2451 struct xlate_cfg *xcfg;
2452 struct xbundle *mcast_xbundle;
2453 struct mcast_port_bundle *fport;
2454
2455 /* Don't learn the OFPP_NONE port. */
2456 if (in_xbundle == &ofpp_none_bundle) {
2457 return;
2458 }
2459
2460 /* Don't learn from flood ports */
2461 mcast_xbundle = NULL;
2462 ovs_rwlock_wrlock(&ms->rwlock);
2463 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2464 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2465 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2466 if (mcast_xbundle == in_xbundle) {
2467 break;
2468 }
2469 }
2470
2471 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
2472 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2473 update_mcast_snooping_table4__(ctx, flow, ms, vlan,
2474 in_xbundle, packet);
2475 } else {
2476 update_mcast_snooping_table6__(ctx, flow, ms, vlan,
2477 in_xbundle, packet);
2478 }
2479 }
2480 ovs_rwlock_unlock(&ms->rwlock);
2481 }
2482
2483 /* send the packet to ports having the multicast group learned */
2484 static void
2485 xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2486 struct mcast_snooping *ms OVS_UNUSED,
2487 struct mcast_group *grp,
2488 struct xbundle *in_xbundle,
2489 const struct xvlan *xvlan)
2490 OVS_REQ_RDLOCK(ms->rwlock)
2491 {
2492 struct xlate_cfg *xcfg;
2493 struct mcast_group_bundle *b;
2494 struct xbundle *mcast_xbundle;
2495
2496 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2497 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2498 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2499 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2500 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast group port");
2501 output_normal(ctx, mcast_xbundle, xvlan);
2502 } else if (!mcast_xbundle) {
2503 xlate_report(ctx, OFT_WARN,
2504 "mcast group port is unknown, dropping");
2505 } else {
2506 xlate_report(ctx, OFT_DETAIL,
2507 "mcast group port is input port, dropping");
2508 }
2509 }
2510 }
2511
2512 /* send the packet to ports connected to multicast routers */
2513 static void
2514 xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2515 struct mcast_snooping *ms,
2516 struct xbundle *in_xbundle,
2517 const struct xvlan *xvlan)
2518 OVS_REQ_RDLOCK(ms->rwlock)
2519 {
2520 struct xlate_cfg *xcfg;
2521 struct mcast_mrouter_bundle *mrouter;
2522 struct xbundle *mcast_xbundle;
2523
2524 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2525 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2526 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2527 if (mcast_xbundle && mcast_xbundle != in_xbundle
2528 && mrouter->vlan == xvlan->v[0].vid) {
2529 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast router port");
2530 output_normal(ctx, mcast_xbundle, xvlan);
2531 } else if (!mcast_xbundle) {
2532 xlate_report(ctx, OFT_WARN,
2533 "mcast router port is unknown, dropping");
2534 } else if (mrouter->vlan != xvlan->v[0].vid) {
2535 xlate_report(ctx, OFT_DETAIL,
2536 "mcast router is on another vlan, dropping");
2537 } else {
2538 xlate_report(ctx, OFT_DETAIL,
2539 "mcast router port is input port, dropping");
2540 }
2541 }
2542 }
2543
2544 /* send the packet to ports flagged to be flooded */
2545 static void
2546 xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2547 struct mcast_snooping *ms,
2548 struct xbundle *in_xbundle,
2549 const struct xvlan *xvlan)
2550 OVS_REQ_RDLOCK(ms->rwlock)
2551 {
2552 struct xlate_cfg *xcfg;
2553 struct mcast_port_bundle *fport;
2554 struct xbundle *mcast_xbundle;
2555
2556 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2557 LIST_FOR_EACH(fport, node, &ms->fport_list) {
2558 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2559 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2560 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast flood port");
2561 output_normal(ctx, mcast_xbundle, xvlan);
2562 } else if (!mcast_xbundle) {
2563 xlate_report(ctx, OFT_WARN,
2564 "mcast flood port is unknown, dropping");
2565 } else {
2566 xlate_report(ctx, OFT_DETAIL,
2567 "mcast flood port is input port, dropping");
2568 }
2569 }
2570 }
2571
2572 /* forward the Reports to configured ports */
2573 static void
2574 xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2575 struct mcast_snooping *ms,
2576 struct xbundle *in_xbundle,
2577 const struct xvlan *xvlan)
2578 OVS_REQ_RDLOCK(ms->rwlock)
2579 {
2580 struct xlate_cfg *xcfg;
2581 struct mcast_port_bundle *rport;
2582 struct xbundle *mcast_xbundle;
2583
2584 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2585 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2586 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2587 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2588 xlate_report(ctx, OFT_DETAIL,
2589 "forwarding report to mcast flagged port");
2590 output_normal(ctx, mcast_xbundle, xvlan);
2591 } else if (!mcast_xbundle) {
2592 xlate_report(ctx, OFT_WARN,
2593 "mcast port is unknown, dropping the report");
2594 } else {
2595 xlate_report(ctx, OFT_DETAIL,
2596 "mcast port is input port, dropping the Report");
2597 }
2598 }
2599 }
2600
2601 static void
2602 xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2603 struct xvlan *xvlan)
2604 {
2605 struct xbundle *xbundle;
2606
2607 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2608 if (xbundle != in_xbundle
2609 && xbundle_includes_vlan(xbundle, xvlan)
2610 && xbundle->floodable
2611 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2612 output_normal(ctx, xbundle, xvlan);
2613 }
2614 }
2615 ctx->nf_output_iface = NF_OUT_FLOOD;
2616 }
2617
2618 static bool
2619 is_ip_local_multicast(const struct flow *flow, struct flow_wildcards *wc)
2620 {
2621 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2622 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2623 return ip_is_local_multicast(flow->nw_dst);
2624 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2625 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2626 return ipv6_is_all_hosts(&flow->ipv6_dst);
2627 } else {
2628 return false;
2629 }
2630 }
2631
2632 static void
2633 xlate_normal(struct xlate_ctx *ctx)
2634 {
2635 struct flow_wildcards *wc = ctx->wc;
2636 struct flow *flow = &ctx->xin->flow;
2637 struct xbundle *in_xbundle;
2638 struct xport *in_port;
2639 struct mac_entry *mac;
2640 void *mac_port;
2641 struct xvlan in_xvlan;
2642 struct xvlan xvlan;
2643 uint16_t vlan;
2644
2645 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2646 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2647 wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2648
2649 in_xbundle = lookup_input_bundle(ctx, flow->in_port.ofp_port, &in_port);
2650 if (!in_xbundle) {
2651 xlate_report(ctx, OFT_WARN, "no input bundle, dropping");
2652 return;
2653 }
2654
2655 /* Drop malformed frames. */
2656 if (eth_type_vlan(flow->dl_type) &&
2657 !(flow->vlans[0].tci & htons(VLAN_CFI))) {
2658 if (ctx->xin->packet != NULL) {
2659 xlate_report_error(ctx, "dropping packet with partial "
2660 "VLAN tag received on port %s",
2661 in_xbundle->name);
2662 }
2663 xlate_report(ctx, OFT_WARN, "partial VLAN tag, dropping");
2664 return;
2665 }
2666
2667 /* Drop frames on bundles reserved for mirroring. */
2668 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
2669 if (ctx->xin->packet != NULL) {
2670 xlate_report_error(ctx, "dropping packet received on port %s, "
2671 "which is reserved exclusively for mirroring",
2672 in_xbundle->name);
2673 }
2674 xlate_report(ctx, OFT_WARN,
2675 "input port is mirror output port, dropping");
2676 return;
2677 }
2678
2679 /* Check VLAN. */
2680 xvlan_extract(flow, &in_xvlan);
2681 if (!input_vid_is_valid(ctx, in_xvlan.v[0].vid, in_xbundle)) {
2682 xlate_report(ctx, OFT_WARN,
2683 "disallowed VLAN VID for this input port, dropping");
2684 return;
2685 }
2686 xvlan_input_translate(in_xbundle, &in_xvlan, &xvlan);
2687 vlan = xvlan.v[0].vid;
2688
2689 /* Check other admissibility requirements. */
2690 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2691 return;
2692 }
2693
2694 /* Learn source MAC. */
2695 bool is_grat_arp = is_gratuitous_arp(flow, wc);
2696 if (ctx->xin->allow_side_effects) {
2697 update_learning_table(ctx, in_xbundle, flow->dl_src, vlan,
2698 is_grat_arp);
2699 }
2700 if (ctx->xin->xcache && in_xbundle != &ofpp_none_bundle) {
2701 struct xc_entry *entry;
2702
2703 /* Save just enough info to update mac learning table later. */
2704 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
2705 entry->normal.ofproto = ctx->xbridge->ofproto;
2706 entry->normal.in_port = flow->in_port.ofp_port;
2707 entry->normal.dl_src = flow->dl_src;
2708 entry->normal.vlan = vlan;
2709 entry->normal.is_gratuitous_arp = is_grat_arp;
2710 }
2711
2712 /* Determine output bundle. */
2713 if (mcast_snooping_enabled(ctx->xbridge->ms)
2714 && !eth_addr_is_broadcast(flow->dl_dst)
2715 && eth_addr_is_multicast(flow->dl_dst)
2716 && is_ip_any(flow)) {
2717 struct mcast_snooping *ms = ctx->xbridge->ms;
2718 struct mcast_group *grp = NULL;
2719
2720 if (is_igmp(flow, wc)) {
2721 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2722 if (mcast_snooping_is_membership(flow->tp_src) ||
2723 mcast_snooping_is_query(flow->tp_src)) {
2724 if (ctx->xin->allow_side_effects && ctx->xin->packet) {
2725 update_mcast_snooping_table(ctx, flow, vlan,
2726 in_xbundle, ctx->xin->packet);
2727 }
2728 /*
2729 * IGMP packets need to take the slow path, in order to be
2730 * processed for mdb updates. That will prevent expires
2731 * firing off even after hosts have sent reports.
2732 */
2733 ctx->xout->slow |= SLOW_ACTION;
2734 }
2735
2736 if (mcast_snooping_is_membership(flow->tp_src)) {
2737 ovs_rwlock_rdlock(&ms->rwlock);
2738 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2739 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2740 * forward IGMP Membership Reports only to those ports where
2741 * multicast routers are attached. Alternatively stated: a
2742 * snooping switch should not forward IGMP Membership Reports
2743 * to ports on which only hosts are attached.
2744 * An administrative control may be provided to override this
2745 * restriction, allowing the report messages to be flooded to
2746 * other ports. */
2747 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, &xvlan);
2748 ovs_rwlock_unlock(&ms->rwlock);
2749 } else {
2750 xlate_report(ctx, OFT_DETAIL, "multicast traffic, flooding");
2751 xlate_normal_flood(ctx, in_xbundle, &xvlan);
2752 }
2753 return;
2754 } else if (is_mld(flow, wc)) {
2755 ctx->xout->slow |= SLOW_ACTION;
2756 if (ctx->xin->allow_side_effects && ctx->xin->packet) {
2757 update_mcast_snooping_table(ctx, flow, vlan,
2758 in_xbundle, ctx->xin->packet);
2759 }
2760 if (is_mld_report(flow, wc)) {
2761 ovs_rwlock_rdlock(&ms->rwlock);
2762 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2763 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, &xvlan);
2764 ovs_rwlock_unlock(&ms->rwlock);
2765 } else {
2766 xlate_report(ctx, OFT_DETAIL, "MLD query, flooding");
2767 xlate_normal_flood(ctx, in_xbundle, &xvlan);
2768 }
2769 } else {
2770 if (is_ip_local_multicast(flow, wc)) {
2771 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2772 * address in the 224.0.0.x range which are not IGMP must
2773 * be forwarded on all ports */
2774 xlate_report(ctx, OFT_DETAIL,
2775 "RFC4541: section 2.1.2, item 2, flooding");
2776 xlate_normal_flood(ctx, in_xbundle, &xvlan);
2777 return;
2778 }
2779 }
2780
2781 /* forwarding to group base ports */
2782 ovs_rwlock_rdlock(&ms->rwlock);
2783 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2784 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2785 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2786 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2787 }
2788 if (grp) {
2789 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, &xvlan);
2790 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, &xvlan);
2791 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2792 } else {
2793 if (mcast_snooping_flood_unreg(ms)) {
2794 xlate_report(ctx, OFT_DETAIL,
2795 "unregistered multicast, flooding");
2796 xlate_normal_flood(ctx, in_xbundle, &xvlan);
2797 } else {
2798 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2799 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, &xvlan);
2800 }
2801 }
2802 ovs_rwlock_unlock(&ms->rwlock);
2803 } else {
2804 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2805 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2806 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
2807 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2808
2809 if (mac_port) {
2810 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2811 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2812 if (mac_xbundle && mac_xbundle != in_xbundle) {
2813 xlate_report(ctx, OFT_DETAIL, "forwarding to learned port");
2814 output_normal(ctx, mac_xbundle, &xvlan);
2815 } else if (!mac_xbundle) {
2816 xlate_report(ctx, OFT_WARN,
2817 "learned port is unknown, dropping");
2818 } else {
2819 xlate_report(ctx, OFT_DETAIL,
2820 "learned port is input port, dropping");
2821 }
2822 } else {
2823 xlate_report(ctx, OFT_DETAIL,
2824 "no learned MAC for destination, flooding");
2825 xlate_normal_flood(ctx, in_xbundle, &xvlan);
2826 }
2827 }
2828 }
2829
2830 /* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2831 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2832 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2833 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2834 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2835 * OVS_USERSPACE_ATTR_ACTIONS attribute is added. If 'emit_set_tunnel',
2836 * sample(sampling_port=1) would translate into datapath sample action
2837 * set(tunnel(...)), sample(...) and it is used for sampling egress tunnel
2838 * information.
2839 */
2840 static size_t
2841 compose_sample_action(struct xlate_ctx *ctx,
2842 const uint32_t probability,
2843 const union user_action_cookie *cookie,
2844 const size_t cookie_size,
2845 const odp_port_t tunnel_out_port,
2846 bool include_actions)
2847 {
2848 if (probability == 0) {
2849 /* No need to generate sampling or the inner action. */
2850 return 0;
2851 }
2852
2853 /* If the slow path meter is configured by the controller,
2854 * insert a meter action before the user space action. */
2855 struct ofproto *ofproto = &ctx->xin->ofproto->up;
2856 uint32_t meter_id = ofproto->slowpath_meter_id;
2857
2858 /* When meter action is not required, avoid generate sample action
2859 * for 100% sampling rate. */
2860 bool is_sample = probability < UINT32_MAX || meter_id != UINT32_MAX;
2861 size_t sample_offset, actions_offset;
2862 if (is_sample) {
2863 sample_offset = nl_msg_start_nested(ctx->odp_actions,
2864 OVS_ACTION_ATTR_SAMPLE);
2865 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY,
2866 probability);
2867 actions_offset = nl_msg_start_nested(ctx->odp_actions,
2868 OVS_SAMPLE_ATTR_ACTIONS);
2869 }
2870
2871 if (meter_id != UINT32_MAX) {
2872 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_METER, meter_id);
2873 }
2874
2875 odp_port_t odp_port = ofp_port_to_odp_port(
2876 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2877 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2878 flow_hash_5tuple(&ctx->xin->flow, 0));
2879 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2880 tunnel_out_port,
2881 include_actions,
2882 ctx->odp_actions);
2883
2884 if (is_sample) {
2885 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2886 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2887 }
2888
2889 return cookie_offset;
2890 }
2891
2892 /* If sFLow is not enabled, returns 0 without doing anything.
2893 *
2894 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2895 * in 'ctx'. This action is a template because some of the information needed
2896 * to fill it out is not available until flow translation is complete. In this
2897 * case, this functions returns an offset, which is always nonzero, to pass
2898 * later to fix_sflow_action() to fill in the rest of the template. */
2899 static size_t
2900 compose_sflow_action(struct xlate_ctx *ctx)
2901 {
2902 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2903 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2904 return 0;
2905 }
2906
2907 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2908 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
2909 &cookie, sizeof cookie.sflow, ODPP_NONE,
2910 true);
2911 }
2912
2913 /* If flow IPFIX is enabled, make sure IPFIX flow sample action
2914 * at egress point of tunnel port is just in front of corresponding
2915 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2916 * sample action to 'ctx->odp_actions'. */
2917 static void
2918 compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
2919 {
2920 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
2921 odp_port_t tunnel_out_port = ODPP_NONE;
2922
2923 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
2924 return;
2925 }
2926
2927 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2928 * number. */
2929 if (output_odp_port == ODPP_NONE &&
2930 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
2931 return;
2932 }
2933
2934 /* For output case, output_odp_port is valid. */
2935 if (output_odp_port != ODPP_NONE) {
2936 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
2937 return;
2938 }
2939 /* If tunnel sampling is enabled, put an additional option attribute:
2940 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2941 */
2942 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2943 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
2944 tunnel_out_port = output_odp_port;
2945 }
2946 }
2947
2948 union user_action_cookie cookie = {
2949 .ipfix = {
2950 .type = USER_ACTION_COOKIE_IPFIX,
2951 .output_odp_port = output_odp_port,
2952 }
2953 };
2954 compose_sample_action(ctx,
2955 dpif_ipfix_get_bridge_exporter_probability(ipfix),
2956 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2957 false);
2958 }
2959
2960 /* Fix "sample" action according to data collected while composing ODP actions,
2961 * as described in compose_sflow_action().
2962 *
2963 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
2964 static void
2965 fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
2966 {
2967 const struct flow *base = &ctx->base_flow;
2968 union user_action_cookie *cookie;
2969
2970 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
2971 sizeof cookie->sflow);
2972 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2973
2974 cookie->type = USER_ACTION_COOKIE_SFLOW;
2975 cookie->sflow.vlan_tci = base->vlans[0].tci;
2976
2977 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2978 * port information") for the interpretation of cookie->output. */
2979 switch (ctx->sflow_n_outputs) {
2980 case 0:
2981 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2982 cookie->sflow.output = 0x40000000 | 256;
2983 break;
2984
2985 case 1:
2986 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2987 ctx->xbridge->sflow, ctx->sflow_odp_port);
2988 if (cookie->sflow.output) {
2989 break;
2990 }
2991 /* Fall through. */
2992 default:
2993 /* 0x80000000 means "multiple output ports. */
2994 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2995 break;
2996 }
2997 }
2998
2999 static bool
3000 process_special(struct xlate_ctx *ctx, const struct xport *xport)
3001 {
3002 const struct flow *flow = &ctx->xin->flow;
3003 struct flow_wildcards *wc = ctx->wc;
3004 const struct xbridge *xbridge = ctx->xbridge;
3005 const struct dp_packet *packet = ctx->xin->packet;
3006 enum slow_path_reason slow;
3007
3008 if (!xport) {
3009 slow = 0;
3010 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
3011 if (packet) {
3012 cfm_process_heartbeat(xport->cfm, packet);
3013 }
3014 slow = SLOW_CFM;
3015 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
3016 if (packet) {
3017 bfd_process_packet(xport->bfd, flow, packet);
3018 /* If POLL received, immediately sends FINAL back. */
3019 if (bfd_should_send_packet(xport->bfd)) {
3020 ofproto_dpif_monitor_port_send_soon(xport->ofport);
3021 }
3022 }
3023 slow = SLOW_BFD;
3024 } else if (xport->xbundle && xport->xbundle->lacp
3025 && flow->dl_type == htons(ETH_TYPE_LACP)) {
3026 if (packet) {
3027 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
3028 }
3029 slow = SLOW_LACP;
3030 } else if ((xbridge->stp || xbridge->rstp) &&
3031 stp_should_process_flow(flow, wc)) {
3032 if (packet) {
3033 xbridge->stp
3034 ? stp_process_packet(xport, packet)
3035 : rstp_process_packet(xport, packet);
3036 }
3037 slow = SLOW_STP;
3038 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
3039 if (packet) {
3040 lldp_process_packet(xport->lldp, packet);
3041 }
3042 slow = SLOW_LLDP;
3043 } else {
3044 slow = 0;
3045 }
3046
3047 if (slow) {
3048 ctx->xout->slow |= slow;
3049 return true;
3050 } else {
3051 return false;
3052 }
3053 }
3054
3055 static int
3056 tnl_route_lookup_flow(const struct flow *oflow,
3057 struct in6_addr *ip, struct in6_addr *src,
3058 struct xport **out_port)
3059 {
3060 char out_dev[IFNAMSIZ];
3061 struct xbridge *xbridge;
3062 struct xlate_cfg *xcfg;
3063 struct in6_addr gw;
3064 struct in6_addr dst;
3065
3066 dst = flow_tnl_dst(&oflow->tunnel);
3067 if (!ovs_router_lookup(oflow->pkt_mark, &dst, out_dev, src, &gw)) {
3068 return -ENOENT;
3069 }
3070
3071 if (ipv6_addr_is_set(&gw) &&
3072 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
3073 *ip = gw;
3074 } else {
3075 *ip = dst;
3076 }
3077
3078 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3079 ovs_assert(xcfg);
3080
3081 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
3082 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
3083 struct xport *port;
3084
3085 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
3086 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
3087 *out_port = port;
3088 return 0;
3089 }
3090 }
3091 }
3092 }
3093 return -ENOENT;
3094 }
3095
3096 static int
3097 compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
3098 struct dp_packet *packet)
3099 {
3100 struct xbridge *xbridge = out_dev->xbridge;
3101 struct ofpact_output output;
3102 struct flow flow;
3103
3104 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3105 flow_extract(packet, &flow);
3106 flow.in_port.ofp_port = out_dev->ofp_port;
3107 output.port = OFPP_TABLE;
3108 output.max_len = 0;
3109
3110 return ofproto_dpif_execute_actions__(xbridge->ofproto,
3111 ctx->xin->tables_version, &flow,
3112 NULL, &output.ofpact, sizeof output,
3113 ctx->depth, ctx->resubmits, packet);
3114 }
3115
3116 static void
3117 tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
3118 const struct eth_addr eth_src,
3119 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
3120 {
3121 struct dp_packet packet;
3122
3123 dp_packet_init(&packet, 0);
3124 compose_nd_ns(&packet, eth_src, ipv6_src, ipv6_dst);
3125 compose_table_xlate(ctx, out_dev, &packet);
3126 dp_packet_uninit(&packet);
3127 }
3128
3129 static void
3130 tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
3131 const struct eth_addr eth_src,
3132 ovs_be32 ip_src, ovs_be32 ip_dst)
3133 {
3134 struct dp_packet packet;
3135
3136 dp_packet_init(&packet, 0);
3137 compose_arp(&packet, ARP_OP_REQUEST,
3138 eth_src, eth_addr_zero, true, ip_src, ip_dst);
3139
3140 compose_table_xlate(ctx, out_dev, &packet);
3141 dp_packet_uninit(&packet);
3142 }
3143
3144 static int
3145 build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
3146 const struct flow *flow, odp_port_t tunnel_odp_port)
3147 {
3148 struct netdev_tnl_build_header_params tnl_params;
3149 struct ovs_action_push_tnl tnl_push_data;
3150 struct xport *out_dev = NULL;
3151 ovs_be32 s_ip = 0, d_ip = 0;
3152 struct in6_addr s_ip6 = in6addr_any;
3153 struct in6_addr d_ip6 = in6addr_any;
3154 struct eth_addr smac;
3155 struct eth_addr dmac;
3156 int err;
3157 char buf_sip6[INET6_ADDRSTRLEN];
3158 char buf_dip6[INET6_ADDRSTRLEN];
3159
3160 err = tnl_route_lookup_flow(flow, &d_ip6, &s_ip6, &out_dev);
3161 if (err) {
3162 xlate_report(ctx, OFT_WARN, "native tunnel routing failed");
3163 return err;
3164 }
3165
3166 xlate_report(ctx, OFT_DETAIL, "tunneling to %s via %s",
3167 ipv6_string_mapped(buf_dip6, &d_ip6),
3168 netdev_get_name(out_dev->netdev));
3169
3170 /* Use mac addr of bridge port of the peer. */
3171 err = netdev_get_etheraddr(out_dev->netdev, &smac);
3172 if (err) {
3173 xlate_report(ctx, OFT_WARN,
3174 "tunnel output device lacks Ethernet address");
3175 return err;
3176 }
3177
3178 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
3179 if (d_ip) {
3180 s_ip = in6_addr_get_mapped_ipv4(&s_ip6);
3181 }
3182
3183 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
3184 if (err) {
3185 xlate_report(ctx, OFT_DETAIL,
3186 "neighbor cache miss for %s on bridge %s, "
3187 "sending %s request",
3188 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
3189 if (d_ip) {
3190 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
3191 } else {
3192 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
3193 }
3194 return err;
3195 }
3196
3197 if (ctx->xin->xcache) {
3198 struct xc_entry *entry;
3199
3200 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
3201 ovs_strlcpy(entry->tnl_neigh_cache.br_name, out_dev->xbridge->name,
3202 sizeof entry->tnl_neigh_cache.br_name);
3203 entry->tnl_neigh_cache.d_ipv6 = d_ip6;
3204 }
3205
3206 xlate_report(ctx, OFT_DETAIL, "tunneling from "ETH_ADDR_FMT" %s"
3207 " to "ETH_ADDR_FMT" %s",
3208 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
3209 ETH_ADDR_ARGS(dmac), buf_dip6);
3210
3211 netdev_init_tnl_build_header_params(&tnl_params, flow, &s_ip6, dmac, smac);
3212 err = tnl_port_build_header(xport->ofport, &tnl_push_data, &tnl_params);
3213 if (err) {
3214 return err;
3215 }
3216 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
3217 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
3218
3219 size_t push_action_size = 0;
3220 size_t clone_ofs = nl_msg_start_nested(ctx->odp_actions,
3221 OVS_ACTION_ATTR_CLONE);
3222 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
3223 push_action_size = ctx->odp_actions->size;
3224 apply_nested_clone_actions(ctx, xport, out_dev);
3225 if (ctx->odp_actions->size > push_action_size) {
3226 /* Update the CLONE action only when combined */
3227 nl_msg_end_nested(ctx->odp_actions, clone_ofs);
3228 }
3229 return 0;
3230 }
3231
3232 static void
3233 xlate_commit_actions(struct xlate_ctx *ctx)
3234 {
3235 bool use_masked = ctx->xbridge->support.masked_set_action;
3236
3237 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3238 ctx->odp_actions, ctx->wc,
3239 use_masked);
3240 }
3241
3242 static void
3243 clear_conntrack(struct xlate_ctx *ctx)
3244 {
3245 ctx->conntracked = false;
3246 flow_clear_conntrack(&ctx->xin->flow);
3247 }
3248
3249 static bool
3250 xlate_flow_is_protected(const struct xlate_ctx *ctx, const struct flow *flow, const struct xport *xport_out)
3251 {
3252 const struct xport *xport_in;
3253
3254 if (!xport_out) {
3255 return false;
3256 }
3257
3258 xport_in = get_ofp_port(ctx->xbridge, flow->in_port.ofp_port);
3259
3260 return (xport_in && xport_in->xbundle && xport_out->xbundle &&
3261 xport_in->xbundle->protected && xport_out->xbundle->protected);
3262 }
3263
3264 /* Populate and apply nested actions on 'out_dev'.
3265 * The nested actions are applied on cloned packets in dp while outputting to
3266 * either patch or tunnel ports.
3267 * On output to a patch port, the output action will be replaced with set of
3268 * nested actions on the peer patch port.
3269 * Similarly on output to a tunnel port, the post nested actions on
3270 * tunnel are chained up with the tunnel-push action.
3271 */
3272 static void
3273 apply_nested_clone_actions(struct xlate_ctx *ctx, const struct xport *in_dev,
3274 struct xport *out_dev)
3275 {
3276 struct flow *flow = &ctx->xin->flow;
3277 struct flow old_flow = ctx->xin->flow;
3278 struct flow_tnl old_flow_tnl_wc = ctx->wc->masks.tunnel;
3279 bool old_conntrack = ctx->conntracked;
3280 bool old_was_mpls = ctx->was_mpls;
3281 ovs_version_t old_version = ctx->xin->tables_version;
3282 struct ofpbuf old_stack = ctx->stack;
3283 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
3284 struct ofpbuf old_action_set = ctx->action_set;
3285 struct ovs_list *old_trace = ctx->xin->trace;
3286 uint64_t actset_stub[1024 / 8];
3287
3288 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
3289 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
3290 flow->in_port.ofp_port = out_dev->ofp_port;
3291 flow->metadata = htonll(0);
3292 memset(&flow->tunnel, 0, sizeof flow->tunnel);
3293 memset(&ctx->wc->masks.tunnel, 0, sizeof ctx->wc->masks.tunnel);
3294 flow->tunnel.metadata.tab =
3295 ofproto_get_tun_tab(&out_dev->xbridge->ofproto->up);
3296 ctx->wc->masks.tunnel.metadata.tab = flow->tunnel.metadata.tab;
3297 memset(flow->regs, 0, sizeof flow->regs);
3298 flow->actset_output = OFPP_UNSET;
3299 ctx->conntracked = false;
3300 clear_conntrack(ctx);
3301 ctx->xin->trace = xlate_report(ctx, OFT_BRIDGE,
3302 "bridge(\"%s\")",
3303 out_dev->xbridge->name);
3304 mirror_mask_t old_mirrors = ctx->mirrors;
3305 bool independent_mirrors = out_dev->xbridge != ctx->xbridge;
3306 if (independent_mirrors) {
3307 ctx->mirrors = 0;
3308 }
3309 ctx->xbridge = out_dev->xbridge;
3310
3311 /* The bridge is now known so obtain its table version. */
3312 ctx->xin->tables_version
3313 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
3314
3315 if (!process_special(ctx, out_dev) && may_receive(out_dev, ctx)) {
3316 if (xport_stp_forward_state(out_dev) &&
3317 xport_rstp_forward_state(out_dev)) {
3318 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true,
3319 false);
3320 if (!ctx->freezing) {
3321 xlate_action_set(ctx);
3322 }
3323 if (ctx->freezing) {
3324 finish_freezing(ctx);
3325 }
3326 } else {
3327 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3328 * the learning action look at the packet, then drop it. */
3329 struct flow old_base_flow = ctx->base_flow;
3330 size_t old_size = ctx->odp_actions->size;
3331 mirror_mask_t old_mirrors2 = ctx->mirrors;
3332
3333 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true,
3334 false);
3335 ctx->mirrors = old_mirrors2;
3336 ctx->base_flow = old_base_flow;
3337 ctx->odp_actions->size = old_size;
3338
3339 /* Undo changes that may have been done for freezing. */
3340 ctx_cancel_freeze(ctx);
3341 }
3342 }
3343
3344 ctx->xin->trace = old_trace;
3345 if (independent_mirrors) {
3346 ctx->mirrors = old_mirrors;
3347 }
3348 ctx->xin->flow = old_flow;
3349 ctx->xbridge = in_dev->xbridge;
3350 ofpbuf_uninit(&ctx->action_set);
3351 ctx->action_set = old_action_set;
3352 ofpbuf_uninit(&ctx->stack);
3353 ctx->stack = old_stack;
3354
3355 /* Restore calling bridge's lookup version. */
3356 ctx->xin->tables_version = old_version;
3357
3358 /* Restore to calling bridge tunneling information */
3359 ctx->wc->masks.tunnel = old_flow_tnl_wc;
3360
3361 /* The out bridge popping MPLS should have no effect on the original
3362 * bridge. */
3363 ctx->was_mpls = old_was_mpls;
3364
3365 /* The out bridge's conntrack execution should have no effect on the
3366 * original bridge. */
3367 ctx->conntracked = old_conntrack;
3368
3369 /* The fact that the out bridge exits (for any reason) does not mean
3370 * that the original bridge should exit. Specifically, if the out
3371 * bridge freezes translation, the original bridge must continue
3372 * processing with the original, not the frozen packet! */
3373 ctx->exit = false;
3374
3375 /* Out bridge errors do not propagate back. */
3376 ctx->error = XLATE_OK;
3377
3378 if (ctx->xin->resubmit_stats) {
3379 netdev_vport_inc_tx(in_dev->netdev, ctx->xin->resubmit_stats);
3380 netdev_vport_inc_rx(out_dev->netdev, ctx->xin->resubmit_stats);
3381 if (out_dev->bfd) {
3382 bfd_account_rx(out_dev->bfd, ctx->xin->resubmit_stats);
3383 }
3384 }
3385 if (ctx->xin->xcache) {
3386 struct xc_entry *entry;
3387 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3388 entry->dev.tx = netdev_ref(in_dev->netdev);
3389 entry->dev.rx = netdev_ref(out_dev->netdev);
3390 entry->dev.bfd = bfd_ref(out_dev->bfd);
3391 }
3392 }
3393
3394 static void
3395 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3396 const struct xlate_bond_recirc *xr, bool check_stp)
3397 {
3398 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
3399 struct flow_wildcards *wc = ctx->wc;
3400 struct flow *flow = &ctx->xin->flow;
3401 struct flow_tnl flow_tnl;
3402 union flow_vlan_hdr flow_vlans[FLOW_MAX_VLAN_HEADERS];
3403 uint8_t flow_nw_tos;
3404 odp_port_t out_port, odp_port;
3405 bool tnl_push_pop_send = false;
3406 uint8_t dscp;
3407
3408 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3409 * before traversing a patch port. */
3410 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
3411 memset(&flow_tnl, 0, sizeof flow_tnl);
3412
3413 if (!xport) {
3414 xlate_report(ctx, OFT_WARN, "Nonexistent output port");
3415 return;
3416 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
3417 xlate_report(ctx, OFT_DETAIL, "OFPPC_NO_FWD set, skipping output");
3418 return;
3419 } else if (ctx->mirror_snaplen != 0 && xport->odp_port == ODPP_NONE) {
3420 xlate_report(ctx, OFT_WARN,
3421 "Mirror truncate to ODPP_NONE, skipping output");
3422 return;
3423 } else if (xlate_flow_is_protected(ctx, flow, xport)) {
3424 xlate_report(ctx, OFT_WARN,
3425 "Flow is between protected ports, skipping output.");
3426 return;
3427 } else if (check_stp) {
3428 if (is_stp(&ctx->base_flow)) {
3429 if (!xport_stp_should_forward_bpdu(xport) &&
3430 !xport_rstp_should_manage_bpdu(xport)) {
3431 if (ctx->xbridge->stp != NULL) {
3432 xlate_report(ctx, OFT_WARN,
3433 "STP not in listening state, "
3434 "skipping bpdu output");
3435 } else if (ctx->xbridge->rstp != NULL) {
3436 xlate_report(ctx, OFT_WARN,
3437 "RSTP not managing BPDU in this state, "
3438 "skipping bpdu output");
3439 }
3440 return;
3441 }
3442 } else if ((xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc))
3443 || (xport->bfd && bfd_should_process_flow(xport->bfd, flow,
3444 wc))) {
3445 /* Pass; STP should not block link health detection. */
3446 } else if (!xport_stp_forward_state(xport) ||
3447 !xport_rstp_forward_state(xport)) {
3448 if (ctx->xbridge->stp != NULL) {
3449 xlate_report(ctx, OFT_WARN,
3450 "STP not in forwarding state, skipping output");
3451 } else if (ctx->xbridge->rstp != NULL) {
3452 xlate_report(ctx, OFT_WARN,
3453 "RSTP not in forwarding state, skipping output");
3454 }
3455 return;
3456 }
3457 }
3458
3459 if (xport->peer) {
3460 apply_nested_clone_actions(ctx, xport, xport->peer);
3461 return;
3462 }
3463
3464 memcpy(flow_vlans, flow->vlans, sizeof flow_vlans);
3465 flow_nw_tos = flow->nw_tos;
3466
3467 if (count_skb_priorities(xport)) {
3468 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3469 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3470 wc->masks.nw_tos |= IP_DSCP_MASK;
3471 flow->nw_tos &= ~IP_DSCP_MASK;
3472 flow->nw_tos |= dscp;
3473 }
3474 }
3475
3476 if (xport->is_tunnel) {
3477 struct in6_addr dst;
3478 /* Save tunnel metadata so that changes made due to
3479 * the Logical (tunnel) Port are not visible for any further
3480 * matches, while explicit set actions on tunnel metadata are.
3481 */
3482 flow_tnl = flow->tunnel;
3483 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
3484 if (odp_port == ODPP_NONE) {
3485 xlate_report(ctx, OFT_WARN, "Tunneling decided against output");
3486 goto out; /* restore flow_nw_tos */
3487 }
3488 dst = flow_tnl_dst(&flow->tunnel);
3489 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
3490 xlate_report(ctx, OFT_WARN, "Not tunneling to our own address");
3491 goto out; /* restore flow_nw_tos */
3492 }
3493 if (ctx->xin->resubmit_stats) {
3494 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3495 }
3496 if (ctx->xin->xcache) {
3497 struct xc_entry *entry;
3498
3499 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3500 entry->dev.tx = netdev_ref(xport->netdev);
3501 }
3502 out_port = odp_port;
3503 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3504 xlate_report(ctx, OFT_DETAIL, "output to native tunnel");
3505 tnl_push_pop_send = true;
3506 } else {
3507 xlate_report(ctx, OFT_DETAIL, "output to kernel tunnel");
3508 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
3509 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3510 }
3511 } else {
3512 odp_port = xport->odp_port;
3513 out_port = odp_port;
3514 }
3515
3516 if (out_port != ODPP_NONE) {
3517 xlate_commit_actions(ctx);
3518
3519 if (xr) {
3520 struct ovs_action_hash *act_hash;
3521
3522 /* Hash action. */
3523 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
3524 OVS_ACTION_ATTR_HASH,
3525 sizeof *act_hash);
3526 act_hash->hash_alg = xr->hash_alg;
3527 act_hash->hash_basis = xr->hash_basis;
3528
3529 /* Recirc action. */
3530 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
3531 xr->recirc_id);
3532 } else {
3533
3534 if (tnl_push_pop_send) {
3535 build_tunnel_send(ctx, xport, flow, odp_port);
3536 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3537 } else {
3538 odp_port_t odp_tnl_port = ODPP_NONE;
3539
3540 /* XXX: Write better Filter for tunnel port. We can use inport
3541 * int tunnel-port flow to avoid these checks completely. */
3542 if (ofp_port == OFPP_LOCAL &&
3543 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3544
3545 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3546 }
3547
3548 if (odp_tnl_port != ODPP_NONE) {
3549 nl_msg_put_odp_port(ctx->odp_actions,
3550 OVS_ACTION_ATTR_TUNNEL_POP,
3551 odp_tnl_port);
3552 } else {
3553 /* Tunnel push-pop action is not compatible with
3554 * IPFIX action. */
3555 compose_ipfix_action(ctx, out_port);
3556
3557 /* Handle truncation of the mirrored packet. */
3558 if (ctx->mirror_snaplen > 0 &&
3559 ctx->mirror_snaplen < UINT16_MAX) {
3560 struct ovs_action_trunc *trunc;
3561
3562 trunc = nl_msg_put_unspec_uninit(ctx->odp_actions,
3563 OVS_ACTION_ATTR_TRUNC,
3564 sizeof *trunc);
3565 trunc->max_len = ctx->mirror_snaplen;
3566 if (!ctx->xbridge->support.trunc) {
3567 ctx->xout->slow |= SLOW_ACTION;
3568 }
3569 }
3570
3571 nl_msg_put_odp_port(ctx->odp_actions,
3572 OVS_ACTION_ATTR_OUTPUT,
3573 out_port);
3574 }
3575 }
3576 }
3577
3578 ctx->sflow_odp_port = odp_port;
3579 ctx->sflow_n_outputs++;
3580 ctx->nf_output_iface = ofp_port;
3581 }
3582
3583 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3584 mirror_packet(ctx, xport->xbundle,
3585 xbundle_mirror_dst(xport->xbundle->xbridge,
3586 xport->xbundle));
3587 }
3588
3589 out:
3590 /* Restore flow */
3591 memcpy(flow->vlans, flow_vlans, sizeof flow->vlans);
3592 flow->nw_tos = flow_nw_tos;
3593 }
3594
3595 static void
3596 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3597 const struct xlate_bond_recirc *xr)
3598 {
3599 compose_output_action__(ctx, ofp_port, xr, true);
3600 }
3601
3602 static void
3603 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule, bool deepens)
3604 {
3605 struct rule_dpif *old_rule = ctx->rule;
3606 ovs_be64 old_cookie = ctx->rule_cookie;
3607 const struct rule_actions *actions;
3608
3609 if (ctx->xin->resubmit_stats) {
3610 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
3611 }
3612
3613 ctx->resubmits++;
3614
3615 ctx->depth += deepens;
3616 ctx->rule = rule;
3617 ctx->rule_cookie = rule->up.flow_cookie;
3618 actions = rule_get_actions(&rule->up);
3619 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
3620 ctx->rule_cookie = old_cookie;
3621 ctx->rule = old_rule;
3622 ctx->depth -= deepens;
3623 }
3624
3625 static bool
3626 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
3627 {
3628 if (ctx->depth >= MAX_DEPTH) {
3629 xlate_report_error(ctx, "over max translation depth %d", MAX_DEPTH);
3630 ctx->error = XLATE_RECURSION_TOO_DEEP;
3631 } else if (ctx->resubmits >= MAX_RESUBMITS) {
3632 xlate_report_error(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3633 ctx->error = XLATE_TOO_MANY_RESUBMITS;
3634 } else if (ctx->odp_actions->size > UINT16_MAX) {
3635 xlate_report_error(ctx, "resubmits yielded over 64 kB of actions");
3636 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3637 ctx->exit = true; /* XXX: translation still terminated! */
3638 } else if (ctx->stack.size >= 65536) {
3639 xlate_report_error(ctx, "resubmits yielded over 64 kB of stack");
3640 ctx->error = XLATE_STACK_TOO_DEEP;
3641 } else {
3642 return true;
3643 }
3644
3645 return false;
3646 }
3647
3648 static void
3649 tuple_swap_flow(struct flow *flow, bool ipv4)
3650 {
3651 uint8_t nw_proto = flow->nw_proto;
3652 flow->nw_proto = flow->ct_nw_proto;
3653 flow->ct_nw_proto = nw_proto;
3654
3655 if (ipv4) {
3656 ovs_be32 nw_src = flow->nw_src;
3657 flow->nw_src = flow->ct_nw_src;
3658 flow->ct_nw_src = nw_src;
3659
3660 ovs_be32 nw_dst = flow->nw_dst;
3661 flow->nw_dst = flow->ct_nw_dst;
3662 flow->ct_nw_dst = nw_dst;
3663 } else {
3664 struct in6_addr ipv6_src = flow->ipv6_src;
3665 flow->ipv6_src = flow->ct_ipv6_src;
3666 flow->ct_ipv6_src = ipv6_src;
3667
3668 struct in6_addr ipv6_dst = flow->ipv6_dst;
3669 flow->ipv6_dst = flow->ct_ipv6_dst;
3670 flow->ct_ipv6_dst = ipv6_dst;
3671 }
3672
3673 ovs_be16 tp_src = flow->tp_src;
3674 flow->tp_src = flow->ct_tp_src;
3675 flow->ct_tp_src = tp_src;
3676
3677 ovs_be16 tp_dst = flow->tp_dst;
3678 flow->tp_dst = flow->ct_tp_dst;
3679 flow->ct_tp_dst = tp_dst;
3680 }
3681
3682 static void
3683 tuple_swap(struct flow *flow, struct flow_wildcards *wc)
3684 {
3685 bool ipv4 = (flow->dl_type == htons(ETH_TYPE_IP));
3686
3687 tuple_swap_flow(flow, ipv4);
3688 tuple_swap_flow(&wc->masks, ipv4);
3689 }
3690
3691 static void
3692 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3693 bool may_packet_in, bool honor_table_miss,
3694 bool with_ct_orig)
3695 {
3696 /* Check if we need to recirculate before matching in a table. */
3697 if (ctx->was_mpls) {
3698 ctx_trigger_freeze(ctx);
3699 return;
3700 }
3701 if (xlate_resubmit_resource_check(ctx)) {
3702 uint8_t old_table_id = ctx->table_id;
3703 struct rule_dpif *rule;
3704
3705 ctx->table_id = table_id;
3706
3707 /* Swap packet fields with CT 5-tuple if requested. */
3708 if (with_ct_orig) {
3709 /* Do not swap if there is no CT tuple, or if key is not IP. */
3710 if (ctx->xin->flow.ct_nw_proto == 0 ||
3711 !is_ip_any(&ctx->xin->flow)) {
3712 xlate_report_error(ctx,
3713 "resubmit(ct) with non-tracked or non-IP packet!");
3714 return;
3715 }
3716 tuple_swap(&ctx->xin->flow, ctx->wc);
3717 }
3718 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
3719 ctx->xin->tables_version,
3720 &ctx->xin->flow, ctx->wc,
3721 ctx->xin->resubmit_stats,
3722 &ctx->table_id, in_port,
3723 may_packet_in, honor_table_miss,
3724 ctx->xin->xcache);
3725 /* Swap back. */
3726 if (with_ct_orig) {
3727 tuple_swap(&ctx->xin->flow, ctx->wc);
3728 }
3729
3730 if (rule) {
3731 /* Fill in the cache entry here instead of xlate_recursively
3732 * to make the reference counting more explicit. We take a
3733 * reference in the lookups above if we are going to cache the
3734 * rule. */
3735 if (ctx->xin->xcache) {
3736 struct xc_entry *entry;
3737
3738 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3739 entry->rule = rule;
3740 ofproto_rule_ref(&rule->up);
3741 }
3742
3743 struct ovs_list *old_trace = ctx->xin->trace;
3744 xlate_report_table(ctx, rule, table_id);
3745 xlate_recursively(ctx, rule, table_id <= old_table_id);
3746 ctx->xin->trace = old_trace;
3747 }
3748
3749 ctx->table_id = old_table_id;
3750 return;
3751 }
3752 }
3753
3754 /* Consumes the group reference, which is only taken if xcache exists. */
3755 static void
3756 xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3757 struct ofputil_bucket *bucket)
3758 {
3759 if (ctx->xin->resubmit_stats) {
3760 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3761 }
3762 if (ctx->xin->xcache) {
3763 struct xc_entry *entry;
3764
3765 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3766 entry->group.group = group;
3767 entry->group.bucket = bucket;
3768 }
3769 }
3770
3771 static void
3772 xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
3773 {
3774 uint64_t action_list_stub[1024 / 8];
3775 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
3776 struct ofpbuf action_set = ofpbuf_const_initializer(bucket->ofpacts,
3777 bucket->ofpacts_len);
3778 struct flow old_flow = ctx->xin->flow;
3779 bool old_was_mpls = ctx->was_mpls;
3780
3781 ofpacts_execute_action_set(&action_list, &action_set);
3782 ctx->depth++;
3783 do_xlate_actions(action_list.data, action_list.size, ctx);
3784 ctx->depth--;
3785
3786 ofpbuf_uninit(&action_list);
3787
3788 /* Check if need to freeze. */
3789 if (ctx->freezing) {
3790 finish_freezing(ctx);
3791 }
3792
3793 /* Roll back flow to previous state.
3794 * This is equivalent to cloning the packet for each bucket.
3795 *
3796 * As a side effect any subsequently applied actions will
3797 * also effectively be applied to a clone of the packet taken
3798 * just before applying the all or indirect group.
3799 *
3800 * Note that group buckets are action sets, hence they cannot modify the
3801 * main action set. Also any stack actions are ignored when executing an
3802 * action set, so group buckets cannot change the stack either.
3803 * However, we do allow resubmit actions in group buckets, which could
3804 * break the above assumptions. It is up to the controller to not mess up
3805 * with the action_set and stack in the tables resubmitted to from
3806 * group buckets. */
3807 ctx->xin->flow = old_flow;
3808
3809 /* The group bucket popping MPLS should have no effect after bucket
3810 * execution. */
3811 ctx->was_mpls = old_was_mpls;
3812
3813 /* The fact that the group bucket exits (for any reason) does not mean that
3814 * the translation after the group action should exit. Specifically, if
3815 * the group bucket freezes translation, the actions after the group action
3816 * must continue processing with the original, not the frozen packet! */
3817 ctx->exit = false;
3818 }
3819
3820 static void
3821 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3822 {
3823 struct ofputil_bucket *bucket;
3824 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3825 xlate_group_bucket(ctx, bucket);
3826 }
3827 xlate_group_stats(ctx, group, NULL);
3828 }
3829
3830 static void
3831 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3832 {
3833 struct ofputil_bucket *bucket;
3834
3835 bucket = group_first_live_bucket(ctx, group, 0);
3836 if (bucket) {
3837 xlate_group_bucket(ctx, bucket);
3838 xlate_group_stats(ctx, group, bucket);
3839 } else if (ctx->xin->xcache) {
3840 ofproto_group_unref(&group->up);
3841 }
3842 }
3843
3844 static void
3845 xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3846 {
3847 struct flow_wildcards *wc = ctx->wc;
3848 struct ofputil_bucket *bucket;
3849 uint32_t basis;
3850
3851 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
3852 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
3853 bucket = group_best_live_bucket(ctx, group, basis);
3854 if (bucket) {
3855 xlate_group_bucket(ctx, bucket);
3856 xlate_group_stats(ctx, group, bucket);
3857 } else if (ctx->xin->xcache) {
3858 ofproto_group_unref(&group->up);
3859 }
3860 }
3861
3862 static void
3863 xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3864 {
3865 const struct field_array *fields = &group->up.props.fields;
3866 const uint8_t *mask_values = fields->values;
3867 uint32_t basis = hash_uint64(group->up.props.selection_method_param);
3868
3869 size_t i;
3870 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->used.bm) {
3871 const struct mf_field *mf = mf_from_id(i);
3872
3873 /* Skip fields for which prerequisities are not met. */
3874 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow, ctx->wc)) {
3875 /* Skip the mask bytes for this field. */
3876 mask_values += mf->n_bytes;
3877 continue;
3878 }
3879
3880 union mf_value value;
3881 union mf_value mask;
3882
3883 mf_get_value(mf, &ctx->xin->flow, &value);
3884 /* Mask the value. */
3885 for (int j = 0; j < mf->n_bytes; j++) {
3886 mask.b[j] = *mask_values++;
3887 value.b[j] &= mask.b[j];
3888 }
3889 basis = hash_bytes(&value, mf->n_bytes, basis);
3890
3891 /* For tunnels, hash in whether the field is present. */
3892 if (mf_is_tun_metadata(mf)) {
3893 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3894 }
3895
3896 mf_mask_field_masked(mf, &mask, ctx->wc);
3897 }
3898
3899 struct ofputil_bucket *bucket = group_best_live_bucket(ctx, group, basis);
3900 if (bucket) {
3901 xlate_group_bucket(ctx, bucket);
3902 xlate_group_stats(ctx, group, bucket);
3903 } else if (ctx->xin->xcache) {
3904 ofproto_group_unref(&group->up);
3905 }
3906 }
3907
3908 static void
3909 xlate_dp_hash_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3910 {
3911 struct ofputil_bucket *bucket;
3912
3913 /* dp_hash value 0 is special since it means that the dp_hash has not been
3914 * computed, as all computed dp_hash values are non-zero. Therefore
3915 * compare to zero can be used to decide if the dp_hash value is valid
3916 * without masking the dp_hash field. */
3917 if (!ctx->xin->flow.dp_hash) {
3918 uint64_t param = group->up.props.selection_method_param;
3919
3920 ctx_trigger_recirculate_with_hash(ctx, param >> 32, (uint32_t)param);
3921 } else {
3922 uint32_t n_buckets = group->up.n_buckets;
3923 if (n_buckets) {
3924 /* Minimal mask to cover the number of buckets. */
3925 uint32_t mask = (1 << log_2_ceil(n_buckets)) - 1;
3926 /* Multiplier chosen to make the trivial 1 bit case to
3927 * actually distribute amongst two equal weight buckets. */
3928 uint32_t basis = 0xc2b73583 * (ctx->xin->flow.dp_hash & mask);
3929
3930 ctx->wc->masks.dp_hash |= mask;
3931 bucket = group_best_live_bucket(ctx, group, basis);
3932 if (bucket) {
3933 xlate_group_bucket(ctx, bucket);
3934 xlate_group_stats(ctx, group, bucket);
3935 }
3936 }
3937 }
3938 }
3939
3940 static void
3941 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3942 {
3943 const char *selection_method = group->up.props.selection_method;
3944
3945 /* Select groups may access flow keys beyond L2 in order to
3946 * select a bucket. Recirculate as appropriate to make this possible.
3947 */
3948 if (ctx->was_mpls) {
3949 ctx_trigger_freeze(ctx);
3950 }
3951
3952 if (selection_method[0] == '\0') {
3953 xlate_default_select_group(ctx, group);
3954 } else if (!strcasecmp("hash", selection_method)) {
3955 xlate_hash_fields_select_group(ctx, group);
3956 } else if (!strcasecmp("dp_hash", selection_method)) {
3957 xlate_dp_hash_select_group(ctx, group);
3958 } else {
3959 /* Parsing of groups should ensure this never happens */
3960 OVS_NOT_REACHED();
3961 }
3962 }
3963
3964 static void
3965 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3966 {
3967 bool was_in_group = ctx->in_group;
3968 ctx->in_group = true;
3969
3970 switch (group->up.type) {
3971 case OFPGT11_ALL:
3972 case OFPGT11_INDIRECT:
3973 xlate_all_group(ctx, group);
3974 break;
3975 case OFPGT11_SELECT:
3976 xlate_select_group(ctx, group);
3977 break;
3978 case OFPGT11_FF:
3979 xlate_ff_group(ctx, group);
3980 break;
3981 default:
3982 OVS_NOT_REACHED();
3983 }
3984
3985 ctx->in_group = was_in_group;
3986 }
3987
3988 static bool
3989 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3990 {
3991 if (xlate_resubmit_resource_check(ctx)) {
3992 struct group_dpif *group;
3993
3994 /* Take ref only if xcache exists. */
3995 group = group_dpif_lookup(ctx->xbridge->ofproto, group_id,
3996 ctx->xin->tables_version, ctx->xin->xcache);
3997 if (!group) {
3998 /* XXX: Should set ctx->error ? */
3999 xlate_report(ctx, OFT_WARN, "output to nonexistent group %"PRIu32,
4000 group_id);
4001 return true;
4002 }
4003 xlate_group_action__(ctx, group);
4004 }
4005
4006 return false;
4007 }
4008
4009 static void
4010 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
4011 const struct ofpact_resubmit *resubmit)
4012 {
4013 ofp_port_t in_port;
4014 uint8_t table_id;
4015 bool may_packet_in = false;
4016 bool honor_table_miss = false;
4017
4018 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
4019 /* Still allow missed packets to be sent to the controller
4020 * if resubmitting from an internal table. */
4021 may_packet_in = true;
4022 honor_table_miss = true;
4023 }
4024
4025 in_port = resubmit->in_port;
4026 if (in_port == OFPP_IN_PORT) {
4027 in_port = ctx->xin->flow.in_port.ofp_port;
4028 }
4029
4030 table_id = resubmit->table_id;
4031 if (table_id == 255) {
4032 table_id = ctx->table_id;
4033 }
4034
4035 xlate_table_action(ctx, in_port, table_id, may_packet_in,
4036 honor_table_miss, resubmit->with_ct_orig);
4037 }
4038
4039 static void
4040 flood_packets(struct xlate_ctx *ctx, bool all)
4041 {
4042 const struct xport *xport;
4043
4044 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
4045 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
4046 continue;
4047 }
4048
4049 if (all) {
4050 compose_output_action__(ctx, xport->ofp_port, NULL, false);
4051 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
4052 compose_output_action(ctx, xport->ofp_port, NULL);
4053 }
4054 }
4055
4056 ctx->nf_output_iface = NF_OUT_FLOOD;
4057 }
4058
4059 /* Copy and reformat a partially xlated odp actions to a new
4060 * odp actions list in 'b', so that the new actions list
4061 * can be executed by odp_execute_actions.
4062 *
4063 * When xlate using nested odp actions, such as sample and clone,
4064 * the nested action created by nl_msg_start_nested() may not
4065 * have been properly closed yet, thus can not be executed
4066 * directly.
4067 *
4068 * Since unclosed nested action has to be last action, it can be
4069 * fixed by skipping the outer header, and treating the actions within
4070 * as if they are outside the nested attribute since the effect
4071 * of executing them on packet is the same.
4072 *
4073 * As an optimization, a fully closed 'sample' or 'clone' action
4074 * is skipped since their execution has no effect to the packet.
4075 *
4076 * Returns true if success. 'b' contains the new actions list.
4077 * The caller is responsible for disposing 'b'.
4078 *
4079 * Returns false if error, 'b' has been freed already. */
4080 static bool
4081 xlate_fixup_actions(struct ofpbuf *b, const struct nlattr *actions,
4082 size_t actions_len)
4083 {
4084 const struct nlattr *a;
4085 unsigned int left;
4086
4087 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
4088 int type = nl_attr_type(a);
4089
4090 switch ((enum ovs_action_attr) type) {
4091 case OVS_ACTION_ATTR_HASH:
4092 case OVS_ACTION_ATTR_PUSH_VLAN:
4093 case OVS_ACTION_ATTR_POP_VLAN:
4094 case OVS_ACTION_ATTR_PUSH_MPLS:
4095 case OVS_ACTION_ATTR_POP_MPLS:
4096 case OVS_ACTION_ATTR_SET:
4097 case OVS_ACTION_ATTR_SET_MASKED:
4098 case OVS_ACTION_ATTR_TRUNC:
4099 case OVS_ACTION_ATTR_OUTPUT:
4100 case OVS_ACTION_ATTR_TUNNEL_PUSH:
4101 case OVS_ACTION_ATTR_TUNNEL_POP:
4102 case OVS_ACTION_ATTR_USERSPACE:
4103 case OVS_ACTION_ATTR_RECIRC:
4104 case OVS_ACTION_ATTR_CT:
4105 case OVS_ACTION_ATTR_PUSH_ETH:
4106 case OVS_ACTION_ATTR_POP_ETH:
4107 case OVS_ACTION_ATTR_METER:
4108 ofpbuf_put(b, a, nl_attr_len_pad(a, left));
4109 break;
4110
4111 case OVS_ACTION_ATTR_CLONE:
4112 /* If the clone action has been fully xlated, it can
4113 * be skipped, since any actions executed within clone
4114 * do not affect the current packet.
4115 *
4116 * When xlating actions within clone, the clone action,
4117 * because it is an nested netlink attribute, do not have
4118 * a valid 'nla_len'; it will be zero instead. Skip
4119 * the clone header to find the start of the actions
4120 * enclosed. Treat those actions as if they are written
4121 * outside of clone. */
4122 if (!a->nla_len) {
4123 bool ok;
4124 if (left < NLA_HDRLEN) {
4125 goto error;
4126 }
4127
4128 ok = xlate_fixup_actions(b, nl_attr_get_unspec(a, 0),
4129 left - NLA_HDRLEN);
4130 if (!ok) {
4131 goto error;
4132 }
4133 }
4134 break;
4135
4136 case OVS_ACTION_ATTR_SAMPLE:
4137 if (!a->nla_len) {
4138 bool ok;
4139 if (left < NLA_HDRLEN) {
4140 goto error;
4141 }
4142 const struct nlattr *attr = nl_attr_get_unspec(a, 0);
4143 left -= NLA_HDRLEN;
4144
4145 while (left > 0 &&
4146 nl_attr_type(attr) != OVS_SAMPLE_ATTR_ACTIONS) {
4147 /* Only OVS_SAMPLE_ATTR_ACTIONS can have unclosed
4148 * nested netlink attribute. */
4149 if (!attr->nla_len) {
4150 goto error;
4151 }
4152
4153 left -= NLA_ALIGN(attr->nla_len);
4154 attr = nl_attr_next(attr);
4155 }
4156
4157 if (left < NLA_HDRLEN) {
4158 goto error;
4159 }
4160
4161 ok = xlate_fixup_actions(b, nl_attr_get_unspec(attr, 0),
4162 left - NLA_HDRLEN);
4163 if (!ok) {
4164 goto error;
4165 }
4166 }
4167 break;
4168
4169 case OVS_ACTION_ATTR_UNSPEC:
4170 case __OVS_ACTION_ATTR_MAX:
4171 OVS_NOT_REACHED();
4172 }
4173 }
4174
4175 return true;
4176
4177 error:
4178 ofpbuf_delete(b);
4179 return false;
4180 }
4181
4182 static bool
4183 xlate_execute_odp_actions(struct dp_packet *packet,
4184 const struct nlattr *actions, int actions_len)
4185 {
4186 struct dp_packet_batch batch;
4187 struct ofpbuf *b = ofpbuf_new(actions_len);
4188
4189 if (!xlate_fixup_actions(b, actions, actions_len)) {
4190 return false;
4191 }
4192
4193 dp_packet_batch_init_packet(&batch, packet);
4194 odp_execute_actions(NULL, &batch, false, b->data, b->size, NULL);
4195 ofpbuf_delete(b);
4196
4197 return true;
4198 }
4199
4200 static void
4201 execute_controller_action(struct xlate_ctx *ctx, int len,
4202 enum ofp_packet_in_reason reason,
4203 uint16_t controller_id,
4204 const uint8_t *userdata, size_t userdata_len)
4205 {
4206 struct dp_packet *packet;
4207
4208 ctx->xout->slow |= SLOW_CONTROLLER;
4209 xlate_commit_actions(ctx);
4210 if (!ctx->xin->packet) {
4211 return;
4212 }
4213
4214 if (!ctx->xin->allow_side_effects && !ctx->xin->xcache) {
4215 return;
4216 }
4217
4218 packet = dp_packet_clone(ctx->xin->packet);
4219 if (!xlate_execute_odp_actions(packet, ctx->odp_actions->data,
4220 ctx->odp_actions->size)) {
4221 xlate_report_error(ctx, "Failed to execute controller action");
4222 dp_packet_delete(packet);
4223 return;
4224 }
4225 /* A packet sent by an action in a table-miss rule is considered an
4226 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
4227 * it will get translated back to OFPR_ACTION for those versions. */
4228 if (reason == OFPR_ACTION
4229 && ctx->rule && rule_is_table_miss(&ctx->rule->up)) {
4230 reason = OFPR_EXPLICIT_MISS;
4231 }
4232
4233 size_t packet_len = dp_packet_size(packet);
4234
4235 struct ofproto_async_msg *am = xmalloc(sizeof *am);
4236 *am = (struct ofproto_async_msg) {
4237 .controller_id = controller_id,
4238 .oam = OAM_PACKET_IN,
4239 .pin = {
4240 .up = {
4241 .public = {
4242 .packet = dp_packet_steal_data(packet),
4243 .packet_len = packet_len,
4244 .reason = reason,
4245 .table_id = ctx->table_id,
4246 .cookie = ctx->rule_cookie,
4247 .userdata = (userdata_len
4248 ? xmemdup(userdata, userdata_len)
4249 : NULL),
4250 .userdata_len = userdata_len,
4251 }
4252 },
4253 .max_len = len,
4254 },
4255 };
4256 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
4257
4258 /* Async messages are only sent once, so if we send one now, no
4259 * xlate cache entry is created. */
4260 if (ctx->xin->allow_side_effects) {
4261 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
4262 } else /* xcache */ {
4263 struct xc_entry *entry;
4264
4265 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_CONTROLLER);
4266 entry->controller.ofproto = ctx->xbridge->ofproto;
4267 entry->controller.am = am;
4268 }
4269
4270 dp_packet_delete(packet);
4271 }
4272
4273 static void
4274 emit_continuation(struct xlate_ctx *ctx, const struct frozen_state *state)
4275 {
4276 if (!ctx->xin->allow_side_effects && !ctx->xin->xcache) {
4277 return;
4278 }
4279
4280 struct ofproto_async_msg *am = xmalloc(sizeof *am);
4281 *am = (struct ofproto_async_msg) {
4282 .controller_id = ctx->pause->controller_id,
4283 .oam = OAM_PACKET_IN,
4284 .pin = {
4285 .up = {
4286 .public = {
4287 .userdata = xmemdup(ctx->pause->userdata,
4288 ctx->pause->userdata_len),
4289 .userdata_len = ctx->pause->userdata_len,
4290 .packet = xmemdup(dp_packet_data(ctx->xin->packet),
4291 dp_packet_size(ctx->xin->packet)),
4292 .packet_len = dp_packet_size(ctx->xin->packet),
4293 .reason = ctx->pause->reason,
4294 },
4295 .bridge = ctx->xbridge->ofproto->uuid,
4296 .stack = xmemdup(state->stack, state->stack_size),
4297 .stack_size = state->stack_size,
4298 .mirrors = state->mirrors,
4299 .conntracked = state->conntracked,
4300 .actions = xmemdup(state->ofpacts, state->ofpacts_len),
4301 .actions_len = state->ofpacts_len,
4302 .action_set = xmemdup(state->action_set,
4303 state->action_set_len),
4304 .action_set_len = state->action_set_len,
4305 },
4306 .max_len = UINT16_MAX,
4307 },
4308 };
4309 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
4310
4311 /* Async messages are only sent once, so if we send one now, no
4312 * xlate cache entry is created. */
4313 if (ctx->xin->allow_side_effects) {
4314 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
4315 } else /* xcache */ {
4316 struct xc_entry *entry;
4317
4318 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_CONTROLLER);
4319 entry->controller.ofproto = ctx->xbridge->ofproto;
4320 entry->controller.am = am;
4321 }
4322 }
4323
4324 static void
4325 finish_freezing__(struct xlate_ctx *ctx, uint8_t table)
4326 {
4327 ovs_assert(ctx->freezing);
4328
4329 struct frozen_state state = {
4330 .table_id = table,
4331 .ofproto_uuid = ctx->xbridge->ofproto->uuid,
4332 .stack = ctx->stack.data,
4333 .stack_size = ctx->stack.size,
4334 .mirrors = ctx->mirrors,
4335 .conntracked = ctx->conntracked,
4336 .ofpacts = ctx->frozen_actions.data,
4337 .ofpacts_len = ctx->frozen_actions.size,
4338 .action_set = ctx->action_set.data,
4339 .action_set_len = ctx->action_set.size,
4340 };
4341 frozen_metadata_from_flow(&state.metadata, &ctx->xin->flow);
4342
4343 if (ctx->pause) {
4344 if (ctx->xin->packet) {
4345 emit_continuation(ctx, &state);
4346 }
4347 } else {
4348 /* Allocate a unique recirc id for the given metadata state in the
4349 * flow. An existing id, with a new reference to the corresponding
4350 * recirculation context, will be returned if possible.
4351 * The life-cycle of this recirc id is managed by associating it
4352 * with the udpif key ('ukey') created for each new datapath flow. */
4353 uint32_t id = recirc_alloc_id_ctx(&state);
4354 if (!id) {
4355 xlate_report_error(ctx, "Failed to allocate recirculation id");
4356 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
4357 return;
4358 }
4359 recirc_refs_add(&ctx->xout->recircs, id);
4360
4361 if (ctx->recirc_update_dp_hash) {
4362 struct ovs_action_hash *act_hash;
4363
4364 /* Hash action. */
4365 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
4366 OVS_ACTION_ATTR_HASH,
4367 sizeof *act_hash);
4368 act_hash->hash_alg = OVS_HASH_ALG_L4; /* Make configurable. */
4369 act_hash->hash_basis = 0; /* Make configurable. */
4370 }
4371 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
4372 }
4373
4374 /* Undo changes done by freezing. */
4375 ctx_cancel_freeze(ctx);
4376 }
4377
4378 /* Called only when we're freezing. */
4379 static void
4380 finish_freezing(struct xlate_ctx *ctx)
4381 {
4382 xlate_commit_actions(ctx);
4383 finish_freezing__(ctx, 0);
4384 }
4385
4386 /* Fork the pipeline here. The current packet will continue processing the
4387 * current action list. A clone of the current packet will recirculate, skip
4388 * the remainder of the current action list and asynchronously resume pipeline
4389 * processing in 'table' with the current metadata and action set. */
4390 static void
4391 compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
4392 {
4393 ctx->freezing = true;
4394 finish_freezing__(ctx, table);
4395 }
4396
4397 static void
4398 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
4399 {
4400 struct flow *flow = &ctx->xin->flow;
4401 int n;
4402
4403 ovs_assert(eth_type_mpls(mpls->ethertype));
4404
4405 n = flow_count_mpls_labels(flow, ctx->wc);
4406 if (!n) {
4407 xlate_commit_actions(ctx);
4408 } else if (n >= FLOW_MAX_MPLS_LABELS) {
4409 if (ctx->xin->packet != NULL) {
4410 xlate_report_error(ctx, "dropping packet on which an MPLS push "
4411 "action can't be performed as it would have "
4412 "more MPLS LSEs than the %d supported.",
4413 FLOW_MAX_MPLS_LABELS);
4414 }
4415 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
4416 return;
4417 }
4418
4419 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4420 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc, true);
4421 }
4422
4423 static void
4424 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
4425 {
4426 struct flow *flow = &ctx->xin->flow;
4427 int n = flow_count_mpls_labels(flow, ctx->wc);
4428
4429 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
4430 if (!eth_type_mpls(eth_type) && ctx->xbridge->support.odp.recirc) {
4431 ctx->was_mpls = true;
4432 }
4433 } else if (n >= FLOW_MAX_MPLS_LABELS) {
4434 if (ctx->xin->packet != NULL) {
4435 xlate_report_error(ctx, "dropping packet on which an "
4436 "MPLS pop action can't be performed as it has "
4437 "more MPLS LSEs than the %d supported.",
4438 FLOW_MAX_MPLS_LABELS);
4439 }
4440 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
4441 ofpbuf_clear(ctx->odp_actions);
4442 }
4443 }
4444
4445 static bool
4446 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
4447 {
4448 struct flow *flow = &ctx->xin->flow;
4449
4450 if (!is_ip_any(flow)) {
4451 return false;
4452 }
4453
4454 ctx->wc->masks.nw_ttl = 0xff;
4455 if (flow->nw_ttl > 1) {
4456 flow->nw_ttl--;
4457 return false;
4458 } else {
4459 size_t i;
4460
4461 for (i = 0; i < ids->n_controllers; i++) {
4462 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
4463 ids->cnt_ids[i], NULL, 0);
4464 }
4465
4466 /* Stop processing for current table. */
4467 xlate_report(ctx, OFT_WARN, "IPv%d decrement TTL exception",
4468 flow->dl_type == htons(ETH_TYPE_IP) ? 4 : 6);
4469 return true;
4470 }
4471 }
4472
4473 static void
4474 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
4475 {
4476 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
4477 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
4478 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
4479 }
4480 }
4481
4482 static void
4483 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
4484 {
4485 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
4486 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
4487 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
4488 }
4489 }
4490
4491 static void
4492 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
4493 {
4494 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
4495 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
4496 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
4497 }
4498 }
4499
4500 static bool
4501 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
4502 {
4503 struct flow *flow = &ctx->xin->flow;
4504
4505 if (eth_type_mpls(flow->dl_type)) {
4506 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
4507
4508 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
4509 if (ttl > 1) {
4510 ttl--;
4511 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
4512 return false;
4513 } else {
4514 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0,
4515 NULL, 0);
4516 }
4517 }
4518
4519 /* Stop processing for current table. */
4520 xlate_report(ctx, OFT_WARN, "MPLS decrement TTL exception");
4521 return true;
4522 }
4523
4524 static void
4525 xlate_output_action(struct xlate_ctx *ctx,
4526 ofp_port_t port, uint16_t max_len, bool may_packet_in)
4527 {
4528 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
4529
4530 ctx->nf_output_iface = NF_OUT_DROP;
4531
4532 switch (port) {
4533 case OFPP_IN_PORT:
4534 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
4535 break;
4536 case OFPP_TABLE:
4537 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
4538 0, may_packet_in, true, false);
4539 break;
4540 case OFPP_NORMAL:
4541 xlate_normal(ctx);
4542 break;
4543 case OFPP_FLOOD:
4544 flood_packets(ctx, false);
4545 break;
4546 case OFPP_ALL:
4547 flood_packets(ctx, true);
4548 break;
4549 case OFPP_CONTROLLER:
4550 execute_controller_action(ctx, max_len,
4551 (ctx->in_packet_out ? OFPR_PACKET_OUT
4552 : ctx->in_group ? OFPR_GROUP
4553 : ctx->in_action_set ? OFPR_ACTION_SET
4554 : OFPR_ACTION),
4555 0, NULL, 0);
4556 break;
4557 case OFPP_NONE:
4558 break;
4559 case OFPP_LOCAL:
4560 default:
4561 if (port != ctx->xin->flow.in_port.ofp_port) {
4562 compose_output_action(ctx, port, NULL);
4563 } else {
4564 xlate_report(ctx, OFT_WARN, "skipping output to input port");
4565 }
4566 break;
4567 }
4568
4569 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4570 ctx->nf_output_iface = NF_OUT_FLOOD;
4571 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4572 ctx->nf_output_iface = prev_nf_output_iface;
4573 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4574 ctx->nf_output_iface != NF_OUT_FLOOD) {
4575 ctx->nf_output_iface = NF_OUT_MULTI;
4576 }
4577 }
4578
4579 static void
4580 xlate_output_reg_action(struct xlate_ctx *ctx,
4581 const struct ofpact_output_reg *or)
4582 {
4583 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
4584 if (port <= UINT16_MAX) {
4585 xlate_report(ctx, OFT_DETAIL, "output port is %"PRIu64, port);
4586
4587 union mf_subvalue value;
4588
4589 memset(&value, 0xff, sizeof value);
4590 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
4591 xlate_output_action(ctx, u16_to_ofp(port), or->max_len, false);
4592 } else {
4593 xlate_report(ctx, OFT_WARN, "output port %"PRIu64" is out of range",
4594 port);
4595 }
4596 }
4597
4598 static void
4599 xlate_output_trunc_action(struct xlate_ctx *ctx,
4600 ofp_port_t port, uint32_t max_len)
4601 {
4602 bool support_trunc = ctx->xbridge->support.trunc;
4603 struct ovs_action_trunc *trunc;
4604 char name[OFP10_MAX_PORT_NAME_LEN];
4605
4606 switch (port) {
4607 case OFPP_TABLE:
4608 case OFPP_NORMAL:
4609 case OFPP_FLOOD:
4610 case OFPP_ALL:
4611 case OFPP_CONTROLLER:
4612 case OFPP_NONE:
4613 ofputil_port_to_string(port, name, sizeof name);
4614 xlate_report(ctx, OFT_WARN,
4615 "output_trunc does not support port: %s", name);
4616 break;
4617 case OFPP_LOCAL:
4618 case OFPP_IN_PORT:
4619 default:
4620 if (port != ctx->xin->flow.in_port.ofp_port) {
4621 const struct xport *xport = get_ofp_port(ctx->xbridge, port);
4622
4623 if (xport == NULL || xport->odp_port == ODPP_NONE) {
4624 /* Since truncate happens at its following output action, if
4625 * the output port is a patch port, the behavior is somehow
4626 * unpredicable. For simpilicity, disallow this case. */
4627 ofputil_port_to_string(port, name, sizeof name);
4628 xlate_report_error(ctx, "output_trunc does not support "
4629 "patch port %s", name);
4630 break;
4631 }
4632
4633 trunc = nl_msg_put_unspec_uninit(ctx->odp_actions,
4634 OVS_ACTION_ATTR_TRUNC,
4635 sizeof *trunc);
4636 trunc->max_len = max_len;
4637 xlate_output_action(ctx, port, max_len, false);
4638 if (!support_trunc) {
4639 ctx->xout->slow |= SLOW_ACTION;
4640 }
4641 } else {
4642 xlate_report(ctx, OFT_WARN, "skipping output to input port");
4643 }
4644 break;
4645 }
4646 }
4647
4648 static void
4649 xlate_enqueue_action(struct xlate_ctx *ctx,
4650 const struct ofpact_enqueue *enqueue)
4651 {
4652 ofp_port_t ofp_port = enqueue->port;
4653 uint32_t queue_id = enqueue->queue;
4654 uint32_t flow_priority, priority;
4655 int error;
4656
4657 /* Translate queue to priority. */
4658 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
4659 if (error) {
4660 /* Fall back to ordinary output action. */
4661 xlate_output_action(ctx, enqueue->port, 0, false);
4662 return;
4663 }
4664
4665 /* Check output port. */
4666 if (ofp_port == OFPP_IN_PORT) {
4667 ofp_port = ctx->xin->flow.in_port.ofp_port;
4668 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
4669 return;
4670 }
4671
4672 /* Add datapath actions. */
4673 flow_priority = ctx->xin->flow.skb_priority;
4674 ctx->xin->flow.skb_priority = priority;
4675 compose_output_action(ctx, ofp_port, NULL);
4676 ctx->xin->flow.skb_priority = flow_priority;
4677
4678 /* Update NetFlow output port. */
4679 if (ctx->nf_output_iface == NF_OUT_DROP) {
4680 ctx->nf_output_iface = ofp_port;
4681 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4682 ctx->nf_output_iface = NF_OUT_MULTI;
4683 }
4684 }
4685
4686 static void
4687 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
4688 {
4689 uint32_t skb_priority;
4690
4691 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
4692 ctx->xin->flow.skb_priority = skb_priority;
4693 } else {
4694 /* Couldn't translate queue to a priority. Nothing to do. A warning
4695 * has already been logged. */
4696 }
4697 }
4698
4699 static bool
4700 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
4701 {
4702 const struct xbridge *xbridge = xbridge_;
4703 struct xport *port;
4704
4705 switch (ofp_port) {
4706 case OFPP_IN_PORT:
4707 case OFPP_TABLE:
4708 case OFPP_NORMAL:
4709 case OFPP_FLOOD:
4710 case OFPP_ALL:
4711 case OFPP_NONE:
4712 return true;
4713 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4714 return false;
4715 default:
4716 port = get_ofp_port(xbridge, ofp_port);
4717 return port ? port->may_enable : false;
4718 }
4719 }
4720
4721 static void
4722 xlate_bundle_action(struct xlate_ctx *ctx,
4723 const struct ofpact_bundle *bundle)
4724 {
4725 ofp_port_t port;
4726
4727 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
4728 CONST_CAST(struct xbridge *, ctx->xbridge));
4729 if (bundle->dst.field) {
4730 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
4731 xlate_report_subfield(ctx, &bundle->dst);
4732 } else {
4733 xlate_output_action(ctx, port, 0, false);
4734 }
4735 }
4736
4737 static void
4738 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
4739 {
4740 learn_mask(learn, ctx->wc);
4741
4742 if (ctx->xin->xcache || ctx->xin->allow_side_effects) {
4743 uint64_t ofpacts_stub[1024 / 8];
4744 struct ofputil_flow_mod fm;
4745 struct ofproto_flow_mod ofm__, *ofm;
4746 struct ofpbuf ofpacts;
4747 enum ofperr error;
4748
4749 if (ctx->xin->xcache) {
4750 ofm = xmalloc(sizeof *ofm);
4751 } else {
4752 ofm = &ofm__;
4753 }
4754
4755 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4756 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
4757 if (OVS_UNLIKELY(ctx->xin->trace)) {
4758 struct ds s = DS_EMPTY_INITIALIZER;
4759 ds_put_format(&s, "table=%"PRIu8" ", fm.table_id);
4760 match_format(&fm.match, &s, OFP_DEFAULT_PRIORITY);
4761 ds_chomp(&s, ' ');
4762 ds_put_format(&s, " priority=%d", fm.priority);
4763 if (fm.new_cookie) {
4764 ds_put_format(&s, " cookie=%#"PRIx64, ntohll(fm.new_cookie));
4765 }
4766 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
4767 ds_put_format(&s, " idle=%"PRIu16, fm.idle_timeout);
4768 }
4769 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
4770 ds_put_format(&s, " hard=%"PRIu16, fm.hard_timeout);
4771 }
4772 if (fm.flags & NX_LEARN_F_SEND_FLOW_REM) {
4773 ds_put_cstr(&s, " send_flow_rem");
4774 }
4775 ds_put_cstr(&s, " actions=");
4776 ofpacts_format(fm.ofpacts, fm.ofpacts_len, &s);
4777 xlate_report(ctx, OFT_DETAIL, "%s", ds_cstr(&s));
4778 ds_destroy(&s);
4779 }
4780 error = ofproto_dpif_flow_mod_init_for_learn(ctx->xbridge->ofproto,
4781 &fm, ofm);
4782 ofpbuf_uninit(&ofpacts);
4783
4784 if (!error) {
4785 bool success = true;
4786 if (ctx->xin->allow_side_effects) {
4787 error = ofproto_flow_mod_learn(ofm, ctx->xin->xcache != NULL,
4788 learn->limit, &success);
4789 } else if (learn->limit) {
4790 if (!ofm->temp_rule
4791 || ofm->temp_rule->state != RULE_INSERTED) {
4792 /* The learned rule expired and there are no packets, so
4793 * we cannot learn again. Since the translated actions
4794 * depend on the result of learning, we tell the caller
4795 * that there's no point in caching this result. */
4796 ctx->xout->avoid_caching = true;
4797 }
4798 }
4799
4800 if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
4801 nxm_reg_load(&learn->result_dst, success ? 1 : 0,
4802 &ctx->xin->flow, ctx->wc);
4803 xlate_report_subfield(ctx, &learn->result_dst);
4804 }
4805
4806 if (success && ctx->xin->xcache) {
4807 struct xc_entry *entry;
4808
4809 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4810 entry->learn.ofm = ofm;
4811 entry->learn.limit = learn->limit;
4812 ofm = NULL;
4813 }
4814
4815 if (OVS_UNLIKELY(ctx->xin->trace && !success)) {
4816 xlate_report(ctx, OFT_DETAIL, "Limit exceeded, learn failed");
4817 }
4818 }
4819
4820 if (ctx->xin->xcache) {
4821 free(ofm);
4822 }
4823
4824 if (error) {
4825 xlate_report_error(ctx, "LEARN action execution failed (%s).",
4826 ofperr_to_string(error));
4827 }
4828 } else {
4829 xlate_report(ctx, OFT_WARN,
4830 "suppressing side effects, so learn action ignored");
4831 }
4832 }
4833
4834 static void
4835 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
4836 uint16_t idle_timeout, uint16_t hard_timeout)
4837 {
4838 if (tcp_flags & (TCP_FIN | TCP_RST)) {
4839 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
4840 }
4841 }
4842
4843 static void
4844 xlate_fin_timeout(struct xlate_ctx *ctx,
4845 const struct ofpact_fin_timeout *oft)
4846 {
4847 if (ctx->rule) {
4848 if (ctx->xin->allow_side_effects) {
4849 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4850 oft->fin_idle_timeout, oft->fin_hard_timeout);
4851 }
4852 if (ctx->xin->xcache) {
4853 struct xc_entry *entry;
4854
4855 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
4856 /* XC_RULE already holds a reference on the rule, none is taken
4857 * here. */
4858 entry->fin.rule = ctx->rule;
4859 entry->fin.idle = oft->fin_idle_timeout;
4860 entry->fin.hard = oft->fin_hard_timeout;
4861 }
4862 }
4863 }
4864
4865 static void
4866 xlate_sample_action(struct xlate_ctx *ctx,
4867 const struct ofpact_sample *os)
4868 {
4869 odp_port_t output_odp_port = ODPP_NONE;
4870 odp_port_t tunnel_out_port = ODPP_NONE;
4871 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
4872 bool emit_set_tunnel = false;
4873
4874 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
4875 return;
4876 }
4877
4878 /* Scale the probability from 16-bit to 32-bit while representing
4879 * the same percentage. */
4880 uint32_t probability = (os->probability << 16) | os->probability;
4881
4882 if (!ctx->xbridge->support.variable_length_userdata) {
4883 xlate_report_error(ctx, "ignoring NXAST_SAMPLE action because "
4884 "datapath lacks support (needs Linux 3.10+ or "
4885 "kernel module from OVS 1.11+)");
4886 return;
4887 }
4888
4889 /* If ofp_port in flow sample action is equel to ofp_port,
4890 * this sample action is a input port action. */
4891 if (os->sampling_port != OFPP_NONE &&
4892 os->sampling_port != ctx->xin->flow.in_port.ofp_port) {
4893 output_odp_port = ofp_port_to_odp_port(ctx->xbridge,
4894 os->sampling_port);
4895 if (output_odp_port == ODPP_NONE) {
4896 xlate_report_error(ctx, "can't use unknown port %d in flow sample "
4897 "action", os->sampling_port);
4898 return;
4899 }
4900
4901 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix,
4902 os->collector_set_id)
4903 && dpif_ipfix_get_tunnel_port(ipfix, output_odp_port)) {
4904 tunnel_out_port = output_odp_port;
4905 emit_set_tunnel = true;
4906 }
4907 }
4908
4909 xlate_commit_actions(ctx);
4910 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
4911 * into datapath sample action set(tunnel(...)), sample(...) and
4912 * it is used for sampling egress tunnel information. */
4913 if (emit_set_tunnel) {
4914 const struct xport *xport = get_ofp_port(ctx->xbridge,
4915 os->sampling_port);
4916
4917 if (xport && xport->is_tunnel) {
4918 struct flow *flow = &ctx->xin->flow;
4919 tnl_port_send(xport->ofport, flow, ctx->wc);
4920 if (!ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4921 struct flow_tnl flow_tnl = flow->tunnel;
4922
4923 commit_odp_tunnel_action(flow, &ctx->base_flow,
4924 ctx->odp_actions);
4925 flow->tunnel = flow_tnl;
4926 }
4927 } else {
4928 xlate_report_error(ctx,
4929 "sampling_port:%d should be a tunnel port.",
4930 os->sampling_port);
4931 }
4932 }
4933
4934 union user_action_cookie cookie = {
4935 .flow_sample = {
4936 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4937 .probability = os->probability,
4938 .collector_set_id = os->collector_set_id,
4939 .obs_domain_id = os->obs_domain_id,
4940 .obs_point_id = os->obs_point_id,
4941 .output_odp_port = output_odp_port,
4942 .direction = os->direction,
4943 }
4944 };
4945 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4946 tunnel_out_port, false);
4947 }
4948
4949 /* Use datapath 'clone' or sample to enclose the translation of 'oc'. */
4950 static void
4951 compose_clone_action(struct xlate_ctx *ctx, const struct ofpact_nest *oc)
4952 {
4953 size_t clone_offset = nl_msg_start_nested(ctx->odp_actions,
4954 OVS_ACTION_ATTR_CLONE);
4955 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
4956 nl_msg_end_non_empty_nested(ctx->odp_actions, clone_offset);
4957 }
4958
4959 /* Use datapath 'sample' action to translate clone. */
4960 static void
4961 compose_clone_action_using_sample(struct xlate_ctx *ctx,
4962 const struct ofpact_nest *oc)
4963 {
4964 size_t offset = nl_msg_start_nested(ctx->odp_actions,
4965 OVS_ACTION_ATTR_SAMPLE);
4966
4967 size_t ac_offset = nl_msg_start_nested(ctx->odp_actions,
4968 OVS_SAMPLE_ATTR_ACTIONS);
4969
4970 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
4971
4972 if (nl_msg_end_non_empty_nested(ctx->odp_actions, ac_offset)) {
4973 nl_msg_cancel_nested(ctx->odp_actions, offset);
4974 } else {
4975 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY,
4976 UINT32_MAX); /* 100% probability. */
4977 nl_msg_end_nested(ctx->odp_actions, offset);
4978 }
4979 }
4980
4981 static void
4982 xlate_clone(struct xlate_ctx *ctx, const struct ofpact_nest *oc)
4983 {
4984 bool old_was_mpls = ctx->was_mpls;
4985 bool old_conntracked = ctx->conntracked;
4986 struct flow old_flow = ctx->xin->flow;
4987
4988 struct ofpbuf old_stack = ctx->stack;
4989 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
4990 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
4991 ofpbuf_put(&ctx->stack, old_stack.data, old_stack.size);
4992
4993 struct ofpbuf old_action_set = ctx->action_set;
4994 uint64_t actset_stub[1024 / 8];
4995 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
4996 ofpbuf_put(&ctx->action_set, old_action_set.data, old_action_set.size);
4997
4998 /* Datapath clone action will make sure the pre clone packets
4999 * are used for actions after clone. Save and restore
5000 * ctx->base_flow to reflect this for the openflow pipeline. */
5001 if (ctx->xbridge->support.clone) {
5002 struct flow old_base_flow = ctx->base_flow;
5003 compose_clone_action(ctx, oc);
5004 ctx->base_flow = old_base_flow;
5005 } else if (ctx->xbridge->support.sample_nesting > 3) {
5006 /* Avoid generate sample action if datapath
5007 * only allow small number of nesting. Deeper nesting
5008 * can cause the datapath to reject the generated flow. */
5009 struct flow old_base_flow = ctx->base_flow;
5010 compose_clone_action_using_sample(ctx, oc);
5011 ctx->base_flow = old_base_flow;
5012 } else {
5013 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
5014 }
5015
5016 ofpbuf_uninit(&ctx->action_set);
5017 ctx->action_set = old_action_set;
5018
5019 ofpbuf_uninit(&ctx->stack);
5020 ctx->stack = old_stack;
5021
5022 ctx->xin->flow = old_flow;
5023
5024 /* The clone's conntrack execution should have no effect on the original
5025 * packet. */
5026 ctx->conntracked = old_conntracked;
5027
5028 /* Popping MPLS from the clone should have no effect on the original
5029 * packet. */
5030 ctx->was_mpls = old_was_mpls;
5031 }
5032
5033 static void
5034 xlate_meter_action(struct xlate_ctx *ctx, const struct ofpact_meter *meter)
5035 {
5036 if (meter->provider_meter_id != UINT32_MAX) {
5037 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_METER,
5038 meter->provider_meter_id);
5039 }
5040 }
5041
5042 static bool
5043 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
5044 {
5045 if (xport->config & (is_stp(&ctx->xin->flow)
5046 ? OFPUTIL_PC_NO_RECV_STP
5047 : OFPUTIL_PC_NO_RECV)) {
5048 return false;
5049 }
5050
5051 /* Only drop packets here if both forwarding and learning are
5052 * disabled. If just learning is enabled, we need to have
5053 * OFPP_NORMAL and the learning action have a look at the packet
5054 * before we can drop it. */
5055 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
5056 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
5057 return false;
5058 }
5059
5060 return true;
5061 }
5062
5063 static void
5064 xlate_write_actions__(struct xlate_ctx *ctx,
5065 const struct ofpact *ofpacts, size_t ofpacts_len)
5066 {
5067 /* Maintain actset_output depending on the contents of the action set:
5068 *
5069 * - OFPP_UNSET, if there is no "output" action.
5070 *
5071 * - The output port, if there is an "output" action and no "group"
5072 * action.
5073 *
5074 * - OFPP_UNSET, if there is a "group" action.
5075 */
5076 if (!ctx->action_set_has_group) {
5077 const struct ofpact *a;
5078 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5079 if (a->type == OFPACT_OUTPUT) {
5080 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(a)->port;
5081 } else if (a->type == OFPACT_GROUP) {
5082 ctx->xin->flow.actset_output = OFPP_UNSET;
5083 ctx->action_set_has_group = true;
5084 break;
5085 }
5086 }
5087 }
5088
5089 ofpbuf_put(&ctx->action_set, ofpacts, ofpacts_len);
5090 }
5091
5092 static void
5093 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact_nest *a)
5094 {
5095 xlate_write_actions__(ctx, a->actions, ofpact_nest_get_action_len(a));
5096 }
5097
5098 static void
5099 xlate_action_set(struct xlate_ctx *ctx)
5100 {
5101 uint64_t action_list_stub[1024 / 8];
5102 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
5103 ofpacts_execute_action_set(&action_list, &ctx->action_set);
5104 /* Clear the action set, as it is not needed any more. */
5105 ofpbuf_clear(&ctx->action_set);
5106 if (action_list.size) {
5107 ctx->in_action_set = true;
5108
5109 struct ovs_list *old_trace = ctx->xin->trace;
5110 ctx->xin->trace = xlate_report(ctx, OFT_TABLE,
5111 "--. Executing action set:");
5112 do_xlate_actions(action_list.data, action_list.size, ctx);
5113 ctx->xin->trace = old_trace;
5114
5115 ctx->in_action_set = false;
5116 }
5117 ofpbuf_uninit(&action_list);
5118 }
5119
5120 static void
5121 freeze_put_unroll_xlate(struct xlate_ctx *ctx)
5122 {
5123 struct ofpact_unroll_xlate *unroll = ctx->frozen_actions.header;
5124
5125 /* Restore the table_id and rule cookie for a potential PACKET
5126 * IN if needed. */
5127 if (!unroll ||
5128 (ctx->table_id != unroll->rule_table_id
5129 || ctx->rule_cookie != unroll->rule_cookie)) {
5130 unroll = ofpact_put_UNROLL_XLATE(&ctx->frozen_actions);
5131 unroll->rule_table_id = ctx->table_id;
5132 unroll->rule_cookie = ctx->rule_cookie;
5133 ctx->frozen_actions.header = unroll;
5134 }
5135 }
5136
5137
5138 /* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
5139 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
5140 * present, before any action that may depend on the current table ID or flow
5141 * cookie. */
5142 static void
5143 freeze_unroll_actions(const struct ofpact *a, const struct ofpact *end,
5144 struct xlate_ctx *ctx)
5145 {
5146 for (; a < end; a = ofpact_next(a)) {
5147 switch (a->type) {
5148 case OFPACT_OUTPUT_REG:
5149 case OFPACT_OUTPUT_TRUNC:
5150 case OFPACT_GROUP:
5151 case OFPACT_OUTPUT:
5152 case OFPACT_CONTROLLER:
5153 case OFPACT_DEC_MPLS_TTL:
5154 case OFPACT_DEC_TTL:
5155 /* These actions may generate asynchronous messages, which include
5156 * table ID and flow cookie information. */
5157 freeze_put_unroll_xlate(ctx);
5158 break;
5159
5160 case OFPACT_RESUBMIT:
5161 if (ofpact_get_RESUBMIT(a)->table_id == 0xff) {
5162 /* This resubmit action is relative to the current table, so we
5163 * need to track what table that is.*/
5164 freeze_put_unroll_xlate(ctx);
5165 }
5166 break;
5167
5168 case OFPACT_SET_TUNNEL:
5169 case OFPACT_REG_MOVE:
5170 case OFPACT_SET_FIELD:
5171 case OFPACT_STACK_PUSH:
5172 case OFPACT_STACK_POP:
5173 case OFPACT_LEARN:
5174 case OFPACT_WRITE_METADATA:
5175 case OFPACT_GOTO_TABLE:
5176 case OFPACT_ENQUEUE:
5177 case OFPACT_SET_VLAN_VID:
5178 case OFPACT_SET_VLAN_PCP:
5179 case OFPACT_STRIP_VLAN:
5180 case OFPACT_PUSH_VLAN:
5181 case OFPACT_SET_ETH_SRC:
5182 case OFPACT_SET_ETH_DST:
5183 case OFPACT_SET_IPV4_SRC:
5184 case OFPACT_SET_IPV4_DST:
5185 case OFPACT_SET_IP_DSCP:
5186 case OFPACT_SET_IP_ECN:
5187 case OFPACT_SET_IP_TTL:
5188 case OFPACT_SET_L4_SRC_PORT:
5189 case OFPACT_SET_L4_DST_PORT:
5190 case OFPACT_SET_QUEUE:
5191 case OFPACT_POP_QUEUE:
5192 case OFPACT_PUSH_MPLS:
5193 case OFPACT_POP_MPLS:
5194 case OFPACT_SET_MPLS_LABEL:
5195 case OFPACT_SET_MPLS_TC:
5196 case OFPACT_SET_MPLS_TTL:
5197 case OFPACT_MULTIPATH:
5198 case OFPACT_BUNDLE:
5199 case OFPACT_EXIT:
5200 case OFPACT_UNROLL_XLATE:
5201 case OFPACT_FIN_TIMEOUT:
5202 case OFPACT_CLEAR_ACTIONS:
5203 case OFPACT_WRITE_ACTIONS:
5204 case OFPACT_METER:
5205 case OFPACT_SAMPLE:
5206 case OFPACT_CLONE:
5207 case OFPACT_DEBUG_RECIRC:
5208 case OFPACT_CT:
5209 case OFPACT_CT_CLEAR:
5210 case OFPACT_NAT:
5211 /* These may not generate PACKET INs. */
5212 break;
5213
5214 case OFPACT_NOTE:
5215 case OFPACT_CONJUNCTION:
5216 /* These need not be copied for restoration. */
5217 continue;
5218 }
5219 /* Copy the action over. */
5220 ofpbuf_put(&ctx->frozen_actions, a, OFPACT_ALIGN(a->len));
5221 }
5222 }
5223
5224 static void
5225 put_ct_mark(const struct flow *flow, struct ofpbuf *odp_actions,
5226 struct flow_wildcards *wc)
5227 {
5228 if (wc->masks.ct_mark) {
5229 struct {
5230 uint32_t key;
5231 uint32_t mask;
5232 } *odp_ct_mark;
5233
5234 odp_ct_mark = nl_msg_put_unspec_uninit(odp_actions, OVS_CT_ATTR_MARK,
5235 sizeof(*odp_ct_mark));
5236 odp_ct_mark->key = flow->ct_mark & wc->masks.ct_mark;
5237 odp_ct_mark->mask = wc->masks.ct_mark;
5238 }
5239 }
5240
5241 static void
5242 put_ct_label(const struct flow *flow, struct ofpbuf *odp_actions,
5243 struct flow_wildcards *wc)
5244 {
5245 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
5246 struct {
5247 ovs_u128 key;
5248 ovs_u128 mask;
5249 } *odp_ct_label;
5250
5251 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
5252 OVS_CT_ATTR_LABELS,
5253 sizeof(*odp_ct_label));
5254 odp_ct_label->key = ovs_u128_and(flow->ct_label, wc->masks.ct_label);
5255 odp_ct_label->mask = wc->masks.ct_label;
5256 }
5257 }
5258
5259 static void
5260 put_ct_helper(struct xlate_ctx *ctx,
5261 struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
5262 {
5263 if (ofc->alg) {
5264 switch(ofc->alg) {
5265 case IPPORT_FTP:
5266 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
5267 break;
5268 case IPPORT_TFTP:
5269 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "tftp");
5270 break;
5271 default:
5272 xlate_report_error(ctx, "cannot serialize ct_helper %d", ofc->alg);
5273 break;
5274 }
5275 }
5276 }
5277
5278 static void
5279 put_ct_nat(struct xlate_ctx *ctx)
5280 {
5281 struct ofpact_nat *ofn = ctx->ct_nat_action;
5282 size_t nat_offset;
5283
5284 if (!ofn) {
5285 return;
5286 }
5287
5288 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
5289 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
5290 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
5291 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
5292 if (ofn->flags & NX_NAT_F_PERSISTENT) {
5293 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
5294 }
5295 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
5296 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
5297 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
5298 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
5299 }
5300 if (ofn->range_af == AF_INET) {
5301 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
5302 ofn->range.addr.ipv4.min);
5303 if (ofn->range.addr.ipv4.max &&
5304 (ntohl(ofn->range.addr.ipv4.max)
5305 > ntohl(ofn->range.addr.ipv4.min))) {
5306 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
5307 ofn->range.addr.ipv4.max);
5308 }
5309 } else if (ofn->range_af == AF_INET6) {
5310 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
5311 &ofn->range.addr.ipv6.min,
5312 sizeof ofn->range.addr.ipv6.min);
5313 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
5314 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
5315 sizeof ofn->range.addr.ipv6.max) > 0) {
5316 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
5317 &ofn->range.addr.ipv6.max,
5318 sizeof ofn->range.addr.ipv6.max);
5319 }
5320 }
5321 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
5322 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
5323 ofn->range.proto.min);
5324 if (ofn->range.proto.max &&
5325 ofn->range.proto.max > ofn->range.proto.min) {
5326 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
5327 ofn->range.proto.max);
5328 }
5329 }
5330 }
5331 nl_msg_end_nested(ctx->odp_actions, nat_offset);
5332 }
5333
5334 static void
5335 compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
5336 {
5337 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
5338 ovs_u128 old_ct_label_mask = ctx->wc->masks.ct_label;
5339 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
5340 uint32_t old_ct_mark_mask = ctx->wc->masks.ct_mark;
5341 size_t ct_offset;
5342 uint16_t zone;
5343
5344 /* Ensure that any prior actions are applied before composing the new
5345 * conntrack action. */
5346 xlate_commit_actions(ctx);
5347
5348 /* Process nested actions first, to populate the key. */
5349 ctx->ct_nat_action = NULL;
5350 ctx->wc->masks.ct_mark = 0;
5351 ctx->wc->masks.ct_label.u64.hi = ctx->wc->masks.ct_label.u64.lo = 0;
5352 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
5353
5354 if (ofc->zone_src.field) {
5355 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
5356 } else {
5357 zone = ofc->zone_imm;
5358 }
5359
5360 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
5361 if (ofc->flags & NX_CT_F_COMMIT) {
5362 nl_msg_put_flag(ctx->odp_actions, ofc->flags & NX_CT_F_FORCE ?
5363 OVS_CT_ATTR_FORCE_COMMIT : OVS_CT_ATTR_COMMIT);
5364 if (ctx->xbridge->support.ct_eventmask) {
5365 nl_msg_put_u32(ctx->odp_actions, OVS_CT_ATTR_EVENTMASK,
5366 OVS_CT_EVENTMASK_DEFAULT);
5367 }
5368 }
5369 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
5370 put_ct_mark(&ctx->xin->flow, ctx->odp_actions, ctx->wc);
5371 put_ct_label(&ctx->xin->flow, ctx->odp_actions, ctx->wc);
5372 put_ct_helper(ctx, ctx->odp_actions, ofc);
5373 put_ct_nat(ctx);
5374 ctx->ct_nat_action = NULL;
5375 nl_msg_end_nested(ctx->odp_actions, ct_offset);
5376
5377 /* Restore the original ct fields in the key. These should only be exposed
5378 * after recirculation to another table. */
5379 ctx->base_flow.ct_mark = old_ct_mark;
5380 ctx->wc->masks.ct_mark = old_ct_mark_mask;
5381 ctx->base_flow.ct_label = old_ct_label;
5382 ctx->wc->masks.ct_label = old_ct_label_mask;
5383
5384 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
5385 /* If we do not recirculate as part of this action, hide the results of
5386 * connection tracking from subsequent recirculations. */
5387 ctx->conntracked = false;
5388 } else {
5389 /* Use ct_* fields from datapath during recirculation upcall. */
5390 ctx->conntracked = true;
5391 compose_recirculate_and_fork(ctx, ofc->recirc_table);
5392 }
5393 }
5394
5395 static void
5396 recirc_for_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
5397 {
5398 /* No need to recirculate if already exiting. */
5399 if (ctx->exit) {
5400 return;
5401 }
5402
5403 /* Do not consider recirculating unless the packet was previously MPLS. */
5404 if (!ctx->was_mpls) {
5405 return;
5406 }
5407
5408 /* Special case these actions, only recirculating if necessary.
5409 * This avoids the overhead of recirculation in common use-cases.
5410 */
5411 switch (a->type) {
5412
5413 /* Output actions do not require recirculation. */
5414 case OFPACT_OUTPUT:
5415 case OFPACT_OUTPUT_TRUNC:
5416 case OFPACT_ENQUEUE:
5417 case OFPACT_OUTPUT_REG:
5418 /* Set actions that don't touch L3+ fields do not require recirculation. */
5419 case OFPACT_SET_VLAN_VID:
5420 case OFPACT_SET_VLAN_PCP:
5421 case OFPACT_SET_ETH_SRC:
5422 case OFPACT_SET_ETH_DST:
5423 case OFPACT_SET_TUNNEL:
5424 case OFPACT_SET_QUEUE:
5425 /* If actions of a group require recirculation that can be detected
5426 * when translating them. */
5427 case OFPACT_GROUP:
5428 return;
5429
5430 /* Set field that don't touch L3+ fields don't require recirculation. */
5431 case OFPACT_SET_FIELD:
5432 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field)) {
5433 break;
5434 }
5435 return;
5436
5437 /* For simplicity, recirculate in all other cases. */
5438 case OFPACT_CONTROLLER:
5439 case OFPACT_BUNDLE:
5440 case OFPACT_STRIP_VLAN:
5441 case OFPACT_PUSH_VLAN:
5442 case OFPACT_SET_IPV4_SRC:
5443 case OFPACT_SET_IPV4_DST:
5444 case OFPACT_SET_IP_DSCP:
5445 case OFPACT_SET_IP_ECN:
5446 case OFPACT_SET_IP_TTL:
5447 case OFPACT_SET_L4_SRC_PORT:
5448 case OFPACT_SET_L4_DST_PORT:
5449 case OFPACT_REG_MOVE:
5450 case OFPACT_STACK_PUSH:
5451 case OFPACT_STACK_POP:
5452 case OFPACT_DEC_TTL:
5453 case OFPACT_SET_MPLS_LABEL:
5454 case OFPACT_SET_MPLS_TC:
5455 case OFPACT_SET_MPLS_TTL:
5456 case OFPACT_DEC_MPLS_TTL:
5457 case OFPACT_PUSH_MPLS:
5458 case OFPACT_POP_MPLS:
5459 case OFPACT_POP_QUEUE:
5460 case OFPACT_FIN_TIMEOUT:
5461 case OFPACT_RESUBMIT:
5462 case OFPACT_LEARN:
5463 case OFPACT_CONJUNCTION:
5464 case OFPACT_MULTIPATH:
5465 case OFPACT_NOTE:
5466 case OFPACT_EXIT:
5467 case OFPACT_SAMPLE:
5468 case OFPACT_CLONE:
5469 case OFPACT_UNROLL_XLATE:
5470 case OFPACT_CT:
5471 case OFPACT_CT_CLEAR:
5472 case OFPACT_NAT:
5473 case OFPACT_DEBUG_RECIRC:
5474 case OFPACT_METER:
5475 case OFPACT_CLEAR_ACTIONS:
5476 case OFPACT_WRITE_ACTIONS:
5477 case OFPACT_WRITE_METADATA:
5478 case OFPACT_GOTO_TABLE:
5479 default:
5480 break;
5481 }
5482
5483 /* Recirculate */
5484 ctx_trigger_freeze(ctx);
5485 }
5486
5487 static void
5488 xlate_ofpact_reg_move(struct xlate_ctx *ctx, const struct ofpact_reg_move *a)
5489 {
5490 mf_subfield_copy(&a->src, &a->dst, &ctx->xin->flow, ctx->wc);
5491 xlate_report_subfield(ctx, &a->dst);
5492 }
5493
5494 static void
5495 xlate_ofpact_stack_pop(struct xlate_ctx *ctx, const struct ofpact_stack *a)
5496 {
5497 if (nxm_execute_stack_pop(a, &ctx->xin->flow, ctx->wc, &ctx->stack)) {
5498 xlate_report_subfield(ctx, &a->subfield);
5499 } else {
5500 xlate_report_error(ctx, "stack underflow");
5501 }
5502 }
5503
5504 /* Restore translation context data that was stored earlier. */
5505 static void
5506 xlate_ofpact_unroll_xlate(struct xlate_ctx *ctx,
5507 const struct ofpact_unroll_xlate *a)
5508 {
5509 ctx->table_id = a->rule_table_id;
5510 ctx->rule_cookie = a->rule_cookie;
5511 xlate_report(ctx, OFT_THAW, "restored state: table=%"PRIu8", "
5512 "cookie=%#"PRIx64, a->rule_table_id, a->rule_cookie);
5513 }
5514
5515 static void
5516 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
5517 struct xlate_ctx *ctx)
5518 {
5519 struct flow_wildcards *wc = ctx->wc;
5520 struct flow *flow = &ctx->xin->flow;
5521 const struct ofpact *a;
5522
5523 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
5524 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
5525 }
5526 /* dl_type already in the mask, not set below. */
5527
5528 if (!ofpacts_len) {
5529 xlate_report(ctx, OFT_ACTION, "drop");
5530 return;
5531 }
5532
5533 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5534 struct ofpact_controller *controller;
5535 const struct ofpact_metadata *metadata;
5536 const struct ofpact_set_field *set_field;
5537 const struct mf_field *mf;
5538
5539 if (ctx->error) {
5540 break;
5541 }
5542
5543 recirc_for_mpls(a, ctx);
5544
5545 if (ctx->exit) {
5546 /* Check if need to store the remaining actions for later
5547 * execution. */
5548 if (ctx->freezing) {
5549 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len),
5550 ctx);
5551 }
5552 break;
5553 }
5554
5555 if (OVS_UNLIKELY(ctx->xin->trace)) {
5556 struct ds s = DS_EMPTY_INITIALIZER;
5557 ofpacts_format(a, OFPACT_ALIGN(a->len), &s);
5558 xlate_report(ctx, OFT_ACTION, "%s", ds_cstr(&s));
5559 ds_destroy(&s);
5560 }
5561
5562 switch (a->type) {
5563 case OFPACT_OUTPUT:
5564 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
5565 ofpact_get_OUTPUT(a)->max_len, true);
5566 break;
5567
5568 case OFPACT_GROUP:
5569 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
5570 /* Group could not be found. */
5571
5572 /* XXX: Terminates action list translation, but does not
5573 * terminate the pipeline. */
5574 return;
5575 }
5576 break;
5577
5578 case OFPACT_CONTROLLER:
5579 controller = ofpact_get_CONTROLLER(a);
5580 if (controller->pause) {
5581 ctx->pause = controller;
5582 ctx->xout->slow |= SLOW_CONTROLLER;
5583 ctx_trigger_freeze(ctx);
5584 a = ofpact_next(a);
5585 } else {
5586 execute_controller_action(ctx, controller->max_len,
5587 controller->reason,
5588 controller->controller_id,
5589 controller->userdata,
5590 controller->userdata_len);
5591 }
5592 break;
5593
5594 case OFPACT_ENQUEUE:
5595 memset(&wc->masks.skb_priority, 0xff,
5596 sizeof wc->masks.skb_priority);
5597 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
5598 break;
5599
5600 case OFPACT_SET_VLAN_VID:
5601 wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
5602 if (flow->vlans[0].tci & htons(VLAN_CFI) ||
5603 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5604 if (!flow->vlans[0].tpid) {
5605 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN);
5606 }
5607 flow->vlans[0].tci &= ~htons(VLAN_VID_MASK);
5608 flow->vlans[0].tci |=
5609 (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid) |
5610 htons(VLAN_CFI));
5611 }
5612 break;
5613
5614 case OFPACT_SET_VLAN_PCP:
5615 wc->masks.vlans[0].tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
5616 if (flow->vlans[0].tci & htons(VLAN_CFI) ||
5617 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5618 if (!flow->vlans[0].tpid) {
5619 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN);
5620 }
5621 flow->vlans[0].tci &= ~htons(VLAN_PCP_MASK);
5622 flow->vlans[0].tci |=
5623 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
5624 << VLAN_PCP_SHIFT) | VLAN_CFI);
5625 }
5626 break;
5627
5628 case OFPACT_STRIP_VLAN:
5629 flow_pop_vlan(flow, wc);
5630 break;
5631
5632 case OFPACT_PUSH_VLAN:
5633 flow_push_vlan_uninit(flow, wc);
5634 flow->vlans[0].tpid = ofpact_get_PUSH_VLAN(a)->ethertype;
5635 flow->vlans[0].tci = htons(VLAN_CFI);
5636 break;
5637
5638 case OFPACT_SET_ETH_SRC:
5639 WC_MASK_FIELD(wc, dl_src);
5640 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
5641 break;
5642
5643 case OFPACT_SET_ETH_DST:
5644 WC_MASK_FIELD(wc, dl_dst);
5645 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
5646 break;
5647
5648 case OFPACT_SET_IPV4_SRC:
5649 if (flow->dl_type == htons(ETH_TYPE_IP)) {
5650 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
5651 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
5652 }
5653 break;
5654
5655 case OFPACT_SET_IPV4_DST:
5656 if (flow->dl_type == htons(ETH_TYPE_IP)) {
5657 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
5658 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
5659 }
5660 break;
5661
5662 case OFPACT_SET_IP_DSCP:
5663 if (is_ip_any(flow)) {
5664 wc->masks.nw_tos |= IP_DSCP_MASK;
5665 flow->nw_tos &= ~IP_DSCP_MASK;
5666 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
5667 }
5668 break;
5669
5670 case OFPACT_SET_IP_ECN:
5671 if (is_ip_any(flow)) {
5672 wc->masks.nw_tos |= IP_ECN_MASK;
5673 flow->nw_tos &= ~IP_ECN_MASK;
5674 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
5675 }
5676 break;
5677
5678 case OFPACT_SET_IP_TTL:
5679 if (is_ip_any(flow)) {
5680 wc->masks.nw_ttl = 0xff;
5681 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
5682 }
5683 break;
5684
5685 case OFPACT_SET_L4_SRC_PORT:
5686 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5687 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
5688 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
5689 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
5690 }
5691 break;
5692
5693 case OFPACT_SET_L4_DST_PORT:
5694 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5695 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
5696 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
5697 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
5698 }
5699 break;
5700
5701 case OFPACT_RESUBMIT:
5702 /* Freezing complicates resubmit. Some action in the flow
5703 * entry found by resubmit might trigger freezing. If that
5704 * happens, then we do not want to execute the resubmit again after
5705 * during thawing, so we want to skip back to the head of the loop
5706 * to avoid that, only adding any actions that follow the resubmit
5707 * to the frozen actions.
5708 */
5709 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
5710 continue;
5711
5712 case OFPACT_SET_TUNNEL:
5713 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
5714 break;
5715
5716 case OFPACT_SET_QUEUE:
5717 memset(&wc->masks.skb_priority, 0xff,
5718 sizeof wc->masks.skb_priority);
5719 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
5720 break;
5721
5722 case OFPACT_POP_QUEUE:
5723 memset(&wc->masks.skb_priority, 0xff,
5724 sizeof wc->masks.skb_priority);
5725 if (flow->skb_priority != ctx->orig_skb_priority) {
5726 flow->skb_priority = ctx->orig_skb_priority;
5727 xlate_report(ctx, OFT_DETAIL, "queue = %#"PRIx32,
5728 flow->skb_priority);
5729 }
5730 break;
5731
5732 case OFPACT_REG_MOVE:
5733 xlate_ofpact_reg_move(ctx, ofpact_get_REG_MOVE(a));
5734 break;
5735
5736 case OFPACT_SET_FIELD:
5737 set_field = ofpact_get_SET_FIELD(a);
5738 mf = set_field->field;
5739
5740 /* Set the field only if the packet actually has it. */
5741 if (mf_are_prereqs_ok(mf, flow, wc)) {
5742 mf_mask_field_masked(mf, ofpact_set_field_mask(set_field), wc);
5743 mf_set_flow_value_masked(mf, set_field->value,
5744 ofpact_set_field_mask(set_field),
5745 flow);
5746 } else {
5747 xlate_report(ctx, OFT_WARN,
5748 "unmet prerequisites for %s, set_field ignored",
5749 mf->name);
5750
5751 }
5752 break;
5753
5754 case OFPACT_STACK_PUSH:
5755 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
5756 &ctx->stack);
5757 break;
5758
5759 case OFPACT_STACK_POP:
5760 xlate_ofpact_stack_pop(ctx, ofpact_get_STACK_POP(a));
5761 break;
5762
5763 case OFPACT_PUSH_MPLS:
5764 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
5765 break;
5766
5767 case OFPACT_POP_MPLS:
5768 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
5769 break;
5770
5771 case OFPACT_SET_MPLS_LABEL:
5772 compose_set_mpls_label_action(
5773 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
5774 break;
5775
5776 case OFPACT_SET_MPLS_TC:
5777 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
5778 break;
5779
5780 case OFPACT_SET_MPLS_TTL:
5781 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
5782 break;
5783
5784 case OFPACT_DEC_MPLS_TTL:
5785 if (compose_dec_mpls_ttl_action(ctx)) {
5786 return;
5787 }
5788 break;
5789
5790 case OFPACT_DEC_TTL:
5791 wc->masks.nw_ttl = 0xff;
5792 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
5793 return;
5794 }
5795 break;
5796
5797 case OFPACT_NOTE:
5798 /* Nothing to do. */
5799 break;
5800
5801 case OFPACT_MULTIPATH:
5802 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
5803 xlate_report_subfield(ctx, &ofpact_get_MULTIPATH(a)->dst);
5804 break;
5805
5806 case OFPACT_BUNDLE:
5807 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
5808 break;
5809
5810 case OFPACT_OUTPUT_REG:
5811 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
5812 break;
5813
5814 case OFPACT_OUTPUT_TRUNC:
5815 xlate_output_trunc_action(ctx, ofpact_get_OUTPUT_TRUNC(a)->port,
5816 ofpact_get_OUTPUT_TRUNC(a)->max_len);
5817 break;
5818
5819 case OFPACT_LEARN:
5820 xlate_learn_action(ctx, ofpact_get_LEARN(a));
5821 break;
5822
5823 case OFPACT_CONJUNCTION:
5824 /* A flow with a "conjunction" action represents part of a special
5825 * kind of "set membership match". Such a flow should not actually
5826 * get executed, but it could via, say, a "packet-out", even though
5827 * that wouldn't be useful. Log it to help debugging. */
5828 xlate_report_error(ctx, "executing no-op conjunction action");
5829 break;
5830
5831 case OFPACT_EXIT:
5832 ctx->exit = true;
5833 break;
5834
5835 case OFPACT_UNROLL_XLATE:
5836 xlate_ofpact_unroll_xlate(ctx, ofpact_get_UNROLL_XLATE(a));
5837 break;
5838
5839 case OFPACT_FIN_TIMEOUT:
5840 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
5841 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
5842 break;
5843
5844 case OFPACT_CLEAR_ACTIONS:
5845 xlate_report_action_set(ctx, "was");
5846 ofpbuf_clear(&ctx->action_set);
5847 ctx->xin->flow.actset_output = OFPP_UNSET;
5848 ctx->action_set_has_group = false;
5849 break;
5850
5851 case OFPACT_WRITE_ACTIONS:
5852 xlate_write_actions(ctx, ofpact_get_WRITE_ACTIONS(a));
5853 xlate_report_action_set(ctx, "is");
5854 break;
5855
5856 case OFPACT_WRITE_METADATA:
5857 metadata = ofpact_get_WRITE_METADATA(a);
5858 flow->metadata &= ~metadata->mask;
5859 flow->metadata |= metadata->metadata & metadata->mask;
5860 break;
5861
5862 case OFPACT_METER:
5863 xlate_meter_action(ctx, ofpact_get_METER(a));
5864 break;
5865
5866 case OFPACT_GOTO_TABLE: {
5867 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
5868
5869 ovs_assert(ctx->table_id < ogt->table_id);
5870
5871 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
5872 ogt->table_id, true, true, false);
5873 break;
5874 }
5875
5876 case OFPACT_SAMPLE:
5877 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
5878 break;
5879
5880 case OFPACT_CLONE:
5881 xlate_clone(ctx, ofpact_get_CLONE(a));
5882 break;
5883
5884 case OFPACT_CT:
5885 compose_conntrack_action(ctx, ofpact_get_CT(a));
5886 break;
5887
5888 case OFPACT_CT_CLEAR:
5889 clear_conntrack(ctx);
5890 break;
5891
5892 case OFPACT_NAT:
5893 /* This will be processed by compose_conntrack_action(). */
5894 ctx->ct_nat_action = ofpact_get_NAT(a);
5895 break;
5896
5897 case OFPACT_DEBUG_RECIRC:
5898 ctx_trigger_freeze(ctx);
5899 a = ofpact_next(a);
5900 break;
5901 }
5902
5903 /* Check if need to store this and the remaining actions for later
5904 * execution. */
5905 if (!ctx->error && ctx->exit && ctx_first_frozen_action(ctx)) {
5906 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len), ctx);
5907 break;
5908 }
5909 }
5910 }
5911
5912 void
5913 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
5914 ovs_version_t version, const struct flow *flow,
5915 ofp_port_t in_port, struct rule_dpif *rule, uint16_t tcp_flags,
5916 const struct dp_packet *packet, struct flow_wildcards *wc,
5917 struct ofpbuf *odp_actions)
5918 {
5919 xin->ofproto = ofproto;
5920 xin->tables_version = version;
5921 xin->flow = *flow;
5922 xin->upcall_flow = flow;
5923 xin->flow.in_port.ofp_port = in_port;
5924 xin->flow.actset_output = OFPP_UNSET;
5925 xin->packet = packet;
5926 xin->allow_side_effects = packet != NULL;
5927 xin->rule = rule;
5928 xin->xcache = NULL;
5929 xin->ofpacts = NULL;
5930 xin->ofpacts_len = 0;
5931 xin->tcp_flags = tcp_flags;
5932 xin->trace = NULL;
5933 xin->resubmit_stats = NULL;
5934 xin->depth = 0;
5935 xin->resubmits = 0;
5936 xin->wc = wc;
5937 xin->odp_actions = odp_actions;
5938 xin->in_packet_out = false;
5939
5940 /* Do recirc lookup. */
5941 xin->frozen_state = NULL;
5942 if (flow->recirc_id) {
5943 const struct recirc_id_node *node
5944 = recirc_id_node_find(flow->recirc_id);
5945 if (node) {
5946 xin->frozen_state = &node->state;
5947 }
5948 }
5949 }
5950
5951 void
5952 xlate_out_uninit(struct xlate_out *xout)
5953 {
5954 if (xout) {
5955 recirc_refs_unref(&xout->recircs);
5956 }
5957 }
5958 \f
5959 static struct skb_priority_to_dscp *
5960 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
5961 {
5962 struct skb_priority_to_dscp *pdscp;
5963 uint32_t hash;
5964
5965 hash = hash_int(skb_priority, 0);
5966 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
5967 if (pdscp->skb_priority == skb_priority) {
5968 return pdscp;
5969 }
5970 }
5971 return NULL;
5972 }
5973
5974 static bool
5975 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
5976 uint8_t *dscp)
5977 {
5978 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
5979 *dscp = pdscp ? pdscp->dscp : 0;
5980 return pdscp != NULL;
5981 }
5982
5983 static size_t
5984 count_skb_priorities(const struct xport *xport)
5985 {
5986 return hmap_count(&xport->skb_priorities);
5987 }
5988
5989 static void
5990 clear_skb_priorities(struct xport *xport)
5991 {
5992 struct skb_priority_to_dscp *pdscp;
5993
5994 HMAP_FOR_EACH_POP (pdscp, hmap_node, &xport->skb_priorities) {
5995 free(pdscp);
5996 }
5997 }
5998
5999 static bool
6000 actions_output_to_local_port(const struct xlate_ctx *ctx)
6001 {
6002 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
6003 const struct nlattr *a;
6004 unsigned int left;
6005
6006 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
6007 ctx->odp_actions->size) {
6008 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
6009 && nl_attr_get_odp_port(a) == local_odp_port) {
6010 return true;
6011 }
6012 }
6013 return false;
6014 }
6015
6016 #if defined(__linux__)
6017 /* Returns the maximum number of packets that the Linux kernel is willing to
6018 * queue up internally to certain kinds of software-implemented ports, or the
6019 * default (and rarely modified) value if it cannot be determined. */
6020 static int
6021 netdev_max_backlog(void)
6022 {
6023 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6024 static int max_backlog = 1000; /* The normal default value. */
6025
6026 if (ovsthread_once_start(&once)) {
6027 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
6028 FILE *stream;
6029 int n;
6030
6031 stream = fopen(filename, "r");
6032 if (!stream) {
6033 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
6034 } else {
6035 if (fscanf(stream, "%d", &n) != 1) {
6036 VLOG_WARN("%s: read error", filename);
6037 } else if (n <= 100) {
6038 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
6039 } else {
6040 max_backlog = n;
6041 }
6042 fclose(stream);
6043 }
6044 ovsthread_once_done(&once);
6045
6046 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
6047 }
6048
6049 return max_backlog;
6050 }
6051
6052 /* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
6053 * 'odp_actions'. */
6054 static int
6055 count_output_actions(const struct ofpbuf *odp_actions)
6056 {
6057 const struct nlattr *a;
6058 size_t left;
6059 int n = 0;
6060
6061 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
6062 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
6063 n++;
6064 }
6065 }
6066 return n;
6067 }
6068 #endif /* defined(__linux__) */
6069
6070 /* Returns true if 'odp_actions' contains more output actions than the datapath
6071 * can reliably handle in one go. On Linux, this is the value of the
6072 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
6073 * packets that the kernel is willing to queue up for processing while the
6074 * datapath is processing a set of actions. */
6075 static bool
6076 too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
6077 {
6078 #ifdef __linux__
6079 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
6080 && count_output_actions(odp_actions) > netdev_max_backlog());
6081 #else
6082 /* OSes other than Linux might have similar limits, but we don't know how
6083 * to determine them.*/
6084 return false;
6085 #endif
6086 }
6087
6088 static void
6089 xlate_wc_init(struct xlate_ctx *ctx)
6090 {
6091 flow_wildcards_init_catchall(ctx->wc);
6092
6093 /* Some fields we consider to always be examined. */
6094 WC_MASK_FIELD(ctx->wc, in_port);
6095 WC_MASK_FIELD(ctx->wc, dl_type);
6096 if (is_ip_any(&ctx->xin->flow)) {
6097 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
6098 }
6099
6100 if (ctx->xbridge->support.odp.recirc) {
6101 /* Always exactly match recirc_id when datapath supports
6102 * recirculation. */
6103 WC_MASK_FIELD(ctx->wc, recirc_id);
6104 }
6105
6106 if (ctx->xbridge->netflow) {
6107 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
6108 }
6109
6110 tnl_wc_init(&ctx->xin->flow, ctx->wc);
6111 }
6112
6113 static void
6114 xlate_wc_finish(struct xlate_ctx *ctx)
6115 {
6116 int i;
6117
6118 /* Clear the metadata and register wildcard masks, because we won't
6119 * use non-header fields as part of the cache. */
6120 flow_wildcards_clear_non_packet_fields(ctx->wc);
6121
6122 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
6123 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
6124 * represent these fields. The datapath interface, on the other hand,
6125 * represents them with just 8 bits each. This means that if the high
6126 * 8 bits of the masks for these fields somehow become set, then they
6127 * will get chopped off by a round trip through the datapath, and
6128 * revalidation will spot that as an inconsistency and delete the flow.
6129 * Avoid the problem here by making sure that only the low 8 bits of
6130 * either field can be unwildcarded for ICMP.
6131 */
6132 if (is_icmpv4(&ctx->xin->flow, NULL) || is_icmpv6(&ctx->xin->flow, NULL)) {
6133 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
6134 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
6135 }
6136 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
6137 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
6138 if (ctx->wc->masks.vlans[i].tci) {
6139 ctx->wc->masks.vlans[i].tci |= htons(VLAN_CFI);
6140 }
6141 }
6142
6143 /* The classifier might return masks that match on tp_src and tp_dst even
6144 * for later fragments. This happens because there might be flows that
6145 * match on tp_src or tp_dst without matching on the frag bits, because
6146 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
6147 * datapath flows and since tp_src and tp_dst are always going to be 0,
6148 * wildcard the fields here. */
6149 if (ctx->xin->flow.nw_frag & FLOW_NW_FRAG_LATER) {
6150 ctx->wc->masks.tp_src = 0;
6151 ctx->wc->masks.tp_dst = 0;
6152 }
6153 }
6154
6155 /* Translates the flow, actions, or rule in 'xin' into datapath actions in
6156 * 'xout'.
6157 * The caller must take responsibility for eventually freeing 'xout', with
6158 * xlate_out_uninit().
6159 * Returns 'XLATE_OK' if translation was successful. In case of an error an
6160 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
6161 * so that most callers may ignore the return value and transparently install a
6162 * drop flow when the translation fails. */
6163 enum xlate_error
6164 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
6165 {
6166 *xout = (struct xlate_out) {
6167 .slow = 0,
6168 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
6169 };
6170
6171 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6172 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
6173 if (!xbridge) {
6174 return XLATE_BRIDGE_NOT_FOUND;
6175 }
6176
6177 struct flow *flow = &xin->flow;
6178
6179 uint8_t stack_stub[1024];
6180 uint64_t action_set_stub[1024 / 8];
6181 uint64_t frozen_actions_stub[1024 / 8];
6182 uint64_t actions_stub[256 / 8];
6183 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
6184 struct xlate_ctx ctx = {
6185 .xin = xin,
6186 .xout = xout,
6187 .base_flow = *flow,
6188 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
6189 .xbridge = xbridge,
6190 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
6191 .rule = xin->rule,
6192 .wc = (xin->wc
6193 ? xin->wc
6194 : &(struct flow_wildcards) { .masks = { .dl_type = 0 } }),
6195 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
6196
6197 .depth = xin->depth,
6198 .resubmits = xin->resubmits,
6199 .in_group = false,
6200 .in_action_set = false,
6201 .in_packet_out = xin->in_packet_out,
6202
6203 .table_id = 0,
6204 .rule_cookie = OVS_BE64_MAX,
6205 .orig_skb_priority = flow->skb_priority,
6206 .sflow_n_outputs = 0,
6207 .sflow_odp_port = 0,
6208 .nf_output_iface = NF_OUT_DROP,
6209 .exit = false,
6210 .error = XLATE_OK,
6211 .mirrors = 0,
6212
6213 .freezing = false,
6214 .recirc_update_dp_hash = false,
6215 .frozen_actions = OFPBUF_STUB_INITIALIZER(frozen_actions_stub),
6216 .pause = NULL,
6217
6218 .was_mpls = false,
6219 .conntracked = false,
6220
6221 .ct_nat_action = NULL,
6222
6223 .action_set_has_group = false,
6224 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
6225 };
6226
6227 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
6228 * the packet as the datapath will treat it for output actions. Our
6229 * datapath doesn't retain tunneling information without us re-setting
6230 * it, so clear the tunnel data.
6231 */
6232
6233 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
6234
6235 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
6236 xlate_wc_init(&ctx);
6237
6238 COVERAGE_INC(xlate_actions);
6239
6240 xin->trace = xlate_report(&ctx, OFT_BRIDGE, "bridge(\"%s\")",
6241 xbridge->name);
6242 if (xin->frozen_state) {
6243 const struct frozen_state *state = xin->frozen_state;
6244
6245 struct ovs_list *old_trace = xin->trace;
6246 xin->trace = xlate_report(&ctx, OFT_THAW, "thaw");
6247
6248 if (xin->ofpacts_len > 0 || ctx.rule) {
6249 xlate_report_error(&ctx, "Recirculation conflict (%s)!",
6250 xin->ofpacts_len ? "actions" : "rule");
6251 ctx.error = XLATE_RECIRCULATION_CONFLICT;
6252 goto exit;
6253 }
6254
6255 /* Set the bridge for post-recirculation processing if needed. */
6256 if (!uuid_equals(&ctx.xbridge->ofproto->uuid, &state->ofproto_uuid)) {
6257 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6258 const struct xbridge *new_bridge
6259 = xbridge_lookup_by_uuid(xcfg, &state->ofproto_uuid);
6260
6261 if (OVS_UNLIKELY(!new_bridge)) {
6262 /* Drop the packet if the bridge cannot be found. */
6263 xlate_report_error(&ctx, "Frozen bridge no longer exists.");
6264 ctx.error = XLATE_BRIDGE_NOT_FOUND;
6265 xin->trace = old_trace;
6266 goto exit;
6267 }
6268 ctx.xbridge = new_bridge;
6269 /* The bridge is now known so obtain its table version. */
6270 ctx.xin->tables_version
6271 = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
6272 }
6273
6274 /* Set the thawed table id. Note: A table lookup is done only if there
6275 * are no frozen actions. */
6276 ctx.table_id = state->table_id;
6277 xlate_report(&ctx, OFT_THAW,
6278 "Resuming from table %"PRIu8, ctx.table_id);
6279
6280 if (!state->conntracked) {
6281 clear_conntrack(&ctx);
6282 }
6283
6284 /* Restore pipeline metadata. May change flow's in_port and other
6285 * metadata to the values that existed when freezing was triggered. */
6286 frozen_metadata_to_flow(&state->metadata, flow);
6287
6288 /* Restore stack, if any. */
6289 if (state->stack) {
6290 ofpbuf_put(&ctx.stack, state->stack, state->stack_size);
6291 }
6292
6293 /* Restore mirror state. */
6294 ctx.mirrors = state->mirrors;
6295
6296 /* Restore action set, if any. */
6297 if (state->action_set_len) {
6298 xlate_report_actions(&ctx, OFT_THAW, "Restoring action set",
6299 state->action_set, state->action_set_len);
6300
6301 flow->actset_output = OFPP_UNSET;
6302 xlate_write_actions__(&ctx, state->action_set,
6303 state->action_set_len);
6304 }
6305
6306 /* Restore frozen actions. If there are no actions, processing will
6307 * start with a lookup in the table set above. */
6308 xin->ofpacts = state->ofpacts;
6309 xin->ofpacts_len = state->ofpacts_len;
6310 if (state->ofpacts_len) {
6311 xlate_report_actions(&ctx, OFT_THAW, "Restoring actions",
6312 xin->ofpacts, xin->ofpacts_len);
6313 }
6314
6315 xin->trace = old_trace;
6316 } else if (OVS_UNLIKELY(flow->recirc_id)) {
6317 xlate_report_error(&ctx,
6318 "Recirculation context not found for ID %"PRIx32,
6319 flow->recirc_id);
6320 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
6321 goto exit;
6322 }
6323
6324 /* Tunnel metadata in udpif format must be normalized before translation. */
6325 if (flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
6326 const struct tun_table *tun_tab = ofproto_get_tun_tab(
6327 &ctx.xbridge->ofproto->up);
6328 int err;
6329
6330 err = tun_metadata_from_geneve_udpif(tun_tab, &xin->upcall_flow->tunnel,
6331 &xin->upcall_flow->tunnel,
6332 &flow->tunnel);
6333 if (err) {
6334 xlate_report_error(&ctx, "Invalid Geneve tunnel metadata");
6335 ctx.error = XLATE_INVALID_TUNNEL_METADATA;
6336 goto exit;
6337 }
6338 } else if (!flow->tunnel.metadata.tab) {
6339 /* If the original flow did not come in on a tunnel, then it won't have
6340 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
6341 * table in case we generate tunnel actions. */
6342 flow->tunnel.metadata.tab = ofproto_get_tun_tab(
6343 &ctx.xbridge->ofproto->up);
6344 }
6345 ctx.wc->masks.tunnel.metadata.tab = flow->tunnel.metadata.tab;
6346
6347 if (!xin->ofpacts && !ctx.rule) {
6348 ctx.rule = rule_dpif_lookup_from_table(
6349 ctx.xbridge->ofproto, ctx.xin->tables_version, flow, ctx.wc,
6350 ctx.xin->resubmit_stats, &ctx.table_id,
6351 flow->in_port.ofp_port, true, true, ctx.xin->xcache);
6352 if (ctx.xin->resubmit_stats) {
6353 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
6354 }
6355 if (ctx.xin->xcache) {
6356 struct xc_entry *entry;
6357
6358 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
6359 entry->rule = ctx.rule;
6360 ofproto_rule_ref(&ctx.rule->up);
6361 }
6362
6363 xlate_report_table(&ctx, ctx.rule, ctx.table_id);
6364 }
6365
6366 /* Get the proximate input port of the packet. (If xin->frozen_state,
6367 * flow->in_port is the ultimate input port of the packet.) */
6368 struct xport *in_port = get_ofp_port(xbridge,
6369 ctx.base_flow.in_port.ofp_port);
6370
6371 /* Tunnel stats only for not-thawed packets. */
6372 if (!xin->frozen_state && in_port && in_port->is_tunnel) {
6373 if (ctx.xin->resubmit_stats) {
6374 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
6375 if (in_port->bfd) {
6376 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
6377 }
6378 }
6379 if (ctx.xin->xcache) {
6380 struct xc_entry *entry;
6381
6382 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
6383 entry->dev.rx = netdev_ref(in_port->netdev);
6384 entry->dev.bfd = bfd_ref(in_port->bfd);
6385 }
6386 }
6387
6388 if (!xin->frozen_state && process_special(&ctx, in_port)) {
6389 /* process_special() did all the processing for this packet.
6390 *
6391 * We do not perform special processing on thawed packets, since that
6392 * was done before they were frozen and should not be redone. */
6393 } else if (in_port && in_port->xbundle
6394 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
6395 xlate_report_error(&ctx, "dropping packet received on port "
6396 "%s, which is reserved exclusively for mirroring",
6397 in_port->xbundle->name);
6398 } else {
6399 /* Sampling is done on initial reception; don't redo after thawing. */
6400 unsigned int user_cookie_offset = 0;
6401 if (!xin->frozen_state) {
6402 user_cookie_offset = compose_sflow_action(&ctx);
6403 compose_ipfix_action(&ctx, ODPP_NONE);
6404 }
6405 size_t sample_actions_len = ctx.odp_actions->size;
6406
6407 if (tnl_process_ecn(flow)
6408 && (!in_port || may_receive(in_port, &ctx))) {
6409 const struct ofpact *ofpacts;
6410 size_t ofpacts_len;
6411
6412 if (xin->ofpacts) {
6413 ofpacts = xin->ofpacts;
6414 ofpacts_len = xin->ofpacts_len;
6415 } else if (ctx.rule) {
6416 const struct rule_actions *actions
6417 = rule_get_actions(&ctx.rule->up);
6418 ofpacts = actions->ofpacts;
6419 ofpacts_len = actions->ofpacts_len;
6420 ctx.rule_cookie = ctx.rule->up.flow_cookie;
6421 } else {
6422 OVS_NOT_REACHED();
6423 }
6424
6425 mirror_ingress_packet(&ctx);
6426 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
6427 if (ctx.error) {
6428 goto exit;
6429 }
6430
6431 /* We've let OFPP_NORMAL and the learning action look at the
6432 * packet, so cancel all actions and freezing if forwarding is
6433 * disabled. */
6434 if (in_port && (!xport_stp_forward_state(in_port) ||
6435 !xport_rstp_forward_state(in_port))) {
6436 ctx.odp_actions->size = sample_actions_len;
6437 ctx_cancel_freeze(&ctx);
6438 ofpbuf_clear(&ctx.action_set);
6439 }
6440
6441 if (!ctx.freezing) {
6442 xlate_action_set(&ctx);
6443 }
6444 if (ctx.freezing) {
6445 finish_freezing(&ctx);
6446 }
6447 }
6448
6449 /* Output only fully processed packets. */
6450 if (!ctx.freezing
6451 && xbridge->has_in_band
6452 && in_band_must_output_to_local_port(flow)
6453 && !actions_output_to_local_port(&ctx)) {
6454 compose_output_action(&ctx, OFPP_LOCAL, NULL);
6455 }
6456
6457 if (user_cookie_offset) {
6458 fix_sflow_action(&ctx, user_cookie_offset);
6459 }
6460 }
6461
6462 if (nl_attr_oversized(ctx.odp_actions->size)) {
6463 /* These datapath actions are too big for a Netlink attribute, so we
6464 * can't hand them to the kernel directly. dpif_execute() can execute
6465 * them one by one with help, so just mark the result as SLOW_ACTION to
6466 * prevent the flow from being installed. */
6467 COVERAGE_INC(xlate_actions_oversize);
6468 ctx.xout->slow |= SLOW_ACTION;
6469 } else if (too_many_output_actions(ctx.odp_actions)) {
6470 COVERAGE_INC(xlate_actions_too_many_output);
6471 ctx.xout->slow |= SLOW_ACTION;
6472 }
6473
6474 /* Update NetFlow for non-frozen traffic. */
6475 if (xbridge->netflow && !xin->frozen_state) {
6476 if (ctx.xin->resubmit_stats) {
6477 netflow_flow_update(xbridge->netflow, flow,
6478 ctx.nf_output_iface,
6479 ctx.xin->resubmit_stats);
6480 }
6481 if (ctx.xin->xcache) {
6482 struct xc_entry *entry;
6483
6484 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
6485 entry->nf.netflow = netflow_ref(xbridge->netflow);
6486 entry->nf.flow = xmemdup(flow, sizeof *flow);
6487 entry->nf.iface = ctx.nf_output_iface;
6488 }
6489 }
6490
6491 /* Translate tunnel metadata masks to udpif format if necessary. */
6492 if (xin->upcall_flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
6493 if (ctx.wc->masks.tunnel.metadata.present.map) {
6494 const struct flow_tnl *upcall_tnl = &xin->upcall_flow->tunnel;
6495 struct geneve_opt opts[TLV_TOT_OPT_SIZE /
6496 sizeof(struct geneve_opt)];
6497
6498 tun_metadata_to_geneve_udpif_mask(&flow->tunnel,
6499 &ctx.wc->masks.tunnel,
6500 upcall_tnl->metadata.opts.gnv,
6501 upcall_tnl->metadata.present.len,
6502 opts);
6503 memset(&ctx.wc->masks.tunnel.metadata, 0,
6504 sizeof ctx.wc->masks.tunnel.metadata);
6505 memcpy(&ctx.wc->masks.tunnel.metadata.opts.gnv, opts,
6506 upcall_tnl->metadata.present.len);
6507 }
6508 ctx.wc->masks.tunnel.metadata.present.len = 0xff;
6509 ctx.wc->masks.tunnel.metadata.tab = NULL;
6510 ctx.wc->masks.tunnel.flags |= FLOW_TNL_F_UDPIF;
6511 } else if (!xin->upcall_flow->tunnel.metadata.tab) {
6512 /* If we didn't have options in UDPIF format and didn't have an existing
6513 * metadata table, then it means that there were no options at all when
6514 * we started processing and any wildcards we picked up were from
6515 * action generation. Without options on the incoming packet, wildcards
6516 * aren't meaningful. To avoid them possibly getting misinterpreted,
6517 * just clear everything. */
6518 if (ctx.wc->masks.tunnel.metadata.present.map) {
6519 memset(&ctx.wc->masks.tunnel.metadata, 0,
6520 sizeof ctx.wc->masks.tunnel.metadata);
6521 } else {
6522 ctx.wc->masks.tunnel.metadata.tab = NULL;
6523 }
6524 }
6525
6526 xlate_wc_finish(&ctx);
6527
6528 exit:
6529 /* Reset the table to what it was when we came in. If we only fetched
6530 * it locally, then it has no meaning outside of flow translation. */
6531 flow->tunnel.metadata.tab = xin->upcall_flow->tunnel.metadata.tab;
6532
6533 ofpbuf_uninit(&ctx.stack);
6534 ofpbuf_uninit(&ctx.action_set);
6535 ofpbuf_uninit(&ctx.frozen_actions);
6536 ofpbuf_uninit(&scratch_actions);
6537
6538 /* Make sure we return a "drop flow" in case of an error. */
6539 if (ctx.error) {
6540 xout->slow = 0;
6541 if (xin->odp_actions) {
6542 ofpbuf_clear(xin->odp_actions);
6543 }
6544 }
6545 return ctx.error;
6546 }
6547
6548 enum ofperr
6549 xlate_resume(struct ofproto_dpif *ofproto,
6550 const struct ofputil_packet_in_private *pin,
6551 struct ofpbuf *odp_actions,
6552 enum slow_path_reason *slow)
6553 {
6554 struct dp_packet packet;
6555 dp_packet_use_const(&packet, pin->public.packet,
6556 pin->public.packet_len);
6557
6558 struct flow flow;
6559 flow_extract(&packet, &flow);
6560
6561 struct xlate_in xin;
6562 xlate_in_init(&xin, ofproto, ofproto_dpif_get_tables_version(ofproto),
6563 &flow, 0, NULL, ntohs(flow.tcp_flags),
6564 &packet, NULL, odp_actions);
6565
6566 struct ofpact_note noop;
6567 ofpact_init_NOTE(&noop);
6568 noop.length = 0;
6569
6570 bool any_actions = pin->actions_len > 0;
6571 struct frozen_state state = {
6572 .table_id = 0, /* Not the table where NXAST_PAUSE was executed. */
6573 .ofproto_uuid = pin->bridge,
6574 .stack = pin->stack,
6575 .stack_size = pin->stack_size,
6576 .mirrors = pin->mirrors,
6577 .conntracked = pin->conntracked,
6578
6579 /* When there are no actions, xlate_actions() will search the flow
6580 * table. We don't want it to do that (we want it to resume), so
6581 * supply a no-op action if there aren't any.
6582 *
6583 * (We can't necessarily avoid translating actions entirely if there
6584 * aren't any actions, because there might be some finishing-up to do
6585 * at the end of the pipeline, and we don't check for those
6586 * conditions.) */
6587 .ofpacts = any_actions ? pin->actions : &noop.ofpact,
6588 .ofpacts_len = any_actions ? pin->actions_len : sizeof noop,
6589
6590 .action_set = pin->action_set,
6591 .action_set_len = pin->action_set_len,
6592 };
6593 frozen_metadata_from_flow(&state.metadata,
6594 &pin->public.flow_metadata.flow);
6595 xin.frozen_state = &state;
6596
6597 struct xlate_out xout;
6598 enum xlate_error error = xlate_actions(&xin, &xout);
6599 *slow = xout.slow;
6600 xlate_out_uninit(&xout);
6601
6602 /* xlate_actions() can generate a number of errors, but only
6603 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
6604 * sure to report over OpenFlow. The others could come up in packet-outs
6605 * or regular flow translation and I don't think that it's going to be too
6606 * useful to report them to the controller. */
6607 return error == XLATE_BRIDGE_NOT_FOUND ? OFPERR_NXR_STALE : 0;
6608 }
6609
6610 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
6611 * supports a notion of an OAM flag, sets it if 'oam' is true.
6612 * May modify 'packet'.
6613 * Returns 0 if successful, otherwise a positive errno value. */
6614 int
6615 xlate_send_packet(const struct ofport_dpif *ofport, bool oam,
6616 struct dp_packet *packet)
6617 {
6618 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6619 struct xport *xport;
6620 uint64_t ofpacts_stub[1024 / 8];
6621 struct ofpbuf ofpacts;
6622 struct flow flow;
6623
6624 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
6625 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
6626 flow_extract(packet, &flow);
6627 flow.in_port.ofp_port = OFPP_NONE;
6628
6629 xport = xport_lookup(xcfg, ofport);
6630 if (!xport) {
6631 return EINVAL;
6632 }
6633
6634 if (oam) {
6635 const ovs_be16 oam = htons(NX_TUN_FLAG_OAM);
6636 ofpact_put_set_field(&ofpacts, mf_from_id(MFF_TUN_FLAGS), &oam, &oam);
6637 }
6638
6639 ofpact_put_OUTPUT(&ofpacts)->port = xport->ofp_port;
6640
6641 /* Actions here are not referring to anything versionable (flow tables or
6642 * groups) so we don't need to worry about the version here. */
6643 return ofproto_dpif_execute_actions(xport->xbridge->ofproto,
6644 OVS_VERSION_MAX, &flow, NULL,
6645 ofpacts.data, ofpacts.size, packet);
6646 }
6647
6648 void
6649 xlate_mac_learning_update(const struct ofproto_dpif *ofproto,
6650 ofp_port_t in_port, struct eth_addr dl_src,
6651 int vlan, bool is_grat_arp)
6652 {
6653 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6654 struct xbridge *xbridge;
6655 struct xbundle *xbundle;
6656
6657 xbridge = xbridge_lookup(xcfg, ofproto);
6658 if (!xbridge) {
6659 return;
6660 }
6661
6662 xbundle = lookup_input_bundle__(xbridge, in_port, NULL);
6663 if (!xbundle) {
6664 return;
6665 }
6666
6667 update_learning_table__(xbridge, xbundle, dl_src, vlan, is_grat_arp);
6668 }
6669
6670 void
6671 xlate_disable_dp_clone(const struct ofproto_dpif *ofproto)
6672 {
6673 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6674 struct xbridge *xbridge = xbridge_lookup(xcfg, ofproto);
6675
6676 if (xbridge) {
6677 xbridge->support.clone = false;
6678 }
6679 }