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