2 * Copyright (c) 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "wdp-xflow.h"
29 #include "openflow/nicira-ext.h"
30 #include "openflow/openflow.h"
32 #include "poll-loop.h"
33 #include "port-array.h"
40 #include "wdp-provider.h"
42 #include "xflow-util.h"
45 #define THIS_MODULE VLM_wdp_xflow
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
50 /* Maximum numbers of rules. */
51 #define WX_MAX_WILD 65536 /* Wildcarded rules. */
52 #define WX_MAX_EXACT 1048576 /* Exact-match rules. */
55 struct list list_node;
58 struct classifier cls;
59 struct netdev_monitor *netdev_monitor;
60 struct port_array ports; /* Index is ODP port nr; wdp_port->opp.port_no
62 struct shash port_by_name;
64 long long int next_expiration;
67 static struct list all_wx = LIST_INITIALIZER(&all_wx);
69 static int wx_port_init(struct wx *);
70 static void wx_port_run(struct wx *);
71 static void wx_port_refresh_groups(struct wx *);
79 wx_cast(const struct wdp *wdp)
81 return CONTAINER_OF(wdp, struct wx, wdp);
85 wx_xlate_actions(struct wx *, const union ofp_action *, size_t n,
86 const flow_t *flow, const struct ofpbuf *packet,
87 struct xflow_actions *out, bool *may_set_up_flow);
92 uint64_t packet_count; /* Number of packets received. */
93 uint64_t byte_count; /* Number of bytes received. */
94 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
95 long long int used; /* Last-used time (0 if never used). */
97 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
98 * exact-match rule (having cr.wc.wildcards of 0) generated from the
99 * wildcard rule 'super'. In this case, 'list' is an element of the
102 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
103 * a list of subrules. A super-rule with no wildcards (where
104 * cr.wc.wildcards is 0) will never have any subrules. */
105 struct wx_rule *super;
110 * A super-rule with wildcard fields never has XFLOW actions (since the
111 * datapath only supports exact-match flows). */
112 bool installed; /* Installed in datapath? */
113 bool may_install; /* True ordinarily; false if actions must
114 * be reassessed for every packet. */
116 union xflow_action *xflow_actions;
119 static void wx_rule_destroy(struct wx *, struct wx_rule *);
120 static void wx_rule_update_actions(struct wx *, struct wx_rule *);
121 static void wx_rule_execute(struct wx *, struct wx_rule *,
122 struct ofpbuf *packet, const flow_t *);
123 static bool wx_rule_make_actions(struct wx *, struct wx_rule *,
124 const struct ofpbuf *packet);
125 static void wx_rule_install(struct wx *, struct wx_rule *,
126 struct wx_rule *displaced_rule);
128 static struct wx_rule *
129 wx_rule_cast(const struct cls_rule *cls_rule)
131 return cls_rule ? CONTAINER_OF(cls_rule, struct wx_rule, wr.cr) : NULL;
134 /* Returns true if 'rule' is merely an implementation detail that should be
135 * hidden from the client. */
137 wx_rule_is_hidden(const struct wx_rule *rule)
139 return rule->super != NULL;
143 wx_rule_free(struct wx_rule *rule)
145 wdp_rule_uninit(&rule->wr);
146 free(rule->xflow_actions);
151 wx_rule_account(struct wx *wx OVS_UNUSED, struct wx_rule *rule OVS_UNUSED,
152 uint64_t extra_bytes OVS_UNUSED)
154 /* XXX call account_cb hook */
158 wx_rule_post_uninstall(struct wx *wx, struct wx_rule *rule)
160 struct wx_rule *super = rule->super;
162 wx_rule_account(wx, rule, 0);
164 /* XXX netflow expiration */
167 super->packet_count += rule->packet_count;
168 super->byte_count += rule->byte_count;
170 /* Reset counters to prevent double counting if the rule ever gets
172 rule->packet_count = 0;
173 rule->byte_count = 0;
174 rule->accounted_bytes = 0;
176 //XXX netflow_flow_clear(&rule->nf_flow);
181 xflow_flow_stats_to_msec(const struct xflow_flow_stats *stats)
183 return (stats->used_sec
184 ? stats->used_sec * 1000 + stats->used_nsec / 1000000
189 wx_rule_update_time(struct wx *wx OVS_UNUSED, struct wx_rule *rule,
190 const struct xflow_flow_stats *stats)
192 long long int used = xflow_flow_stats_to_msec(stats);
193 if (used > rule->used) {
195 if (rule->super && used > rule->super->used) {
196 rule->super->used = used;
198 //XXX netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
203 wx_rule_update_stats(struct wx *wx, struct wx_rule *rule,
204 const struct xflow_flow_stats *stats)
206 if (stats->n_packets) {
207 wx_rule_update_time(wx, rule, stats);
208 rule->packet_count += stats->n_packets;
209 rule->byte_count += stats->n_bytes;
210 /* XXX netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos,
211 stats->tcp_flags); */
216 wx_rule_uninstall(struct wx *wx, struct wx_rule *rule)
218 assert(!rule->wr.cr.flow.wildcards);
219 if (rule->installed) {
220 struct xflow_flow xflow_flow;
222 xflow_key_from_flow(&xflow_flow.key, &rule->wr.cr.flow);
223 xflow_flow.actions = NULL;
224 xflow_flow.n_actions = 0;
225 xflow_flow.flags = 0;
226 if (!xfif_flow_del(wx->xfif, &xflow_flow)) {
227 wx_rule_update_stats(wx, rule, &xflow_flow.stats);
229 rule->installed = false;
231 wx_rule_post_uninstall(wx, rule);
237 is_controller_rule(struct wx_rule *rule)
239 /* If the only action is send to the controller then don't report
240 * NetFlow expiration messages since it is just part of the control
241 * logic for the network and not real traffic. */
243 if (rule && rule->super) {
244 struct wdp_rule *super = &rule->super->wr;
246 return super->n_actions == 1 &&
247 super->actions[0].type == htons(OFPAT_OUTPUT) &&
248 super->actions[0].output.port == htons(OFPP_CONTROLLER);
256 wx_rule_remove(struct wx *wx, struct wx_rule *rule)
258 if (rule->wr.cr.flow.wildcards) {
259 COVERAGE_INC(wx_del_wc_flow);
260 wx->need_revalidate = true;
262 wx_rule_uninstall(wx, rule);
264 classifier_remove(&wx->cls, &rule->wr.cr);
265 wx_rule_destroy(wx, rule);
269 wx_rule_revalidate(struct wx *wx, struct wx_rule *rule)
271 const flow_t *flow = &rule->wr.cr.flow;
273 COVERAGE_INC(wx_rule_revalidate);
275 struct wx_rule *super;
276 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
278 wx_rule_remove(wx, rule);
280 } else if (super != rule->super) {
281 COVERAGE_INC(wx_revalidate_moved);
282 list_remove(&rule->list);
283 list_push_back(&super->list, &rule->list);
285 rule->wr.hard_timeout = super->wr.hard_timeout;
286 rule->wr.idle_timeout = super->wr.idle_timeout;
287 rule->wr.created = super->wr.created;
292 wx_rule_update_actions(wx, rule);
296 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
297 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
298 * through all of its subrules and revalidates them, destroying any that no
299 * longer has a super-rule (which is probably all of them).
301 * Before calling this function, the caller must make have removed 'rule' from
302 * the classifier. If 'rule' is an exact-match rule, the caller is also
303 * responsible for ensuring that it has been uninstalled from the datapath. */
305 wx_rule_destroy(struct wx *wx, struct wx_rule *rule)
308 struct wx_rule *subrule, *next;
309 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
310 wx_rule_revalidate(wx, subrule);
313 list_remove(&rule->list);
320 wx_rule_has_out_port(const struct wx_rule *rule, uint16_t out_port)
322 const union ofp_action *oa;
323 struct actions_iterator i;
325 if (out_port == htons(OFPP_NONE)) {
328 for (oa = actions_first(&i, rule->wr.actions,
331 oa = actions_next(&i)) {
332 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
340 /* Caller is responsible for initializing the 'cr' member of the returned
342 static struct wx_rule *
343 wx_rule_create(struct wx_rule *super,
344 const union ofp_action *actions, size_t n_actions,
345 uint16_t idle_timeout, uint16_t hard_timeout)
347 struct wx_rule *rule = xzalloc(sizeof *rule);
348 wdp_rule_init(&rule->wr, actions, n_actions);
349 rule->wr.idle_timeout = idle_timeout;
350 rule->wr.hard_timeout = hard_timeout;
351 rule->used = rule->wr.created;
354 list_push_back(&super->list, &rule->list);
356 list_init(&rule->list);
359 netflow_flow_clear(&rule->nf_flow);
360 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
366 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
367 * 'flow' and is considered to have arrived on XFLOW port 'in_port'.
369 * The flow that 'packet' actually contains does not need to actually match
370 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
371 * the packet and byte counters for 'rule' will be credited for the packet sent
372 * out whether or not the packet actually matches 'rule'.
374 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
375 * the caller must already have accurately composed XFLOW actions for it given
376 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
377 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
378 * function will compose a set of XFLOW actions based on 'rule''s OpenFlow
379 * actions and apply them to 'packet'. */
381 wx_rule_execute(struct wx *wx, struct wx_rule *rule,
382 struct ofpbuf *packet, const flow_t *flow)
384 const union xflow_action *actions;
386 struct xflow_actions a;
388 /* Grab or compose the XFLOW actions.
390 * The special case for an exact-match 'rule' where 'flow' is not the
391 * rule's flow is important to avoid, e.g., sending a packet out its input
392 * port simply because the XFLOW actions were composed for the wrong
394 if (rule->wr.cr.flow.wildcards
395 || !flow_equal(flow, &rule->wr.cr.flow))
397 struct wx_rule *super = rule->super ? rule->super : rule;
398 if (wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions, flow,
403 n_actions = a.n_actions;
405 actions = rule->xflow_actions;
406 n_actions = rule->n_xflow_actions;
409 /* Execute the XFLOW actions. */
410 if (!xfif_execute(wx->xfif, flow->in_port,
411 actions, n_actions, packet)) {
412 struct xflow_flow_stats stats;
413 flow_extract_stats(flow, packet, &stats);
414 wx_rule_update_stats(wx, rule, &stats);
415 rule->used = time_msec();
416 //XXX netflow_flow_update_time(wx->netflow, &rule->nf_flow, rule->used);
421 wx_rule_insert(struct wx *wx, struct wx_rule *rule, struct ofpbuf *packet,
424 struct wx_rule *displaced_rule;
426 /* Insert the rule in the classifier. */
427 displaced_rule = wx_rule_cast(classifier_insert(&wx->cls, &rule->wr.cr));
428 if (!rule->wr.cr.flow.wildcards) {
429 wx_rule_make_actions(wx, rule, packet);
432 /* Send the packet and credit it to the rule. */
435 flow_extract(packet, 0, in_port, &flow);
436 wx_rule_execute(wx, rule, packet, &flow);
439 /* Install the rule in the datapath only after sending the packet, to
440 * avoid packet reordering. */
441 if (rule->wr.cr.flow.wildcards) {
442 COVERAGE_INC(wx_add_wc_flow);
443 wx->need_revalidate = true;
445 wx_rule_install(wx, rule, displaced_rule);
448 /* Free the rule that was displaced, if any. */
449 if (displaced_rule) {
450 rule->wr.client_data = displaced_rule->wr.client_data;
451 wx_rule_destroy(wx, displaced_rule);
455 static struct wx_rule *
456 wx_rule_create_subrule(struct wx *wx, struct wx_rule *rule, const flow_t *flow)
458 struct wx_rule *subrule;
460 subrule = wx_rule_create(rule, NULL, 0,
461 rule->wr.idle_timeout,
462 rule->wr.hard_timeout);
463 COVERAGE_INC(wx_subrule_create);
464 cls_rule_from_flow(flow, &subrule->wr.cr);
465 classifier_insert_exact(&wx->cls, &subrule->wr.cr);
470 /* Returns true if the actions changed, false otherwise. */
472 wx_rule_make_actions(struct wx *wx, struct wx_rule *rule,
473 const struct ofpbuf *packet)
475 const struct wx_rule *super;
476 struct xflow_actions a;
479 assert(!rule->wr.cr.flow.wildcards);
481 super = rule->super ? rule->super : rule;
482 wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions,
483 &rule->wr.cr.flow, packet, &a, &rule->may_install);
485 actions_len = a.n_actions * sizeof *a.actions;
486 if (rule->n_xflow_actions != a.n_actions
487 || memcmp(rule->xflow_actions, a.actions, actions_len)) {
488 COVERAGE_INC(wx_xflow_unchanged);
489 free(rule->xflow_actions);
490 rule->n_xflow_actions = a.n_actions;
491 rule->xflow_actions = xmemdup(a.actions, actions_len);
499 do_put_flow(struct wx *wx, struct wx_rule *rule, int flags,
500 struct xflow_flow_put *put)
502 memset(&put->flow.stats, 0, sizeof put->flow.stats);
503 xflow_key_from_flow(&put->flow.key, &rule->wr.cr.flow);
504 put->flow.actions = rule->xflow_actions;
505 put->flow.n_actions = rule->n_xflow_actions;
508 return xfif_flow_put(wx->xfif, put);
512 wx_rule_install(struct wx *wx, struct wx_rule *rule, struct wx_rule *displaced_rule)
514 assert(!rule->wr.cr.flow.wildcards);
516 if (rule->may_install) {
517 struct xflow_flow_put put;
518 if (!do_put_flow(wx, rule,
519 XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS,
521 rule->installed = true;
522 if (displaced_rule) {
523 wx_rule_update_stats(wx, displaced_rule, &put.flow.stats);
524 wx_rule_post_uninstall(wx, displaced_rule);
527 } else if (displaced_rule) {
528 wx_rule_uninstall(wx, displaced_rule);
533 wx_rule_reinstall(struct wx *wx, struct wx_rule *rule)
535 if (rule->installed) {
536 struct xflow_flow_put put;
537 COVERAGE_INC(wx_dp_missed);
538 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY, &put);
540 wx_rule_install(wx, rule, NULL);
545 wx_rule_update_actions(struct wx *wx, struct wx_rule *rule)
547 bool actions_changed;
549 uint16_t new_out_iface, old_out_iface;
551 old_out_iface = rule->nf_flow.output_iface;
553 actions_changed = wx_rule_make_actions(wx, rule, NULL);
555 if (rule->may_install) {
556 if (rule->installed) {
557 if (actions_changed) {
558 struct xflow_flow_put put;
559 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY
560 | XFLOWPF_ZERO_STATS, &put);
561 wx_rule_update_stats(wx, rule, &put.flow.stats);
563 /* Temporarily set the old output iface so that NetFlow
564 * messages have the correct output interface for the old
566 new_out_iface = rule->nf_flow.output_iface;
567 rule->nf_flow.output_iface = old_out_iface;
569 wx_rule_post_uninstall(wx, rule);
570 //rule->nf_flow.output_iface = new_out_iface;
573 wx_rule_install(wx, rule, NULL);
576 wx_rule_uninstall(wx, rule);
581 add_output_group_action(struct xflow_actions *actions, uint16_t group,
582 uint16_t *nf_output_iface)
584 xflow_actions_add(actions, XFLOWAT_OUTPUT_GROUP)->output_group.group = group;
586 if (group == WX_GROUP_ALL || group == WX_GROUP_FLOOD) {
587 *nf_output_iface = NF_OUT_FLOOD;
592 add_controller_action(struct xflow_actions *actions,
593 const struct ofp_action_output *oao)
595 union xflow_action *a = xflow_actions_add(actions, XFLOWAT_CONTROLLER);
596 a->controller.arg = ntohs(oao->max_len);
599 struct wx_xlate_ctx {
601 flow_t flow; /* Flow to which these actions correspond. */
602 int recurse; /* Recursion level, via xlate_table_action. */
604 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
605 * null pointer if we are revalidating
606 * without a packet to refer to. */
609 struct xflow_actions *out; /* Datapath actions. */
610 //tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
611 bool may_set_up_flow; /* True ordinarily; false if the actions must
612 * be reassessed for every packet. */
613 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
616 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
617 struct wx_xlate_ctx *ctx);
620 add_output_action(struct wx_xlate_ctx *ctx, uint16_t port)
622 const struct wdp_port *wdp_port = port_array_get(&ctx->wx->ports, port);
625 if (wdp_port->opp.config & OFPPC_NO_FWD) {
626 /* Forwarding disabled on port. */
631 * We don't have an ofport record for this port, but it doesn't hurt to
632 * allow forwarding to it anyhow. Maybe such a port will appear later
633 * and we're pre-populating the flow table.
637 xflow_actions_add(ctx->out, XFLOWAT_OUTPUT)->output.port = port;
638 //ctx->nf_output_iface = port;
641 static struct wx_rule *
642 wx_rule_lookup_valid(struct wx *wx, const flow_t *flow)
644 struct wx_rule *rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
646 /* The rule we found might not be valid, since we could be in need of
647 * revalidation. If it is not valid, don't return it. */
650 && wx->need_revalidate
651 && !wx_rule_revalidate(wx, rule)) {
652 COVERAGE_INC(wx_invalidated);
660 xlate_table_action(struct wx_xlate_ctx *ctx, uint16_t in_port)
663 uint16_t old_in_port;
664 struct wx_rule *rule;
666 /* Look up a flow with 'in_port' as the input port. Then restore the
667 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
668 * have surprising behavior). */
669 old_in_port = ctx->flow.in_port;
670 ctx->flow.in_port = in_port;
671 rule = wx_rule_lookup_valid(ctx->wx, &ctx->flow);
672 ctx->flow.in_port = old_in_port;
680 do_xlate_actions(rule->wr.actions, rule->wr.n_actions, ctx);
687 xlate_output_action(struct wx_xlate_ctx *ctx,
688 const struct ofp_action_output *oao)
691 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
693 ctx->nf_output_iface = NF_OUT_DROP;
695 switch (ntohs(oao->port)) {
697 add_output_action(ctx, ctx->flow.in_port);
700 xlate_table_action(ctx, ctx->flow.in_port);
704 if (!ctx->wx->ofhooks->normal_cb(ctx->flow, ctx->packet,
706 &ctx->nf_output_iface,
708 COVERAGE_INC(wx_uninstallable);
709 ctx->may_set_up_flow = false;
713 /* fall through to flood for now */
716 add_output_group_action(ctx->out, WX_GROUP_FLOOD,
717 &ctx->nf_output_iface);
720 add_output_group_action(ctx->out, WX_GROUP_ALL, &ctx->nf_output_iface);
722 case OFPP_CONTROLLER:
723 add_controller_action(ctx->out, oao);
726 add_output_action(ctx, XFLOWP_LOCAL);
729 xflow_port = ofp_port_to_xflow_port(ntohs(oao->port));
730 if (xflow_port != ctx->flow.in_port) {
731 add_output_action(ctx, xflow_port);
736 if (prev_nf_output_iface == NF_OUT_FLOOD) {
737 ctx->nf_output_iface = NF_OUT_FLOOD;
738 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
739 ctx->nf_output_iface = prev_nf_output_iface;
740 } else if (prev_nf_output_iface != NF_OUT_DROP &&
741 ctx->nf_output_iface != NF_OUT_FLOOD) {
742 ctx->nf_output_iface = NF_OUT_MULTI;
747 xlate_nicira_action(struct wx_xlate_ctx *ctx,
748 const struct nx_action_header *nah)
750 const struct nx_action_resubmit *nar;
751 const struct nx_action_set_tunnel *nast;
752 union xflow_action *oa;
753 int subtype = ntohs(nah->subtype);
755 assert(nah->vendor == htonl(NX_VENDOR_ID));
758 nar = (const struct nx_action_resubmit *) nah;
759 xlate_table_action(ctx, ofp_port_to_xflow_port(ntohs(nar->in_port)));
762 case NXAST_SET_TUNNEL:
763 nast = (const struct nx_action_set_tunnel *) nah;
764 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TUNNEL);
765 ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id;
768 /* If you add a new action here that modifies flow data, don't forget to
769 * update the flow key in ctx->flow in the same key. */
772 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
778 do_xlate_actions(const union ofp_action *in, size_t n_in,
779 struct wx_xlate_ctx *ctx)
781 struct actions_iterator iter;
782 const union ofp_action *ia;
783 const struct wdp_port *port;
785 port = port_array_get(&ctx->wx->ports, ctx->flow.in_port);
786 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
787 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, stp_eth_addr)
788 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
789 /* Drop this flow. */
793 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
794 uint16_t type = ntohs(ia->type);
795 union xflow_action *oa;
799 xlate_output_action(ctx, &ia->output);
802 case OFPAT_SET_VLAN_VID:
803 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
804 oa->dl_tci.tci = ia->vlan_vid.vlan_vid & htons(VLAN_VID_MASK);
805 oa->dl_tci.mask = htons(VLAN_VID_MASK);
806 ctx->flow.dl_vlan = ia->vlan_vid.vlan_vid;
809 case OFPAT_SET_VLAN_PCP:
810 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
811 oa->dl_tci.tci = htons((ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT)
813 oa->dl_tci.mask = htons(VLAN_PCP_MASK);
815 if (ctx->flow.dl_vlan == htons(OFP_VLAN_NONE)) {
816 ctx->flow.dl_vlan = htons(0);
818 ctx->flow.dl_vlan_pcp = ia->vlan_pcp.vlan_pcp;
821 case OFPAT_STRIP_VLAN:
822 xflow_actions_add(ctx->out, XFLOWAT_STRIP_VLAN);
823 ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
824 ctx->flow.dl_vlan_pcp = 0;
827 case OFPAT_SET_DL_SRC:
828 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_SRC);
829 memcpy(oa->dl_addr.dl_addr,
830 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
831 memcpy(ctx->flow.dl_src,
832 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
835 case OFPAT_SET_DL_DST:
836 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_DST);
837 memcpy(oa->dl_addr.dl_addr,
838 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
839 memcpy(ctx->flow.dl_dst,
840 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
843 case OFPAT_SET_NW_SRC:
844 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_SRC);
845 ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
848 case OFPAT_SET_NW_DST:
849 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_DST);
850 ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
853 case OFPAT_SET_NW_TOS:
854 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_TOS);
855 ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
858 case OFPAT_SET_TP_SRC:
859 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_SRC);
860 ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
863 case OFPAT_SET_TP_DST:
864 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_DST);
865 ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
869 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
873 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
879 /* Returns true if 'flow' and 'actions' may be set up as a flow in the kernel.
880 * This is true most of the time, but we don't allow flows that would prevent
881 * DHCP replies from being seen by the local port to be set up in the
884 * We only need this, strictly speaking, when in-band control is turned on. */
886 wx_may_set_up(const flow_t *flow, const struct xflow_actions *actions)
888 if (flow->dl_type == htons(ETH_TYPE_IP)
889 && flow->nw_proto == IP_TYPE_UDP
890 && flow->tp_src == htons(DHCP_SERVER_PORT)
891 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
894 for (i = 0; i < actions->n_actions; i++) {
895 const struct xflow_action_output *oao = &actions->actions[i].output;
896 if (oao->type == XFLOWAT_OUTPUT && oao->port == XFLOWP_LOCAL) {
907 wx_xlate_actions(struct wx *wx, const union ofp_action *in, size_t n_in,
908 const flow_t *flow, const struct ofpbuf *packet,
909 struct xflow_actions *out, bool *may_set_up_flow)
911 //tag_type no_tags = 0;
912 struct wx_xlate_ctx ctx;
913 COVERAGE_INC(wx_ofp2xflow);
914 xflow_actions_init(out);
920 //ctx.tags = tags ? tags : &no_tags;
921 ctx.may_set_up_flow = true;
922 ctx.nf_output_iface = NF_OUT_DROP;
923 do_xlate_actions(in, n_in, &ctx);
925 if (may_set_up_flow) {
926 *may_set_up_flow = ctx.may_set_up_flow && wx_may_set_up(flow, out);
929 if (nf_output_iface) {
930 *nf_output_iface = ctx.nf_output_iface;
933 if (xflow_actions_overflow(out)) {
934 xflow_actions_init(out);
935 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
941 update_used(struct wx *wx)
943 struct xflow_flow *flows;
948 error = xfif_flow_list_all(wx->xfif, &flows, &n_flows);
953 for (i = 0; i < n_flows; i++) {
954 struct xflow_flow *f = &flows[i];
955 struct wx_rule *rule;
958 xflow_key_to_flow(&f->key, &flow);
959 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, &flow));
960 if (!rule || !rule->installed) {
961 COVERAGE_INC(wx_unexpected_rule);
962 xfif_flow_del(wx->xfif, f);
966 wx_rule_update_time(wx, rule, &f->stats);
967 wx_rule_account(wx, rule, f->stats.n_bytes);
973 uninstall_idle_flow(struct wx *wx, struct wx_rule *rule)
975 assert(rule->installed);
976 assert(!rule->wr.cr.flow.wildcards);
979 wx_rule_remove(wx, rule);
981 wx_rule_uninstall(wx, rule);
986 expire_rule(struct cls_rule *cls_rule, void *wx_)
989 struct wx_rule *rule = wx_rule_cast(cls_rule);
990 long long int hard_expire, idle_expire, expire, now;
992 hard_expire = (rule->wr.hard_timeout
993 ? rule->wr.created + rule->wr.hard_timeout * 1000
995 idle_expire = (rule->wr.idle_timeout
996 && (rule->super || list_is_empty(&rule->list))
997 ? rule->used + rule->wr.idle_timeout * 1000
999 expire = MIN(hard_expire, idle_expire);
1003 if (rule->installed && now >= rule->used + 5000) {
1004 uninstall_idle_flow(wx, rule);
1005 } else if (!rule->wr.cr.flow.wildcards) {
1006 //XXX active_timeout(wx, rule);
1012 COVERAGE_INC(wx_expired);
1014 /* Update stats. This code will be a no-op if the rule expired
1015 * due to an idle timeout. */
1016 if (rule->wr.cr.flow.wildcards) {
1017 struct wx_rule *subrule, *next;
1018 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
1019 wx_rule_remove(wx, subrule);
1022 wx_rule_uninstall(wx, rule);
1026 if (!wx_rule_is_hidden(rule)) {
1027 send_flow_removed(wx, rule, now,
1029 ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
1032 wx_rule_remove(wx, rule);
1035 struct revalidate_cbdata {
1037 bool revalidate_all; /* Revalidate all exact-match rules? */
1038 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
1039 //struct tag_set revalidate_set; /* Set of tags to revalidate. */
1043 revalidate_rule(struct wx *wx, struct wx_rule *rule)
1045 const flow_t *flow = &rule->wr.cr.flow;
1047 COVERAGE_INC(wx_revalidate_rule);
1049 struct wx_rule *super;
1050 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
1052 wx_rule_remove(wx, rule);
1054 } else if (super != rule->super) {
1055 COVERAGE_INC(wx_revalidate_moved);
1056 list_remove(&rule->list);
1057 list_push_back(&super->list, &rule->list);
1058 rule->super = super;
1059 rule->wr.hard_timeout = super->wr.hard_timeout;
1060 rule->wr.idle_timeout = super->wr.idle_timeout;
1061 rule->wr.created = super->wr.created;
1066 wx_rule_update_actions(wx, rule);
1071 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
1073 struct wx_rule *sub = wx_rule_cast(sub_);
1074 struct revalidate_cbdata *cbdata = cbdata_;
1076 if (cbdata->revalidate_all
1077 || (cbdata->revalidate_subrules && sub->super)
1078 /*|| (tag_set_intersects(&cbdata->revalidate_set, sub->tags))*/) {
1079 revalidate_rule(cbdata->wx, sub);
1084 wx_run_one(struct wx *wx)
1088 if (time_msec() >= wx->next_expiration) {
1089 COVERAGE_INC(wx_expiration);
1090 wx->next_expiration = time_msec() + 1000;
1093 classifier_for_each(&wx->cls, CLS_INC_ALL, expire_rule, wx);
1095 /* XXX account_checkpoint_cb */
1098 if (wx->need_revalidate /*|| !tag_set_is_empty(&p->revalidate_set)*/) {
1099 struct revalidate_cbdata cbdata;
1101 cbdata.revalidate_all = false;
1102 cbdata.revalidate_subrules = wx->need_revalidate;
1103 //cbdata.revalidate_set = wx->revalidate_set;
1104 //tag_set_init(&wx->revalidate_set);
1105 COVERAGE_INC(wx_revalidate);
1106 classifier_for_each(&wx->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
1107 wx->need_revalidate = false;
1116 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1123 wx_wait_one(struct wx *wx)
1125 xfif_port_poll_wait(wx->xfif);
1126 netdev_monitor_poll_wait(wx->netdev_monitor);
1127 if (wx->need_revalidate /*|| !tag_set_is_empty(&p->revalidate_set)*/) {
1128 poll_immediate_wake();
1129 } else if (wx->next_expiration != LLONG_MAX) {
1130 poll_timer_wait_until(wx->next_expiration);
1139 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1145 static int wx_flow_flush(struct wdp *);
1148 wx_enumerate(const struct wdp_class *wdp_class, struct svec *all_wdps)
1150 struct svec names = SVEC_EMPTY_INITIALIZER;
1151 int error = xf_enumerate_names(wdp_class->type, &names);
1152 svec_move(all_wdps, &names);
1157 wx_open(const struct wdp_class *wdp_class, const char *name, bool create,
1164 ? xfif_create_and_open(name, wdp_class->type, &xfif)
1165 : xfif_open(name, wdp_class->type, &xfif));
1169 wx = xzalloc(sizeof *wx);
1170 list_push_back(&all_wx, &wx->list_node);
1171 wdp_init(&wx->wdp, wdp_class, name, 0, 0);
1173 classifier_init(&wx->cls);
1174 wx->netdev_monitor = netdev_monitor_create();
1175 port_array_init(&wx->ports);
1176 shash_init(&wx->port_by_name);
1177 wx->next_expiration = time_msec() + 1000;
1188 wx_close(struct wdp *wdp)
1190 struct wx *wx = wx_cast(wdp);
1193 xfif_close(wx->xfif);
1194 classifier_destroy(&wx->cls);
1195 netdev_monitor_destroy(wx->netdev_monitor);
1196 list_remove(&wx->list_node);
1201 wx_get_all_names(const struct wdp *wdp, struct svec *all_names)
1203 struct wx *wx = wx_cast(wdp);
1205 return xfif_get_all_names(wx->xfif, all_names);
1209 wx_destroy(struct wdp *wdp)
1211 struct wx *wx = wx_cast(wdp);
1213 return xfif_delete(wx->xfif);
1217 hton_ofp_phy_port(struct ofp_phy_port *opp)
1219 opp->port_no = htons(opp->port_no);
1220 opp->config = htonl(opp->config);
1221 opp->state = htonl(opp->state);
1222 opp->curr = htonl(opp->curr);
1223 opp->advertised = htonl(opp->advertised);
1224 opp->supported = htonl(opp->supported);
1225 opp->peer = htonl(opp->peer);
1229 wx_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
1231 struct wx *wx = wx_cast(wdp);
1232 struct ofp_switch_features *osf;
1234 unsigned int port_no;
1235 struct wdp_port *port;
1237 buf = ofpbuf_new(sizeof *osf);
1238 osf = ofpbuf_put_zeros(buf, sizeof *osf);
1240 osf->capabilities = htonl(OFPC_ARP_MATCH_IP);
1241 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1242 (1u << OFPAT_SET_VLAN_VID) |
1243 (1u << OFPAT_SET_VLAN_PCP) |
1244 (1u << OFPAT_STRIP_VLAN) |
1245 (1u << OFPAT_SET_DL_SRC) |
1246 (1u << OFPAT_SET_DL_DST) |
1247 (1u << OFPAT_SET_NW_SRC) |
1248 (1u << OFPAT_SET_NW_DST) |
1249 (1u << OFPAT_SET_NW_TOS) |
1250 (1u << OFPAT_SET_TP_SRC) |
1251 (1u << OFPAT_SET_TP_DST));
1253 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1254 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1262 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
1264 struct wx_rule *rule = wx_rule_cast(cls_rule);
1265 int *n_subrules = n_subrules_;
1273 wx_get_stats(const struct wdp *wdp, struct wdp_stats *stats)
1275 struct wx *wx = wx_cast(wdp);
1276 struct xflow_stats xflow_stats;
1280 error = xfif_get_xf_stats(wx->xfif, &xflow_stats);
1283 classifier_for_each(&wx->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
1285 stats->exact.n_flows = classifier_count_exact(&wx->cls) - n_subrules;
1286 stats->exact.cur_capacity = xflow_stats.cur_capacity;
1287 stats->exact.max_capacity = MIN(WX_MAX_EXACT, xflow_stats.max_capacity);
1288 stats->exact.n_hit = xflow_stats.n_hit;
1289 stats->exact.n_missed = xflow_stats.n_missed;
1290 stats->exact.n_lost = xflow_stats.n_lost;
1292 stats->wild.n_flows = classifier_count_wild(&wx->cls);
1293 stats->wild.cur_capacity = WX_MAX_WILD;
1294 stats->wild.max_capacity = WX_MAX_WILD;
1295 stats->wild.n_hit = 0; /* XXX */
1296 stats->wild.n_missed = 0; /* XXX */
1297 stats->wild.n_lost = 0; /* XXX */
1299 stats->n_ports = xflow_stats.n_ports;
1300 stats->max_ports = xflow_stats.max_ports;
1302 stats->n_frags = xflow_stats.n_frags;
1304 stats->max_miss_queue = xflow_stats.max_miss_queue;
1305 stats->max_action_queue = xflow_stats.max_action_queue;
1306 stats->max_sflow_queue = xflow_stats.max_sflow_queue;
1312 wx_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
1314 struct wx *wx = wx_cast(wdp);
1316 return xfif_get_drop_frags(wx->xfif, drop_frags);
1320 wx_set_drop_frags(struct wdp *wdp, bool drop_frags)
1322 struct wx *wx = wx_cast(wdp);
1324 return xfif_set_drop_frags(wx->xfif, drop_frags);
1328 wx_port_add(struct wdp *wdp, const char *devname,
1329 bool internal, uint16_t *port_no)
1331 struct wx *wx = wx_cast(wdp);
1332 uint16_t xflow_flags = internal ? XFLOW_PORT_INTERNAL : 0;
1333 return xfif_port_add(wx->xfif, devname, xflow_flags, port_no);
1337 wx_port_del(struct wdp *wdp, uint16_t port_no)
1339 struct wx *wx = wx_cast(wdp);
1341 return xfif_port_del(wx->xfif, port_no);
1345 wx_answer_port_query(const struct wdp_port *port, struct wdp_port *portp)
1348 wdp_port_copy(portp, port);
1356 wx_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
1357 struct wdp_port *portp)
1359 struct wx *wx = wx_cast(wdp);
1360 const struct wdp_port *port;
1362 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1363 return wx_answer_port_query(port, portp);
1367 wx_port_query_by_name(const struct wdp *wdp, const char *devname,
1368 struct wdp_port *portp)
1370 struct wx *wx = wx_cast(wdp);
1372 return wx_answer_port_query(shash_find_data(&wx->port_by_name, devname),
1377 wx_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
1379 struct wx *wx = wx_cast(wdp);
1380 struct wdp_port *port;
1383 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1387 changes = config ^ port->opp.config;
1389 if (changes & OFPPC_PORT_DOWN) {
1391 if (config & OFPPC_PORT_DOWN) {
1392 error = netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1394 error = netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1397 port->opp.config ^= OFPPC_PORT_DOWN;
1401 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
1402 if (changes & REVALIDATE_BITS) {
1403 COVERAGE_INC(wx_costly_flags);
1404 port->opp.config ^= changes & REVALIDATE_BITS;
1405 wx->need_revalidate = true;
1407 #undef REVALIDATE_BITS
1409 if (changes & OFPPC_NO_FLOOD) {
1410 port->opp.config ^= OFPPC_NO_FLOOD;
1411 wx_port_refresh_groups(wx);
1414 if (changes & OFPPC_NO_PACKET_IN) {
1415 port->opp.config ^= OFPPC_NO_PACKET_IN;
1422 wx_port_list(const struct wdp *wdp, struct wdp_port **portsp, size_t *n_portsp)
1424 struct wx *wx = wx_cast(wdp);
1425 struct wdp_port *ports, *port;
1426 unsigned int port_no;
1429 *n_portsp = n_ports = port_array_count(&wx->ports);
1430 *portsp = ports = xmalloc(n_ports * sizeof *ports);
1432 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1433 wdp_port_copy(&ports[i++], port);
1435 assert(i == n_ports);
1441 wx_port_poll(const struct wdp *wdp, char **devnamep)
1443 struct wx *wx = wx_cast(wdp);
1445 return xfif_port_poll(wx->xfif, devnamep);
1449 wx_port_poll_wait(const struct wdp *wdp)
1451 struct wx *wx = wx_cast(wdp);
1453 xfif_port_poll_wait(wx->xfif);
1456 static struct wdp_rule *
1457 wx_flow_get(const struct wdp *wdp, const flow_t *flow)
1459 struct wx *wx = wx_cast(wdp);
1460 struct wx_rule *rule;
1462 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, flow));
1463 return rule && !wx_rule_is_hidden(rule) ? &rule->wr : NULL;
1466 static struct wdp_rule *
1467 wx_flow_match(const struct wdp *wdp, const flow_t *flow)
1469 struct wx *wx = wx_cast(wdp);
1470 struct wx_rule *rule;
1472 rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
1474 if (wx_rule_is_hidden(rule)) {
1483 struct wx_for_each_thunk_aux {
1484 wdp_flow_cb_func *client_callback;
1489 wx_for_each_thunk(struct cls_rule *cls_rule, void *aux_)
1491 struct wx_for_each_thunk_aux *aux = aux_;
1492 struct wx_rule *rule = wx_rule_cast(cls_rule);
1494 if (!wx_rule_is_hidden(rule)) {
1495 aux->client_callback(&rule->wr, aux->client_aux);
1500 wx_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
1502 wdp_flow_cb_func *client_callback, void *client_aux)
1504 struct wx *wx = wx_cast(wdp);
1505 struct wx_for_each_thunk_aux aux;
1507 aux.client_callback = client_callback;
1508 aux.client_aux = client_aux;
1509 classifier_for_each_match(&wx->cls, target, include,
1510 wx_for_each_thunk, &aux);
1513 /* Obtains statistic counters for 'rule' within 'wx' and stores them into
1514 * '*stats'. If 'rule' is a wildcarded rule, the returned statistic include
1515 * statistics for all of 'rule''s subrules. */
1517 query_stats(struct wx *wx, struct wx_rule *rule, struct wdp_flow_stats *stats)
1519 struct wx_rule *subrule;
1520 struct xflow_flow *xflow_flows;
1521 size_t n_xflow_flows;
1523 /* Start from historical data for 'rule' itself that are no longer tracked
1524 * by the datapath. This counts, for example, subrules that have
1526 stats->n_packets = rule->packet_count;
1527 stats->n_bytes = rule->byte_count;
1528 stats->inserted = rule->wr.created;
1529 stats->used = LLONG_MIN;
1530 stats->tcp_flags = 0;
1533 /* Prepare to ask the datapath for statistics on 'rule', or if it is
1534 * wildcarded then on all of its subrules.
1536 * Also, add any statistics that are not tracked by the datapath for each
1537 * subrule. This includes, for example, statistics for packets that were
1538 * executed "by hand" by ofproto via xfif_execute() but must be accounted
1540 n_xflow_flows = rule->wr.cr.flow.wildcards ? list_size(&rule->list) : 1;
1541 xflow_flows = xzalloc(n_xflow_flows * sizeof *xflow_flows);
1542 if (rule->wr.cr.flow.wildcards) {
1544 LIST_FOR_EACH (subrule, struct wx_rule, list, &rule->list) {
1545 xflow_key_from_flow(&xflow_flows[i++].key, &subrule->wr.cr.flow);
1546 stats->n_packets += subrule->packet_count;
1547 stats->n_bytes += subrule->byte_count;
1550 xflow_key_from_flow(&xflow_flows[0].key, &rule->wr.cr.flow);
1553 /* Fetch up-to-date statistics from the datapath and add them in. */
1554 if (!xfif_flow_get_multiple(wx->xfif, xflow_flows, n_xflow_flows)) {
1556 for (i = 0; i < n_xflow_flows; i++) {
1557 struct xflow_flow *xflow_flow = &xflow_flows[i];
1560 stats->n_packets += xflow_flow->stats.n_packets;
1561 stats->n_bytes += xflow_flow->stats.n_bytes;
1562 used = xflow_flow_stats_to_msec(&xflow_flow->stats);
1563 if (used > stats->used) {
1565 if (xflow_flow->key.dl_type == htons(ETH_TYPE_IP)
1566 && xflow_flow->key.nw_proto == IP_TYPE_TCP) {
1567 stats->ip_tos = xflow_flow->stats.ip_tos;
1570 stats->tcp_flags |= xflow_flow->stats.tcp_flags;
1577 wx_flow_get_stats(const struct wdp *wdp,
1578 const struct wdp_rule *wdp_rule,
1579 struct wdp_flow_stats *stats)
1581 struct wx *wx = wx_cast(wdp);
1582 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1584 query_stats(wx, rule, stats);
1589 wx_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
1591 struct wx *wx = wx_cast(wdp);
1593 /* XXX overlap with a subrule? */
1594 return classifier_rule_overlaps(&wx->cls, flow);
1598 wx_flow_put(struct wdp *wdp, const struct wdp_flow_put *put,
1599 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
1601 struct wx *wx = wx_cast(wdp);
1602 struct wx_rule *rule;
1604 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, put->flow));
1605 if (rule && wx_rule_is_hidden(rule)) {
1610 if (!(put->flags & WDP_PUT_MODIFY)) {
1614 if (!(put->flags & WDP_PUT_CREATE)) {
1617 if ((put->flow->wildcards
1618 ? classifier_count_wild(&wx->cls) >= WX_MAX_WILD
1619 : classifier_count_exact(&wx->cls) >= WX_MAX_EXACT)) {
1620 /* XXX subrules should not count against exact-match limit */
1625 rule = wx_rule_create(NULL, put->actions, put->n_actions,
1626 put->idle_timeout, put->hard_timeout);
1627 cls_rule_from_flow(put->flow, &rule->wr.cr);
1628 wx_rule_insert(wx, rule, NULL, 0);
1632 memset(old_stats, 0, sizeof *old_stats);
1642 wx_flow_delete(struct wdp *wdp, struct wdp_rule *wdp_rule,
1643 struct wdp_flow_stats *final_stats)
1645 struct wx *wx = wx_cast(wdp);
1646 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1648 wx_rule_remove(wx, rule);
1650 memset(final_stats, 0, sizeof *final_stats); /* XXX */
1656 wx_flush_rule(struct cls_rule *cls_rule, void *wx_)
1658 struct wx_rule *rule = wx_rule_cast(cls_rule);
1659 struct wx *wx = wx_;
1661 /* Mark the flow as not installed, even though it might really be
1662 * installed, so that wx_rule_remove() doesn't bother trying to uninstall
1663 * it. There is no point in uninstalling it individually since we are
1664 * about to blow away all the flows with xfif_flow_flush(). */
1665 rule->installed = false;
1667 wx_rule_remove(wx, rule);
1671 wx_flow_flush(struct wdp *wdp)
1673 struct wx *wx = wx_cast(wdp);
1675 COVERAGE_INC(wx_flow_flush);
1676 classifier_for_each(&wx->cls, CLS_INC_ALL, wx_flush_rule, wx);
1677 xfif_flow_flush(wx->xfif);
1682 wx_execute(struct wdp *wdp, uint16_t in_port,
1683 const union ofp_action actions[], int n_actions,
1684 const struct ofpbuf *packet)
1686 struct wx *wx = wx_cast(wdp);
1687 struct xflow_actions xflow_actions;
1691 flow_extract((struct ofpbuf *) packet, 0, in_port, &flow);
1692 error = wx_xlate_actions(wx, actions, n_actions, &flow, packet,
1693 &xflow_actions, NULL);
1697 xfif_execute(wx->xfif, ofp_port_to_xflow_port(in_port),
1698 xflow_actions.actions, xflow_actions.n_actions, packet);
1703 wx_flow_inject(struct wdp *wdp, struct wdp_rule *wdp_rule,
1704 uint16_t in_port, const struct ofpbuf *packet)
1706 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1709 error = wx_execute(wdp, in_port, rule->wr.actions, rule->wr.n_actions,
1712 rule->packet_count++;
1713 rule->byte_count += packet->size;
1714 rule->used = time_msec();
1720 wx_recv_get_mask(const struct wdp *wdp, int *listen_mask)
1722 struct wx *wx = wx_cast(wdp);
1723 int xflow_listen_mask;
1726 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
1729 if (xflow_listen_mask & XFLOWL_MISS) {
1730 *listen_mask |= 1 << WDP_CHAN_MISS;
1732 if (xflow_listen_mask & XFLOWL_ACTION) {
1733 *listen_mask |= 1 << WDP_CHAN_ACTION;
1735 if (xflow_listen_mask & XFLOWL_SFLOW) {
1736 *listen_mask |= 1 << WDP_CHAN_SFLOW;
1743 wx_recv_set_mask(struct wdp *wdp, int listen_mask)
1745 struct wx *wx = wx_cast(wdp);
1746 int xflow_listen_mask;
1748 xflow_listen_mask = 0;
1749 if (listen_mask & (1 << WDP_CHAN_MISS)) {
1750 xflow_listen_mask |= XFLOWL_MISS;
1752 if (listen_mask & (1 << WDP_CHAN_ACTION)) {
1753 xflow_listen_mask |= XFLOWL_ACTION;
1755 if (listen_mask & (1 << WDP_CHAN_SFLOW)) {
1756 xflow_listen_mask |= XFLOWL_SFLOW;
1759 return xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
1763 wx_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
1765 struct wx *wx = wx_cast(wdp);
1767 return xfif_get_sflow_probability(wx->xfif, probability);
1771 wx_set_sflow_probability(struct wdp *wdp, uint32_t probability)
1773 struct wx *wx = wx_cast(wdp);
1775 return xfif_set_sflow_probability(wx->xfif, probability);
1779 wx_translate_xflow_msg(struct xflow_msg *msg, struct ofpbuf *payload,
1780 struct wdp_packet *packet)
1782 packet->in_port = xflow_port_to_ofp_port(msg->port);
1783 packet->send_len = 0;
1786 switch (msg->type) {
1787 case _XFLOWL_MISS_NR:
1788 packet->channel = WDP_CHAN_MISS;
1789 packet->payload = payload;
1790 packet->tun_id = msg->arg;
1793 case _XFLOWL_ACTION_NR:
1794 packet->channel = WDP_CHAN_ACTION;
1795 packet->payload = payload;
1796 packet->send_len = msg->arg;
1799 case _XFLOWL_SFLOW_NR:
1801 ofpbuf_delete(payload);
1805 VLOG_WARN_RL(&rl, "received XFLOW message of unexpected type %"PRIu32,
1807 ofpbuf_delete(payload);
1812 static const uint8_t *
1813 get_local_mac(const struct wx *wx)
1815 const struct wdp_port *port = port_array_get(&wx->ports, XFLOWP_LOCAL);
1816 return port ? port->opp.hw_addr : NULL;
1819 /* Returns true if 'packet' is a DHCP reply to the local port. Such a reply
1820 * should be sent to the local port regardless of the flow table.
1822 * We only need this, strictly speaking, when in-band control is turned on. */
1824 wx_is_local_dhcp_reply(const struct wx *wx,
1825 const flow_t *flow, const struct ofpbuf *packet)
1827 if (flow->dl_type == htons(ETH_TYPE_IP)
1828 && flow->nw_proto == IP_TYPE_UDP
1829 && flow->tp_src == htons(DHCP_SERVER_PORT)
1830 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
1833 const uint8_t *local_mac = get_local_mac(wx);
1834 struct dhcp_header *dhcp = ofpbuf_at(
1835 packet, (char *)packet->l7 - (char *)packet->data, sizeof *dhcp);
1836 return dhcp && local_mac && eth_addr_equals(dhcp->chaddr, local_mac);
1843 wx_explode_rule(struct wx *wx, struct xflow_msg *msg, struct ofpbuf *payload)
1845 struct wx_rule *rule;
1848 flow_extract(payload, 0, xflow_port_to_ofp_port(msg->port), &flow);
1850 if (wx_is_local_dhcp_reply(wx, &flow, payload)) {
1851 union xflow_action action;
1853 memset(&action, 0, sizeof(action));
1854 action.output.type = XFLOWAT_OUTPUT;
1855 action.output.port = XFLOWP_LOCAL;
1856 xfif_execute(wx->xfif, msg->port, &action, 1, payload);
1859 rule = wx_rule_lookup_valid(wx, &flow);
1864 if (rule->wr.cr.flow.wildcards) {
1865 rule = wx_rule_create_subrule(wx, rule, &flow);
1866 wx_rule_make_actions(wx, rule, payload);
1868 if (!rule->may_install) {
1869 /* The rule is not installable, that is, we need to process every
1870 * packet, so process the current packet and set its actions into
1872 wx_rule_make_actions(wx, rule, payload);
1874 /* XXX revalidate rule if it needs it */
1878 wx_rule_execute(wx, rule, payload, &flow);
1879 wx_rule_reinstall(wx, rule);
1885 wx_recv(struct wdp *wdp, struct wdp_packet *packet)
1887 struct wx *wx = wx_cast(wdp);
1890 /* XXX need to avoid 50*50 potential cost for caller. */
1891 for (i = 0; i < 50; i++) {
1892 struct xflow_msg *msg;
1896 error = xfif_recv(wx->xfif, &buf);
1901 msg = ofpbuf_pull(buf, sizeof *msg);
1902 if (msg->type != _XFLOWL_MISS_NR || !wx_explode_rule(wx, msg, buf)) {
1903 return wx_translate_xflow_msg(msg, buf, packet);
1911 wx_recv_wait(struct wdp *wdp)
1913 struct wx *wx = wx_cast(wdp);
1915 xfif_recv_wait(wx->xfif);
1918 static void wx_port_update(struct wx *, const char *devname);
1919 static void wx_port_reinit(struct wx *);
1922 wx_port_process_change(struct wx *wx, int error, char *devname)
1924 if (error == ENOBUFS) {
1926 } else if (!error) {
1927 wx_port_update(wx, devname);
1933 wx_port_run(struct wx *wx)
1938 while ((error = xfif_port_poll(wx->xfif, &devname)) != EAGAIN) {
1939 wx_port_process_change(wx, error, devname);
1941 while ((error = netdev_monitor_poll(wx->netdev_monitor,
1942 &devname)) != EAGAIN) {
1943 wx_port_process_change(wx, error, devname);
1948 wx_port_refresh_group(struct wx *wx, unsigned int group)
1952 struct wdp_port *port;
1953 unsigned int port_no;
1955 assert(group == WX_GROUP_ALL || group == WX_GROUP_FLOOD);
1957 ports = xmalloc(port_array_count(&wx->ports) * sizeof *ports);
1959 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1960 if (group == WX_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
1961 ports[n_ports++] = port_no;
1964 xfif_port_group_set(wx->xfif, group, ports, n_ports);
1971 wx_port_refresh_groups(struct wx *wx)
1973 wx_port_refresh_group(wx, WX_GROUP_FLOOD);
1974 wx_port_refresh_group(wx, WX_GROUP_ALL);
1978 wx_port_reinit(struct wx *wx)
1980 struct svec devnames;
1981 struct wdp_port *wdp_port;
1982 unsigned int port_no;
1983 struct xflow_port *xflow_ports;
1984 size_t n_xflow_ports;
1987 svec_init(&devnames);
1988 PORT_ARRAY_FOR_EACH (wdp_port, &wx->ports, port_no) {
1989 svec_add (&devnames, (char *) wdp_port->opp.name);
1991 xfif_port_list(wx->xfif, &xflow_ports, &n_xflow_ports);
1992 for (i = 0; i < n_xflow_ports; i++) {
1993 svec_add(&devnames, xflow_ports[i].devname);
1997 svec_sort_unique(&devnames);
1998 for (i = 0; i < devnames.n; i++) {
1999 wx_port_update(wx, devnames.names[i]);
2001 svec_destroy(&devnames);
2003 wx_port_refresh_groups(wx);
2006 static struct wdp_port *
2007 make_wdp_port(const struct xflow_port *xflow_port)
2009 struct netdev_options netdev_options;
2010 enum netdev_flags flags;
2011 struct wdp_port *wdp_port;
2012 struct netdev *netdev;
2016 memset(&netdev_options, 0, sizeof netdev_options);
2017 netdev_options.name = xflow_port->devname;
2018 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
2020 error = netdev_open(&netdev_options, &netdev);
2022 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
2023 "cannot be opened (%s)",
2024 xflow_port->devname, xflow_port->port,
2025 xflow_port->devname, strerror(error));
2029 wdp_port = xmalloc(sizeof *wdp_port);
2030 wdp_port->netdev = netdev;
2031 wdp_port->opp.port_no = xflow_port_to_ofp_port(xflow_port->port);
2032 netdev_get_etheraddr(netdev, wdp_port->opp.hw_addr);
2033 strncpy((char *) wdp_port->opp.name, xflow_port->devname,
2034 sizeof wdp_port->opp.name);
2035 wdp_port->opp.name[sizeof wdp_port->opp.name - 1] = '\0';
2037 netdev_get_flags(netdev, &flags);
2038 wdp_port->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
2040 netdev_get_carrier(netdev, &carrier);
2041 wdp_port->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
2043 netdev_get_features(netdev,
2044 &wdp_port->opp.curr, &wdp_port->opp.advertised,
2045 &wdp_port->opp.supported, &wdp_port->opp.peer);
2047 wdp_port->devname = xstrdup(xflow_port->devname);
2048 wdp_port->internal = (xflow_port->flags & XFLOW_PORT_INTERNAL) != 0;
2053 wx_port_conflicts(const struct wx *wx, const struct xflow_port *xflow_port)
2055 if (port_array_get(&wx->ports, xflow_port->port)) {
2056 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
2059 } else if (shash_find(&wx->port_by_name, xflow_port->devname)) {
2060 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
2061 xflow_port->devname);
2069 wdp_port_equal(const struct wdp_port *a_, const struct wdp_port *b_)
2071 const struct ofp_phy_port *a = &a_->opp;
2072 const struct ofp_phy_port *b = &b_->opp;
2074 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
2075 return (a->port_no == b->port_no
2076 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
2077 && !strcmp((char *) a->name, (char *) b->name)
2078 && a->state == b->state
2079 && a->config == b->config
2080 && a->curr == b->curr
2081 && a->advertised == b->advertised
2082 && a->supported == b->supported
2083 && a->peer == b->peer);
2087 wx_port_install(struct wx *wx, struct wdp_port *wdp_port)
2089 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2090 const char *netdev_name = (const char *) wdp_port->opp.name;
2092 netdev_monitor_add(wx->netdev_monitor, wdp_port->netdev);
2093 port_array_set(&wx->ports, xflow_port, wdp_port);
2094 shash_add(&wx->port_by_name, netdev_name, wdp_port);
2098 wx_port_remove(struct wx *wx, struct wdp_port *wdp_port)
2100 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2102 netdev_monitor_remove(wx->netdev_monitor, wdp_port->netdev);
2103 port_array_set(&wx->ports, xflow_port, NULL);
2104 shash_delete(&wx->port_by_name,
2105 shash_find(&wx->port_by_name, (char *) wdp_port->opp.name));
2109 wx_port_free(struct wdp_port *wdp_port)
2112 netdev_close(wdp_port->netdev);
2118 wx_port_update(struct wx *wx, const char *devname)
2120 struct xflow_port xflow_port;
2121 struct wdp_port *old_wdp_port;
2122 struct wdp_port *new_wdp_port;
2125 COVERAGE_INC(wx_update_port);
2127 /* Query the datapath for port information. */
2128 error = xfif_port_query_by_name(wx->xfif, devname, &xflow_port);
2130 /* Find the old wdp_port. */
2131 old_wdp_port = shash_find_data(&wx->port_by_name, devname);
2133 if (!old_wdp_port) {
2134 /* There's no port named 'devname' but there might be a port with
2135 * the same port number. This could happen if a port is deleted
2136 * and then a new one added in its place very quickly, or if a port
2137 * is renamed. In the former case we want to send an OFPPR_DELETE
2138 * and an OFPPR_ADD, and in the latter case we want to send a
2139 * single OFPPR_MODIFY. We can distinguish the cases by comparing
2140 * the old port's ifindex against the new port, or perhaps less
2141 * reliably but more portably by comparing the old port's MAC
2142 * against the new port's MAC. However, this code isn't that smart
2143 * and always sends an OFPPR_MODIFY (XXX). */
2144 old_wdp_port = port_array_get(&wx->ports, xflow_port.port);
2146 } else if (error != ENOENT && error != ENODEV) {
2147 VLOG_WARN_RL(&rl, "xfif_port_query_by_name returned unexpected error "
2148 "%s", strerror(error));
2152 /* Create a new wdp_port. */
2153 new_wdp_port = !error ? make_wdp_port(&xflow_port) : NULL;
2155 /* Eliminate a few pathological cases. */
2156 if (!old_wdp_port && !new_wdp_port) {
2158 } else if (old_wdp_port && new_wdp_port) {
2159 /* Most of the 'config' bits are OpenFlow soft state, but
2160 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
2161 * OpenFlow bits from old_wdp_port. (make_wdp_port() only sets
2162 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
2163 new_wdp_port->opp.config |= old_wdp_port->opp.config & ~OFPPC_PORT_DOWN;
2165 if (wdp_port_equal(old_wdp_port, new_wdp_port)) {
2166 /* False alarm--no change. */
2167 wx_port_free(new_wdp_port);
2172 /* Now deal with the normal cases. */
2174 wx_port_remove(wx, old_wdp_port);
2177 wx_port_install(wx, new_wdp_port);
2179 wx_port_free(old_wdp_port);
2181 /* Update port groups. */
2182 wx_port_refresh_groups(wx);
2186 wx_port_init(struct wx *wx)
2188 struct xflow_port *ports;
2193 error = xfif_port_list(wx->xfif, &ports, &n_ports);
2198 for (i = 0; i < n_ports; i++) {
2199 const struct xflow_port *xflow_port = &ports[i];
2200 if (!wx_port_conflicts(wx, xflow_port)) {
2201 struct wdp_port *wdp_port = make_wdp_port(xflow_port);
2203 wx_port_install(wx, wdp_port);
2208 wx_port_refresh_groups(wx);
2213 wdp_xflow_register(void)
2215 static const struct wdp_class wdp_xflow_class = {
2230 wx_port_query_by_number,
2231 wx_port_query_by_name,
2238 wx_flow_for_each_match,
2248 wx_get_sflow_probability,
2249 wx_set_sflow_probability,
2254 static bool inited = false;
2267 xf_enumerate_types(&types);
2270 SVEC_FOR_EACH (i, type, &types) {
2271 struct wdp_class *class;
2273 class = xmalloc(sizeof *class);
2274 *class = wdp_xflow_class;
2275 class->type = xstrdup(type);
2280 if (!wdp_register_provider(class)) {
2285 svec_destroy(&types);