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