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