1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58 /* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60 #define MAX_RESUBMIT_RECURSION 64
61 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
64 /* Maximum number of resubmit actions in a flow translation, whether they are
65 * recursive or not. */
66 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
68 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
71 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
72 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
74 struct list xbundles; /* Owned xbundles. */
75 struct hmap xports; /* Indexed by ofp_port. */
77 char *name; /* Name used in log messages. */
78 struct dpif *dpif; /* Datapath interface. */
79 struct mac_learning *ml; /* Mac learning handle. */
80 struct mbridge *mbridge; /* Mirroring. */
81 struct dpif_sflow *sflow; /* SFlow handle, or null. */
82 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
83 struct netflow *netflow; /* Netflow handle, or null. */
84 struct stp *stp; /* STP or null if disabled. */
86 /* Special rules installed by ofproto-dpif. */
87 struct rule_dpif *miss_rule;
88 struct rule_dpif *no_packet_in_rule;
90 enum ofp_config_flags frag; /* Fragmentation handling. */
91 bool has_in_band; /* Bridge has in band control? */
92 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
94 /* True if the datapath supports recirculation. */
97 /* True if the datapath supports variable-length
98 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
99 * False if the datapath supports only 8-byte (or shorter) userdata. */
100 bool variable_length_userdata;
102 /* Number of MPLS label stack entries that the datapath supports
104 size_t max_mpls_depth;
108 struct hmap_node hmap_node; /* In global 'xbundles' map. */
109 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
111 struct list list_node; /* In parent 'xbridges' list. */
112 struct xbridge *xbridge; /* Parent xbridge. */
114 struct list xports; /* Contains "struct xport"s. */
116 char *name; /* Name used in log messages. */
117 struct bond *bond; /* Nonnull iff more than one port. */
118 struct lacp *lacp; /* LACP handle or null. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode. */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
125 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
129 struct hmap_node hmap_node; /* Node in global 'xports' map. */
130 struct ofport_dpif *ofport; /* Key in global 'xports map. */
132 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
133 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
135 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
137 struct list bundle_node; /* In parent xbundle (if it exists). */
138 struct xbundle *xbundle; /* Parent xbundle or null. */
140 struct netdev *netdev; /* 'ofport''s netdev. */
142 struct xbridge *xbridge; /* Parent bridge. */
143 struct xport *peer; /* Patch port peer or null. */
145 enum ofputil_port_config config; /* OpenFlow port configuration. */
146 enum ofputil_port_state state; /* OpenFlow port state. */
147 int stp_port_no; /* STP port number or -1 if not in use. */
149 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
151 bool may_enable; /* May be enabled in bonds. */
152 bool is_tunnel; /* Is a tunnel port. */
154 struct cfm *cfm; /* CFM handle or null. */
155 struct bfd *bfd; /* BFD handle or null. */
159 struct xlate_in *xin;
160 struct xlate_out *xout;
162 const struct xbridge *xbridge;
164 /* Flow at the last commit. */
165 struct flow base_flow;
167 /* Tunnel IP destination address as received. This is stored separately
168 * as the base_flow.tunnel is cleared on init to reflect the datapath
169 * behavior. Used to make sure not to send tunneled output to ourselves,
170 * which might lead to an infinite loop. This could happen easily
171 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
172 * actually set the tun_dst field. */
173 ovs_be32 orig_tunnel_ip_dst;
175 /* Stack for the push and pop actions. Each stack element is of type
176 * "union mf_subvalue". */
177 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
183 /* Resubmit statistics, via xlate_table_action(). */
184 int recurse; /* Current resubmit nesting depth. */
185 int resubmits; /* Total number of resubmits. */
186 bool in_group; /* Currently translating ofgroup, if true. */
188 uint32_t orig_skb_priority; /* Priority when packet arrived. */
189 uint8_t table_id; /* OpenFlow table ID where flow was found. */
190 uint32_t sflow_n_outputs; /* Number of output ports. */
191 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
192 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
193 bool exit; /* No further actions should be processed. */
195 bool use_recirc; /* Should generate recirc? */
196 struct xlate_recirc recirc; /* Information used for generating
197 * recirculation actions */
199 /* OpenFlow 1.1+ action set.
201 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
202 * When translation is otherwise complete, ofpacts_execute_action_set()
203 * converts it to a set of "struct ofpact"s that can be translated into
204 * datapath actions. */
205 struct ofpbuf action_set; /* Action set. */
206 uint64_t action_set_stub[1024 / 8];
209 /* A controller may use OFPP_NONE as the ingress port to indicate that
210 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
211 * when an input bundle is needed for validation (e.g., mirroring or
212 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
213 * any 'port' structs, so care must be taken when dealing with it. */
214 static struct xbundle ofpp_none_bundle = {
216 .vlan_mode = PORT_VLAN_TRUNK
219 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
220 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
221 * traffic egressing the 'ofport' with that priority should be marked with. */
222 struct skb_priority_to_dscp {
223 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
224 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
226 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
240 /* xlate_cache entries hold enough information to perform the side effects of
241 * xlate_actions() for a rule, without needing to perform rule translation
242 * from scratch. The primary usage of these is to submit statistics to objects
243 * that a flow relates to, although they may be used for other effects as well
244 * (for instance, refreshing hard timeouts for learned flows). */
248 struct rule_dpif *rule;
255 struct netflow *netflow;
260 struct mbridge *mbridge;
261 mirror_mask_t mirrors;
269 struct ofproto_dpif *ofproto;
270 struct rule_dpif *rule;
273 struct ofproto_dpif *ofproto;
278 struct rule_dpif *rule;
285 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
286 entries = xcache->entries; \
287 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
289 entry = ofpbuf_try_pull(&entries, sizeof *entry))
292 struct ofpbuf entries;
295 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
296 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
297 static struct hmap xports = HMAP_INITIALIZER(&xports);
299 static bool may_receive(const struct xport *, struct xlate_ctx *);
300 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
302 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
303 OVS_REQ_RDLOCK(xlate_rwlock);
304 static void xlate_normal(struct xlate_ctx *);
305 static void xlate_report(struct xlate_ctx *, const char *);
306 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
307 uint8_t table_id, bool may_packet_in,
308 bool honor_table_miss);
309 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
310 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
311 static void output_normal(struct xlate_ctx *, const struct xbundle *,
313 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
315 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
316 static struct xbundle *xbundle_lookup(const struct ofbundle *);
317 static struct xport *xport_lookup(const struct ofport_dpif *);
318 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
319 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
320 uint32_t skb_priority);
321 static void clear_skb_priorities(struct xport *);
322 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
325 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
329 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
330 struct dpif *dpif, struct rule_dpif *miss_rule,
331 struct rule_dpif *no_packet_in_rule,
332 const struct mac_learning *ml, struct stp *stp,
333 const struct mbridge *mbridge,
334 const struct dpif_sflow *sflow,
335 const struct dpif_ipfix *ipfix,
336 const struct netflow *netflow, enum ofp_config_flags frag,
337 bool forward_bpdu, bool has_in_band,
339 bool variable_length_userdata,
340 size_t max_mpls_depth)
342 struct xbridge *xbridge = xbridge_lookup(ofproto);
345 xbridge = xzalloc(sizeof *xbridge);
346 xbridge->ofproto = ofproto;
348 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
349 hmap_init(&xbridge->xports);
350 list_init(&xbridge->xbundles);
353 if (xbridge->ml != ml) {
354 mac_learning_unref(xbridge->ml);
355 xbridge->ml = mac_learning_ref(ml);
358 if (xbridge->mbridge != mbridge) {
359 mbridge_unref(xbridge->mbridge);
360 xbridge->mbridge = mbridge_ref(mbridge);
363 if (xbridge->sflow != sflow) {
364 dpif_sflow_unref(xbridge->sflow);
365 xbridge->sflow = dpif_sflow_ref(sflow);
368 if (xbridge->ipfix != ipfix) {
369 dpif_ipfix_unref(xbridge->ipfix);
370 xbridge->ipfix = dpif_ipfix_ref(ipfix);
373 if (xbridge->stp != stp) {
374 stp_unref(xbridge->stp);
375 xbridge->stp = stp_ref(stp);
378 if (xbridge->netflow != netflow) {
379 netflow_unref(xbridge->netflow);
380 xbridge->netflow = netflow_ref(netflow);
384 xbridge->name = xstrdup(name);
386 xbridge->dpif = dpif;
387 xbridge->forward_bpdu = forward_bpdu;
388 xbridge->has_in_band = has_in_band;
389 xbridge->frag = frag;
390 xbridge->miss_rule = miss_rule;
391 xbridge->no_packet_in_rule = no_packet_in_rule;
392 xbridge->enable_recirc = enable_recirc;
393 xbridge->variable_length_userdata = variable_length_userdata;
394 xbridge->max_mpls_depth = max_mpls_depth;
398 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
400 struct xbridge *xbridge = xbridge_lookup(ofproto);
401 struct xbundle *xbundle, *next_xbundle;
402 struct xport *xport, *next_xport;
408 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
409 xlate_ofport_remove(xport->ofport);
412 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
413 xlate_bundle_remove(xbundle->ofbundle);
416 hmap_remove(&xbridges, &xbridge->hmap_node);
417 mac_learning_unref(xbridge->ml);
418 mbridge_unref(xbridge->mbridge);
419 dpif_sflow_unref(xbridge->sflow);
420 dpif_ipfix_unref(xbridge->ipfix);
421 stp_unref(xbridge->stp);
422 hmap_destroy(&xbridge->xports);
428 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
429 const char *name, enum port_vlan_mode vlan_mode, int vlan,
430 unsigned long *trunks, bool use_priority_tags,
431 const struct bond *bond, const struct lacp *lacp,
434 struct xbundle *xbundle = xbundle_lookup(ofbundle);
437 xbundle = xzalloc(sizeof *xbundle);
438 xbundle->ofbundle = ofbundle;
439 xbundle->xbridge = xbridge_lookup(ofproto);
441 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
442 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
443 list_init(&xbundle->xports);
446 ovs_assert(xbundle->xbridge);
449 xbundle->name = xstrdup(name);
451 xbundle->vlan_mode = vlan_mode;
452 xbundle->vlan = vlan;
453 xbundle->trunks = trunks;
454 xbundle->use_priority_tags = use_priority_tags;
455 xbundle->floodable = floodable;
457 if (xbundle->bond != bond) {
458 bond_unref(xbundle->bond);
459 xbundle->bond = bond_ref(bond);
462 if (xbundle->lacp != lacp) {
463 lacp_unref(xbundle->lacp);
464 xbundle->lacp = lacp_ref(lacp);
469 xlate_bundle_remove(struct ofbundle *ofbundle)
471 struct xbundle *xbundle = xbundle_lookup(ofbundle);
472 struct xport *xport, *next;
478 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
479 list_remove(&xport->bundle_node);
480 xport->xbundle = NULL;
483 hmap_remove(&xbundles, &xbundle->hmap_node);
484 list_remove(&xbundle->list_node);
485 bond_unref(xbundle->bond);
486 lacp_unref(xbundle->lacp);
492 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
493 struct ofport_dpif *ofport, ofp_port_t ofp_port,
494 odp_port_t odp_port, const struct netdev *netdev,
495 const struct cfm *cfm, const struct bfd *bfd,
496 struct ofport_dpif *peer, int stp_port_no,
497 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
498 enum ofputil_port_config config,
499 enum ofputil_port_state state, bool is_tunnel,
502 struct xport *xport = xport_lookup(ofport);
506 xport = xzalloc(sizeof *xport);
507 xport->ofport = ofport;
508 xport->xbridge = xbridge_lookup(ofproto);
509 xport->ofp_port = ofp_port;
511 hmap_init(&xport->skb_priorities);
512 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
513 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
514 hash_ofp_port(xport->ofp_port));
517 ovs_assert(xport->ofp_port == ofp_port);
519 xport->config = config;
520 xport->state = state;
521 xport->stp_port_no = stp_port_no;
522 xport->is_tunnel = is_tunnel;
523 xport->may_enable = may_enable;
524 xport->odp_port = odp_port;
526 if (xport->netdev != netdev) {
527 netdev_close(xport->netdev);
528 xport->netdev = netdev_ref(netdev);
531 if (xport->cfm != cfm) {
532 cfm_unref(xport->cfm);
533 xport->cfm = cfm_ref(cfm);
536 if (xport->bfd != bfd) {
537 bfd_unref(xport->bfd);
538 xport->bfd = bfd_ref(bfd);
542 xport->peer->peer = NULL;
544 xport->peer = xport_lookup(peer);
546 xport->peer->peer = xport;
549 if (xport->xbundle) {
550 list_remove(&xport->bundle_node);
552 xport->xbundle = xbundle_lookup(ofbundle);
553 if (xport->xbundle) {
554 list_insert(&xport->xbundle->xports, &xport->bundle_node);
557 clear_skb_priorities(xport);
558 for (i = 0; i < n_qdscp; i++) {
559 struct skb_priority_to_dscp *pdscp;
560 uint32_t skb_priority;
562 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
567 pdscp = xmalloc(sizeof *pdscp);
568 pdscp->skb_priority = skb_priority;
569 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
570 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
571 hash_int(pdscp->skb_priority, 0));
576 xlate_ofport_remove(struct ofport_dpif *ofport)
578 struct xport *xport = xport_lookup(ofport);
585 xport->peer->peer = NULL;
589 if (xport->xbundle) {
590 list_remove(&xport->bundle_node);
593 clear_skb_priorities(xport);
594 hmap_destroy(&xport->skb_priorities);
596 hmap_remove(&xports, &xport->hmap_node);
597 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
599 netdev_close(xport->netdev);
600 cfm_unref(xport->cfm);
601 bfd_unref(xport->bfd);
605 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
606 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
607 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
608 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
609 * 'netflow' with the appropriate handles for those protocols if they're
610 * enabled. Caller is responsible for unrefing them.
612 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
613 * 'flow''s in_port to OFPP_NONE.
615 * This function does post-processing on data returned from
616 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
617 * of the upcall processing logic. In particular, if the extracted in_port is
618 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
619 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
620 * a VLAN header onto 'packet' (if it is nonnull).
622 * Similarly, this function also includes some logic to help with tunnels. It
623 * may modify 'flow' as necessary to make the tunneling implementation
624 * transparent to the upcall processing logic.
626 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
627 * or some other positive errno if there are other problems. */
629 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
630 const struct nlattr *key, size_t key_len, struct flow *flow,
631 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
632 struct dpif_sflow **sflow, struct netflow **netflow,
633 odp_port_t *odp_in_port)
635 const struct xport *xport;
638 ovs_rwlock_rdlock(&xlate_rwlock);
639 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
645 *odp_in_port = flow->in_port.odp_port;
648 xport = xport_lookup(tnl_port_should_receive(flow)
649 ? tnl_port_receive(flow)
650 : odp_port_to_ofport(backer, flow->in_port.odp_port));
652 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
657 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
659 /* Make the packet resemble the flow, so that it gets sent to
660 * an OpenFlow controller properly, so that it looks correct
661 * for sFlow, and so that flow_extract() will get the correct
662 * vlan_tci if it is called on 'packet'. */
663 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
669 *ofproto = xport->xbridge->ofproto;
673 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
677 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
681 *netflow = netflow_ref(xport->xbridge->netflow);
685 ovs_rwlock_unlock(&xlate_rwlock);
689 static struct xbridge *
690 xbridge_lookup(const struct ofproto_dpif *ofproto)
692 struct xbridge *xbridge;
698 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
700 if (xbridge->ofproto == ofproto) {
707 static struct xbundle *
708 xbundle_lookup(const struct ofbundle *ofbundle)
710 struct xbundle *xbundle;
716 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
718 if (xbundle->ofbundle == ofbundle) {
725 static struct xport *
726 xport_lookup(const struct ofport_dpif *ofport)
734 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
736 if (xport->ofport == ofport) {
743 static struct stp_port *
744 xport_get_stp_port(const struct xport *xport)
746 return xport->xbridge->stp && xport->stp_port_no != -1
747 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
752 xport_stp_learn_state(const struct xport *xport)
754 struct stp_port *sp = xport_get_stp_port(xport);
755 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
759 xport_stp_forward_state(const struct xport *xport)
761 struct stp_port *sp = xport_get_stp_port(xport);
762 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
766 xport_stp_listen_state(const struct xport *xport)
768 struct stp_port *sp = xport_get_stp_port(xport);
769 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
772 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
773 * were used to make the determination.*/
775 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
777 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
778 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
783 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
785 struct stp_port *sp = xport_get_stp_port(xport);
786 struct ofpbuf payload = *packet;
787 struct eth_header *eth = ofpbuf_data(&payload);
789 /* Sink packets on ports that have STP disabled when the bridge has
791 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
795 /* Trim off padding on payload. */
796 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
797 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
800 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
801 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
805 static struct xport *
806 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
810 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
812 if (xport->ofp_port == ofp_port) {
820 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
822 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
823 return xport ? xport->odp_port : ODPP_NONE;
827 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
831 xport = get_ofp_port(ctx->xbridge, ofp_port);
832 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
833 xport->state & OFPUTIL_PS_LINK_DOWN) {
840 static const struct ofputil_bucket *
841 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
845 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
847 struct group_dpif *group;
850 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
855 hit = group_first_live_bucket(ctx, group, depth) != NULL;
857 group_dpif_release(group);
861 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
864 bucket_is_alive(const struct xlate_ctx *ctx,
865 const struct ofputil_bucket *bucket, int depth)
867 if (depth >= MAX_LIVENESS_RECURSION) {
868 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
870 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
871 MAX_LIVENESS_RECURSION);
875 return !ofputil_bucket_has_liveness(bucket) ||
876 (bucket->watch_port != OFPP_ANY &&
877 odp_port_is_alive(ctx, bucket->watch_port)) ||
878 (bucket->watch_group != OFPG_ANY &&
879 group_is_alive(ctx, bucket->watch_group, depth + 1));
882 static const struct ofputil_bucket *
883 group_first_live_bucket(const struct xlate_ctx *ctx,
884 const struct group_dpif *group, int depth)
886 struct ofputil_bucket *bucket;
887 const struct list *buckets;
889 group_dpif_get_buckets(group, &buckets);
890 LIST_FOR_EACH (bucket, list_node, buckets) {
891 if (bucket_is_alive(ctx, bucket, depth)) {
899 static const struct ofputil_bucket *
900 group_best_live_bucket(const struct xlate_ctx *ctx,
901 const struct group_dpif *group,
904 const struct ofputil_bucket *best_bucket = NULL;
905 uint32_t best_score = 0;
908 const struct ofputil_bucket *bucket;
909 const struct list *buckets;
911 group_dpif_get_buckets(group, &buckets);
912 LIST_FOR_EACH (bucket, list_node, buckets) {
913 if (bucket_is_alive(ctx, bucket, 0)) {
914 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
915 if (score >= best_score) {
916 best_bucket = bucket;
927 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
929 return (bundle->vlan_mode != PORT_VLAN_ACCESS
930 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
934 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
936 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
940 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
942 return xbundle != &ofpp_none_bundle
943 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
948 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
950 return xbundle != &ofpp_none_bundle
951 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
956 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
958 return xbundle != &ofpp_none_bundle
959 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
963 static struct xbundle *
964 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
965 bool warn, struct xport **in_xportp)
969 /* Find the port and bundle for the received packet. */
970 xport = get_ofp_port(xbridge, in_port);
974 if (xport && xport->xbundle) {
975 return xport->xbundle;
978 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
979 * which a controller may use as the ingress port for traffic that
981 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
982 return &ofpp_none_bundle;
985 /* Odd. A few possible reasons here:
987 * - We deleted a port but there are still a few packets queued up
990 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
991 * we don't know about.
993 * - The ofproto client didn't configure the port as part of a bundle.
994 * This is particularly likely to happen if a packet was received on the
995 * port after it was created, but before the client had a chance to
996 * configure its bundle.
999 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1001 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1002 "port %"PRIu16, xbridge->name, in_port);
1008 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1010 const struct xbridge *xbridge = ctx->xbridge;
1011 mirror_mask_t mirrors;
1012 struct xbundle *in_xbundle;
1016 mirrors = ctx->xout->mirrors;
1017 ctx->xout->mirrors = 0;
1019 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1020 ctx->xin->packet != NULL, NULL);
1024 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1026 /* Drop frames on bundles reserved for mirroring. */
1027 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1028 if (ctx->xin->packet != NULL) {
1029 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1030 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1031 "%s, which is reserved exclusively for mirroring",
1032 ctx->xbridge->name, in_xbundle->name);
1034 ofpbuf_clear(&ctx->xout->odp_actions);
1039 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1040 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1043 vlan = input_vid_to_vlan(in_xbundle, vid);
1049 /* Restore the original packet before adding the mirror actions. */
1050 ctx->xin->flow = *orig_flow;
1053 mirror_mask_t dup_mirrors;
1054 struct ofbundle *out;
1055 unsigned long *vlans;
1060 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1061 &vlans, &dup_mirrors, &out, &out_vlan);
1062 ovs_assert(has_mirror);
1065 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1067 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1070 if (!vlan_mirrored) {
1071 mirrors = zero_rightmost_1bit(mirrors);
1075 mirrors &= ~dup_mirrors;
1076 ctx->xout->mirrors |= dup_mirrors;
1078 struct xbundle *out_xbundle = xbundle_lookup(out);
1080 output_normal(ctx, out_xbundle, vlan);
1082 } else if (vlan != out_vlan
1083 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1084 struct xbundle *xbundle;
1086 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1087 if (xbundle_includes_vlan(xbundle, out_vlan)
1088 && !xbundle_mirror_out(xbridge, xbundle)) {
1089 output_normal(ctx, xbundle, out_vlan);
1096 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1097 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1098 * the bundle on which the packet was received, returns the VLAN to which the
1101 * Both 'vid' and the return value are in the range 0...4095. */
1103 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1105 switch (in_xbundle->vlan_mode) {
1106 case PORT_VLAN_ACCESS:
1107 return in_xbundle->vlan;
1110 case PORT_VLAN_TRUNK:
1113 case PORT_VLAN_NATIVE_UNTAGGED:
1114 case PORT_VLAN_NATIVE_TAGGED:
1115 return vid ? vid : in_xbundle->vlan;
1122 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1123 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1126 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1127 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1130 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1132 /* Allow any VID on the OFPP_NONE port. */
1133 if (in_xbundle == &ofpp_none_bundle) {
1137 switch (in_xbundle->vlan_mode) {
1138 case PORT_VLAN_ACCESS:
1141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1142 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1143 "packet received on port %s configured as VLAN "
1144 "%"PRIu16" access port", vid, in_xbundle->name,
1151 case PORT_VLAN_NATIVE_UNTAGGED:
1152 case PORT_VLAN_NATIVE_TAGGED:
1154 /* Port must always carry its native VLAN. */
1158 case PORT_VLAN_TRUNK:
1159 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1161 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1162 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1163 "received on port %s not configured for trunking "
1164 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1176 /* Given 'vlan', the VLAN that a packet belongs to, and
1177 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1178 * that should be included in the 802.1Q header. (If the return value is 0,
1179 * then the 802.1Q header should only be included in the packet if there is a
1182 * Both 'vlan' and the return value are in the range 0...4095. */
1184 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1186 switch (out_xbundle->vlan_mode) {
1187 case PORT_VLAN_ACCESS:
1190 case PORT_VLAN_TRUNK:
1191 case PORT_VLAN_NATIVE_TAGGED:
1194 case PORT_VLAN_NATIVE_UNTAGGED:
1195 return vlan == out_xbundle->vlan ? 0 : vlan;
1203 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1206 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1208 ovs_be16 tci, old_tci;
1209 struct xport *xport;
1211 vid = output_vlan_to_vid(out_xbundle, vlan);
1212 if (list_is_empty(&out_xbundle->xports)) {
1213 /* Partially configured bundle with no slaves. Drop the packet. */
1215 } else if (!out_xbundle->bond) {
1216 ctx->use_recirc = false;
1217 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1220 struct ofport_dpif *ofport;
1221 struct xlate_recirc *xr = &ctx->recirc;
1222 struct flow_wildcards *wc = &ctx->xout->wc;
1224 if (ctx->xbridge->enable_recirc) {
1225 ctx->use_recirc = bond_may_recirc(
1226 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1228 if (ctx->use_recirc) {
1229 /* Only TCP mode uses recirculation. */
1230 xr->hash_alg = OVS_HASH_ALG_L4;
1231 bond_update_post_recirc_rules(out_xbundle->bond, false);
1233 /* Recirculation does not require unmasking hash fields. */
1238 ofport = bond_choose_output_slave(out_xbundle->bond,
1239 &ctx->xin->flow, wc, vid);
1240 xport = xport_lookup(ofport);
1243 /* No slaves enabled, so drop packet. */
1247 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1248 * accounting for this bond. */
1249 if (!ctx->use_recirc) {
1250 if (ctx->xin->resubmit_stats) {
1251 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1252 ctx->xin->resubmit_stats->n_bytes);
1254 if (ctx->xin->xcache) {
1255 struct xc_entry *entry;
1258 flow = &ctx->xin->flow;
1259 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1260 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1261 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1262 entry->u.bond.vid = vid;
1267 old_tci = *flow_tci;
1269 if (tci || out_xbundle->use_priority_tags) {
1270 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1272 tci |= htons(VLAN_CFI);
1277 compose_output_action(ctx, xport->ofp_port);
1278 *flow_tci = old_tci;
1281 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1282 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1283 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1285 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1287 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1291 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1292 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1296 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1297 if (flow->nw_proto == ARP_OP_REPLY) {
1299 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1300 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1301 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1303 return flow->nw_src == flow->nw_dst;
1309 /* Checks whether a MAC learning update is necessary for MAC learning table
1310 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1313 * Most packets processed through the MAC learning table do not actually
1314 * change it in any way. This function requires only a read lock on the MAC
1315 * learning table, so it is much cheaper in this common case.
1317 * Keep the code here synchronized with that in update_learning_table__()
1320 is_mac_learning_update_needed(const struct mac_learning *ml,
1321 const struct flow *flow,
1322 struct flow_wildcards *wc,
1323 int vlan, struct xbundle *in_xbundle)
1324 OVS_REQ_RDLOCK(ml->rwlock)
1326 struct mac_entry *mac;
1328 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1332 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1333 if (!mac || mac_entry_age(ml, mac)) {
1337 if (is_gratuitous_arp(flow, wc)) {
1338 /* We don't want to learn from gratuitous ARP packets that are
1339 * reflected back over bond slaves so we lock the learning table. */
1340 if (!in_xbundle->bond) {
1342 } else if (mac_entry_is_grat_arp_locked(mac)) {
1347 return mac->port.p != in_xbundle->ofbundle;
1351 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1352 * received on 'in_xbundle' in 'vlan'.
1354 * This code repeats all the checks in is_mac_learning_update_needed() because
1355 * the lock was released between there and here and thus the MAC learning state
1356 * could have changed.
1358 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1361 update_learning_table__(const struct xbridge *xbridge,
1362 const struct flow *flow, struct flow_wildcards *wc,
1363 int vlan, struct xbundle *in_xbundle)
1364 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1366 struct mac_entry *mac;
1368 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1372 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1373 if (is_gratuitous_arp(flow, wc)) {
1374 /* We don't want to learn from gratuitous ARP packets that are
1375 * reflected back over bond slaves so we lock the learning table. */
1376 if (!in_xbundle->bond) {
1377 mac_entry_set_grat_arp_lock(mac);
1378 } else if (mac_entry_is_grat_arp_locked(mac)) {
1383 if (mac->port.p != in_xbundle->ofbundle) {
1384 /* The log messages here could actually be useful in debugging,
1385 * so keep the rate limit relatively high. */
1386 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1388 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1389 "on port %s in VLAN %d",
1390 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1391 in_xbundle->name, vlan);
1393 mac->port.p = in_xbundle->ofbundle;
1394 mac_learning_changed(xbridge->ml);
1399 update_learning_table(const struct xbridge *xbridge,
1400 const struct flow *flow, struct flow_wildcards *wc,
1401 int vlan, struct xbundle *in_xbundle)
1405 /* Don't learn the OFPP_NONE port. */
1406 if (in_xbundle == &ofpp_none_bundle) {
1410 /* First try the common case: no change to MAC learning table. */
1411 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1412 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1414 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1417 /* Slow path: MAC learning table might need an update. */
1418 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1419 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1420 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1424 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1425 * dropped. Returns true if they may be forwarded, false if they should be
1428 * 'in_port' must be the xport that corresponds to flow->in_port.
1429 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1431 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1432 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1433 * checked by input_vid_is_valid().
1435 * May also add tags to '*tags', although the current implementation only does
1436 * so in one special case.
1439 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1442 struct xbundle *in_xbundle = in_port->xbundle;
1443 const struct xbridge *xbridge = ctx->xbridge;
1444 struct flow *flow = &ctx->xin->flow;
1446 /* Drop frames for reserved multicast addresses
1447 * only if forward_bpdu option is absent. */
1448 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1449 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1453 if (in_xbundle->bond) {
1454 struct mac_entry *mac;
1456 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1462 xlate_report(ctx, "bonding refused admissibility, dropping");
1465 case BV_DROP_IF_MOVED:
1466 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1467 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1468 if (mac && mac->port.p != in_xbundle->ofbundle &&
1469 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1470 || mac_entry_is_grat_arp_locked(mac))) {
1471 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1472 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1476 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1485 xlate_normal(struct xlate_ctx *ctx)
1487 struct flow_wildcards *wc = &ctx->xout->wc;
1488 struct flow *flow = &ctx->xin->flow;
1489 struct xbundle *in_xbundle;
1490 struct xport *in_port;
1491 struct mac_entry *mac;
1496 ctx->xout->has_normal = true;
1498 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1499 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1500 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1502 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1503 ctx->xin->packet != NULL, &in_port);
1505 xlate_report(ctx, "no input bundle, dropping");
1509 /* Drop malformed frames. */
1510 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1511 !(flow->vlan_tci & htons(VLAN_CFI))) {
1512 if (ctx->xin->packet != NULL) {
1513 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1514 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1515 "VLAN tag received on port %s",
1516 ctx->xbridge->name, in_xbundle->name);
1518 xlate_report(ctx, "partial VLAN tag, dropping");
1522 /* Drop frames on bundles reserved for mirroring. */
1523 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1524 if (ctx->xin->packet != NULL) {
1525 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1526 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1527 "%s, which is reserved exclusively for mirroring",
1528 ctx->xbridge->name, in_xbundle->name);
1530 xlate_report(ctx, "input port is mirror output port, dropping");
1535 vid = vlan_tci_to_vid(flow->vlan_tci);
1536 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1537 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1540 vlan = input_vid_to_vlan(in_xbundle, vid);
1542 /* Check other admissibility requirements. */
1543 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1547 /* Learn source MAC. */
1548 if (ctx->xin->may_learn) {
1549 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1551 if (ctx->xin->xcache) {
1552 struct xc_entry *entry;
1554 /* Save enough info to update mac learning table later. */
1555 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
1556 entry->u.normal.ofproto = ctx->xin->ofproto;
1557 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
1558 entry->u.normal.vlan = vlan;
1561 /* Determine output bundle. */
1562 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1563 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1564 mac_port = mac ? mac->port.p : NULL;
1565 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1568 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1569 if (mac_xbundle && mac_xbundle != in_xbundle) {
1570 xlate_report(ctx, "forwarding to learned port");
1571 output_normal(ctx, mac_xbundle, vlan);
1572 } else if (!mac_xbundle) {
1573 xlate_report(ctx, "learned port is unknown, dropping");
1575 xlate_report(ctx, "learned port is input port, dropping");
1578 struct xbundle *xbundle;
1580 xlate_report(ctx, "no learned MAC for destination, flooding");
1581 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1582 if (xbundle != in_xbundle
1583 && xbundle_includes_vlan(xbundle, vlan)
1584 && xbundle->floodable
1585 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1586 output_normal(ctx, xbundle, vlan);
1589 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1593 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1594 * the number of packets out of UINT32_MAX to sample. The given
1595 * cookie is passed back in the callback for each sampled packet.
1598 compose_sample_action(const struct xbridge *xbridge,
1599 struct ofpbuf *odp_actions,
1600 const struct flow *flow,
1601 const uint32_t probability,
1602 const union user_action_cookie *cookie,
1603 const size_t cookie_size)
1605 size_t sample_offset, actions_offset;
1606 odp_port_t odp_port;
1610 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1612 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1614 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1616 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1617 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
1618 flow_hash_5tuple(flow, 0));
1619 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
1622 nl_msg_end_nested(odp_actions, actions_offset);
1623 nl_msg_end_nested(odp_actions, sample_offset);
1624 return cookie_offset;
1628 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1629 odp_port_t odp_port, unsigned int n_outputs,
1630 union user_action_cookie *cookie)
1634 cookie->type = USER_ACTION_COOKIE_SFLOW;
1635 cookie->sflow.vlan_tci = vlan_tci;
1637 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1638 * port information") for the interpretation of cookie->output. */
1639 switch (n_outputs) {
1641 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1642 cookie->sflow.output = 0x40000000 | 256;
1646 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1648 cookie->sflow.output = ifindex;
1653 /* 0x80000000 means "multiple output ports. */
1654 cookie->sflow.output = 0x80000000 | n_outputs;
1659 /* Compose SAMPLE action for sFlow bridge sampling. */
1661 compose_sflow_action(const struct xbridge *xbridge,
1662 struct ofpbuf *odp_actions,
1663 const struct flow *flow,
1664 odp_port_t odp_port)
1666 uint32_t probability;
1667 union user_action_cookie cookie;
1669 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1673 probability = dpif_sflow_get_probability(xbridge->sflow);
1674 compose_sflow_cookie(xbridge, htons(0), odp_port,
1675 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1677 return compose_sample_action(xbridge, odp_actions, flow, probability,
1678 &cookie, sizeof cookie.sflow);
1682 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1683 uint32_t obs_domain_id, uint32_t obs_point_id,
1684 union user_action_cookie *cookie)
1686 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1687 cookie->flow_sample.probability = probability;
1688 cookie->flow_sample.collector_set_id = collector_set_id;
1689 cookie->flow_sample.obs_domain_id = obs_domain_id;
1690 cookie->flow_sample.obs_point_id = obs_point_id;
1694 compose_ipfix_cookie(union user_action_cookie *cookie)
1696 cookie->type = USER_ACTION_COOKIE_IPFIX;
1699 /* Compose SAMPLE action for IPFIX bridge sampling. */
1701 compose_ipfix_action(const struct xbridge *xbridge,
1702 struct ofpbuf *odp_actions,
1703 const struct flow *flow)
1705 uint32_t probability;
1706 union user_action_cookie cookie;
1708 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1712 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1713 compose_ipfix_cookie(&cookie);
1715 compose_sample_action(xbridge, odp_actions, flow, probability,
1716 &cookie, sizeof cookie.ipfix);
1719 /* SAMPLE action for sFlow must be first action in any given list of
1720 * actions. At this point we do not have all information required to
1721 * build it. So try to build sample action as complete as possible. */
1723 add_sflow_action(struct xlate_ctx *ctx)
1725 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1726 &ctx->xout->odp_actions,
1727 &ctx->xin->flow, ODPP_NONE);
1728 ctx->sflow_odp_port = 0;
1729 ctx->sflow_n_outputs = 0;
1732 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1733 * of actions, eventually after the SAMPLE action for sFlow. */
1735 add_ipfix_action(struct xlate_ctx *ctx)
1737 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1741 /* Fix SAMPLE action according to data collected while composing ODP actions.
1742 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1743 * USERSPACE action's user-cookie which is required for sflow. */
1745 fix_sflow_action(struct xlate_ctx *ctx)
1747 const struct flow *base = &ctx->base_flow;
1748 union user_action_cookie *cookie;
1750 if (!ctx->user_cookie_offset) {
1754 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1755 sizeof cookie->sflow);
1756 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1758 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1759 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1762 static enum slow_path_reason
1763 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1764 const struct xport *xport, const struct ofpbuf *packet)
1766 struct flow_wildcards *wc = &ctx->xout->wc;
1767 const struct xbridge *xbridge = ctx->xbridge;
1771 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1773 cfm_process_heartbeat(xport->cfm, packet);
1776 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1778 bfd_process_packet(xport->bfd, flow, packet);
1779 /* If POLL received, immediately sends FINAL back. */
1780 if (bfd_should_send_packet(xport->bfd)) {
1781 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1785 } else if (xport->xbundle && xport->xbundle->lacp
1786 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1788 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1791 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1793 stp_process_packet(xport, packet);
1802 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1805 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1806 struct flow_wildcards *wc = &ctx->xout->wc;
1807 struct flow *flow = &ctx->xin->flow;
1808 ovs_be16 flow_vlan_tci;
1809 uint32_t flow_pkt_mark;
1810 uint8_t flow_nw_tos;
1811 odp_port_t out_port, odp_port;
1814 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1815 * before traversing a patch port. */
1816 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
1819 xlate_report(ctx, "Nonexistent output port");
1821 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1822 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1824 } else if (check_stp) {
1825 if (is_stp(&ctx->base_flow)) {
1826 if (!xport_stp_listen_state(xport)) {
1827 xlate_report(ctx, "STP not in listening state, "
1828 "skipping bpdu output");
1831 } else if (!xport_stp_forward_state(xport)) {
1832 xlate_report(ctx, "STP not in forwarding state, "
1838 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1839 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1844 const struct xport *peer = xport->peer;
1845 struct flow old_flow = ctx->xin->flow;
1846 enum slow_path_reason special;
1848 ctx->xbridge = peer->xbridge;
1849 flow->in_port.ofp_port = peer->ofp_port;
1850 flow->metadata = htonll(0);
1851 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1852 memset(flow->regs, 0, sizeof flow->regs);
1854 special = process_special(ctx, &ctx->xin->flow, peer,
1857 ctx->xout->slow |= special;
1858 } else if (may_receive(peer, ctx)) {
1859 if (xport_stp_forward_state(peer)) {
1860 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1862 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1863 * learning action look at the packet, then drop it. */
1864 struct flow old_base_flow = ctx->base_flow;
1865 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
1866 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1867 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1868 ctx->xout->mirrors = old_mirrors;
1869 ctx->base_flow = old_base_flow;
1870 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
1874 ctx->xin->flow = old_flow;
1875 ctx->xbridge = xport->xbridge;
1877 if (ctx->xin->resubmit_stats) {
1878 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1879 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1881 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1884 if (ctx->xin->xcache) {
1885 struct xc_entry *entry;
1887 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
1888 entry->u.dev.tx = netdev_ref(xport->netdev);
1889 entry->u.dev.rx = netdev_ref(peer->netdev);
1890 entry->u.dev.bfd = bfd_ref(peer->bfd);
1896 flow_vlan_tci = flow->vlan_tci;
1897 flow_pkt_mark = flow->pkt_mark;
1898 flow_nw_tos = flow->nw_tos;
1900 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1901 wc->masks.nw_tos |= IP_DSCP_MASK;
1902 flow->nw_tos &= ~IP_DSCP_MASK;
1903 flow->nw_tos |= dscp;
1906 if (xport->is_tunnel) {
1907 /* Save tunnel metadata so that changes made due to
1908 * the Logical (tunnel) Port are not visible for any further
1909 * matches, while explicit set actions on tunnel metadata are.
1911 struct flow_tnl flow_tnl = flow->tunnel;
1912 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1913 if (odp_port == ODPP_NONE) {
1914 xlate_report(ctx, "Tunneling decided against output");
1915 goto out; /* restore flow_nw_tos */
1917 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1918 xlate_report(ctx, "Not tunneling to our own address");
1919 goto out; /* restore flow_nw_tos */
1921 if (ctx->xin->resubmit_stats) {
1922 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1924 if (ctx->xin->xcache) {
1925 struct xc_entry *entry;
1927 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
1928 entry->u.dev.tx = netdev_ref(xport->netdev);
1930 out_port = odp_port;
1931 commit_odp_tunnel_action(flow, &ctx->base_flow,
1932 &ctx->xout->odp_actions);
1933 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1935 odp_port = xport->odp_port;
1936 out_port = odp_port;
1937 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1938 ofp_port_t vlandev_port;
1940 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1941 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
1942 ofp_port, flow->vlan_tci);
1943 if (vlandev_port != ofp_port) {
1944 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1945 flow->vlan_tci = htons(0);
1950 if (out_port != ODPP_NONE) {
1951 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1952 &ctx->xout->odp_actions,
1955 if (ctx->use_recirc) {
1956 struct ovs_action_hash *act_hash;
1957 struct xlate_recirc *xr = &ctx->recirc;
1960 act_hash = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
1961 OVS_ACTION_ATTR_HASH,
1963 act_hash->hash_alg = xr->hash_alg;
1964 act_hash->hash_basis = xr->hash_basis;
1966 /* Recirc action. */
1967 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
1970 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1974 ctx->sflow_odp_port = odp_port;
1975 ctx->sflow_n_outputs++;
1976 ctx->xout->nf_output_iface = ofp_port;
1981 flow->vlan_tci = flow_vlan_tci;
1982 flow->pkt_mark = flow_pkt_mark;
1983 flow->nw_tos = flow_nw_tos;
1987 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1989 compose_output_action__(ctx, ofp_port, true);
1993 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1995 struct rule_dpif *old_rule = ctx->rule;
1996 const struct rule_actions *actions;
1998 if (ctx->xin->resubmit_stats) {
1999 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2005 actions = rule_dpif_get_actions(rule);
2006 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2007 ctx->rule = old_rule;
2012 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2014 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2016 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2017 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2018 MAX_RESUBMIT_RECURSION);
2019 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2020 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2021 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
2022 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2023 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2024 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2033 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2034 bool may_packet_in, bool honor_table_miss)
2036 if (xlate_resubmit_resource_check(ctx)) {
2037 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2038 bool skip_wildcards = ctx->xin->skip_wildcards;
2039 uint8_t old_table_id = ctx->table_id;
2040 struct rule_dpif *rule;
2041 enum rule_dpif_lookup_verdict verdict;
2042 enum ofputil_port_config config = 0;
2044 ctx->table_id = table_id;
2046 /* Look up a flow with 'in_port' as the input port. Then restore the
2047 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2048 * have surprising behavior). */
2049 ctx->xin->flow.in_port.ofp_port = in_port;
2050 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2053 ? &ctx->xout->wc : NULL,
2055 &ctx->table_id, &rule,
2056 ctx->xin->xcache != NULL);
2057 ctx->xin->flow.in_port.ofp_port = old_in_port;
2059 if (ctx->xin->resubmit_hook) {
2060 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2064 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2066 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2067 if (may_packet_in) {
2068 struct xport *xport;
2070 xport = get_ofp_port(ctx->xbridge,
2071 ctx->xin->flow.in_port.ofp_port);
2072 config = xport ? xport->config : 0;
2075 /* Fall through to drop */
2076 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2077 config = OFPUTIL_PC_NO_PACKET_IN;
2079 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2080 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2081 config = OFPUTIL_PC_NO_PACKET_IN;
2088 choose_miss_rule(config, ctx->xbridge->miss_rule,
2089 ctx->xbridge->no_packet_in_rule, &rule,
2090 ctx->xin->xcache != NULL);
2094 /* Fill in the cache entry here instead of xlate_recursively
2095 * to make the reference counting more explicit. We take a
2096 * reference in the lookups above if we are going to cache the
2098 if (ctx->xin->xcache) {
2099 struct xc_entry *entry;
2101 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2102 entry->u.rule = rule;
2104 xlate_recursively(ctx, rule);
2107 ctx->table_id = old_table_id;
2115 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
2117 uint64_t action_list_stub[1024 / 8];
2118 struct ofpbuf action_list, action_set;
2120 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2121 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2123 ofpacts_execute_action_set(&action_list, &action_set);
2125 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2128 ofpbuf_uninit(&action_set);
2129 ofpbuf_uninit(&action_list);
2133 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2135 const struct ofputil_bucket *bucket;
2136 const struct list *buckets;
2137 struct flow old_flow = ctx->xin->flow;
2139 group_dpif_get_buckets(group, &buckets);
2141 LIST_FOR_EACH (bucket, list_node, buckets) {
2142 xlate_group_bucket(ctx, bucket);
2143 /* Roll back flow to previous state.
2144 * This is equivalent to cloning the packet for each bucket.
2146 * As a side effect any subsequently applied actions will
2147 * also effectively be applied to a clone of the packet taken
2148 * just before applying the all or indirect group. */
2149 ctx->xin->flow = old_flow;
2154 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2156 const struct ofputil_bucket *bucket;
2158 bucket = group_first_live_bucket(ctx, group, 0);
2160 xlate_group_bucket(ctx, bucket);
2165 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2167 struct flow_wildcards *wc = &ctx->xout->wc;
2168 const struct ofputil_bucket *bucket;
2171 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2172 bucket = group_best_live_bucket(ctx, group, basis);
2174 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2175 xlate_group_bucket(ctx, bucket);
2180 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2182 ctx->in_group = true;
2184 switch (group_dpif_get_type(group)) {
2186 case OFPGT11_INDIRECT:
2187 xlate_all_group(ctx, group);
2189 case OFPGT11_SELECT:
2190 xlate_select_group(ctx, group);
2193 xlate_ff_group(ctx, group);
2198 group_dpif_release(group);
2200 ctx->in_group = false;
2204 xlate_group_resource_check(struct xlate_ctx *ctx)
2206 if (!xlate_resubmit_resource_check(ctx)) {
2208 } else if (ctx->in_group) {
2209 /* Prevent nested translation of OpenFlow groups.
2211 * OpenFlow allows this restriction. We enforce this restriction only
2212 * because, with the current architecture, we would otherwise have to
2213 * take a possibly recursive read lock on the ofgroup rwlock, which is
2214 * unsafe given that POSIX allows taking a read lock to block if there
2215 * is a thread blocked on taking the write lock. Other solutions
2216 * without this restriction are also possible, but seem unwarranted
2217 * given the current limited use of groups. */
2218 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2220 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2228 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2230 if (xlate_group_resource_check(ctx)) {
2231 struct group_dpif *group;
2234 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2236 xlate_group_action__(ctx, group);
2246 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2247 const struct ofpact_resubmit *resubmit)
2251 bool may_packet_in = false;
2252 bool honor_table_miss = false;
2254 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2255 /* Still allow missed packets to be sent to the controller
2256 * if resubmitting from an internal table. */
2257 may_packet_in = true;
2258 honor_table_miss = true;
2261 in_port = resubmit->in_port;
2262 if (in_port == OFPP_IN_PORT) {
2263 in_port = ctx->xin->flow.in_port.ofp_port;
2266 table_id = resubmit->table_id;
2267 if (table_id == 255) {
2268 table_id = ctx->table_id;
2271 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2276 flood_packets(struct xlate_ctx *ctx, bool all)
2278 const struct xport *xport;
2280 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2281 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2286 compose_output_action__(ctx, xport->ofp_port, false);
2287 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2288 compose_output_action(ctx, xport->ofp_port);
2292 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2296 execute_controller_action(struct xlate_ctx *ctx, int len,
2297 enum ofp_packet_in_reason reason,
2298 uint16_t controller_id)
2300 struct ofproto_packet_in *pin;
2301 struct ofpbuf *packet;
2302 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2304 ctx->xout->slow |= SLOW_CONTROLLER;
2305 if (!ctx->xin->packet) {
2309 packet = ofpbuf_clone(ctx->xin->packet);
2311 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2312 &ctx->xout->odp_actions,
2315 odp_execute_actions(NULL, packet, false, &md,
2316 ofpbuf_data(&ctx->xout->odp_actions),
2317 ofpbuf_size(&ctx->xout->odp_actions), NULL);
2319 pin = xmalloc(sizeof *pin);
2320 pin->up.packet_len = ofpbuf_size(packet);
2321 pin->up.packet = ofpbuf_steal_data(packet);
2322 pin->up.reason = reason;
2323 pin->up.table_id = ctx->table_id;
2324 pin->up.cookie = (ctx->rule
2325 ? rule_dpif_get_flow_cookie(ctx->rule)
2328 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2330 pin->controller_id = controller_id;
2331 pin->send_len = len;
2332 /* If a rule is a table-miss rule then this is
2333 * a table-miss handled by a table-miss rule.
2335 * Else, if rule is internal and has a controller action,
2336 * the later being implied by the rule being processed here,
2337 * then this is a table-miss handled without a table-miss rule.
2339 * Otherwise this is not a table-miss. */
2340 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2342 if (rule_dpif_is_table_miss(ctx->rule)) {
2343 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2344 } else if (rule_dpif_is_internal(ctx->rule)) {
2345 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2348 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2349 ofpbuf_delete(packet);
2353 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2355 struct flow_wildcards *wc = &ctx->xout->wc;
2356 struct flow *flow = &ctx->xin->flow;
2359 ovs_assert(eth_type_mpls(mpls->ethertype));
2361 n = flow_count_mpls_labels(flow, wc);
2363 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2364 &ctx->xout->odp_actions,
2366 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2367 if (ctx->xin->packet != NULL) {
2368 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2369 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2370 "MPLS push action can't be performed as it would "
2371 "have more MPLS LSEs than the %d supported.",
2372 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2376 } else if (n >= ctx->xbridge->max_mpls_depth) {
2377 COVERAGE_INC(xlate_actions_mpls_overflow);
2378 ctx->xout->slow |= SLOW_ACTION;
2381 flow_push_mpls(flow, n, mpls->ethertype, wc);
2385 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2387 struct flow_wildcards *wc = &ctx->xout->wc;
2388 struct flow *flow = &ctx->xin->flow;
2389 int n = flow_count_mpls_labels(flow, wc);
2391 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2392 if (ctx->xin->packet != NULL) {
2393 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2394 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2395 "MPLS pop action can't be performed as it has "
2396 "more MPLS LSEs than the %d supported.",
2397 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2400 ofpbuf_clear(&ctx->xout->odp_actions);
2405 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2407 struct flow *flow = &ctx->xin->flow;
2409 if (!is_ip_any(flow)) {
2413 ctx->xout->wc.masks.nw_ttl = 0xff;
2414 if (flow->nw_ttl > 1) {
2420 for (i = 0; i < ids->n_controllers; i++) {
2421 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2425 /* Stop processing for current table. */
2431 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2433 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2434 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2435 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2440 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2442 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2443 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2444 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2449 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2451 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2452 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2453 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2458 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2460 struct flow *flow = &ctx->xin->flow;
2461 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2462 struct flow_wildcards *wc = &ctx->xout->wc;
2464 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2465 if (eth_type_mpls(flow->dl_type)) {
2468 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2471 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2473 /* Stop processing for current table. */
2482 xlate_output_action(struct xlate_ctx *ctx,
2483 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2485 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2487 ctx->xout->nf_output_iface = NF_OUT_DROP;
2491 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2494 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2495 0, may_packet_in, true);
2501 flood_packets(ctx, false);
2504 flood_packets(ctx, true);
2506 case OFPP_CONTROLLER:
2507 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2513 if (port != ctx->xin->flow.in_port.ofp_port) {
2514 compose_output_action(ctx, port);
2516 xlate_report(ctx, "skipping output to input port");
2521 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2522 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2523 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2524 ctx->xout->nf_output_iface = prev_nf_output_iface;
2525 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2526 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2527 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2532 xlate_output_reg_action(struct xlate_ctx *ctx,
2533 const struct ofpact_output_reg *or)
2535 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2536 if (port <= UINT16_MAX) {
2537 union mf_subvalue value;
2539 memset(&value, 0xff, sizeof value);
2540 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2541 xlate_output_action(ctx, u16_to_ofp(port),
2542 or->max_len, false);
2547 xlate_enqueue_action(struct xlate_ctx *ctx,
2548 const struct ofpact_enqueue *enqueue)
2550 ofp_port_t ofp_port = enqueue->port;
2551 uint32_t queue_id = enqueue->queue;
2552 uint32_t flow_priority, priority;
2555 /* Translate queue to priority. */
2556 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2558 /* Fall back to ordinary output action. */
2559 xlate_output_action(ctx, enqueue->port, 0, false);
2563 /* Check output port. */
2564 if (ofp_port == OFPP_IN_PORT) {
2565 ofp_port = ctx->xin->flow.in_port.ofp_port;
2566 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2570 /* Add datapath actions. */
2571 flow_priority = ctx->xin->flow.skb_priority;
2572 ctx->xin->flow.skb_priority = priority;
2573 compose_output_action(ctx, ofp_port);
2574 ctx->xin->flow.skb_priority = flow_priority;
2576 /* Update NetFlow output port. */
2577 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2578 ctx->xout->nf_output_iface = ofp_port;
2579 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2580 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2585 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2587 uint32_t skb_priority;
2589 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2590 ctx->xin->flow.skb_priority = skb_priority;
2592 /* Couldn't translate queue to a priority. Nothing to do. A warning
2593 * has already been logged. */
2598 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2600 const struct xbridge *xbridge = xbridge_;
2611 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2614 port = get_ofp_port(xbridge, ofp_port);
2615 return port ? port->may_enable : false;
2620 xlate_bundle_action(struct xlate_ctx *ctx,
2621 const struct ofpact_bundle *bundle)
2625 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2627 CONST_CAST(struct xbridge *, ctx->xbridge));
2628 if (bundle->dst.field) {
2629 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2632 xlate_output_action(ctx, port, 0, false);
2637 xlate_learn_action(struct xlate_ctx *ctx,
2638 const struct ofpact_learn *learn)
2640 uint64_t ofpacts_stub[1024 / 8];
2641 struct ofputil_flow_mod fm;
2642 struct ofpbuf ofpacts;
2644 ctx->xout->has_learn = true;
2646 learn_mask(learn, &ctx->xout->wc);
2648 if (!ctx->xin->may_learn) {
2652 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2653 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2654 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2655 ofpbuf_uninit(&ofpacts);
2657 if (ctx->xin->xcache) {
2658 struct xc_entry *entry;
2660 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
2661 entry->u.learn.ofproto = ctx->xin->ofproto;
2662 /* Lookup the learned rule, taking a reference on it. The reference
2663 * is released when this cache entry is deleted. */
2664 rule_dpif_lookup(ctx->xbridge->ofproto, &ctx->xin->flow, NULL,
2665 &entry->u.learn.rule, true);
2670 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
2671 uint16_t idle_timeout, uint16_t hard_timeout)
2673 if (tcp_flags & (TCP_FIN | TCP_RST)) {
2674 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
2679 xlate_fin_timeout(struct xlate_ctx *ctx,
2680 const struct ofpact_fin_timeout *oft)
2683 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
2684 oft->fin_idle_timeout, oft->fin_hard_timeout);
2685 if (ctx->xin->xcache) {
2686 struct xc_entry *entry;
2688 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
2689 /* XC_RULE already holds a reference on the rule, none is taken
2691 entry->u.fin.rule = ctx->rule;
2692 entry->u.fin.idle = oft->fin_idle_timeout;
2693 entry->u.fin.hard = oft->fin_hard_timeout;
2699 xlate_sample_action(struct xlate_ctx *ctx,
2700 const struct ofpact_sample *os)
2702 union user_action_cookie cookie;
2703 /* Scale the probability from 16-bit to 32-bit while representing
2704 * the same percentage. */
2705 uint32_t probability = (os->probability << 16) | os->probability;
2707 if (!ctx->xbridge->variable_length_userdata) {
2708 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2710 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2711 "lacks support (needs Linux 3.10+ or kernel module from "
2716 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2717 &ctx->xout->odp_actions,
2720 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2721 os->obs_domain_id, os->obs_point_id, &cookie);
2722 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2723 probability, &cookie, sizeof cookie.flow_sample);
2727 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2729 if (xport->config & (is_stp(&ctx->xin->flow)
2730 ? OFPUTIL_PC_NO_RECV_STP
2731 : OFPUTIL_PC_NO_RECV)) {
2735 /* Only drop packets here if both forwarding and learning are
2736 * disabled. If just learning is enabled, we need to have
2737 * OFPP_NORMAL and the learning action have a look at the packet
2738 * before we can drop it. */
2739 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2747 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2749 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2750 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2751 ofpact_pad(&ctx->action_set);
2755 xlate_action_set(struct xlate_ctx *ctx)
2757 uint64_t action_list_stub[1024 / 64];
2758 struct ofpbuf action_list;
2760 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2761 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2762 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2763 ofpbuf_uninit(&action_list);
2767 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2768 struct xlate_ctx *ctx)
2770 struct flow_wildcards *wc = &ctx->xout->wc;
2771 struct flow *flow = &ctx->xin->flow;
2772 const struct ofpact *a;
2774 /* dl_type already in the mask, not set below. */
2776 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2777 struct ofpact_controller *controller;
2778 const struct ofpact_metadata *metadata;
2779 const struct ofpact_set_field *set_field;
2780 const struct mf_field *mf;
2788 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2789 ofpact_get_OUTPUT(a)->max_len, true);
2793 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2798 case OFPACT_CONTROLLER:
2799 controller = ofpact_get_CONTROLLER(a);
2800 execute_controller_action(ctx, controller->max_len,
2802 controller->controller_id);
2805 case OFPACT_ENQUEUE:
2806 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2809 case OFPACT_SET_VLAN_VID:
2810 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2811 if (flow->vlan_tci & htons(VLAN_CFI) ||
2812 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2813 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2814 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2819 case OFPACT_SET_VLAN_PCP:
2820 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2821 if (flow->vlan_tci & htons(VLAN_CFI) ||
2822 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2823 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2824 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2825 << VLAN_PCP_SHIFT) | VLAN_CFI);
2829 case OFPACT_STRIP_VLAN:
2830 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2831 flow->vlan_tci = htons(0);
2834 case OFPACT_PUSH_VLAN:
2835 /* XXX 802.1AD(QinQ) */
2836 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2837 flow->vlan_tci = htons(VLAN_CFI);
2840 case OFPACT_SET_ETH_SRC:
2841 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2842 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2845 case OFPACT_SET_ETH_DST:
2846 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2847 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2850 case OFPACT_SET_IPV4_SRC:
2851 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2852 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2853 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2857 case OFPACT_SET_IPV4_DST:
2858 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2859 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2860 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2864 case OFPACT_SET_IP_DSCP:
2865 if (is_ip_any(flow)) {
2866 wc->masks.nw_tos |= IP_DSCP_MASK;
2867 flow->nw_tos &= ~IP_DSCP_MASK;
2868 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2872 case OFPACT_SET_IP_ECN:
2873 if (is_ip_any(flow)) {
2874 wc->masks.nw_tos |= IP_ECN_MASK;
2875 flow->nw_tos &= ~IP_ECN_MASK;
2876 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2880 case OFPACT_SET_IP_TTL:
2881 if (is_ip_any(flow)) {
2882 wc->masks.nw_ttl = 0xff;
2883 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2887 case OFPACT_SET_L4_SRC_PORT:
2888 if (is_ip_any(flow)) {
2889 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2890 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2891 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2895 case OFPACT_SET_L4_DST_PORT:
2896 if (is_ip_any(flow)) {
2897 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2898 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2899 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2903 case OFPACT_RESUBMIT:
2904 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2907 case OFPACT_SET_TUNNEL:
2908 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2911 case OFPACT_SET_QUEUE:
2912 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2915 case OFPACT_POP_QUEUE:
2916 flow->skb_priority = ctx->orig_skb_priority;
2919 case OFPACT_REG_MOVE:
2920 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2923 case OFPACT_REG_LOAD:
2924 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2927 case OFPACT_SET_FIELD:
2928 set_field = ofpact_get_SET_FIELD(a);
2929 mf = set_field->field;
2931 /* Set field action only ever overwrites packet's outermost
2932 * applicable header fields. Do nothing if no header exists. */
2933 if (mf->id == MFF_VLAN_VID) {
2934 wc->masks.vlan_tci |= htons(VLAN_CFI);
2935 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2938 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
2939 /* 'dl_type' is already unwildcarded. */
2940 && !eth_type_mpls(flow->dl_type)) {
2944 mf_mask_field_and_prereqs(mf, &wc->masks);
2945 mf_set_flow_value(mf, &set_field->value, flow);
2948 case OFPACT_STACK_PUSH:
2949 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2953 case OFPACT_STACK_POP:
2954 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2958 case OFPACT_PUSH_MPLS:
2959 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
2962 case OFPACT_POP_MPLS:
2963 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2966 case OFPACT_SET_MPLS_LABEL:
2967 compose_set_mpls_label_action(
2968 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
2971 case OFPACT_SET_MPLS_TC:
2972 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
2975 case OFPACT_SET_MPLS_TTL:
2976 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
2979 case OFPACT_DEC_MPLS_TTL:
2980 if (compose_dec_mpls_ttl_action(ctx)) {
2985 case OFPACT_DEC_TTL:
2986 wc->masks.nw_ttl = 0xff;
2987 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2993 /* Nothing to do. */
2996 case OFPACT_MULTIPATH:
2997 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3001 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3004 case OFPACT_OUTPUT_REG:
3005 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3009 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3016 case OFPACT_FIN_TIMEOUT:
3017 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3018 ctx->xout->has_fin_timeout = true;
3019 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3022 case OFPACT_CLEAR_ACTIONS:
3023 ofpbuf_clear(&ctx->action_set);
3026 case OFPACT_WRITE_ACTIONS:
3027 xlate_write_actions(ctx, a);
3030 case OFPACT_WRITE_METADATA:
3031 metadata = ofpact_get_WRITE_METADATA(a);
3032 flow->metadata &= ~metadata->mask;
3033 flow->metadata |= metadata->metadata & metadata->mask;
3037 /* Not implemented yet. */
3040 case OFPACT_GOTO_TABLE: {
3041 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3043 ovs_assert(ctx->table_id < ogt->table_id);
3044 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3045 ogt->table_id, true, true);
3050 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3057 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3058 const struct flow *flow, struct rule_dpif *rule,
3059 uint16_t tcp_flags, const struct ofpbuf *packet)
3061 xin->ofproto = ofproto;
3063 xin->packet = packet;
3064 xin->may_learn = packet != NULL;
3067 xin->ofpacts = NULL;
3068 xin->ofpacts_len = 0;
3069 xin->tcp_flags = tcp_flags;
3070 xin->resubmit_hook = NULL;
3071 xin->report_hook = NULL;
3072 xin->resubmit_stats = NULL;
3073 xin->skip_wildcards = false;
3077 xlate_out_uninit(struct xlate_out *xout)
3080 ofpbuf_uninit(&xout->odp_actions);
3084 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3085 * into datapath actions, using 'ctx', and discards the datapath actions. */
3087 xlate_actions_for_side_effects(struct xlate_in *xin)
3089 struct xlate_out xout;
3091 xlate_actions(xin, &xout);
3092 xlate_out_uninit(&xout);
3096 xlate_report(struct xlate_ctx *ctx, const char *s)
3098 if (ctx->xin->report_hook) {
3099 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3104 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3107 dst->slow = src->slow;
3108 dst->has_learn = src->has_learn;
3109 dst->has_normal = src->has_normal;
3110 dst->has_fin_timeout = src->has_fin_timeout;
3111 dst->nf_output_iface = src->nf_output_iface;
3112 dst->mirrors = src->mirrors;
3114 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
3115 sizeof dst->odp_actions_stub);
3116 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
3117 ofpbuf_size(&src->odp_actions));
3120 static struct skb_priority_to_dscp *
3121 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3123 struct skb_priority_to_dscp *pdscp;
3126 hash = hash_int(skb_priority, 0);
3127 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3128 if (pdscp->skb_priority == skb_priority) {
3136 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
3139 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
3140 *dscp = pdscp ? pdscp->dscp : 0;
3141 return pdscp != NULL;
3145 clear_skb_priorities(struct xport *xport)
3147 struct skb_priority_to_dscp *pdscp, *next;
3149 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
3150 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
3156 actions_output_to_local_port(const struct xlate_ctx *ctx)
3158 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
3159 const struct nlattr *a;
3162 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
3163 ofpbuf_size(&ctx->xout->odp_actions)) {
3164 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3165 && nl_attr_get_odp_port(a) == local_odp_port) {
3172 /* Thread safe call to xlate_actions__(). */
3174 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3175 OVS_EXCLUDED(xlate_rwlock)
3177 ovs_rwlock_rdlock(&xlate_rwlock);
3178 xlate_actions__(xin, xout);
3179 ovs_rwlock_unlock(&xlate_rwlock);
3182 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3183 * into datapath actions in 'odp_actions', using 'ctx'.
3185 * The caller must take responsibility for eventually freeing 'xout', with
3186 * xlate_out_uninit(). */
3188 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3189 OVS_REQ_RDLOCK(xlate_rwlock)
3191 struct flow_wildcards *wc = &xout->wc;
3192 struct flow *flow = &xin->flow;
3193 struct rule_dpif *rule = NULL;
3195 const struct rule_actions *actions = NULL;
3196 enum slow_path_reason special;
3197 const struct ofpact *ofpacts;
3198 struct xport *in_port;
3199 struct flow orig_flow;
3200 struct xlate_ctx ctx;
3205 COVERAGE_INC(xlate_actions);
3207 /* Flow initialization rules:
3208 * - 'base_flow' must match the kernel's view of the packet at the
3209 * time that action processing starts. 'flow' represents any
3210 * transformations we wish to make through actions.
3211 * - By default 'base_flow' and 'flow' are the same since the input
3212 * packet matches the output before any actions are applied.
3213 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3214 * of the received packet as seen by the kernel. If we later output
3215 * to another device without any modifications this will cause us to
3216 * insert a new tag since the original one was stripped off by the
3218 * - Tunnel metadata as received is retained in 'flow'. This allows
3219 * tunnel metadata matching also in later tables.
3220 * Since a kernel action for setting the tunnel metadata will only be
3221 * generated with actual tunnel output, changing the tunnel metadata
3222 * values in 'flow' (such as tun_id) will only have effect with a later
3223 * tunnel output action.
3224 * - Tunnel 'base_flow' is completely cleared since that is what the
3225 * kernel does. If we wish to maintain the original values an action
3226 * needs to be generated. */
3231 ctx.xout->has_learn = false;
3232 ctx.xout->has_normal = false;
3233 ctx.xout->has_fin_timeout = false;
3234 ctx.xout->nf_output_iface = NF_OUT_DROP;
3235 ctx.xout->mirrors = 0;
3236 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3237 sizeof ctx.xout->odp_actions_stub);
3238 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3240 ctx.xbridge = xbridge_lookup(xin->ofproto);
3245 ctx.rule = xin->rule;
3247 ctx.base_flow = *flow;
3248 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3249 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3251 flow_wildcards_init_catchall(wc);
3252 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3253 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3254 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3255 if (is_ip_any(flow)) {
3256 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3258 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3260 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3261 if (ctx.xbridge->netflow) {
3262 netflow_mask_wc(flow, wc);
3267 ctx.in_group = false;
3268 ctx.orig_skb_priority = flow->skb_priority;
3271 ctx.use_recirc = false;
3273 if (!xin->ofpacts && !ctx.rule) {
3274 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3275 !xin->skip_wildcards ? wc : NULL,
3276 &rule, ctx.xin->xcache != NULL);
3277 if (ctx.xin->resubmit_stats) {
3278 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3280 if (ctx.xin->xcache) {
3281 struct xc_entry *entry;
3283 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
3284 entry->u.rule = rule;
3288 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3291 ofpacts = xin->ofpacts;
3292 ofpacts_len = xin->ofpacts_len;
3293 } else if (ctx.rule) {
3294 actions = rule_dpif_get_actions(ctx.rule);
3295 ofpacts = actions->ofpacts;
3296 ofpacts_len = actions->ofpacts_len;
3301 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3302 ofpbuf_use_stub(&ctx.action_set,
3303 ctx.action_set_stub, sizeof ctx.action_set_stub);
3305 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3306 /* Do this conditionally because the copy is expensive enough that it
3307 * shows up in profiles. */
3311 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3312 switch (ctx.xbridge->frag) {
3313 case OFPC_FRAG_NORMAL:
3314 /* We must pretend that transport ports are unavailable. */
3315 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3316 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3319 case OFPC_FRAG_DROP:
3322 case OFPC_FRAG_REASM:
3325 case OFPC_FRAG_NX_MATCH:
3326 /* Nothing to do. */
3329 case OFPC_INVALID_TTL_TO_CONTROLLER:
3334 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3335 if (in_port && in_port->is_tunnel) {
3336 if (ctx.xin->resubmit_stats) {
3337 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3339 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3342 if (ctx.xin->xcache) {
3343 struct xc_entry *entry;
3345 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
3346 entry->u.dev.rx = netdev_ref(in_port->netdev);
3347 entry->u.dev.bfd = bfd_ref(in_port->bfd);
3351 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3353 ctx.xout->slow |= special;
3355 size_t sample_actions_len;
3357 if (flow->in_port.ofp_port
3358 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3359 flow->in_port.ofp_port,
3361 ctx.base_flow.vlan_tci = 0;
3364 add_sflow_action(&ctx);
3365 add_ipfix_action(&ctx);
3366 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
3368 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3369 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3371 /* We've let OFPP_NORMAL and the learning action look at the
3372 * packet, so drop it now if forwarding is disabled. */
3373 if (in_port && !xport_stp_forward_state(in_port)) {
3374 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3378 if (ofpbuf_size(&ctx.action_set)) {
3379 xlate_action_set(&ctx);
3382 if (ctx.xbridge->has_in_band
3383 && in_band_must_output_to_local_port(flow)
3384 && !actions_output_to_local_port(&ctx)) {
3385 compose_output_action(&ctx, OFPP_LOCAL);
3388 fix_sflow_action(&ctx);
3390 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3391 add_mirror_actions(&ctx, &orig_flow);
3395 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
3396 /* These datapath actions are too big for a Netlink attribute, so we
3397 * can't hand them to the kernel directly. dpif_execute() can execute
3398 * them one by one with help, so just mark the result as SLOW_ACTION to
3399 * prevent the flow from being installed. */
3400 COVERAGE_INC(xlate_actions_oversize);
3401 ctx.xout->slow |= SLOW_ACTION;
3404 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3405 if (ctx.xin->resubmit_stats) {
3406 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3407 ctx.xin->resubmit_stats->n_packets,
3408 ctx.xin->resubmit_stats->n_bytes);
3410 if (ctx.xin->xcache) {
3411 struct xc_entry *entry;
3413 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
3414 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
3415 entry->u.mirror.mirrors = xout->mirrors;
3419 if (ctx.xbridge->netflow) {
3420 const struct ofpact *ofpacts = actions->ofpacts;
3421 size_t ofpacts_len = actions->ofpacts_len;
3423 /* Only update netflow if we don't have controller flow. We don't
3424 * report NetFlow expiration messages for such facets because they
3425 * are just part of the control logic for the network, not real
3427 if (ofpacts_len == 0
3428 || ofpacts->type != OFPACT_CONTROLLER
3429 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3430 if (ctx.xin->resubmit_stats) {
3431 netflow_flow_update(ctx.xbridge->netflow, flow,
3432 xout->nf_output_iface,
3433 ctx.xin->resubmit_stats);
3435 if (ctx.xin->xcache) {
3436 struct xc_entry *entry;
3438 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
3439 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
3440 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
3441 entry->u.nf.iface = xout->nf_output_iface;
3446 ofpbuf_uninit(&ctx.stack);
3447 ofpbuf_uninit(&ctx.action_set);
3449 /* Clear the metadata and register wildcard masks, because we won't
3450 * use non-header fields as part of the cache. */
3451 flow_wildcards_clear_non_packet_fields(wc);
3453 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3454 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3455 * these fields. The datapath interface, on the other hand, represents
3456 * them with just 8 bits each. This means that if the high 8 bits of the
3457 * masks for these fields somehow become set, then they will get chopped
3458 * off by a round trip through the datapath, and revalidation will spot
3459 * that as an inconsistency and delete the flow. Avoid the problem here by
3460 * making sure that only the low 8 bits of either field can be unwildcarded
3464 wc->masks.tp_src &= htons(UINT8_MAX);
3465 wc->masks.tp_dst &= htons(UINT8_MAX);
3469 /* Sends 'packet' out 'ofport'.
3470 * May modify 'packet'.
3471 * Returns 0 if successful, otherwise a positive errno value. */
3473 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3475 struct xport *xport;
3476 struct ofpact_output output;
3479 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3480 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3481 flow_extract(packet, NULL, &flow);
3482 flow.in_port.ofp_port = OFPP_NONE;
3484 ovs_rwlock_rdlock(&xlate_rwlock);
3485 xport = xport_lookup(ofport);
3487 ovs_rwlock_unlock(&xlate_rwlock);
3490 output.port = xport->ofp_port;
3492 ovs_rwlock_unlock(&xlate_rwlock);
3494 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3495 &output.ofpact, sizeof output,
3499 struct xlate_cache *
3500 xlate_cache_new(void)
3502 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
3504 ofpbuf_init(&xcache->entries, 512);
3508 static struct xc_entry *
3509 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
3511 struct xc_entry *entry;
3513 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
3520 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
3522 if (entry->u.dev.tx) {
3523 netdev_vport_inc_tx(entry->u.dev.tx, stats);
3525 if (entry->u.dev.rx) {
3526 netdev_vport_inc_rx(entry->u.dev.rx, stats);
3528 if (entry->u.dev.bfd) {
3529 bfd_account_rx(entry->u.dev.bfd, stats);
3534 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
3536 struct xbridge *xbridge;
3537 struct xbundle *xbundle;
3538 struct flow_wildcards wc;
3540 xbridge = xbridge_lookup(ofproto);
3545 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
3551 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
3554 /* Push stats and perform side effects of flow translation. */
3556 xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
3557 const struct dpif_flow_stats *stats)
3559 struct xc_entry *entry;
3560 struct ofpbuf entries = xcache->entries;
3562 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3563 switch (entry->type) {
3565 rule_dpif_credit_stats(entry->u.rule, stats);
3568 bond_account(entry->u.bond.bond, entry->u.bond.flow,
3569 entry->u.bond.vid, stats->n_bytes);
3572 xlate_cache_netdev(entry, stats);
3575 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
3576 entry->u.nf.iface, stats);
3579 mirror_update_stats(entry->u.mirror.mbridge,
3580 entry->u.mirror.mirrors,
3581 stats->n_packets, stats->n_bytes);
3585 struct rule_dpif *rule = entry->u.learn.rule;
3587 /* Reset the modified time for a rule that is equivalent to
3588 * the currently cached rule. If the rule is not the exact
3589 * rule we have cached, update the reference that we have. */
3590 entry->u.learn.rule = ofproto_dpif_refresh_rule(rule);
3594 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
3595 entry->u.normal.vlan);
3597 case XC_FIN_TIMEOUT:
3598 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
3599 entry->u.fin.idle, entry->u.fin.hard);
3608 xlate_dev_unref(struct xc_entry *entry)
3610 if (entry->u.dev.tx) {
3611 netdev_close(entry->u.dev.tx);
3613 if (entry->u.dev.rx) {
3614 netdev_close(entry->u.dev.rx);
3616 if (entry->u.dev.bfd) {
3617 bfd_unref(entry->u.dev.bfd);
3622 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
3624 netflow_expire(netflow, flow);
3625 netflow_flow_clear(netflow, flow);
3626 netflow_unref(netflow);
3631 xlate_cache_clear(struct xlate_cache *xcache)
3633 struct xc_entry *entry;
3634 struct ofpbuf entries;
3640 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3641 switch (entry->type) {
3643 rule_dpif_unref(entry->u.rule);
3646 free(entry->u.bond.flow);
3647 bond_unref(entry->u.bond.bond);
3650 xlate_dev_unref(entry);
3653 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
3656 mbridge_unref(entry->u.mirror.mbridge);
3659 /* 'u.learn.rule' is the learned rule. */
3660 rule_dpif_unref(entry->u.learn.rule);
3663 free(entry->u.normal.flow);
3665 case XC_FIN_TIMEOUT:
3666 /* 'u.fin.rule' is always already held as a XC_RULE, which
3667 * has already released it's reference above. */
3674 ofpbuf_clear(&xcache->entries);
3678 xlate_cache_delete(struct xlate_cache *xcache)
3680 xlate_cache_clear(xcache);
3681 ofpbuf_uninit(&xcache->entries);