1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-sflow.h"
46 #include "ofproto/ofproto-dpif.h"
47 #include "ofproto/ofproto-provider.h"
51 COVERAGE_DEFINE(xlate_actions);
52 COVERAGE_DEFINE(xlate_actions_oversize);
54 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
56 /* Maximum depth of flow table recursion (due to resubmit actions) in a
57 * flow translation. */
58 #define MAX_RESUBMIT_RECURSION 64
60 /* Maximum number of resubmit actions in a flow translation, whether they are
61 * recursive or not. */
62 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
64 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
67 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
68 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
70 struct list xbundles; /* Owned xbundles. */
71 struct hmap xports; /* Indexed by ofp_port. */
73 char *name; /* Name used in log messages. */
74 struct dpif *dpif; /* Datapath interface. */
75 struct mac_learning *ml; /* Mac learning handle. */
76 struct mbridge *mbridge; /* Mirroring. */
77 struct dpif_sflow *sflow; /* SFlow handle, or null. */
78 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
79 struct stp *stp; /* STP or null if disabled. */
81 /* Special rules installed by ofproto-dpif. */
82 struct rule_dpif *miss_rule;
83 struct rule_dpif *no_packet_in_rule;
85 enum ofp_config_flags frag; /* Fragmentation handling. */
86 bool has_netflow; /* Bridge runs netflow? */
87 bool has_in_band; /* Bridge has in band control? */
88 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
92 struct hmap_node hmap_node; /* In global 'xbundles' map. */
93 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
95 struct list list_node; /* In parent 'xbridges' list. */
96 struct xbridge *xbridge; /* Parent xbridge. */
98 struct list xports; /* Contains "struct xport"s. */
100 char *name; /* Name used in log messages. */
101 struct bond *bond; /* Nonnull iff more than one port. */
102 struct lacp *lacp; /* LACP handle or null. */
104 enum port_vlan_mode vlan_mode; /* VLAN mode. */
105 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
106 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
107 * NULL if all VLANs are trunked. */
108 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
109 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
113 struct hmap_node hmap_node; /* Node in global 'xports' map. */
114 struct ofport_dpif *ofport; /* Key in global 'xports map. */
116 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
117 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
119 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
121 struct list bundle_node; /* In parent xbundle (if it exists). */
122 struct xbundle *xbundle; /* Parent xbundle or null. */
124 struct netdev *netdev; /* 'ofport''s netdev. */
126 struct xbridge *xbridge; /* Parent bridge. */
127 struct xport *peer; /* Patch port peer or null. */
129 enum ofputil_port_config config; /* OpenFlow port configuration. */
130 enum ofputil_port_state state; /* OpenFlow port state. */
131 int stp_port_no; /* STP port number or -1 if not in use. */
133 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
135 bool may_enable; /* May be enabled in bonds. */
136 bool is_tunnel; /* Is a tunnel port. */
138 struct cfm *cfm; /* CFM handle or null. */
139 struct bfd *bfd; /* BFD handle or null. */
143 struct xlate_in *xin;
144 struct xlate_out *xout;
146 const struct xbridge *xbridge;
148 /* Flow at the last commit. */
149 struct flow base_flow;
151 /* Tunnel IP destination address as received. This is stored separately
152 * as the base_flow.tunnel is cleared on init to reflect the datapath
153 * behavior. Used to make sure not to send tunneled output to ourselves,
154 * which might lead to an infinite loop. This could happen easily
155 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
156 * actually set the tun_dst field. */
157 ovs_be32 orig_tunnel_ip_dst;
159 /* Stack for the push and pop actions. Each stack element is of type
160 * "union mf_subvalue". */
161 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
164 /* The rule that we are currently translating, or NULL. */
165 struct rule_dpif *rule;
167 int mpls_depth_delta; /* Delta of the mpls stack depth since
168 * actions were last committed.
169 * Must be between -1 and 1 inclusive. */
170 ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE
171 * prior to an mpls_push so that it may be
172 * used for a subsequent mpls_pop. */
174 /* Resubmit statistics, via xlate_table_action(). */
175 int recurse; /* Current resubmit nesting depth. */
176 int resubmits; /* Total number of resubmits. */
178 uint32_t orig_skb_priority; /* Priority when packet arrived. */
179 uint8_t table_id; /* OpenFlow table ID where flow was found. */
180 uint32_t sflow_n_outputs; /* Number of output ports. */
181 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
182 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
183 bool exit; /* No further actions should be processed. */
185 /* OpenFlow 1.1+ action set.
187 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
188 * When translation is otherwise complete, ofpacts_execute_action_set()
189 * converts it to a set of "struct ofpact"s that can be translated into
190 * datapath actions. */
191 struct ofpbuf action_set; /* Action set. */
192 uint64_t action_set_stub[1024 / 8];
195 /* A controller may use OFPP_NONE as the ingress port to indicate that
196 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
197 * when an input bundle is needed for validation (e.g., mirroring or
198 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
199 * any 'port' structs, so care must be taken when dealing with it.
200 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
201 static struct xbundle ofpp_none_bundle;
203 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
204 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
205 * traffic egressing the 'ofport' with that priority should be marked with. */
206 struct skb_priority_to_dscp {
207 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
208 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
210 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
213 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
214 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
215 static struct hmap xports = HMAP_INITIALIZER(&xports);
217 static bool may_receive(const struct xport *, struct xlate_ctx *);
218 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
220 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
221 OVS_REQ_RDLOCK(xlate_rwlock);
222 static void xlate_normal(struct xlate_ctx *);
223 static void xlate_report(struct xlate_ctx *, const char *);
224 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
225 uint8_t table_id, bool may_packet_in);
226 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
227 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
228 static void output_normal(struct xlate_ctx *, const struct xbundle *,
230 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
232 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
233 static struct xbundle *xbundle_lookup(const struct ofbundle *);
234 static struct xport *xport_lookup(const struct ofport_dpif *);
235 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
236 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
237 uint32_t skb_priority);
238 static void clear_skb_priorities(struct xport *);
239 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
243 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
244 struct dpif *dpif, struct rule_dpif *miss_rule,
245 struct rule_dpif *no_packet_in_rule,
246 const struct mac_learning *ml, struct stp *stp,
247 const struct mbridge *mbridge,
248 const struct dpif_sflow *sflow,
249 const struct dpif_ipfix *ipfix, enum ofp_config_flags frag,
250 bool forward_bpdu, bool has_in_band, bool has_netflow)
252 struct xbridge *xbridge = xbridge_lookup(ofproto);
255 xbridge = xzalloc(sizeof *xbridge);
256 xbridge->ofproto = ofproto;
258 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
259 hmap_init(&xbridge->xports);
260 list_init(&xbridge->xbundles);
263 if (xbridge->ml != ml) {
264 mac_learning_unref(xbridge->ml);
265 xbridge->ml = mac_learning_ref(ml);
268 if (xbridge->mbridge != mbridge) {
269 mbridge_unref(xbridge->mbridge);
270 xbridge->mbridge = mbridge_ref(mbridge);
273 if (xbridge->sflow != sflow) {
274 dpif_sflow_unref(xbridge->sflow);
275 xbridge->sflow = dpif_sflow_ref(sflow);
278 if (xbridge->ipfix != ipfix) {
279 dpif_ipfix_unref(xbridge->ipfix);
280 xbridge->ipfix = dpif_ipfix_ref(ipfix);
283 if (xbridge->stp != stp) {
284 stp_unref(xbridge->stp);
285 xbridge->stp = stp_ref(stp);
289 xbridge->name = xstrdup(name);
291 xbridge->dpif = dpif;
292 xbridge->forward_bpdu = forward_bpdu;
293 xbridge->has_in_band = has_in_band;
294 xbridge->has_netflow = has_netflow;
295 xbridge->frag = frag;
296 xbridge->miss_rule = miss_rule;
297 xbridge->no_packet_in_rule = no_packet_in_rule;
301 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
303 struct xbridge *xbridge = xbridge_lookup(ofproto);
304 struct xbundle *xbundle, *next_xbundle;
305 struct xport *xport, *next_xport;
311 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
312 xlate_ofport_remove(xport->ofport);
315 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
316 xlate_bundle_remove(xbundle->ofbundle);
319 hmap_remove(&xbridges, &xbridge->hmap_node);
320 mac_learning_unref(xbridge->ml);
321 mbridge_unref(xbridge->mbridge);
322 dpif_sflow_unref(xbridge->sflow);
323 dpif_ipfix_unref(xbridge->ipfix);
324 stp_unref(xbridge->stp);
325 hmap_destroy(&xbridge->xports);
331 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
332 const char *name, enum port_vlan_mode vlan_mode, int vlan,
333 unsigned long *trunks, bool use_priority_tags,
334 const struct bond *bond, const struct lacp *lacp,
337 struct xbundle *xbundle = xbundle_lookup(ofbundle);
340 xbundle = xzalloc(sizeof *xbundle);
341 xbundle->ofbundle = ofbundle;
342 xbundle->xbridge = xbridge_lookup(ofproto);
344 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
345 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
346 list_init(&xbundle->xports);
349 ovs_assert(xbundle->xbridge);
352 xbundle->name = xstrdup(name);
354 xbundle->vlan_mode = vlan_mode;
355 xbundle->vlan = vlan;
356 xbundle->trunks = trunks;
357 xbundle->use_priority_tags = use_priority_tags;
358 xbundle->floodable = floodable;
360 if (xbundle->bond != bond) {
361 bond_unref(xbundle->bond);
362 xbundle->bond = bond_ref(bond);
365 if (xbundle->lacp != lacp) {
366 lacp_unref(xbundle->lacp);
367 xbundle->lacp = lacp_ref(lacp);
372 xlate_bundle_remove(struct ofbundle *ofbundle)
374 struct xbundle *xbundle = xbundle_lookup(ofbundle);
375 struct xport *xport, *next;
381 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
382 list_remove(&xport->bundle_node);
383 xport->xbundle = NULL;
386 hmap_remove(&xbundles, &xbundle->hmap_node);
387 list_remove(&xbundle->list_node);
388 bond_unref(xbundle->bond);
389 lacp_unref(xbundle->lacp);
395 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
396 struct ofport_dpif *ofport, ofp_port_t ofp_port,
397 odp_port_t odp_port, const struct netdev *netdev,
398 const struct cfm *cfm, const struct bfd *bfd,
399 struct ofport_dpif *peer, int stp_port_no,
400 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
401 enum ofputil_port_config config,
402 enum ofputil_port_state state, bool is_tunnel,
405 struct xport *xport = xport_lookup(ofport);
409 xport = xzalloc(sizeof *xport);
410 xport->ofport = ofport;
411 xport->xbridge = xbridge_lookup(ofproto);
412 xport->ofp_port = ofp_port;
414 hmap_init(&xport->skb_priorities);
415 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
416 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
417 hash_ofp_port(xport->ofp_port));
420 ovs_assert(xport->ofp_port == ofp_port);
422 xport->config = config;
423 xport->state = state;
424 xport->stp_port_no = stp_port_no;
425 xport->is_tunnel = is_tunnel;
426 xport->may_enable = may_enable;
427 xport->odp_port = odp_port;
429 if (xport->netdev != netdev) {
430 netdev_close(xport->netdev);
431 xport->netdev = netdev_ref(netdev);
434 if (xport->cfm != cfm) {
435 cfm_unref(xport->cfm);
436 xport->cfm = cfm_ref(cfm);
439 if (xport->bfd != bfd) {
440 bfd_unref(xport->bfd);
441 xport->bfd = bfd_ref(bfd);
445 xport->peer->peer = NULL;
447 xport->peer = xport_lookup(peer);
449 xport->peer->peer = xport;
452 if (xport->xbundle) {
453 list_remove(&xport->bundle_node);
455 xport->xbundle = xbundle_lookup(ofbundle);
456 if (xport->xbundle) {
457 list_insert(&xport->xbundle->xports, &xport->bundle_node);
460 clear_skb_priorities(xport);
461 for (i = 0; i < n_qdscp; i++) {
462 struct skb_priority_to_dscp *pdscp;
463 uint32_t skb_priority;
465 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
470 pdscp = xmalloc(sizeof *pdscp);
471 pdscp->skb_priority = skb_priority;
472 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
473 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
474 hash_int(pdscp->skb_priority, 0));
479 xlate_ofport_remove(struct ofport_dpif *ofport)
481 struct xport *xport = xport_lookup(ofport);
488 xport->peer->peer = NULL;
492 if (xport->xbundle) {
493 list_remove(&xport->bundle_node);
496 clear_skb_priorities(xport);
497 hmap_destroy(&xport->skb_priorities);
499 hmap_remove(&xports, &xport->hmap_node);
500 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
502 netdev_close(xport->netdev);
503 cfm_unref(xport->cfm);
504 bfd_unref(xport->bfd);
508 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
509 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
510 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
511 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
512 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
513 * 'packet' ingressed.
515 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
516 * 'flow''s in_port to OFPP_NONE.
518 * This function does post-processing on data returned from
519 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
520 * of the upcall processing logic. In particular, if the extracted in_port is
521 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
522 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
523 * a VLAN header onto 'packet' (if it is nonnull).
525 * Similarly, this function also includes some logic to help with tunnels. It
526 * may modify 'flow' as necessary to make the tunneling implementation
527 * transparent to the upcall processing logic.
529 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
530 * or some other positive errno if there are other problems. */
532 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
533 const struct nlattr *key, size_t key_len,
534 struct flow *flow, enum odp_key_fitness *fitnessp,
535 struct ofproto_dpif **ofproto, odp_port_t *odp_in_port)
537 enum odp_key_fitness fitness;
538 const struct xport *xport;
541 ovs_rwlock_rdlock(&xlate_rwlock);
542 fitness = odp_flow_key_to_flow(key, key_len, flow);
543 if (fitness == ODP_FIT_ERROR) {
549 *odp_in_port = flow->in_port.odp_port;
552 xport = xport_lookup(tnl_port_should_receive(flow)
553 ? tnl_port_receive(flow)
554 : odp_port_to_ofport(backer, flow->in_port.odp_port));
556 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
561 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
563 /* Make the packet resemble the flow, so that it gets sent to
564 * an OpenFlow controller properly, so that it looks correct
565 * for sFlow, and so that flow_extract() will get the correct
566 * vlan_tci if it is called on 'packet'.
568 * The allocated space inside 'packet' probably also contains
569 * 'key', that is, both 'packet' and 'key' are probably part of
570 * a struct dpif_upcall (see the large comment on that
571 * structure definition), so pushing data on 'packet' is in
572 * general not a good idea since it could overwrite 'key' or
573 * free it as a side effect. However, it's OK in this special
574 * case because we know that 'packet' is inside a Netlink
575 * attribute: pushing 4 bytes will just overwrite the 4-byte
576 * "struct nlattr", which is fine since we don't need that
578 eth_push_vlan(packet, flow->vlan_tci);
580 /* We can't reproduce 'key' from 'flow'. */
581 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
586 *ofproto = xport->xbridge->ofproto;
593 ovs_rwlock_unlock(&xlate_rwlock);
597 static struct xbridge *
598 xbridge_lookup(const struct ofproto_dpif *ofproto)
600 struct xbridge *xbridge;
606 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
608 if (xbridge->ofproto == ofproto) {
615 static struct xbundle *
616 xbundle_lookup(const struct ofbundle *ofbundle)
618 struct xbundle *xbundle;
624 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
626 if (xbundle->ofbundle == ofbundle) {
633 static struct xport *
634 xport_lookup(const struct ofport_dpif *ofport)
642 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
644 if (xport->ofport == ofport) {
651 static struct stp_port *
652 xport_get_stp_port(const struct xport *xport)
654 return xport->xbridge->stp && xport->stp_port_no != -1
655 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
659 static enum stp_state
660 xport_stp_learn_state(const struct xport *xport)
662 struct stp_port *sp = xport_get_stp_port(xport);
663 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
667 xport_stp_forward_state(const struct xport *xport)
669 struct stp_port *sp = xport_get_stp_port(xport);
670 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
673 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
674 * were used to make the determination.*/
676 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
678 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
679 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
683 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
685 struct stp_port *sp = xport_get_stp_port(xport);
686 struct ofpbuf payload = *packet;
687 struct eth_header *eth = payload.data;
689 /* Sink packets on ports that have STP disabled when the bridge has
691 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
695 /* Trim off padding on payload. */
696 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
697 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
700 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
701 stp_received_bpdu(sp, payload.data, payload.size);
705 static struct xport *
706 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
710 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
712 if (xport->ofp_port == ofp_port) {
720 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
722 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
723 return xport ? xport->odp_port : ODPP_NONE;
727 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
731 xport = get_ofp_port(ctx->xbridge, ofp_port);
732 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
733 xport->state & OFPUTIL_PS_LINK_DOWN) {
740 static const struct ofputil_bucket *
741 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
745 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
747 struct group_dpif *group;
750 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
755 hit = group_first_live_bucket(ctx, group, depth) != NULL;
757 group_dpif_release(group);
761 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
764 bucket_is_alive(const struct xlate_ctx *ctx,
765 const struct ofputil_bucket *bucket, int depth)
767 if (depth >= MAX_LIVENESS_RECURSION) {
768 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
770 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
771 MAX_LIVENESS_RECURSION);
775 return (bucket->watch_port != OFPP_ANY &&
776 odp_port_is_alive(ctx, bucket->watch_port)) ||
777 (bucket->watch_group != OFPG_ANY &&
778 group_is_alive(ctx, bucket->watch_group, depth + 1));
781 static const struct ofputil_bucket *
782 group_first_live_bucket(const struct xlate_ctx *ctx,
783 const struct group_dpif *group, int depth)
785 struct ofputil_bucket *bucket;
786 const struct list *buckets;
788 group_dpif_get_buckets(group, &buckets);
789 LIST_FOR_EACH (bucket, list_node, buckets) {
790 if (bucket_is_alive(ctx, bucket, depth)) {
799 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
801 return (bundle->vlan_mode != PORT_VLAN_ACCESS
802 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
806 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
808 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
812 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
814 return xbundle != &ofpp_none_bundle
815 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
820 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
822 return xbundle != &ofpp_none_bundle
823 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
828 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
830 return xbundle != &ofpp_none_bundle
831 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
835 static struct xbundle *
836 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
837 bool warn, struct xport **in_xportp)
841 /* Find the port and bundle for the received packet. */
842 xport = get_ofp_port(xbridge, in_port);
846 if (xport && xport->xbundle) {
847 return xport->xbundle;
850 /* Special-case OFPP_NONE, which a controller may use as the ingress
851 * port for traffic that it is sourcing. */
852 if (in_port == OFPP_NONE) {
853 ofpp_none_bundle.name = "OFPP_NONE";
854 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
855 return &ofpp_none_bundle;
858 /* Odd. A few possible reasons here:
860 * - We deleted a port but there are still a few packets queued up
863 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
864 * we don't know about.
866 * - The ofproto client didn't configure the port as part of a bundle.
867 * This is particularly likely to happen if a packet was received on the
868 * port after it was created, but before the client had a chance to
869 * configure its bundle.
872 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
874 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
875 "port %"PRIu16, xbridge->name, in_port);
881 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
883 const struct xbridge *xbridge = ctx->xbridge;
884 mirror_mask_t mirrors;
885 struct xbundle *in_xbundle;
889 mirrors = ctx->xout->mirrors;
890 ctx->xout->mirrors = 0;
892 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
893 ctx->xin->packet != NULL, NULL);
897 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
899 /* Drop frames on bundles reserved for mirroring. */
900 if (xbundle_mirror_out(xbridge, in_xbundle)) {
901 if (ctx->xin->packet != NULL) {
902 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
903 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
904 "%s, which is reserved exclusively for mirroring",
905 ctx->xbridge->name, in_xbundle->name);
907 ofpbuf_clear(&ctx->xout->odp_actions);
912 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
913 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
916 vlan = input_vid_to_vlan(in_xbundle, vid);
922 /* Restore the original packet before adding the mirror actions. */
923 ctx->xin->flow = *orig_flow;
926 mirror_mask_t dup_mirrors;
927 struct ofbundle *out;
928 unsigned long *vlans;
933 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
934 &vlans, &dup_mirrors, &out, &out_vlan);
935 ovs_assert(has_mirror);
938 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
940 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
943 if (!vlan_mirrored) {
944 mirrors = zero_rightmost_1bit(mirrors);
948 mirrors &= ~dup_mirrors;
949 ctx->xout->mirrors |= dup_mirrors;
951 struct xbundle *out_xbundle = xbundle_lookup(out);
953 output_normal(ctx, out_xbundle, vlan);
955 } else if (vlan != out_vlan
956 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
957 struct xbundle *xbundle;
959 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
960 if (xbundle_includes_vlan(xbundle, out_vlan)
961 && !xbundle_mirror_out(xbridge, xbundle)) {
962 output_normal(ctx, xbundle, out_vlan);
969 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
970 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
971 * the bundle on which the packet was received, returns the VLAN to which the
974 * Both 'vid' and the return value are in the range 0...4095. */
976 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
978 switch (in_xbundle->vlan_mode) {
979 case PORT_VLAN_ACCESS:
980 return in_xbundle->vlan;
983 case PORT_VLAN_TRUNK:
986 case PORT_VLAN_NATIVE_UNTAGGED:
987 case PORT_VLAN_NATIVE_TAGGED:
988 return vid ? vid : in_xbundle->vlan;
995 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
996 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
999 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1000 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1003 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1005 /* Allow any VID on the OFPP_NONE port. */
1006 if (in_xbundle == &ofpp_none_bundle) {
1010 switch (in_xbundle->vlan_mode) {
1011 case PORT_VLAN_ACCESS:
1014 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1015 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1016 "packet received on port %s configured as VLAN "
1017 "%"PRIu16" access port", vid, in_xbundle->name,
1024 case PORT_VLAN_NATIVE_UNTAGGED:
1025 case PORT_VLAN_NATIVE_TAGGED:
1027 /* Port must always carry its native VLAN. */
1031 case PORT_VLAN_TRUNK:
1032 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1035 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1036 "received on port %s not configured for trunking "
1037 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1049 /* Given 'vlan', the VLAN that a packet belongs to, and
1050 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1051 * that should be included in the 802.1Q header. (If the return value is 0,
1052 * then the 802.1Q header should only be included in the packet if there is a
1055 * Both 'vlan' and the return value are in the range 0...4095. */
1057 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1059 switch (out_xbundle->vlan_mode) {
1060 case PORT_VLAN_ACCESS:
1063 case PORT_VLAN_TRUNK:
1064 case PORT_VLAN_NATIVE_TAGGED:
1067 case PORT_VLAN_NATIVE_UNTAGGED:
1068 return vlan == out_xbundle->vlan ? 0 : vlan;
1076 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1079 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1081 ovs_be16 tci, old_tci;
1082 struct xport *xport;
1084 vid = output_vlan_to_vid(out_xbundle, vlan);
1085 if (list_is_empty(&out_xbundle->xports)) {
1086 /* Partially configured bundle with no slaves. Drop the packet. */
1088 } else if (!out_xbundle->bond) {
1089 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1092 struct ofport_dpif *ofport;
1094 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1095 &ctx->xout->wc, vid);
1096 xport = xport_lookup(ofport);
1099 /* No slaves enabled, so drop packet. */
1104 old_tci = *flow_tci;
1106 if (tci || out_xbundle->use_priority_tags) {
1107 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1109 tci |= htons(VLAN_CFI);
1114 compose_output_action(ctx, xport->ofp_port);
1115 *flow_tci = old_tci;
1118 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1119 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1120 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1122 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1124 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1128 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1129 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1133 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1134 if (flow->nw_proto == ARP_OP_REPLY) {
1136 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1137 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1138 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1140 return flow->nw_src == flow->nw_dst;
1146 /* Checks whether a MAC learning update is necessary for MAC learning table
1147 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1150 * Most packets processed through the MAC learning table do not actually
1151 * change it in any way. This function requires only a read lock on the MAC
1152 * learning table, so it is much cheaper in this common case.
1154 * Keep the code here synchronized with that in update_learning_table__()
1157 is_mac_learning_update_needed(const struct mac_learning *ml,
1158 const struct flow *flow,
1159 struct flow_wildcards *wc,
1160 int vlan, struct xbundle *in_xbundle)
1161 OVS_REQ_RDLOCK(ml->rwlock)
1163 struct mac_entry *mac;
1165 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1169 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1170 if (!mac || mac_entry_age(ml, mac)) {
1174 if (is_gratuitous_arp(flow, wc)) {
1175 /* We don't want to learn from gratuitous ARP packets that are
1176 * reflected back over bond slaves so we lock the learning table. */
1177 if (!in_xbundle->bond) {
1179 } else if (mac_entry_is_grat_arp_locked(mac)) {
1184 return mac->port.p != in_xbundle->ofbundle;
1188 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1189 * received on 'in_xbundle' in 'vlan'.
1191 * This code repeats all the checks in is_mac_learning_update_needed() because
1192 * the lock was released between there and here and thus the MAC learning state
1193 * could have changed.
1195 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1198 update_learning_table__(const struct xbridge *xbridge,
1199 const struct flow *flow, struct flow_wildcards *wc,
1200 int vlan, struct xbundle *in_xbundle)
1201 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1203 struct mac_entry *mac;
1205 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1209 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1210 if (is_gratuitous_arp(flow, wc)) {
1211 /* We don't want to learn from gratuitous ARP packets that are
1212 * reflected back over bond slaves so we lock the learning table. */
1213 if (!in_xbundle->bond) {
1214 mac_entry_set_grat_arp_lock(mac);
1215 } else if (mac_entry_is_grat_arp_locked(mac)) {
1220 if (mac->port.p != in_xbundle->ofbundle) {
1221 /* The log messages here could actually be useful in debugging,
1222 * so keep the rate limit relatively high. */
1223 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1225 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1226 "on port %s in VLAN %d",
1227 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1228 in_xbundle->name, vlan);
1230 mac->port.p = in_xbundle->ofbundle;
1231 mac_learning_changed(xbridge->ml);
1236 update_learning_table(const struct xbridge *xbridge,
1237 const struct flow *flow, struct flow_wildcards *wc,
1238 int vlan, struct xbundle *in_xbundle)
1242 /* Don't learn the OFPP_NONE port. */
1243 if (in_xbundle == &ofpp_none_bundle) {
1247 /* First try the common case: no change to MAC learning table. */
1248 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1249 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1251 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1254 /* Slow path: MAC learning table might need an update. */
1255 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1256 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1257 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1261 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1262 * dropped. Returns true if they may be forwarded, false if they should be
1265 * 'in_port' must be the xport that corresponds to flow->in_port.
1266 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1268 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1269 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1270 * checked by input_vid_is_valid().
1272 * May also add tags to '*tags', although the current implementation only does
1273 * so in one special case.
1276 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1279 struct xbundle *in_xbundle = in_port->xbundle;
1280 const struct xbridge *xbridge = ctx->xbridge;
1281 struct flow *flow = &ctx->xin->flow;
1283 /* Drop frames for reserved multicast addresses
1284 * only if forward_bpdu option is absent. */
1285 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1286 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1290 if (in_xbundle->bond) {
1291 struct mac_entry *mac;
1293 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1299 xlate_report(ctx, "bonding refused admissibility, dropping");
1302 case BV_DROP_IF_MOVED:
1303 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1304 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1305 if (mac && mac->port.p != in_xbundle->ofbundle &&
1306 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1307 || mac_entry_is_grat_arp_locked(mac))) {
1308 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1309 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1313 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1322 xlate_normal(struct xlate_ctx *ctx)
1324 struct flow_wildcards *wc = &ctx->xout->wc;
1325 struct flow *flow = &ctx->xin->flow;
1326 struct xbundle *in_xbundle;
1327 struct xport *in_port;
1328 struct mac_entry *mac;
1333 ctx->xout->has_normal = true;
1335 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1336 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1337 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1339 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1340 ctx->xin->packet != NULL, &in_port);
1342 xlate_report(ctx, "no input bundle, dropping");
1346 /* Drop malformed frames. */
1347 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1348 !(flow->vlan_tci & htons(VLAN_CFI))) {
1349 if (ctx->xin->packet != NULL) {
1350 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1351 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1352 "VLAN tag received on port %s",
1353 ctx->xbridge->name, in_xbundle->name);
1355 xlate_report(ctx, "partial VLAN tag, dropping");
1359 /* Drop frames on bundles reserved for mirroring. */
1360 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1361 if (ctx->xin->packet != NULL) {
1362 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1363 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1364 "%s, which is reserved exclusively for mirroring",
1365 ctx->xbridge->name, in_xbundle->name);
1367 xlate_report(ctx, "input port is mirror output port, dropping");
1372 vid = vlan_tci_to_vid(flow->vlan_tci);
1373 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1374 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1377 vlan = input_vid_to_vlan(in_xbundle, vid);
1379 /* Check other admissibility requirements. */
1380 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1384 /* Learn source MAC. */
1385 if (ctx->xin->may_learn) {
1386 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1389 /* Determine output bundle. */
1390 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1391 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1392 mac_port = mac ? mac->port.p : NULL;
1393 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1396 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1397 if (mac_xbundle && mac_xbundle != in_xbundle) {
1398 xlate_report(ctx, "forwarding to learned port");
1399 output_normal(ctx, mac_xbundle, vlan);
1400 } else if (!mac_xbundle) {
1401 xlate_report(ctx, "learned port is unknown, dropping");
1403 xlate_report(ctx, "learned port is input port, dropping");
1406 struct xbundle *xbundle;
1408 xlate_report(ctx, "no learned MAC for destination, flooding");
1409 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1410 if (xbundle != in_xbundle
1411 && xbundle_includes_vlan(xbundle, vlan)
1412 && xbundle->floodable
1413 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1414 output_normal(ctx, xbundle, vlan);
1417 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1421 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1422 * the number of packets out of UINT32_MAX to sample. The given
1423 * cookie is passed back in the callback for each sampled packet.
1426 compose_sample_action(const struct xbridge *xbridge,
1427 struct ofpbuf *odp_actions,
1428 const struct flow *flow,
1429 const uint32_t probability,
1430 const union user_action_cookie *cookie,
1431 const size_t cookie_size)
1433 size_t sample_offset, actions_offset;
1434 odp_port_t odp_port;
1438 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1440 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1442 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1444 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1445 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1446 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1448 nl_msg_end_nested(odp_actions, actions_offset);
1449 nl_msg_end_nested(odp_actions, sample_offset);
1450 return cookie_offset;
1454 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1455 odp_port_t odp_port, unsigned int n_outputs,
1456 union user_action_cookie *cookie)
1460 cookie->type = USER_ACTION_COOKIE_SFLOW;
1461 cookie->sflow.vlan_tci = vlan_tci;
1463 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1464 * port information") for the interpretation of cookie->output. */
1465 switch (n_outputs) {
1467 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1468 cookie->sflow.output = 0x40000000 | 256;
1472 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1474 cookie->sflow.output = ifindex;
1479 /* 0x80000000 means "multiple output ports. */
1480 cookie->sflow.output = 0x80000000 | n_outputs;
1485 /* Compose SAMPLE action for sFlow bridge sampling. */
1487 compose_sflow_action(const struct xbridge *xbridge,
1488 struct ofpbuf *odp_actions,
1489 const struct flow *flow,
1490 odp_port_t odp_port)
1492 uint32_t probability;
1493 union user_action_cookie cookie;
1495 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1499 probability = dpif_sflow_get_probability(xbridge->sflow);
1500 compose_sflow_cookie(xbridge, htons(0), odp_port,
1501 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1503 return compose_sample_action(xbridge, odp_actions, flow, probability,
1504 &cookie, sizeof cookie.sflow);
1508 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1509 uint32_t obs_domain_id, uint32_t obs_point_id,
1510 union user_action_cookie *cookie)
1512 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1513 cookie->flow_sample.probability = probability;
1514 cookie->flow_sample.collector_set_id = collector_set_id;
1515 cookie->flow_sample.obs_domain_id = obs_domain_id;
1516 cookie->flow_sample.obs_point_id = obs_point_id;
1520 compose_ipfix_cookie(union user_action_cookie *cookie)
1522 cookie->type = USER_ACTION_COOKIE_IPFIX;
1525 /* Compose SAMPLE action for IPFIX bridge sampling. */
1527 compose_ipfix_action(const struct xbridge *xbridge,
1528 struct ofpbuf *odp_actions,
1529 const struct flow *flow)
1531 uint32_t probability;
1532 union user_action_cookie cookie;
1534 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1538 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1539 compose_ipfix_cookie(&cookie);
1541 compose_sample_action(xbridge, odp_actions, flow, probability,
1542 &cookie, sizeof cookie.ipfix);
1545 /* SAMPLE action for sFlow must be first action in any given list of
1546 * actions. At this point we do not have all information required to
1547 * build it. So try to build sample action as complete as possible. */
1549 add_sflow_action(struct xlate_ctx *ctx)
1551 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1552 &ctx->xout->odp_actions,
1553 &ctx->xin->flow, ODPP_NONE);
1554 ctx->sflow_odp_port = 0;
1555 ctx->sflow_n_outputs = 0;
1558 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1559 * of actions, eventually after the SAMPLE action for sFlow. */
1561 add_ipfix_action(struct xlate_ctx *ctx)
1563 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1567 /* Fix SAMPLE action according to data collected while composing ODP actions.
1568 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1569 * USERSPACE action's user-cookie which is required for sflow. */
1571 fix_sflow_action(struct xlate_ctx *ctx)
1573 const struct flow *base = &ctx->base_flow;
1574 union user_action_cookie *cookie;
1576 if (!ctx->user_cookie_offset) {
1580 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1581 sizeof cookie->sflow);
1582 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1584 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1585 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1588 static enum slow_path_reason
1589 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1590 const struct xport *xport, const struct ofpbuf *packet)
1592 struct flow_wildcards *wc = &ctx->xout->wc;
1593 const struct xbridge *xbridge = ctx->xbridge;
1597 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1599 cfm_process_heartbeat(xport->cfm, packet);
1602 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1604 bfd_process_packet(xport->bfd, flow, packet);
1607 } else if (xport->xbundle && xport->xbundle->lacp
1608 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1610 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1613 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1615 stp_process_packet(xport, packet);
1624 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1627 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1628 struct flow_wildcards *wc = &ctx->xout->wc;
1629 struct flow *flow = &ctx->xin->flow;
1630 ovs_be16 flow_vlan_tci;
1631 uint32_t flow_pkt_mark;
1632 uint8_t flow_nw_tos;
1633 odp_port_t out_port, odp_port;
1636 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1637 * before traversing a patch port. */
1638 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 22);
1641 xlate_report(ctx, "Nonexistent output port");
1643 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1644 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1646 } else if (check_stp && !xport_stp_forward_state(xport)) {
1647 xlate_report(ctx, "STP not in forwarding state, skipping output");
1651 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1652 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1657 const struct xport *peer = xport->peer;
1658 struct flow old_flow = ctx->xin->flow;
1659 enum slow_path_reason special;
1661 ctx->xbridge = peer->xbridge;
1662 flow->in_port.ofp_port = peer->ofp_port;
1663 flow->metadata = htonll(0);
1664 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1665 memset(flow->regs, 0, sizeof flow->regs);
1667 special = process_special(ctx, &ctx->xin->flow, peer,
1670 ctx->xout->slow |= special;
1671 } else if (may_receive(peer, ctx)) {
1672 if (xport_stp_forward_state(peer)) {
1673 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1675 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1676 * learning action look at the packet, then drop it. */
1677 struct flow old_base_flow = ctx->base_flow;
1678 size_t old_size = ctx->xout->odp_actions.size;
1679 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1680 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1681 ctx->xout->mirrors = old_mirrors;
1682 ctx->base_flow = old_base_flow;
1683 ctx->xout->odp_actions.size = old_size;
1687 ctx->xin->flow = old_flow;
1688 ctx->xbridge = xport->xbridge;
1690 if (ctx->xin->resubmit_stats) {
1691 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1692 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1698 flow_vlan_tci = flow->vlan_tci;
1699 flow_pkt_mark = flow->pkt_mark;
1700 flow_nw_tos = flow->nw_tos;
1702 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1703 wc->masks.nw_tos |= IP_ECN_MASK;
1704 flow->nw_tos &= ~IP_DSCP_MASK;
1705 flow->nw_tos |= dscp;
1708 if (xport->is_tunnel) {
1709 /* Save tunnel metadata so that changes made due to
1710 * the Logical (tunnel) Port are not visible for any further
1711 * matches, while explicit set actions on tunnel metadata are.
1713 struct flow_tnl flow_tnl = flow->tunnel;
1714 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1715 if (odp_port == ODPP_NONE) {
1716 xlate_report(ctx, "Tunneling decided against output");
1717 goto out; /* restore flow_nw_tos */
1719 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1720 xlate_report(ctx, "Not tunneling to our own address");
1721 goto out; /* restore flow_nw_tos */
1723 if (ctx->xin->resubmit_stats) {
1724 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1726 out_port = odp_port;
1727 commit_odp_tunnel_action(flow, &ctx->base_flow,
1728 &ctx->xout->odp_actions);
1729 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1731 ofp_port_t vlandev_port;
1733 odp_port = xport->odp_port;
1734 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1735 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1737 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1739 if (vlandev_port == ofp_port) {
1740 out_port = odp_port;
1742 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1743 flow->vlan_tci = htons(0);
1747 if (out_port != ODPP_NONE) {
1748 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1749 &ctx->xout->odp_actions,
1751 &ctx->mpls_depth_delta);
1752 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1755 ctx->sflow_odp_port = odp_port;
1756 ctx->sflow_n_outputs++;
1757 ctx->xout->nf_output_iface = ofp_port;
1762 flow->vlan_tci = flow_vlan_tci;
1763 flow->pkt_mark = flow_pkt_mark;
1764 flow->nw_tos = flow_nw_tos;
1768 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1770 compose_output_action__(ctx, ofp_port, true);
1774 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1776 struct rule_dpif *old_rule = ctx->rule;
1777 struct rule_actions *actions;
1779 if (ctx->xin->resubmit_stats) {
1780 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1786 actions = rule_dpif_get_actions(rule);
1787 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1788 rule_actions_unref(actions);
1789 ctx->rule = old_rule;
1794 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1796 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1798 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1799 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1800 MAX_RESUBMIT_RECURSION);
1801 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1802 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1803 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1804 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1805 } else if (ctx->stack.size >= 65536) {
1806 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1815 xlate_table_action(struct xlate_ctx *ctx,
1816 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1818 if (xlate_resubmit_resource_check(ctx)) {
1819 struct rule_dpif *rule;
1820 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1821 uint8_t old_table_id = ctx->table_id;
1823 ctx->table_id = table_id;
1825 /* Look up a flow with 'in_port' as the input port. Then restore the
1826 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1827 * have surprising behavior). */
1828 ctx->xin->flow.in_port.ofp_port = in_port;
1829 rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1830 &ctx->xin->flow, &ctx->xout->wc,
1832 ctx->xin->flow.in_port.ofp_port = old_in_port;
1834 if (ctx->xin->resubmit_hook) {
1835 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1838 if (!rule && may_packet_in) {
1839 struct xport *xport;
1842 * check if table configuration flags
1843 * OFPTC_TABLE_MISS_CONTROLLER, default.
1844 * OFPTC_TABLE_MISS_CONTINUE,
1845 * OFPTC_TABLE_MISS_DROP
1846 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1847 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1848 choose_miss_rule(xport ? xport->config : 0,
1849 ctx->xbridge->miss_rule,
1850 ctx->xbridge->no_packet_in_rule, &rule);
1853 xlate_recursively(ctx, rule);
1854 rule_dpif_unref(rule);
1857 ctx->table_id = old_table_id;
1865 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1867 uint64_t action_list_stub[1024 / 8];
1868 struct ofpbuf action_list, action_set;
1870 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1871 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1873 ofpacts_execute_action_set(&action_list, &action_set);
1875 do_xlate_actions(action_list.data, action_list.size, ctx);
1878 ofpbuf_uninit(&action_set);
1879 ofpbuf_uninit(&action_list);
1883 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1885 const struct ofputil_bucket *bucket;
1886 const struct list *buckets;
1887 struct flow old_flow = ctx->xin->flow;
1889 group_dpif_get_buckets(group, &buckets);
1891 LIST_FOR_EACH (bucket, list_node, buckets) {
1892 xlate_group_bucket(ctx, bucket);
1893 /* Roll back flow to previous state.
1894 * This is equivalent to cloning the packet for each bucket.
1896 * As a side effect any subsequently applied actions will
1897 * also effectively be applied to a clone of the packet taken
1898 * just before applying the all or indirect group. */
1899 ctx->xin->flow = old_flow;
1904 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1906 const struct ofputil_bucket *bucket;
1908 bucket = group_first_live_bucket(ctx, group, 0);
1910 xlate_group_bucket(ctx, bucket);
1915 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
1917 switch (group_dpif_get_type(group)) {
1919 case OFPGT11_INDIRECT:
1920 xlate_all_group(ctx, group);
1922 case OFPGT11_SELECT:
1923 /* XXX not yet implemented */
1926 xlate_ff_group(ctx, group);
1931 group_dpif_release(group);
1935 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
1937 if (xlate_resubmit_resource_check(ctx)) {
1938 struct group_dpif *group;
1941 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
1943 xlate_group_action__(ctx, group);
1953 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1954 const struct ofpact_resubmit *resubmit)
1959 in_port = resubmit->in_port;
1960 if (in_port == OFPP_IN_PORT) {
1961 in_port = ctx->xin->flow.in_port.ofp_port;
1964 table_id = resubmit->table_id;
1965 if (table_id == 255) {
1966 table_id = ctx->table_id;
1969 xlate_table_action(ctx, in_port, table_id, false);
1973 flood_packets(struct xlate_ctx *ctx, bool all)
1975 const struct xport *xport;
1977 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1978 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
1983 compose_output_action__(ctx, xport->ofp_port, false);
1984 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1985 compose_output_action(ctx, xport->ofp_port);
1989 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1993 execute_controller_action(struct xlate_ctx *ctx, int len,
1994 enum ofp_packet_in_reason reason,
1995 uint16_t controller_id)
1997 struct ofproto_packet_in *pin;
1998 struct ofpbuf *packet;
2001 ctx->xout->slow |= SLOW_CONTROLLER;
2002 if (!ctx->xin->packet) {
2006 packet = ofpbuf_clone(ctx->xin->packet);
2008 key.skb_priority = 0;
2010 memset(&key.tunnel, 0, sizeof key.tunnel);
2012 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2013 &ctx->xout->odp_actions,
2015 &ctx->mpls_depth_delta);
2017 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
2018 ctx->xout->odp_actions.size, NULL, NULL);
2020 pin = xmalloc(sizeof *pin);
2021 pin->up.packet_len = packet->size;
2022 pin->up.packet = ofpbuf_steal_data(packet);
2023 pin->up.reason = reason;
2024 pin->up.table_id = ctx->table_id;
2025 pin->up.cookie = (ctx->rule
2026 ? rule_dpif_get_flow_cookie(ctx->rule)
2029 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2031 pin->controller_id = controller_id;
2032 pin->send_len = len;
2033 pin->generated_by_table_miss = (ctx->rule
2034 && rule_dpif_is_table_miss(ctx->rule));
2035 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2036 ofpbuf_delete(packet);
2040 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2042 struct flow_wildcards *wc = &ctx->xout->wc;
2043 struct flow *flow = &ctx->xin->flow;
2045 ovs_assert(eth_type_mpls(eth_type));
2047 /* If mpls_depth_delta is negative then an MPLS POP action has been
2048 * composed and the resulting MPLS label stack is unknown. This means
2049 * an MPLS PUSH action can't be composed as it needs to know either the
2050 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2051 * there is no MPLS label stack present. Thus, stop processing.
2053 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2054 * composed and no further MPLS PUSH action may be performed without
2055 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2056 * Thus, stop processing.
2058 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2059 * has been updated. Performing a MPLS PUSH action may be would result in
2060 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2061 * Thus, stop processing.
2063 * It is planned that in the future this case will be handled
2064 * by recirculation */
2065 if (ctx->mpls_depth_delta ||
2066 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
2070 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2072 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
2074 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2075 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
2080 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2081 label = htonl(0x2); /* IPV6 Explicit Null. */
2083 label = htonl(0x0); /* IPV4 Explicit Null. */
2085 wc->masks.nw_tos |= IP_DSCP_MASK;
2086 wc->masks.nw_ttl = 0xff;
2087 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
2088 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
2089 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
2091 flow->dl_type = eth_type;
2092 ctx->mpls_depth_delta++;
2098 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2100 struct flow_wildcards *wc = &ctx->xout->wc;
2102 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2106 /* If mpls_depth_delta is negative then an MPLS POP action has been
2107 * composed. Performing another MPLS POP action
2108 * would result in losing ether type that results from
2109 * the already composed MPLS POP. Thus, stop processing.
2111 * It is planned that in the future this case will be handled
2112 * by recirculation */
2113 if (ctx->mpls_depth_delta < 0) {
2117 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2119 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2120 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2121 * flow's MPLS LSE should be restored to that value to allow any
2122 * subsequent actions that update of the LSE to be executed correctly.
2124 if (ctx->mpls_depth_delta > 0) {
2125 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
2128 ctx->xin->flow.dl_type = eth_type;
2129 ctx->mpls_depth_delta--;
2135 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2137 struct flow *flow = &ctx->xin->flow;
2139 if (!is_ip_any(flow)) {
2143 ctx->xout->wc.masks.nw_ttl = 0xff;
2144 if (flow->nw_ttl > 1) {
2150 for (i = 0; i < ids->n_controllers; i++) {
2151 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2155 /* Stop processing for current table. */
2161 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2163 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2167 /* If mpls_depth_delta is negative then an MPLS POP action has been
2168 * executed and the resulting MPLS label stack is unknown. This means
2169 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2170 * the top-most MPLS LSE. Thus, stop processing.
2172 * It is planned that in the future this case will be handled
2175 if (ctx->mpls_depth_delta < 0) {
2179 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
2180 set_mpls_lse_label(&ctx->xin->flow.mpls_lse, label);
2185 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2187 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2191 /* If mpls_depth_delta is negative then an MPLS POP action has been
2192 * executed and the resulting MPLS label stack is unknown. This means
2193 * a SET MPLS TC action can't be executed as it needs to manipulate
2194 * the top-most MPLS LSE. Thus, stop processing.
2196 * It is planned that in the future this case will be handled
2199 if (ctx->mpls_depth_delta < 0) {
2203 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
2204 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse, tc);
2209 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2211 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2215 /* If mpls_depth_delta is negative then an MPLS POP action has been
2216 * executed and the resulting MPLS label stack is unknown. This means
2217 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2218 * the top-most MPLS LSE. Thus, stop processing.
2220 * It is planned that in the future this case will be handled
2223 if (ctx->mpls_depth_delta < 0) {
2227 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
2228 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
2233 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2235 struct flow *flow = &ctx->xin->flow;
2236 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2237 struct flow_wildcards *wc = &ctx->xout->wc;
2239 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2241 if (!eth_type_mpls(flow->dl_type)) {
2247 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2250 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2252 /* Stop processing for current table. */
2258 xlate_output_action(struct xlate_ctx *ctx,
2259 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2261 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2263 ctx->xout->nf_output_iface = NF_OUT_DROP;
2267 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2270 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2277 flood_packets(ctx, false);
2280 flood_packets(ctx, true);
2282 case OFPP_CONTROLLER:
2283 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2289 if (port != ctx->xin->flow.in_port.ofp_port) {
2290 compose_output_action(ctx, port);
2292 xlate_report(ctx, "skipping output to input port");
2297 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2298 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2299 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2300 ctx->xout->nf_output_iface = prev_nf_output_iface;
2301 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2302 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2303 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2308 xlate_output_reg_action(struct xlate_ctx *ctx,
2309 const struct ofpact_output_reg *or)
2311 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2312 if (port <= UINT16_MAX) {
2313 union mf_subvalue value;
2315 memset(&value, 0xff, sizeof value);
2316 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2317 xlate_output_action(ctx, u16_to_ofp(port),
2318 or->max_len, false);
2323 xlate_enqueue_action(struct xlate_ctx *ctx,
2324 const struct ofpact_enqueue *enqueue)
2326 ofp_port_t ofp_port = enqueue->port;
2327 uint32_t queue_id = enqueue->queue;
2328 uint32_t flow_priority, priority;
2331 /* Translate queue to priority. */
2332 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2334 /* Fall back to ordinary output action. */
2335 xlate_output_action(ctx, enqueue->port, 0, false);
2339 /* Check output port. */
2340 if (ofp_port == OFPP_IN_PORT) {
2341 ofp_port = ctx->xin->flow.in_port.ofp_port;
2342 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2346 /* Add datapath actions. */
2347 flow_priority = ctx->xin->flow.skb_priority;
2348 ctx->xin->flow.skb_priority = priority;
2349 compose_output_action(ctx, ofp_port);
2350 ctx->xin->flow.skb_priority = flow_priority;
2352 /* Update NetFlow output port. */
2353 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2354 ctx->xout->nf_output_iface = ofp_port;
2355 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2356 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2361 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2363 uint32_t skb_priority;
2365 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2366 ctx->xin->flow.skb_priority = skb_priority;
2368 /* Couldn't translate queue to a priority. Nothing to do. A warning
2369 * has already been logged. */
2374 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2376 const struct xbridge *xbridge = xbridge_;
2387 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2390 port = get_ofp_port(xbridge, ofp_port);
2391 return port ? port->may_enable : false;
2396 xlate_bundle_action(struct xlate_ctx *ctx,
2397 const struct ofpact_bundle *bundle)
2401 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2403 CONST_CAST(struct xbridge *, ctx->xbridge));
2404 if (bundle->dst.field) {
2405 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2408 xlate_output_action(ctx, port, 0, false);
2413 xlate_learn_action(struct xlate_ctx *ctx,
2414 const struct ofpact_learn *learn)
2416 uint64_t ofpacts_stub[1024 / 8];
2417 struct ofputil_flow_mod fm;
2418 struct ofpbuf ofpacts;
2420 ctx->xout->has_learn = true;
2422 learn_mask(learn, &ctx->xout->wc);
2424 if (!ctx->xin->may_learn) {
2428 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2429 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2430 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2431 ofpbuf_uninit(&ofpacts);
2435 xlate_fin_timeout(struct xlate_ctx *ctx,
2436 const struct ofpact_fin_timeout *oft)
2438 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2439 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2440 oft->fin_hard_timeout);
2445 xlate_sample_action(struct xlate_ctx *ctx,
2446 const struct ofpact_sample *os)
2448 union user_action_cookie cookie;
2449 /* Scale the probability from 16-bit to 32-bit while representing
2450 * the same percentage. */
2451 uint32_t probability = (os->probability << 16) | os->probability;
2453 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2454 &ctx->xout->odp_actions,
2456 &ctx->mpls_depth_delta);
2458 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2459 os->obs_domain_id, os->obs_point_id, &cookie);
2460 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2461 probability, &cookie, sizeof cookie.flow_sample);
2465 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2467 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2468 ? OFPUTIL_PC_NO_RECV_STP
2469 : OFPUTIL_PC_NO_RECV)) {
2473 /* Only drop packets here if both forwarding and learning are
2474 * disabled. If just learning is enabled, we need to have
2475 * OFPP_NORMAL and the learning action have a look at the packet
2476 * before we can drop it. */
2477 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2485 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2487 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2488 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2489 ofpact_pad(&ctx->action_set);
2493 xlate_action_set(struct xlate_ctx *ctx)
2495 uint64_t action_list_stub[1024 / 64];
2496 struct ofpbuf action_list;
2498 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2499 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2500 do_xlate_actions(action_list.data, action_list.size, ctx);
2501 ofpbuf_uninit(&action_list);
2505 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2506 struct xlate_ctx *ctx)
2508 struct flow_wildcards *wc = &ctx->xout->wc;
2509 struct flow *flow = &ctx->xin->flow;
2510 const struct ofpact *a;
2512 /* dl_type already in the mask, not set below. */
2514 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2515 struct ofpact_controller *controller;
2516 const struct ofpact_metadata *metadata;
2517 const struct ofpact_set_field *set_field;
2518 const struct mf_field *mf;
2526 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2527 ofpact_get_OUTPUT(a)->max_len, true);
2531 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2536 case OFPACT_CONTROLLER:
2537 controller = ofpact_get_CONTROLLER(a);
2538 execute_controller_action(ctx, controller->max_len,
2540 controller->controller_id);
2543 case OFPACT_ENQUEUE:
2544 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2547 case OFPACT_SET_VLAN_VID:
2548 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2549 if (flow->vlan_tci & htons(VLAN_CFI) ||
2550 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2551 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2552 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2557 case OFPACT_SET_VLAN_PCP:
2558 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2559 if (flow->vlan_tci & htons(VLAN_CFI) ||
2560 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2561 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2562 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2563 << VLAN_PCP_SHIFT) | VLAN_CFI);
2567 case OFPACT_STRIP_VLAN:
2568 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2569 flow->vlan_tci = htons(0);
2572 case OFPACT_PUSH_VLAN:
2573 /* XXX 802.1AD(QinQ) */
2574 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2575 flow->vlan_tci = htons(VLAN_CFI);
2578 case OFPACT_SET_ETH_SRC:
2579 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2580 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2583 case OFPACT_SET_ETH_DST:
2584 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2585 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2588 case OFPACT_SET_IPV4_SRC:
2589 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2590 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2591 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2595 case OFPACT_SET_IPV4_DST:
2596 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2597 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2598 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2602 case OFPACT_SET_IP_DSCP:
2603 if (is_ip_any(flow)) {
2604 wc->masks.nw_tos |= IP_DSCP_MASK;
2605 flow->nw_tos &= ~IP_DSCP_MASK;
2606 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2610 case OFPACT_SET_IP_ECN:
2611 if (is_ip_any(flow)) {
2612 wc->masks.nw_tos |= IP_ECN_MASK;
2613 flow->nw_tos &= ~IP_ECN_MASK;
2614 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2618 case OFPACT_SET_IP_TTL:
2619 if (is_ip_any(flow)) {
2620 wc->masks.nw_ttl = 0xff;
2621 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2625 case OFPACT_SET_L4_SRC_PORT:
2626 if (is_ip_any(flow)) {
2627 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2628 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2629 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2633 case OFPACT_SET_L4_DST_PORT:
2634 if (is_ip_any(flow)) {
2635 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2636 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2637 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2641 case OFPACT_RESUBMIT:
2642 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2645 case OFPACT_SET_TUNNEL:
2646 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2649 case OFPACT_SET_QUEUE:
2650 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2653 case OFPACT_POP_QUEUE:
2654 flow->skb_priority = ctx->orig_skb_priority;
2657 case OFPACT_REG_MOVE:
2658 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2661 case OFPACT_REG_LOAD:
2662 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2665 case OFPACT_SET_FIELD:
2666 set_field = ofpact_get_SET_FIELD(a);
2667 mf = set_field->field;
2668 mf_mask_field_and_prereqs(mf, &wc->masks);
2670 /* Set field action only ever overwrites packet's outermost
2671 * applicable header fields. Do nothing if no header exists. */
2672 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2673 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2674 || flow->mpls_lse)) {
2675 mf_set_flow_value(mf, &set_field->value, flow);
2679 case OFPACT_STACK_PUSH:
2680 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2684 case OFPACT_STACK_POP:
2685 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2689 case OFPACT_PUSH_MPLS:
2690 if (compose_mpls_push_action(ctx,
2691 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2696 case OFPACT_POP_MPLS:
2697 if (compose_mpls_pop_action(ctx,
2698 ofpact_get_POP_MPLS(a)->ethertype)) {
2703 case OFPACT_SET_MPLS_LABEL:
2704 if (compose_set_mpls_label_action(ctx,
2705 ofpact_get_SET_MPLS_LABEL(a)->label)) {
2710 case OFPACT_SET_MPLS_TC:
2711 if (compose_set_mpls_tc_action(ctx,
2712 ofpact_get_SET_MPLS_TC(a)->tc)) {
2717 case OFPACT_SET_MPLS_TTL:
2718 if (compose_set_mpls_ttl_action(ctx,
2719 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2724 case OFPACT_DEC_MPLS_TTL:
2725 if (compose_dec_mpls_ttl_action(ctx)) {
2730 case OFPACT_DEC_TTL:
2731 wc->masks.nw_ttl = 0xff;
2732 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2738 /* Nothing to do. */
2741 case OFPACT_MULTIPATH:
2742 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2746 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2749 case OFPACT_OUTPUT_REG:
2750 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2754 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2761 case OFPACT_FIN_TIMEOUT:
2762 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2763 ctx->xout->has_fin_timeout = true;
2764 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2767 case OFPACT_CLEAR_ACTIONS:
2768 ofpbuf_clear(&ctx->action_set);
2771 case OFPACT_WRITE_ACTIONS:
2772 xlate_write_actions(ctx, a);
2775 case OFPACT_WRITE_METADATA:
2776 metadata = ofpact_get_WRITE_METADATA(a);
2777 flow->metadata &= ~metadata->mask;
2778 flow->metadata |= metadata->metadata & metadata->mask;
2782 /* Not implemented yet. */
2785 case OFPACT_GOTO_TABLE: {
2786 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2788 ovs_assert(ctx->table_id < ogt->table_id);
2789 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2790 ogt->table_id, true);
2795 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2802 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2803 const struct flow *flow, struct rule_dpif *rule,
2804 uint16_t tcp_flags, const struct ofpbuf *packet)
2806 xin->ofproto = ofproto;
2808 xin->packet = packet;
2809 xin->may_learn = packet != NULL;
2811 xin->ofpacts = NULL;
2812 xin->ofpacts_len = 0;
2813 xin->tcp_flags = tcp_flags;
2814 xin->resubmit_hook = NULL;
2815 xin->report_hook = NULL;
2816 xin->resubmit_stats = NULL;
2820 xlate_out_uninit(struct xlate_out *xout)
2823 ofpbuf_uninit(&xout->odp_actions);
2827 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2828 * into datapath actions, using 'ctx', and discards the datapath actions. */
2830 xlate_actions_for_side_effects(struct xlate_in *xin)
2832 struct xlate_out xout;
2834 xlate_actions(xin, &xout);
2835 xlate_out_uninit(&xout);
2839 xlate_report(struct xlate_ctx *ctx, const char *s)
2841 if (ctx->xin->report_hook) {
2842 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2847 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2850 dst->slow = src->slow;
2851 dst->has_learn = src->has_learn;
2852 dst->has_normal = src->has_normal;
2853 dst->has_fin_timeout = src->has_fin_timeout;
2854 dst->nf_output_iface = src->nf_output_iface;
2855 dst->mirrors = src->mirrors;
2857 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2858 sizeof dst->odp_actions_stub);
2859 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2860 src->odp_actions.size);
2863 /* Returns a reference to the sflow handled associated with ofproto, or NULL if
2864 * there is none. The caller is responsible for decrementing the results ref
2865 * count with dpif_sflow_unref(). */
2867 xlate_get_sflow(const struct ofproto_dpif *ofproto)
2869 struct dpif_sflow *sflow = NULL;
2870 struct xbridge *xbridge;
2872 ovs_rwlock_rdlock(&xlate_rwlock);
2873 xbridge = xbridge_lookup(ofproto);
2875 sflow = dpif_sflow_ref(xbridge->sflow);
2877 ovs_rwlock_unlock(&xlate_rwlock);
2882 /* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2883 * there is none. The caller is responsible for decrementing the results ref
2884 * count with dpif_ipfix_unref(). */
2886 xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2888 struct dpif_ipfix *ipfix = NULL;
2889 struct xbridge *xbridge;
2891 ovs_rwlock_rdlock(&xlate_rwlock);
2892 xbridge = xbridge_lookup(ofproto);
2894 ipfix = dpif_ipfix_ref(xbridge->ipfix);
2896 ovs_rwlock_unlock(&xlate_rwlock);
2901 static struct skb_priority_to_dscp *
2902 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2904 struct skb_priority_to_dscp *pdscp;
2907 hash = hash_int(skb_priority, 0);
2908 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2909 if (pdscp->skb_priority == skb_priority) {
2917 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2920 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2921 *dscp = pdscp ? pdscp->dscp : 0;
2922 return pdscp != NULL;
2926 clear_skb_priorities(struct xport *xport)
2928 struct skb_priority_to_dscp *pdscp, *next;
2930 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2931 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2937 actions_output_to_local_port(const struct xlate_ctx *ctx)
2939 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2940 const struct nlattr *a;
2943 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2944 ctx->xout->odp_actions.size) {
2945 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2946 && nl_attr_get_odp_port(a) == local_odp_port) {
2953 /* Thread safe call to xlate_actions__(). */
2955 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2957 ovs_rwlock_rdlock(&xlate_rwlock);
2958 xlate_actions__(xin, xout);
2959 ovs_rwlock_unlock(&xlate_rwlock);
2962 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2963 * into datapath actions in 'odp_actions', using 'ctx'.
2965 * The caller must take responsibility for eventually freeing 'xout', with
2966 * xlate_out_uninit(). */
2968 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
2969 OVS_REQ_RDLOCK(xlate_rwlock)
2971 struct flow_wildcards *wc = &xout->wc;
2972 struct flow *flow = &xin->flow;
2973 struct rule_dpif *rule = NULL;
2975 struct rule_actions *actions = NULL;
2976 enum slow_path_reason special;
2977 const struct ofpact *ofpacts;
2978 struct xport *in_port;
2979 struct flow orig_flow;
2980 struct xlate_ctx ctx;
2984 COVERAGE_INC(xlate_actions);
2986 /* Flow initialization rules:
2987 * - 'base_flow' must match the kernel's view of the packet at the
2988 * time that action processing starts. 'flow' represents any
2989 * transformations we wish to make through actions.
2990 * - By default 'base_flow' and 'flow' are the same since the input
2991 * packet matches the output before any actions are applied.
2992 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2993 * of the received packet as seen by the kernel. If we later output
2994 * to another device without any modifications this will cause us to
2995 * insert a new tag since the original one was stripped off by the
2997 * - Tunnel metadata as received is retained in 'flow'. This allows
2998 * tunnel metadata matching also in later tables.
2999 * Since a kernel action for setting the tunnel metadata will only be
3000 * generated with actual tunnel output, changing the tunnel metadata
3001 * values in 'flow' (such as tun_id) will only have effect with a later
3002 * tunnel output action.
3003 * - Tunnel 'base_flow' is completely cleared since that is what the
3004 * kernel does. If we wish to maintain the original values an action
3005 * needs to be generated. */
3010 ctx.xout->has_learn = false;
3011 ctx.xout->has_normal = false;
3012 ctx.xout->has_fin_timeout = false;
3013 ctx.xout->nf_output_iface = NF_OUT_DROP;
3014 ctx.xout->mirrors = 0;
3015 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3016 sizeof ctx.xout->odp_actions_stub);
3017 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3019 ctx.xbridge = xbridge_lookup(xin->ofproto);
3024 ctx.rule = xin->rule;
3026 ctx.base_flow = *flow;
3027 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3028 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3030 flow_wildcards_init_catchall(wc);
3031 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3032 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3033 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3034 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3036 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3037 if (ctx.xbridge->has_netflow) {
3038 netflow_mask_wc(flow, wc);
3043 ctx.orig_skb_priority = flow->skb_priority;
3046 ctx.mpls_depth_delta = 0;
3048 if (!xin->ofpacts && !ctx.rule) {
3049 rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule);
3050 if (ctx.xin->resubmit_stats) {
3051 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3055 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3058 ofpacts = xin->ofpacts;
3059 ofpacts_len = xin->ofpacts_len;
3060 } else if (ctx.rule) {
3061 actions = rule_dpif_get_actions(ctx.rule);
3062 ofpacts = actions->ofpacts;
3063 ofpacts_len = actions->ofpacts_len;
3068 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3069 ofpbuf_use_stub(&ctx.action_set,
3070 ctx.action_set_stub, sizeof ctx.action_set_stub);
3072 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3073 /* Do this conditionally because the copy is expensive enough that it
3074 * shows up in profiles. */
3078 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3079 switch (ctx.xbridge->frag) {
3080 case OFPC_FRAG_NORMAL:
3081 /* We must pretend that transport ports are unavailable. */
3082 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3083 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3086 case OFPC_FRAG_DROP:
3089 case OFPC_FRAG_REASM:
3092 case OFPC_FRAG_NX_MATCH:
3093 /* Nothing to do. */
3096 case OFPC_INVALID_TTL_TO_CONTROLLER:
3101 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3102 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3104 ctx.xout->slow |= special;
3106 size_t sample_actions_len;
3108 if (flow->in_port.ofp_port
3109 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3110 flow->in_port.ofp_port,
3112 ctx.base_flow.vlan_tci = 0;
3115 add_sflow_action(&ctx);
3116 add_ipfix_action(&ctx);
3117 sample_actions_len = ctx.xout->odp_actions.size;
3119 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3120 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3122 /* We've let OFPP_NORMAL and the learning action look at the
3123 * packet, so drop it now if forwarding is disabled. */
3124 if (in_port && !xport_stp_forward_state(in_port)) {
3125 ctx.xout->odp_actions.size = sample_actions_len;
3129 if (ctx.action_set.size) {
3130 xlate_action_set(&ctx);
3133 if (ctx.xbridge->has_in_band
3134 && in_band_must_output_to_local_port(flow)
3135 && !actions_output_to_local_port(&ctx)) {
3136 compose_output_action(&ctx, OFPP_LOCAL);
3139 fix_sflow_action(&ctx);
3141 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3142 add_mirror_actions(&ctx, &orig_flow);
3146 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3147 /* These datapath actions are too big for a Netlink attribute, so we
3148 * can't hand them to the kernel directly. dpif_execute() can execute
3149 * them one by one with help, so just mark the result as SLOW_ACTION to
3150 * prevent the flow from being installed. */
3151 COVERAGE_INC(xlate_actions_oversize);
3152 ctx.xout->slow |= SLOW_ACTION;
3155 ofpbuf_uninit(&ctx.stack);
3156 ofpbuf_uninit(&ctx.action_set);
3158 /* Clear the metadata and register wildcard masks, because we won't
3159 * use non-header fields as part of the cache. */
3160 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
3161 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
3164 rule_actions_unref(actions);
3165 rule_dpif_unref(rule);
3168 /* Sends 'packet' out 'ofport'.
3169 * May modify 'packet'.
3170 * Returns 0 if successful, otherwise a positive errno value. */
3172 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3174 struct xport *xport;
3175 struct ofpact_output output;
3177 union flow_in_port in_port_;
3180 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3181 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3182 in_port_.ofp_port = OFPP_NONE;
3183 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
3185 ovs_rwlock_rdlock(&xlate_rwlock);
3186 xport = xport_lookup(ofport);
3188 ovs_rwlock_unlock(&xlate_rwlock);
3191 output.port = xport->ofp_port;
3193 error = ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3194 &output.ofpact, sizeof output,
3196 ovs_rwlock_unlock(&xlate_rwlock);