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 !ofputil_bucket_has_liveness(bucket) ||
776 (bucket->watch_port != OFPP_ANY &&
777 odp_port_is_alive(ctx, bucket->watch_port)) ||
778 (bucket->watch_group != OFPG_ANY &&
779 group_is_alive(ctx, bucket->watch_group, depth + 1));
782 static const struct ofputil_bucket *
783 group_first_live_bucket(const struct xlate_ctx *ctx,
784 const struct group_dpif *group, int depth)
786 struct ofputil_bucket *bucket;
787 const struct list *buckets;
789 group_dpif_get_buckets(group, &buckets);
790 LIST_FOR_EACH (bucket, list_node, buckets) {
791 if (bucket_is_alive(ctx, bucket, depth)) {
799 static const struct ofputil_bucket *
800 group_best_live_bucket(const struct xlate_ctx *ctx,
801 const struct group_dpif *group,
804 const struct ofputil_bucket *best_bucket = NULL;
805 uint32_t best_score = 0;
808 const struct ofputil_bucket *bucket;
809 const struct list *buckets;
811 group_dpif_get_buckets(group, &buckets);
812 LIST_FOR_EACH (bucket, list_node, buckets) {
813 if (bucket_is_alive(ctx, bucket, 0)) {
814 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
815 if (score >= best_score) {
816 best_bucket = bucket;
827 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
829 return (bundle->vlan_mode != PORT_VLAN_ACCESS
830 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
834 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
836 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
840 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
842 return xbundle != &ofpp_none_bundle
843 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
848 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
850 return xbundle != &ofpp_none_bundle
851 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
856 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
858 return xbundle != &ofpp_none_bundle
859 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
863 static struct xbundle *
864 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
865 bool warn, struct xport **in_xportp)
869 /* Find the port and bundle for the received packet. */
870 xport = get_ofp_port(xbridge, in_port);
874 if (xport && xport->xbundle) {
875 return xport->xbundle;
878 /* Special-case OFPP_NONE, which a controller may use as the ingress
879 * port for traffic that it is sourcing. */
880 if (in_port == OFPP_NONE) {
881 ofpp_none_bundle.name = "OFPP_NONE";
882 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
883 return &ofpp_none_bundle;
886 /* Odd. A few possible reasons here:
888 * - We deleted a port but there are still a few packets queued up
891 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
892 * we don't know about.
894 * - The ofproto client didn't configure the port as part of a bundle.
895 * This is particularly likely to happen if a packet was received on the
896 * port after it was created, but before the client had a chance to
897 * configure its bundle.
900 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
902 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
903 "port %"PRIu16, xbridge->name, in_port);
909 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
911 const struct xbridge *xbridge = ctx->xbridge;
912 mirror_mask_t mirrors;
913 struct xbundle *in_xbundle;
917 mirrors = ctx->xout->mirrors;
918 ctx->xout->mirrors = 0;
920 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
921 ctx->xin->packet != NULL, NULL);
925 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
927 /* Drop frames on bundles reserved for mirroring. */
928 if (xbundle_mirror_out(xbridge, in_xbundle)) {
929 if (ctx->xin->packet != NULL) {
930 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
931 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
932 "%s, which is reserved exclusively for mirroring",
933 ctx->xbridge->name, in_xbundle->name);
935 ofpbuf_clear(&ctx->xout->odp_actions);
940 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
941 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
944 vlan = input_vid_to_vlan(in_xbundle, vid);
950 /* Restore the original packet before adding the mirror actions. */
951 ctx->xin->flow = *orig_flow;
954 mirror_mask_t dup_mirrors;
955 struct ofbundle *out;
956 unsigned long *vlans;
961 has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1,
962 &vlans, &dup_mirrors, &out, &out_vlan);
963 ovs_assert(has_mirror);
966 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
968 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
971 if (!vlan_mirrored) {
972 mirrors = zero_rightmost_1bit(mirrors);
976 mirrors &= ~dup_mirrors;
977 ctx->xout->mirrors |= dup_mirrors;
979 struct xbundle *out_xbundle = xbundle_lookup(out);
981 output_normal(ctx, out_xbundle, vlan);
983 } else if (vlan != out_vlan
984 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
985 struct xbundle *xbundle;
987 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
988 if (xbundle_includes_vlan(xbundle, out_vlan)
989 && !xbundle_mirror_out(xbridge, xbundle)) {
990 output_normal(ctx, xbundle, out_vlan);
997 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
998 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
999 * the bundle on which the packet was received, returns the VLAN to which the
1002 * Both 'vid' and the return value are in the range 0...4095. */
1004 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1006 switch (in_xbundle->vlan_mode) {
1007 case PORT_VLAN_ACCESS:
1008 return in_xbundle->vlan;
1011 case PORT_VLAN_TRUNK:
1014 case PORT_VLAN_NATIVE_UNTAGGED:
1015 case PORT_VLAN_NATIVE_TAGGED:
1016 return vid ? vid : in_xbundle->vlan;
1023 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1024 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1027 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1028 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1031 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1033 /* Allow any VID on the OFPP_NONE port. */
1034 if (in_xbundle == &ofpp_none_bundle) {
1038 switch (in_xbundle->vlan_mode) {
1039 case PORT_VLAN_ACCESS:
1042 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1043 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1044 "packet received on port %s configured as VLAN "
1045 "%"PRIu16" access port", vid, in_xbundle->name,
1052 case PORT_VLAN_NATIVE_UNTAGGED:
1053 case PORT_VLAN_NATIVE_TAGGED:
1055 /* Port must always carry its native VLAN. */
1059 case PORT_VLAN_TRUNK:
1060 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1062 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1063 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1064 "received on port %s not configured for trunking "
1065 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1077 /* Given 'vlan', the VLAN that a packet belongs to, and
1078 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1079 * that should be included in the 802.1Q header. (If the return value is 0,
1080 * then the 802.1Q header should only be included in the packet if there is a
1083 * Both 'vlan' and the return value are in the range 0...4095. */
1085 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1087 switch (out_xbundle->vlan_mode) {
1088 case PORT_VLAN_ACCESS:
1091 case PORT_VLAN_TRUNK:
1092 case PORT_VLAN_NATIVE_TAGGED:
1095 case PORT_VLAN_NATIVE_UNTAGGED:
1096 return vlan == out_xbundle->vlan ? 0 : vlan;
1104 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1107 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1109 ovs_be16 tci, old_tci;
1110 struct xport *xport;
1112 vid = output_vlan_to_vid(out_xbundle, vlan);
1113 if (list_is_empty(&out_xbundle->xports)) {
1114 /* Partially configured bundle with no slaves. Drop the packet. */
1116 } else if (!out_xbundle->bond) {
1117 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1120 struct ofport_dpif *ofport;
1122 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1123 &ctx->xout->wc, vid);
1124 xport = xport_lookup(ofport);
1127 /* No slaves enabled, so drop packet. */
1132 old_tci = *flow_tci;
1134 if (tci || out_xbundle->use_priority_tags) {
1135 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1137 tci |= htons(VLAN_CFI);
1142 compose_output_action(ctx, xport->ofp_port);
1143 *flow_tci = old_tci;
1146 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1147 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1148 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1150 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1152 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1156 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1157 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1161 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1162 if (flow->nw_proto == ARP_OP_REPLY) {
1164 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1165 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1166 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1168 return flow->nw_src == flow->nw_dst;
1174 /* Checks whether a MAC learning update is necessary for MAC learning table
1175 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1178 * Most packets processed through the MAC learning table do not actually
1179 * change it in any way. This function requires only a read lock on the MAC
1180 * learning table, so it is much cheaper in this common case.
1182 * Keep the code here synchronized with that in update_learning_table__()
1185 is_mac_learning_update_needed(const struct mac_learning *ml,
1186 const struct flow *flow,
1187 struct flow_wildcards *wc,
1188 int vlan, struct xbundle *in_xbundle)
1189 OVS_REQ_RDLOCK(ml->rwlock)
1191 struct mac_entry *mac;
1193 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1197 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1198 if (!mac || mac_entry_age(ml, mac)) {
1202 if (is_gratuitous_arp(flow, wc)) {
1203 /* We don't want to learn from gratuitous ARP packets that are
1204 * reflected back over bond slaves so we lock the learning table. */
1205 if (!in_xbundle->bond) {
1207 } else if (mac_entry_is_grat_arp_locked(mac)) {
1212 return mac->port.p != in_xbundle->ofbundle;
1216 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1217 * received on 'in_xbundle' in 'vlan'.
1219 * This code repeats all the checks in is_mac_learning_update_needed() because
1220 * the lock was released between there and here and thus the MAC learning state
1221 * could have changed.
1223 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1226 update_learning_table__(const struct xbridge *xbridge,
1227 const struct flow *flow, struct flow_wildcards *wc,
1228 int vlan, struct xbundle *in_xbundle)
1229 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1231 struct mac_entry *mac;
1233 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1237 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1238 if (is_gratuitous_arp(flow, wc)) {
1239 /* We don't want to learn from gratuitous ARP packets that are
1240 * reflected back over bond slaves so we lock the learning table. */
1241 if (!in_xbundle->bond) {
1242 mac_entry_set_grat_arp_lock(mac);
1243 } else if (mac_entry_is_grat_arp_locked(mac)) {
1248 if (mac->port.p != in_xbundle->ofbundle) {
1249 /* The log messages here could actually be useful in debugging,
1250 * so keep the rate limit relatively high. */
1251 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1253 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1254 "on port %s in VLAN %d",
1255 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1256 in_xbundle->name, vlan);
1258 mac->port.p = in_xbundle->ofbundle;
1259 mac_learning_changed(xbridge->ml);
1264 update_learning_table(const struct xbridge *xbridge,
1265 const struct flow *flow, struct flow_wildcards *wc,
1266 int vlan, struct xbundle *in_xbundle)
1270 /* Don't learn the OFPP_NONE port. */
1271 if (in_xbundle == &ofpp_none_bundle) {
1275 /* First try the common case: no change to MAC learning table. */
1276 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1277 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1279 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1282 /* Slow path: MAC learning table might need an update. */
1283 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1284 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1285 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1289 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1290 * dropped. Returns true if they may be forwarded, false if they should be
1293 * 'in_port' must be the xport that corresponds to flow->in_port.
1294 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1296 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1297 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1298 * checked by input_vid_is_valid().
1300 * May also add tags to '*tags', although the current implementation only does
1301 * so in one special case.
1304 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1307 struct xbundle *in_xbundle = in_port->xbundle;
1308 const struct xbridge *xbridge = ctx->xbridge;
1309 struct flow *flow = &ctx->xin->flow;
1311 /* Drop frames for reserved multicast addresses
1312 * only if forward_bpdu option is absent. */
1313 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1314 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1318 if (in_xbundle->bond) {
1319 struct mac_entry *mac;
1321 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1327 xlate_report(ctx, "bonding refused admissibility, dropping");
1330 case BV_DROP_IF_MOVED:
1331 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1332 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1333 if (mac && mac->port.p != in_xbundle->ofbundle &&
1334 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1335 || mac_entry_is_grat_arp_locked(mac))) {
1336 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1337 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1341 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1350 xlate_normal(struct xlate_ctx *ctx)
1352 struct flow_wildcards *wc = &ctx->xout->wc;
1353 struct flow *flow = &ctx->xin->flow;
1354 struct xbundle *in_xbundle;
1355 struct xport *in_port;
1356 struct mac_entry *mac;
1361 ctx->xout->has_normal = true;
1363 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1364 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1365 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1367 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1368 ctx->xin->packet != NULL, &in_port);
1370 xlate_report(ctx, "no input bundle, dropping");
1374 /* Drop malformed frames. */
1375 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1376 !(flow->vlan_tci & htons(VLAN_CFI))) {
1377 if (ctx->xin->packet != NULL) {
1378 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1379 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1380 "VLAN tag received on port %s",
1381 ctx->xbridge->name, in_xbundle->name);
1383 xlate_report(ctx, "partial VLAN tag, dropping");
1387 /* Drop frames on bundles reserved for mirroring. */
1388 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1389 if (ctx->xin->packet != NULL) {
1390 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1391 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1392 "%s, which is reserved exclusively for mirroring",
1393 ctx->xbridge->name, in_xbundle->name);
1395 xlate_report(ctx, "input port is mirror output port, dropping");
1400 vid = vlan_tci_to_vid(flow->vlan_tci);
1401 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1402 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1405 vlan = input_vid_to_vlan(in_xbundle, vid);
1407 /* Check other admissibility requirements. */
1408 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1412 /* Learn source MAC. */
1413 if (ctx->xin->may_learn) {
1414 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1417 /* Determine output bundle. */
1418 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1419 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1420 mac_port = mac ? mac->port.p : NULL;
1421 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1424 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1425 if (mac_xbundle && mac_xbundle != in_xbundle) {
1426 xlate_report(ctx, "forwarding to learned port");
1427 output_normal(ctx, mac_xbundle, vlan);
1428 } else if (!mac_xbundle) {
1429 xlate_report(ctx, "learned port is unknown, dropping");
1431 xlate_report(ctx, "learned port is input port, dropping");
1434 struct xbundle *xbundle;
1436 xlate_report(ctx, "no learned MAC for destination, flooding");
1437 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1438 if (xbundle != in_xbundle
1439 && xbundle_includes_vlan(xbundle, vlan)
1440 && xbundle->floodable
1441 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1442 output_normal(ctx, xbundle, vlan);
1445 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1449 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1450 * the number of packets out of UINT32_MAX to sample. The given
1451 * cookie is passed back in the callback for each sampled packet.
1454 compose_sample_action(const struct xbridge *xbridge,
1455 struct ofpbuf *odp_actions,
1456 const struct flow *flow,
1457 const uint32_t probability,
1458 const union user_action_cookie *cookie,
1459 const size_t cookie_size)
1461 size_t sample_offset, actions_offset;
1462 odp_port_t odp_port;
1466 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1468 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1470 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1472 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1473 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1474 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1476 nl_msg_end_nested(odp_actions, actions_offset);
1477 nl_msg_end_nested(odp_actions, sample_offset);
1478 return cookie_offset;
1482 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1483 odp_port_t odp_port, unsigned int n_outputs,
1484 union user_action_cookie *cookie)
1488 cookie->type = USER_ACTION_COOKIE_SFLOW;
1489 cookie->sflow.vlan_tci = vlan_tci;
1491 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1492 * port information") for the interpretation of cookie->output. */
1493 switch (n_outputs) {
1495 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1496 cookie->sflow.output = 0x40000000 | 256;
1500 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1502 cookie->sflow.output = ifindex;
1507 /* 0x80000000 means "multiple output ports. */
1508 cookie->sflow.output = 0x80000000 | n_outputs;
1513 /* Compose SAMPLE action for sFlow bridge sampling. */
1515 compose_sflow_action(const struct xbridge *xbridge,
1516 struct ofpbuf *odp_actions,
1517 const struct flow *flow,
1518 odp_port_t odp_port)
1520 uint32_t probability;
1521 union user_action_cookie cookie;
1523 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1527 probability = dpif_sflow_get_probability(xbridge->sflow);
1528 compose_sflow_cookie(xbridge, htons(0), odp_port,
1529 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1531 return compose_sample_action(xbridge, odp_actions, flow, probability,
1532 &cookie, sizeof cookie.sflow);
1536 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1537 uint32_t obs_domain_id, uint32_t obs_point_id,
1538 union user_action_cookie *cookie)
1540 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1541 cookie->flow_sample.probability = probability;
1542 cookie->flow_sample.collector_set_id = collector_set_id;
1543 cookie->flow_sample.obs_domain_id = obs_domain_id;
1544 cookie->flow_sample.obs_point_id = obs_point_id;
1548 compose_ipfix_cookie(union user_action_cookie *cookie)
1550 cookie->type = USER_ACTION_COOKIE_IPFIX;
1553 /* Compose SAMPLE action for IPFIX bridge sampling. */
1555 compose_ipfix_action(const struct xbridge *xbridge,
1556 struct ofpbuf *odp_actions,
1557 const struct flow *flow)
1559 uint32_t probability;
1560 union user_action_cookie cookie;
1562 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1566 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1567 compose_ipfix_cookie(&cookie);
1569 compose_sample_action(xbridge, odp_actions, flow, probability,
1570 &cookie, sizeof cookie.ipfix);
1573 /* SAMPLE action for sFlow must be first action in any given list of
1574 * actions. At this point we do not have all information required to
1575 * build it. So try to build sample action as complete as possible. */
1577 add_sflow_action(struct xlate_ctx *ctx)
1579 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1580 &ctx->xout->odp_actions,
1581 &ctx->xin->flow, ODPP_NONE);
1582 ctx->sflow_odp_port = 0;
1583 ctx->sflow_n_outputs = 0;
1586 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1587 * of actions, eventually after the SAMPLE action for sFlow. */
1589 add_ipfix_action(struct xlate_ctx *ctx)
1591 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1595 /* Fix SAMPLE action according to data collected while composing ODP actions.
1596 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1597 * USERSPACE action's user-cookie which is required for sflow. */
1599 fix_sflow_action(struct xlate_ctx *ctx)
1601 const struct flow *base = &ctx->base_flow;
1602 union user_action_cookie *cookie;
1604 if (!ctx->user_cookie_offset) {
1608 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1609 sizeof cookie->sflow);
1610 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1612 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1613 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1616 static enum slow_path_reason
1617 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1618 const struct xport *xport, const struct ofpbuf *packet)
1620 struct flow_wildcards *wc = &ctx->xout->wc;
1621 const struct xbridge *xbridge = ctx->xbridge;
1625 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1627 cfm_process_heartbeat(xport->cfm, packet);
1630 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1632 bfd_process_packet(xport->bfd, flow, packet);
1635 } else if (xport->xbundle && xport->xbundle->lacp
1636 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1638 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1641 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1643 stp_process_packet(xport, packet);
1652 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1655 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1656 struct flow_wildcards *wc = &ctx->xout->wc;
1657 struct flow *flow = &ctx->xin->flow;
1658 ovs_be16 flow_vlan_tci;
1659 uint32_t flow_pkt_mark;
1660 uint8_t flow_nw_tos;
1661 odp_port_t out_port, odp_port;
1664 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1665 * before traversing a patch port. */
1666 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 23);
1669 xlate_report(ctx, "Nonexistent output port");
1671 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1672 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1674 } else if (check_stp && !xport_stp_forward_state(xport)) {
1675 xlate_report(ctx, "STP not in forwarding state, skipping output");
1679 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1680 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1685 const struct xport *peer = xport->peer;
1686 struct flow old_flow = ctx->xin->flow;
1687 enum slow_path_reason special;
1689 ctx->xbridge = peer->xbridge;
1690 flow->in_port.ofp_port = peer->ofp_port;
1691 flow->metadata = htonll(0);
1692 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1693 memset(flow->regs, 0, sizeof flow->regs);
1695 special = process_special(ctx, &ctx->xin->flow, peer,
1698 ctx->xout->slow |= special;
1699 } else if (may_receive(peer, ctx)) {
1700 if (xport_stp_forward_state(peer)) {
1701 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1703 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1704 * learning action look at the packet, then drop it. */
1705 struct flow old_base_flow = ctx->base_flow;
1706 size_t old_size = ctx->xout->odp_actions.size;
1707 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1708 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1709 ctx->xout->mirrors = old_mirrors;
1710 ctx->base_flow = old_base_flow;
1711 ctx->xout->odp_actions.size = old_size;
1715 ctx->xin->flow = old_flow;
1716 ctx->xbridge = xport->xbridge;
1718 if (ctx->xin->resubmit_stats) {
1719 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1720 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1722 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1729 flow_vlan_tci = flow->vlan_tci;
1730 flow_pkt_mark = flow->pkt_mark;
1731 flow_nw_tos = flow->nw_tos;
1733 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1734 wc->masks.nw_tos |= IP_ECN_MASK;
1735 flow->nw_tos &= ~IP_DSCP_MASK;
1736 flow->nw_tos |= dscp;
1739 if (xport->is_tunnel) {
1740 /* Save tunnel metadata so that changes made due to
1741 * the Logical (tunnel) Port are not visible for any further
1742 * matches, while explicit set actions on tunnel metadata are.
1744 struct flow_tnl flow_tnl = flow->tunnel;
1745 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1746 if (odp_port == ODPP_NONE) {
1747 xlate_report(ctx, "Tunneling decided against output");
1748 goto out; /* restore flow_nw_tos */
1750 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1751 xlate_report(ctx, "Not tunneling to our own address");
1752 goto out; /* restore flow_nw_tos */
1754 if (ctx->xin->resubmit_stats) {
1755 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1757 out_port = odp_port;
1758 commit_odp_tunnel_action(flow, &ctx->base_flow,
1759 &ctx->xout->odp_actions);
1760 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1762 ofp_port_t vlandev_port;
1764 odp_port = xport->odp_port;
1765 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1766 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1768 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1770 if (vlandev_port == ofp_port) {
1771 out_port = odp_port;
1773 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1774 flow->vlan_tci = htons(0);
1778 if (out_port != ODPP_NONE) {
1779 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1780 &ctx->xout->odp_actions,
1782 &ctx->mpls_depth_delta);
1783 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1786 ctx->sflow_odp_port = odp_port;
1787 ctx->sflow_n_outputs++;
1788 ctx->xout->nf_output_iface = ofp_port;
1793 flow->vlan_tci = flow_vlan_tci;
1794 flow->pkt_mark = flow_pkt_mark;
1795 flow->nw_tos = flow_nw_tos;
1799 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1801 compose_output_action__(ctx, ofp_port, true);
1805 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1807 struct rule_dpif *old_rule = ctx->rule;
1808 struct rule_actions *actions;
1810 if (ctx->xin->resubmit_stats) {
1811 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1817 actions = rule_dpif_get_actions(rule);
1818 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1819 rule_actions_unref(actions);
1820 ctx->rule = old_rule;
1825 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1827 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1829 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1830 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1831 MAX_RESUBMIT_RECURSION);
1832 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1833 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1834 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1835 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1836 } else if (ctx->stack.size >= 65536) {
1837 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1846 xlate_table_action(struct xlate_ctx *ctx,
1847 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1849 if (xlate_resubmit_resource_check(ctx)) {
1850 struct rule_dpif *rule;
1851 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1852 uint8_t old_table_id = ctx->table_id;
1854 ctx->table_id = table_id;
1856 /* Look up a flow with 'in_port' as the input port. Then restore the
1857 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1858 * have surprising behavior). */
1859 ctx->xin->flow.in_port.ofp_port = in_port;
1860 rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1861 &ctx->xin->flow, &ctx->xout->wc,
1863 ctx->xin->flow.in_port.ofp_port = old_in_port;
1865 if (ctx->xin->resubmit_hook) {
1866 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1869 if (!rule && may_packet_in) {
1870 struct xport *xport;
1873 * check if table configuration flags
1874 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1875 * OFPTC11_TABLE_MISS_CONTINUE,
1876 * OFPTC11_TABLE_MISS_DROP
1877 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1878 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1879 choose_miss_rule(xport ? xport->config : 0,
1880 ctx->xbridge->miss_rule,
1881 ctx->xbridge->no_packet_in_rule, &rule);
1884 xlate_recursively(ctx, rule);
1885 rule_dpif_unref(rule);
1888 ctx->table_id = old_table_id;
1896 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1898 uint64_t action_list_stub[1024 / 8];
1899 struct ofpbuf action_list, action_set;
1901 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1902 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1904 ofpacts_execute_action_set(&action_list, &action_set);
1906 do_xlate_actions(action_list.data, action_list.size, ctx);
1909 ofpbuf_uninit(&action_set);
1910 ofpbuf_uninit(&action_list);
1914 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1916 const struct ofputil_bucket *bucket;
1917 const struct list *buckets;
1918 struct flow old_flow = ctx->xin->flow;
1920 group_dpif_get_buckets(group, &buckets);
1922 LIST_FOR_EACH (bucket, list_node, buckets) {
1923 xlate_group_bucket(ctx, bucket);
1924 /* Roll back flow to previous state.
1925 * This is equivalent to cloning the packet for each bucket.
1927 * As a side effect any subsequently applied actions will
1928 * also effectively be applied to a clone of the packet taken
1929 * just before applying the all or indirect group. */
1930 ctx->xin->flow = old_flow;
1935 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1937 const struct ofputil_bucket *bucket;
1939 bucket = group_first_live_bucket(ctx, group, 0);
1941 xlate_group_bucket(ctx, bucket);
1946 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
1948 struct flow_wildcards *wc = &ctx->xout->wc;
1949 const struct ofputil_bucket *bucket;
1952 basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
1953 bucket = group_best_live_bucket(ctx, group, basis);
1955 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1956 xlate_group_bucket(ctx, bucket);
1961 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
1963 switch (group_dpif_get_type(group)) {
1965 case OFPGT11_INDIRECT:
1966 xlate_all_group(ctx, group);
1968 case OFPGT11_SELECT:
1969 xlate_select_group(ctx, group);
1972 xlate_ff_group(ctx, group);
1977 group_dpif_release(group);
1981 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
1983 if (xlate_resubmit_resource_check(ctx)) {
1984 struct group_dpif *group;
1987 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
1989 xlate_group_action__(ctx, group);
1999 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2000 const struct ofpact_resubmit *resubmit)
2005 in_port = resubmit->in_port;
2006 if (in_port == OFPP_IN_PORT) {
2007 in_port = ctx->xin->flow.in_port.ofp_port;
2010 table_id = resubmit->table_id;
2011 if (table_id == 255) {
2012 table_id = ctx->table_id;
2015 xlate_table_action(ctx, in_port, table_id, false);
2019 flood_packets(struct xlate_ctx *ctx, bool all)
2021 const struct xport *xport;
2023 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2024 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2029 compose_output_action__(ctx, xport->ofp_port, false);
2030 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2031 compose_output_action(ctx, xport->ofp_port);
2035 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2039 execute_controller_action(struct xlate_ctx *ctx, int len,
2040 enum ofp_packet_in_reason reason,
2041 uint16_t controller_id)
2043 struct ofproto_packet_in *pin;
2044 struct ofpbuf *packet;
2047 ctx->xout->slow |= SLOW_CONTROLLER;
2048 if (!ctx->xin->packet) {
2052 packet = ofpbuf_clone(ctx->xin->packet);
2054 key.skb_priority = 0;
2056 memset(&key.tunnel, 0, sizeof key.tunnel);
2058 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2059 &ctx->xout->odp_actions,
2061 &ctx->mpls_depth_delta);
2063 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
2064 ctx->xout->odp_actions.size, NULL, NULL);
2066 pin = xmalloc(sizeof *pin);
2067 pin->up.packet_len = packet->size;
2068 pin->up.packet = ofpbuf_steal_data(packet);
2069 pin->up.reason = reason;
2070 pin->up.table_id = ctx->table_id;
2071 pin->up.cookie = (ctx->rule
2072 ? rule_dpif_get_flow_cookie(ctx->rule)
2075 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2077 pin->controller_id = controller_id;
2078 pin->send_len = len;
2079 pin->generated_by_table_miss = (ctx->rule
2080 && rule_dpif_is_table_miss(ctx->rule));
2081 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2082 ofpbuf_delete(packet);
2086 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2088 struct flow_wildcards *wc = &ctx->xout->wc;
2089 struct flow *flow = &ctx->xin->flow;
2091 ovs_assert(eth_type_mpls(eth_type));
2093 /* If mpls_depth_delta is negative then an MPLS POP action has been
2094 * composed and the resulting MPLS label stack is unknown. This means
2095 * an MPLS PUSH action can't be composed as it needs to know either the
2096 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2097 * there is no MPLS label stack present. Thus, stop processing.
2099 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2100 * composed and no further MPLS PUSH action may be performed without
2101 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2102 * Thus, stop processing.
2104 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2105 * has been updated. Performing a MPLS PUSH action may be would result in
2106 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2107 * Thus, stop processing.
2109 * It is planned that in the future this case will be handled
2110 * by recirculation */
2111 if (ctx->mpls_depth_delta ||
2112 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
2116 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2118 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
2120 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2121 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
2126 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2127 label = htonl(0x2); /* IPV6 Explicit Null. */
2129 label = htonl(0x0); /* IPV4 Explicit Null. */
2131 wc->masks.nw_tos |= IP_DSCP_MASK;
2132 wc->masks.nw_ttl = 0xff;
2133 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
2134 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
2135 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
2137 flow->dl_type = eth_type;
2138 ctx->mpls_depth_delta++;
2144 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2146 struct flow_wildcards *wc = &ctx->xout->wc;
2148 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2152 /* If mpls_depth_delta is negative then an MPLS POP action has been
2153 * composed. Performing another MPLS POP action
2154 * would result in losing ether type that results from
2155 * the already composed MPLS POP. Thus, stop processing.
2157 * It is planned that in the future this case will be handled
2158 * by recirculation */
2159 if (ctx->mpls_depth_delta < 0) {
2163 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2165 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2166 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2167 * flow's MPLS LSE should be restored to that value to allow any
2168 * subsequent actions that update of the LSE to be executed correctly.
2170 if (ctx->mpls_depth_delta > 0) {
2171 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
2174 ctx->xin->flow.dl_type = eth_type;
2175 ctx->mpls_depth_delta--;
2181 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2183 struct flow *flow = &ctx->xin->flow;
2185 if (!is_ip_any(flow)) {
2189 ctx->xout->wc.masks.nw_ttl = 0xff;
2190 if (flow->nw_ttl > 1) {
2196 for (i = 0; i < ids->n_controllers; i++) {
2197 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2201 /* Stop processing for current table. */
2207 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2209 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2213 /* If mpls_depth_delta is negative then an MPLS POP action has been
2214 * executed and the resulting MPLS label stack is unknown. This means
2215 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2216 * the top-most MPLS LSE. Thus, stop processing.
2218 * It is planned that in the future this case will be handled
2221 if (ctx->mpls_depth_delta < 0) {
2225 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
2226 set_mpls_lse_label(&ctx->xin->flow.mpls_lse, label);
2231 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2233 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2237 /* If mpls_depth_delta is negative then an MPLS POP action has been
2238 * executed and the resulting MPLS label stack is unknown. This means
2239 * a SET MPLS TC action can't be executed as it needs to manipulate
2240 * the top-most MPLS LSE. Thus, stop processing.
2242 * It is planned that in the future this case will be handled
2245 if (ctx->mpls_depth_delta < 0) {
2249 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
2250 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse, tc);
2255 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2257 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2261 /* If mpls_depth_delta is negative then an MPLS POP action has been
2262 * executed and the resulting MPLS label stack is unknown. This means
2263 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2264 * the top-most MPLS LSE. Thus, stop processing.
2266 * It is planned that in the future this case will be handled
2269 if (ctx->mpls_depth_delta < 0) {
2273 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
2274 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
2279 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2281 struct flow *flow = &ctx->xin->flow;
2282 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2283 struct flow_wildcards *wc = &ctx->xout->wc;
2285 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2287 if (!eth_type_mpls(flow->dl_type)) {
2293 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2296 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2298 /* Stop processing for current table. */
2304 xlate_output_action(struct xlate_ctx *ctx,
2305 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2307 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2309 ctx->xout->nf_output_iface = NF_OUT_DROP;
2313 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2316 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2323 flood_packets(ctx, false);
2326 flood_packets(ctx, true);
2328 case OFPP_CONTROLLER:
2329 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2335 if (port != ctx->xin->flow.in_port.ofp_port) {
2336 compose_output_action(ctx, port);
2338 xlate_report(ctx, "skipping output to input port");
2343 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2344 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2345 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2346 ctx->xout->nf_output_iface = prev_nf_output_iface;
2347 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2348 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2349 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2354 xlate_output_reg_action(struct xlate_ctx *ctx,
2355 const struct ofpact_output_reg *or)
2357 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2358 if (port <= UINT16_MAX) {
2359 union mf_subvalue value;
2361 memset(&value, 0xff, sizeof value);
2362 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2363 xlate_output_action(ctx, u16_to_ofp(port),
2364 or->max_len, false);
2369 xlate_enqueue_action(struct xlate_ctx *ctx,
2370 const struct ofpact_enqueue *enqueue)
2372 ofp_port_t ofp_port = enqueue->port;
2373 uint32_t queue_id = enqueue->queue;
2374 uint32_t flow_priority, priority;
2377 /* Translate queue to priority. */
2378 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2380 /* Fall back to ordinary output action. */
2381 xlate_output_action(ctx, enqueue->port, 0, false);
2385 /* Check output port. */
2386 if (ofp_port == OFPP_IN_PORT) {
2387 ofp_port = ctx->xin->flow.in_port.ofp_port;
2388 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2392 /* Add datapath actions. */
2393 flow_priority = ctx->xin->flow.skb_priority;
2394 ctx->xin->flow.skb_priority = priority;
2395 compose_output_action(ctx, ofp_port);
2396 ctx->xin->flow.skb_priority = flow_priority;
2398 /* Update NetFlow output port. */
2399 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2400 ctx->xout->nf_output_iface = ofp_port;
2401 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2402 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2407 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2409 uint32_t skb_priority;
2411 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2412 ctx->xin->flow.skb_priority = skb_priority;
2414 /* Couldn't translate queue to a priority. Nothing to do. A warning
2415 * has already been logged. */
2420 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2422 const struct xbridge *xbridge = xbridge_;
2433 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2436 port = get_ofp_port(xbridge, ofp_port);
2437 return port ? port->may_enable : false;
2442 xlate_bundle_action(struct xlate_ctx *ctx,
2443 const struct ofpact_bundle *bundle)
2447 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2449 CONST_CAST(struct xbridge *, ctx->xbridge));
2450 if (bundle->dst.field) {
2451 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2454 xlate_output_action(ctx, port, 0, false);
2459 xlate_learn_action(struct xlate_ctx *ctx,
2460 const struct ofpact_learn *learn)
2462 uint64_t ofpacts_stub[1024 / 8];
2463 struct ofputil_flow_mod fm;
2464 struct ofpbuf ofpacts;
2466 ctx->xout->has_learn = true;
2468 learn_mask(learn, &ctx->xout->wc);
2470 if (!ctx->xin->may_learn) {
2474 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2475 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2476 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2477 ofpbuf_uninit(&ofpacts);
2481 xlate_fin_timeout(struct xlate_ctx *ctx,
2482 const struct ofpact_fin_timeout *oft)
2484 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2485 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2486 oft->fin_hard_timeout);
2491 xlate_sample_action(struct xlate_ctx *ctx,
2492 const struct ofpact_sample *os)
2494 union user_action_cookie cookie;
2495 /* Scale the probability from 16-bit to 32-bit while representing
2496 * the same percentage. */
2497 uint32_t probability = (os->probability << 16) | os->probability;
2499 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2500 &ctx->xout->odp_actions,
2502 &ctx->mpls_depth_delta);
2504 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2505 os->obs_domain_id, os->obs_point_id, &cookie);
2506 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2507 probability, &cookie, sizeof cookie.flow_sample);
2511 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2513 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2514 ? OFPUTIL_PC_NO_RECV_STP
2515 : OFPUTIL_PC_NO_RECV)) {
2519 /* Only drop packets here if both forwarding and learning are
2520 * disabled. If just learning is enabled, we need to have
2521 * OFPP_NORMAL and the learning action have a look at the packet
2522 * before we can drop it. */
2523 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2531 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2533 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2534 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2535 ofpact_pad(&ctx->action_set);
2539 xlate_action_set(struct xlate_ctx *ctx)
2541 uint64_t action_list_stub[1024 / 64];
2542 struct ofpbuf action_list;
2544 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2545 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2546 do_xlate_actions(action_list.data, action_list.size, ctx);
2547 ofpbuf_uninit(&action_list);
2551 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2552 struct xlate_ctx *ctx)
2554 struct flow_wildcards *wc = &ctx->xout->wc;
2555 struct flow *flow = &ctx->xin->flow;
2556 const struct ofpact *a;
2558 /* dl_type already in the mask, not set below. */
2560 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2561 struct ofpact_controller *controller;
2562 const struct ofpact_metadata *metadata;
2563 const struct ofpact_set_field *set_field;
2564 const struct mf_field *mf;
2572 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2573 ofpact_get_OUTPUT(a)->max_len, true);
2577 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2582 case OFPACT_CONTROLLER:
2583 controller = ofpact_get_CONTROLLER(a);
2584 execute_controller_action(ctx, controller->max_len,
2586 controller->controller_id);
2589 case OFPACT_ENQUEUE:
2590 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2593 case OFPACT_SET_VLAN_VID:
2594 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2595 if (flow->vlan_tci & htons(VLAN_CFI) ||
2596 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2597 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2598 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2603 case OFPACT_SET_VLAN_PCP:
2604 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2605 if (flow->vlan_tci & htons(VLAN_CFI) ||
2606 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2607 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2608 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2609 << VLAN_PCP_SHIFT) | VLAN_CFI);
2613 case OFPACT_STRIP_VLAN:
2614 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2615 flow->vlan_tci = htons(0);
2618 case OFPACT_PUSH_VLAN:
2619 /* XXX 802.1AD(QinQ) */
2620 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2621 flow->vlan_tci = htons(VLAN_CFI);
2624 case OFPACT_SET_ETH_SRC:
2625 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2626 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2629 case OFPACT_SET_ETH_DST:
2630 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2631 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2634 case OFPACT_SET_IPV4_SRC:
2635 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2636 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2637 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2641 case OFPACT_SET_IPV4_DST:
2642 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2643 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2644 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2648 case OFPACT_SET_IP_DSCP:
2649 if (is_ip_any(flow)) {
2650 wc->masks.nw_tos |= IP_DSCP_MASK;
2651 flow->nw_tos &= ~IP_DSCP_MASK;
2652 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2656 case OFPACT_SET_IP_ECN:
2657 if (is_ip_any(flow)) {
2658 wc->masks.nw_tos |= IP_ECN_MASK;
2659 flow->nw_tos &= ~IP_ECN_MASK;
2660 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2664 case OFPACT_SET_IP_TTL:
2665 if (is_ip_any(flow)) {
2666 wc->masks.nw_ttl = 0xff;
2667 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2671 case OFPACT_SET_L4_SRC_PORT:
2672 if (is_ip_any(flow)) {
2673 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2674 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2675 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2679 case OFPACT_SET_L4_DST_PORT:
2680 if (is_ip_any(flow)) {
2681 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2682 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2683 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2687 case OFPACT_RESUBMIT:
2688 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2691 case OFPACT_SET_TUNNEL:
2692 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2695 case OFPACT_SET_QUEUE:
2696 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2699 case OFPACT_POP_QUEUE:
2700 flow->skb_priority = ctx->orig_skb_priority;
2703 case OFPACT_REG_MOVE:
2704 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2707 case OFPACT_REG_LOAD:
2708 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2711 case OFPACT_SET_FIELD:
2712 set_field = ofpact_get_SET_FIELD(a);
2713 mf = set_field->field;
2714 mf_mask_field_and_prereqs(mf, &wc->masks);
2716 /* Set field action only ever overwrites packet's outermost
2717 * applicable header fields. Do nothing if no header exists. */
2718 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2719 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2720 || flow->mpls_lse)) {
2721 mf_set_flow_value(mf, &set_field->value, flow);
2725 case OFPACT_STACK_PUSH:
2726 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2730 case OFPACT_STACK_POP:
2731 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2735 case OFPACT_PUSH_MPLS:
2736 if (compose_mpls_push_action(ctx,
2737 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2742 case OFPACT_POP_MPLS:
2743 if (compose_mpls_pop_action(ctx,
2744 ofpact_get_POP_MPLS(a)->ethertype)) {
2749 case OFPACT_SET_MPLS_LABEL:
2750 if (compose_set_mpls_label_action(ctx,
2751 ofpact_get_SET_MPLS_LABEL(a)->label)) {
2756 case OFPACT_SET_MPLS_TC:
2757 if (compose_set_mpls_tc_action(ctx,
2758 ofpact_get_SET_MPLS_TC(a)->tc)) {
2763 case OFPACT_SET_MPLS_TTL:
2764 if (compose_set_mpls_ttl_action(ctx,
2765 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2770 case OFPACT_DEC_MPLS_TTL:
2771 if (compose_dec_mpls_ttl_action(ctx)) {
2776 case OFPACT_DEC_TTL:
2777 wc->masks.nw_ttl = 0xff;
2778 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2784 /* Nothing to do. */
2787 case OFPACT_MULTIPATH:
2788 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2792 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2795 case OFPACT_OUTPUT_REG:
2796 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2800 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2807 case OFPACT_FIN_TIMEOUT:
2808 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2809 ctx->xout->has_fin_timeout = true;
2810 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2813 case OFPACT_CLEAR_ACTIONS:
2814 ofpbuf_clear(&ctx->action_set);
2817 case OFPACT_WRITE_ACTIONS:
2818 xlate_write_actions(ctx, a);
2821 case OFPACT_WRITE_METADATA:
2822 metadata = ofpact_get_WRITE_METADATA(a);
2823 flow->metadata &= ~metadata->mask;
2824 flow->metadata |= metadata->metadata & metadata->mask;
2828 /* Not implemented yet. */
2831 case OFPACT_GOTO_TABLE: {
2832 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2834 ovs_assert(ctx->table_id < ogt->table_id);
2835 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2836 ogt->table_id, true);
2841 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2848 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2849 const struct flow *flow, struct rule_dpif *rule,
2850 uint16_t tcp_flags, const struct ofpbuf *packet)
2852 xin->ofproto = ofproto;
2854 xin->packet = packet;
2855 xin->may_learn = packet != NULL;
2857 xin->ofpacts = NULL;
2858 xin->ofpacts_len = 0;
2859 xin->tcp_flags = tcp_flags;
2860 xin->resubmit_hook = NULL;
2861 xin->report_hook = NULL;
2862 xin->resubmit_stats = NULL;
2866 xlate_out_uninit(struct xlate_out *xout)
2869 ofpbuf_uninit(&xout->odp_actions);
2873 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2874 * into datapath actions, using 'ctx', and discards the datapath actions. */
2876 xlate_actions_for_side_effects(struct xlate_in *xin)
2878 struct xlate_out xout;
2880 xlate_actions(xin, &xout);
2881 xlate_out_uninit(&xout);
2885 xlate_report(struct xlate_ctx *ctx, const char *s)
2887 if (ctx->xin->report_hook) {
2888 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2893 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2896 dst->slow = src->slow;
2897 dst->has_learn = src->has_learn;
2898 dst->has_normal = src->has_normal;
2899 dst->has_fin_timeout = src->has_fin_timeout;
2900 dst->nf_output_iface = src->nf_output_iface;
2901 dst->mirrors = src->mirrors;
2903 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2904 sizeof dst->odp_actions_stub);
2905 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2906 src->odp_actions.size);
2909 /* Returns a reference to the sflow handled associated with ofproto, or NULL if
2910 * there is none. The caller is responsible for decrementing the results ref
2911 * count with dpif_sflow_unref(). */
2913 xlate_get_sflow(const struct ofproto_dpif *ofproto)
2915 struct dpif_sflow *sflow = NULL;
2916 struct xbridge *xbridge;
2918 ovs_rwlock_rdlock(&xlate_rwlock);
2919 xbridge = xbridge_lookup(ofproto);
2921 sflow = dpif_sflow_ref(xbridge->sflow);
2923 ovs_rwlock_unlock(&xlate_rwlock);
2928 /* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2929 * there is none. The caller is responsible for decrementing the results ref
2930 * count with dpif_ipfix_unref(). */
2932 xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2934 struct dpif_ipfix *ipfix = NULL;
2935 struct xbridge *xbridge;
2937 ovs_rwlock_rdlock(&xlate_rwlock);
2938 xbridge = xbridge_lookup(ofproto);
2940 ipfix = dpif_ipfix_ref(xbridge->ipfix);
2942 ovs_rwlock_unlock(&xlate_rwlock);
2947 static struct skb_priority_to_dscp *
2948 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2950 struct skb_priority_to_dscp *pdscp;
2953 hash = hash_int(skb_priority, 0);
2954 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2955 if (pdscp->skb_priority == skb_priority) {
2963 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2966 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2967 *dscp = pdscp ? pdscp->dscp : 0;
2968 return pdscp != NULL;
2972 clear_skb_priorities(struct xport *xport)
2974 struct skb_priority_to_dscp *pdscp, *next;
2976 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2977 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2983 actions_output_to_local_port(const struct xlate_ctx *ctx)
2985 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2986 const struct nlattr *a;
2989 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2990 ctx->xout->odp_actions.size) {
2991 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2992 && nl_attr_get_odp_port(a) == local_odp_port) {
2999 /* Thread safe call to xlate_actions__(). */
3001 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3003 ovs_rwlock_rdlock(&xlate_rwlock);
3004 xlate_actions__(xin, xout);
3005 ovs_rwlock_unlock(&xlate_rwlock);
3008 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3009 * into datapath actions in 'odp_actions', using 'ctx'.
3011 * The caller must take responsibility for eventually freeing 'xout', with
3012 * xlate_out_uninit(). */
3014 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3015 OVS_REQ_RDLOCK(xlate_rwlock)
3017 struct flow_wildcards *wc = &xout->wc;
3018 struct flow *flow = &xin->flow;
3019 struct rule_dpif *rule = NULL;
3021 struct rule_actions *actions = NULL;
3022 enum slow_path_reason special;
3023 const struct ofpact *ofpacts;
3024 struct xport *in_port;
3025 struct flow orig_flow;
3026 struct xlate_ctx ctx;
3030 COVERAGE_INC(xlate_actions);
3032 /* Flow initialization rules:
3033 * - 'base_flow' must match the kernel's view of the packet at the
3034 * time that action processing starts. 'flow' represents any
3035 * transformations we wish to make through actions.
3036 * - By default 'base_flow' and 'flow' are the same since the input
3037 * packet matches the output before any actions are applied.
3038 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3039 * of the received packet as seen by the kernel. If we later output
3040 * to another device without any modifications this will cause us to
3041 * insert a new tag since the original one was stripped off by the
3043 * - Tunnel metadata as received is retained in 'flow'. This allows
3044 * tunnel metadata matching also in later tables.
3045 * Since a kernel action for setting the tunnel metadata will only be
3046 * generated with actual tunnel output, changing the tunnel metadata
3047 * values in 'flow' (such as tun_id) will only have effect with a later
3048 * tunnel output action.
3049 * - Tunnel 'base_flow' is completely cleared since that is what the
3050 * kernel does. If we wish to maintain the original values an action
3051 * needs to be generated. */
3056 ctx.xout->has_learn = false;
3057 ctx.xout->has_normal = false;
3058 ctx.xout->has_fin_timeout = false;
3059 ctx.xout->nf_output_iface = NF_OUT_DROP;
3060 ctx.xout->mirrors = 0;
3061 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3062 sizeof ctx.xout->odp_actions_stub);
3063 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3065 ctx.xbridge = xbridge_lookup(xin->ofproto);
3070 ctx.rule = xin->rule;
3072 ctx.base_flow = *flow;
3073 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3074 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3076 flow_wildcards_init_catchall(wc);
3077 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3078 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3079 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3080 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3082 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3083 if (ctx.xbridge->has_netflow) {
3084 netflow_mask_wc(flow, wc);
3089 ctx.orig_skb_priority = flow->skb_priority;
3092 ctx.mpls_depth_delta = 0;
3094 if (!xin->ofpacts && !ctx.rule) {
3095 rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule);
3096 if (ctx.xin->resubmit_stats) {
3097 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3101 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3104 ofpacts = xin->ofpacts;
3105 ofpacts_len = xin->ofpacts_len;
3106 } else if (ctx.rule) {
3107 actions = rule_dpif_get_actions(ctx.rule);
3108 ofpacts = actions->ofpacts;
3109 ofpacts_len = actions->ofpacts_len;
3114 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3115 ofpbuf_use_stub(&ctx.action_set,
3116 ctx.action_set_stub, sizeof ctx.action_set_stub);
3118 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3119 /* Do this conditionally because the copy is expensive enough that it
3120 * shows up in profiles. */
3124 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3125 switch (ctx.xbridge->frag) {
3126 case OFPC_FRAG_NORMAL:
3127 /* We must pretend that transport ports are unavailable. */
3128 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3129 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3132 case OFPC_FRAG_DROP:
3135 case OFPC_FRAG_REASM:
3138 case OFPC_FRAG_NX_MATCH:
3139 /* Nothing to do. */
3142 case OFPC_INVALID_TTL_TO_CONTROLLER:
3147 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3148 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3150 ctx.xout->slow |= special;
3152 size_t sample_actions_len;
3154 if (flow->in_port.ofp_port
3155 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3156 flow->in_port.ofp_port,
3158 ctx.base_flow.vlan_tci = 0;
3161 add_sflow_action(&ctx);
3162 add_ipfix_action(&ctx);
3163 sample_actions_len = ctx.xout->odp_actions.size;
3165 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3166 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3168 /* We've let OFPP_NORMAL and the learning action look at the
3169 * packet, so drop it now if forwarding is disabled. */
3170 if (in_port && !xport_stp_forward_state(in_port)) {
3171 ctx.xout->odp_actions.size = sample_actions_len;
3175 if (ctx.action_set.size) {
3176 xlate_action_set(&ctx);
3179 if (ctx.xbridge->has_in_band
3180 && in_band_must_output_to_local_port(flow)
3181 && !actions_output_to_local_port(&ctx)) {
3182 compose_output_action(&ctx, OFPP_LOCAL);
3185 fix_sflow_action(&ctx);
3187 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3188 add_mirror_actions(&ctx, &orig_flow);
3192 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3193 /* These datapath actions are too big for a Netlink attribute, so we
3194 * can't hand them to the kernel directly. dpif_execute() can execute
3195 * them one by one with help, so just mark the result as SLOW_ACTION to
3196 * prevent the flow from being installed. */
3197 COVERAGE_INC(xlate_actions_oversize);
3198 ctx.xout->slow |= SLOW_ACTION;
3201 ofpbuf_uninit(&ctx.stack);
3202 ofpbuf_uninit(&ctx.action_set);
3204 /* Clear the metadata and register wildcard masks, because we won't
3205 * use non-header fields as part of the cache. */
3206 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
3207 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
3210 rule_actions_unref(actions);
3211 rule_dpif_unref(rule);
3214 /* Sends 'packet' out 'ofport'.
3215 * May modify 'packet'.
3216 * Returns 0 if successful, otherwise a positive errno value. */
3218 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3220 struct xport *xport;
3221 struct ofpact_output output;
3223 union flow_in_port in_port_;
3226 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3227 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3228 in_port_.ofp_port = OFPP_NONE;
3229 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
3231 ovs_rwlock_rdlock(&xlate_rwlock);
3232 xport = xport_lookup(ofport);
3234 ovs_rwlock_unlock(&xlate_rwlock);
3237 output.port = xport->ofp_port;
3239 error = ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3240 &output.ofpact, sizeof output,
3242 ovs_rwlock_unlock(&xlate_rwlock);