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"
30 #include "openflow/nicira-ext.h"
31 #include "openflow/openflow.h"
33 #include "poll-loop.h"
34 #include "port-array.h"
41 #include "wdp-provider.h"
43 #include "xflow-util.h"
46 #include <linux/types.h> /* XXX */
47 #include <linux/pkt_sched.h> /* XXX */
49 #define THIS_MODULE VLM_wdp_xflow
52 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
54 /* Maximum numbers of rules. */
55 #define WX_MAX_WILD 65536 /* Wildcarded rules. */
56 #define WX_MAX_EXACT 1048576 /* Exact-match rules. */
59 struct list list_node;
62 struct classifier cls;
63 struct netdev_monitor *netdev_monitor;
64 struct port_array ports; /* Index is xflow port nr;
65 * wdp_port->opp.port_no is OFP port nr. */
66 struct shash port_by_name;
68 long long int next_expiration;
71 static struct list all_wx = LIST_INITIALIZER(&all_wx);
73 static int wx_port_init(struct wx *);
74 static void wx_port_process_change(struct wx *wx, int error, char *devname,
75 wdp_port_poll_cb_func *cb, void *aux);
76 static void wx_port_refresh_groups(struct wx *);
84 wx_cast(const struct wdp *wdp)
86 return CONTAINER_OF(wdp, struct wx, wdp);
90 wx_xlate_actions(struct wx *, const union ofp_action *, size_t n,
91 const flow_t *flow, const struct ofpbuf *packet,
92 struct xflow_actions *out, bool *may_set_up_flow);
97 uint64_t packet_count; /* Number of packets received. */
98 uint64_t byte_count; /* Number of bytes received. */
99 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
100 long long int used; /* Last-used time (0 if never used). */
102 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
103 * exact-match rule (having cr.wc.wildcards of 0) generated from the
104 * wildcard rule 'super'. In this case, 'list' is an element of the
107 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
108 * a list of subrules. A super-rule with no wildcards (where
109 * cr.wc.wildcards is 0) will never have any subrules. */
110 struct wx_rule *super;
115 * A super-rule with wildcard fields never has xflow actions (since the
116 * datapath only supports exact-match flows). */
117 bool installed; /* Installed in datapath? */
118 bool may_install; /* True ordinarily; false if actions must
119 * be reassessed for every packet. */
121 union xflow_action *xflow_actions;
124 static void wx_rule_destroy(struct wx *, struct wx_rule *);
125 static void wx_rule_update_actions(struct wx *, struct wx_rule *);
126 static void wx_rule_execute(struct wx *, struct wx_rule *,
127 struct ofpbuf *packet, const flow_t *);
128 static bool wx_rule_make_actions(struct wx *, struct wx_rule *,
129 const struct ofpbuf *packet);
130 static void wx_rule_install(struct wx *, struct wx_rule *,
131 struct wx_rule *displaced_rule);
133 static struct wx_rule *
134 wx_rule_cast(const struct cls_rule *cls_rule)
136 return cls_rule ? CONTAINER_OF(cls_rule, struct wx_rule, wr.cr) : NULL;
139 /* Returns true if 'rule' is merely an implementation detail that should be
140 * hidden from the client. */
142 wx_rule_is_hidden(const struct wx_rule *rule)
144 return rule->super != NULL;
148 wx_rule_free(struct wx_rule *rule)
150 wdp_rule_uninit(&rule->wr);
151 free(rule->xflow_actions);
156 wx_rule_account(struct wx *wx OVS_UNUSED, struct wx_rule *rule OVS_UNUSED,
157 uint64_t extra_bytes OVS_UNUSED)
159 /* XXX call account_cb hook */
163 wx_rule_post_uninstall(struct wx *wx, struct wx_rule *rule)
165 struct wx_rule *super = rule->super;
167 wx_rule_account(wx, rule, 0);
169 /* XXX netflow expiration */
172 super->packet_count += rule->packet_count;
173 super->byte_count += rule->byte_count;
175 /* Reset counters to prevent double counting if the rule ever gets
177 rule->packet_count = 0;
178 rule->byte_count = 0;
179 rule->accounted_bytes = 0;
181 //XXX netflow_flow_clear(&rule->nf_flow);
186 xflow_flow_stats_to_msec(const struct xflow_flow_stats *stats)
188 return (stats->used_sec
189 ? stats->used_sec * 1000 + stats->used_nsec / 1000000
194 wx_rule_update_time(struct wx *wx OVS_UNUSED, struct wx_rule *rule,
195 const struct xflow_flow_stats *stats)
197 long long int used = xflow_flow_stats_to_msec(stats);
198 if (used > rule->used) {
200 if (rule->super && used > rule->super->used) {
201 rule->super->used = used;
203 //XXX netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
208 wx_rule_update_stats(struct wx *wx, struct wx_rule *rule,
209 const struct xflow_flow_stats *stats)
211 if (stats->n_packets) {
212 wx_rule_update_time(wx, rule, stats);
213 rule->packet_count += stats->n_packets;
214 rule->byte_count += stats->n_bytes;
215 /* XXX netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos,
216 stats->tcp_flags); */
221 wx_rule_uninstall(struct wx *wx, struct wx_rule *rule)
223 assert(!rule->wr.cr.flow.wildcards);
224 if (rule->installed) {
225 struct xflow_flow xflow_flow;
227 xflow_key_from_flow(&xflow_flow.key, &rule->wr.cr.flow);
228 xflow_flow.actions = NULL;
229 xflow_flow.n_actions = 0;
230 xflow_flow.flags = 0;
231 if (!xfif_flow_del(wx->xfif, &xflow_flow)) {
232 wx_rule_update_stats(wx, rule, &xflow_flow.stats);
234 rule->installed = false;
236 wx_rule_post_uninstall(wx, rule);
242 is_controller_rule(struct wx_rule *rule)
244 /* If the only action is send to the controller then don't report
245 * NetFlow expiration messages since it is just part of the control
246 * logic for the network and not real traffic. */
250 && rule->super->n_actions == 1
251 && action_outputs_to_port(&rule->super->actions[0],
252 htons(OFPP_CONTROLLER)));
257 wx_rule_remove(struct wx *wx, struct wx_rule *rule)
259 if (rule->wr.cr.flow.wildcards) {
260 COVERAGE_INC(wx_del_wc_flow);
261 wx->need_revalidate = true;
263 wx_rule_uninstall(wx, rule);
265 classifier_remove(&wx->cls, &rule->wr.cr);
266 wx_rule_destroy(wx, rule);
270 wx_rule_revalidate(struct wx *wx, struct wx_rule *rule)
272 const flow_t *flow = &rule->wr.cr.flow;
274 COVERAGE_INC(wx_rule_revalidate);
276 struct wx_rule *super;
277 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
279 wx_rule_remove(wx, rule);
281 } else if (super != rule->super) {
282 COVERAGE_INC(wx_revalidate_moved);
283 list_remove(&rule->list);
284 list_push_back(&super->list, &rule->list);
286 rule->wr.hard_timeout = super->wr.hard_timeout;
287 rule->wr.idle_timeout = super->wr.idle_timeout;
288 rule->wr.created = super->wr.created;
293 wx_rule_update_actions(wx, rule);
297 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
298 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
299 * through all of its subrules and revalidates them, destroying any that no
300 * longer has a super-rule (which is probably all of them).
302 * Before calling this function, the caller must make have removed 'rule' from
303 * the classifier. If 'rule' is an exact-match rule, the caller is also
304 * responsible for ensuring that it has been uninstalled from the datapath. */
306 wx_rule_destroy(struct wx *wx, struct wx_rule *rule)
309 struct wx_rule *subrule, *next;
310 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
311 wx_rule_revalidate(wx, subrule);
314 list_remove(&rule->list);
321 wx_rule_has_out_port(const struct wx_rule *rule, uint16_t out_port)
323 const union ofp_action *oa;
324 struct actions_iterator i;
326 if (out_port == htons(OFPP_NONE)) {
329 for (oa = actions_first(&i, rule->wr.actions,
332 oa = actions_next(&i)) {
333 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
341 /* Caller is responsible for initializing the 'cr' member of the returned
343 static struct wx_rule *
344 wx_rule_create(struct wx_rule *super,
345 const union ofp_action *actions, size_t n_actions,
346 uint16_t idle_timeout, uint16_t hard_timeout)
348 struct wx_rule *rule = xzalloc(sizeof *rule);
349 wdp_rule_init(&rule->wr, actions, n_actions);
350 rule->wr.idle_timeout = idle_timeout;
351 rule->wr.hard_timeout = hard_timeout;
352 rule->used = rule->wr.created;
355 list_push_back(&super->list, &rule->list);
357 list_init(&rule->list);
360 netflow_flow_clear(&rule->nf_flow);
361 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
367 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
368 * 'flow' and is considered to have arrived on xflow port 'in_port'.
370 * The flow that 'packet' actually contains does not need to actually match
371 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
372 * the packet and byte counters for 'rule' will be credited for the packet sent
373 * out whether or not the packet actually matches 'rule'.
375 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
376 * the caller must already have accurately composed xflow actions for it given
377 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
378 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
379 * function will compose a set of xflow actions based on 'rule''s OpenFlow
380 * actions and apply them to 'packet'. */
382 wx_rule_execute(struct wx *wx, struct wx_rule *rule,
383 struct ofpbuf *packet, const flow_t *flow)
385 const union xflow_action *actions;
387 struct xflow_actions a;
389 /* Grab or compose the xflow actions.
391 * The special case for an exact-match 'rule' where 'flow' is not the
392 * rule's flow is important to avoid, e.g., sending a packet out its input
393 * port simply because the xflow actions were composed for the wrong
395 if (rule->wr.cr.flow.wildcards
396 || !flow_equal(flow, &rule->wr.cr.flow))
398 struct wx_rule *super = rule->super ? rule->super : rule;
399 if (wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions, flow,
404 n_actions = a.n_actions;
406 actions = rule->xflow_actions;
407 n_actions = rule->n_xflow_actions;
410 /* Execute the xflow actions. */
411 if (!xfif_execute(wx->xfif, flow->in_port,
412 actions, n_actions, packet)) {
413 struct xflow_flow_stats stats;
414 flow_extract_stats(flow, packet, &stats);
415 wx_rule_update_stats(wx, rule, &stats);
416 rule->used = time_msec();
417 //XXX netflow_flow_update_time(wx->netflow, &rule->nf_flow, rule->used);
422 wx_rule_insert(struct wx *wx, struct wx_rule *rule, struct ofpbuf *packet,
425 struct wx_rule *displaced_rule;
427 /* Insert the rule in the classifier. */
428 displaced_rule = wx_rule_cast(classifier_insert(&wx->cls, &rule->wr.cr));
429 if (!rule->wr.cr.flow.wildcards) {
430 wx_rule_make_actions(wx, rule, packet);
433 /* Send the packet and credit it to the rule. */
436 flow_extract(packet, 0, in_port, &flow);
437 wx_rule_execute(wx, rule, packet, &flow);
440 /* Install the rule in the datapath only after sending the packet, to
441 * avoid packet reordering. */
442 if (rule->wr.cr.flow.wildcards) {
443 COVERAGE_INC(wx_add_wc_flow);
444 wx->need_revalidate = true;
446 wx_rule_install(wx, rule, displaced_rule);
449 /* Free the rule that was displaced, if any. */
450 if (displaced_rule) {
451 rule->wr.client_data = displaced_rule->wr.client_data;
452 wx_rule_destroy(wx, displaced_rule);
456 static struct wx_rule *
457 wx_rule_create_subrule(struct wx *wx, struct wx_rule *rule, const flow_t *flow)
459 struct wx_rule *subrule;
461 subrule = wx_rule_create(rule, NULL, 0,
462 rule->wr.idle_timeout,
463 rule->wr.hard_timeout);
464 COVERAGE_INC(wx_subrule_create);
465 cls_rule_from_flow(flow, &subrule->wr.cr);
466 classifier_insert_exact(&wx->cls, &subrule->wr.cr);
471 /* Returns true if the actions changed, false otherwise. */
473 wx_rule_make_actions(struct wx *wx, struct wx_rule *rule,
474 const struct ofpbuf *packet)
476 const struct wx_rule *super;
477 struct xflow_actions a;
480 assert(!rule->wr.cr.flow.wildcards);
482 super = rule->super ? rule->super : rule;
483 wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions,
484 &rule->wr.cr.flow, packet, &a, &rule->may_install);
486 actions_len = a.n_actions * sizeof *a.actions;
487 if (rule->n_xflow_actions != a.n_actions
488 || memcmp(rule->xflow_actions, a.actions, actions_len)) {
489 COVERAGE_INC(wx_xflow_unchanged);
490 free(rule->xflow_actions);
491 rule->n_xflow_actions = a.n_actions;
492 rule->xflow_actions = xmemdup(a.actions, actions_len);
500 do_put_flow(struct wx *wx, struct wx_rule *rule, int flags,
501 struct xflow_flow_put *put)
503 memset(&put->flow.stats, 0, sizeof put->flow.stats);
504 xflow_key_from_flow(&put->flow.key, &rule->wr.cr.flow);
505 put->flow.actions = rule->xflow_actions;
506 put->flow.n_actions = rule->n_xflow_actions;
509 return xfif_flow_put(wx->xfif, put);
513 wx_rule_install(struct wx *wx, struct wx_rule *rule, struct wx_rule *displaced_rule)
515 assert(!rule->wr.cr.flow.wildcards);
517 if (rule->may_install) {
518 struct xflow_flow_put put;
519 if (!do_put_flow(wx, rule,
520 XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS,
522 rule->installed = true;
523 if (displaced_rule) {
524 wx_rule_update_stats(wx, displaced_rule, &put.flow.stats);
525 wx_rule_post_uninstall(wx, displaced_rule);
528 } else if (displaced_rule) {
529 wx_rule_uninstall(wx, displaced_rule);
534 wx_rule_reinstall(struct wx *wx, struct wx_rule *rule)
536 if (rule->installed) {
537 struct xflow_flow_put put;
538 COVERAGE_INC(wx_dp_missed);
539 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY, &put);
541 wx_rule_install(wx, rule, NULL);
546 wx_rule_update_actions(struct wx *wx, struct wx_rule *rule)
548 bool actions_changed;
550 uint16_t new_out_iface, old_out_iface;
552 old_out_iface = rule->nf_flow.output_iface;
554 actions_changed = wx_rule_make_actions(wx, rule, NULL);
556 if (rule->may_install) {
557 if (rule->installed) {
558 if (actions_changed) {
559 struct xflow_flow_put put;
560 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY
561 | XFLOWPF_ZERO_STATS, &put);
562 wx_rule_update_stats(wx, rule, &put.flow.stats);
564 /* Temporarily set the old output iface so that NetFlow
565 * messages have the correct output interface for the old
567 new_out_iface = rule->nf_flow.output_iface;
568 rule->nf_flow.output_iface = old_out_iface;
570 wx_rule_post_uninstall(wx, rule);
571 //rule->nf_flow.output_iface = new_out_iface;
574 wx_rule_install(wx, rule, NULL);
577 wx_rule_uninstall(wx, rule);
582 add_output_group_action(struct xflow_actions *actions, uint16_t group,
583 uint16_t *nf_output_iface)
585 xflow_actions_add(actions, XFLOWAT_OUTPUT_GROUP)->output_group.group = group;
587 if (group == WX_GROUP_ALL || group == WX_GROUP_FLOOD) {
588 *nf_output_iface = NF_OUT_FLOOD;
593 add_controller_action(struct xflow_actions *actions,
594 const struct ofp_action_output *oao)
596 union xflow_action *a = xflow_actions_add(actions, XFLOWAT_CONTROLLER);
597 a->controller.arg = ntohs(oao->max_len);
600 struct wx_xlate_ctx {
602 flow_t flow; /* Flow to which these actions correspond. */
603 int recurse; /* Recursion level, via xlate_table_action. */
605 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
606 * null pointer if we are revalidating
607 * without a packet to refer to. */
610 struct xflow_actions *out; /* Datapath actions. */
611 //tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
612 bool may_set_up_flow; /* True ordinarily; false if the actions must
613 * be reassessed for every packet. */
614 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
617 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
618 struct wx_xlate_ctx *ctx);
621 add_output_action(struct wx_xlate_ctx *ctx, uint16_t port)
623 const struct wdp_port *wdp_port = port_array_get(&ctx->wx->ports, port);
626 if (wdp_port->opp.config & OFPPC_NO_FWD) {
627 /* Forwarding disabled on port. */
632 * We don't have an ofport record for this port, but it doesn't hurt to
633 * allow forwarding to it anyhow. Maybe such a port will appear later
634 * and we're pre-populating the flow table.
638 xflow_actions_add(ctx->out, XFLOWAT_OUTPUT)->output.port = port;
639 //ctx->nf_output_iface = port;
642 static struct wx_rule *
643 wx_rule_lookup_valid(struct wx *wx, const flow_t *flow)
645 struct wx_rule *rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
647 /* The rule we found might not be valid, since we could be in need of
648 * revalidation. If it is not valid, don't return it. */
651 && wx->need_revalidate
652 && !wx_rule_revalidate(wx, rule)) {
653 COVERAGE_INC(wx_invalidated);
661 xlate_table_action(struct wx_xlate_ctx *ctx, uint16_t in_port)
664 uint16_t old_in_port;
665 struct wx_rule *rule;
667 /* Look up a flow with 'in_port' as the input port. Then restore the
668 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
669 * have surprising behavior). */
670 old_in_port = ctx->flow.in_port;
671 ctx->flow.in_port = in_port;
672 rule = wx_rule_lookup_valid(ctx->wx, &ctx->flow);
673 ctx->flow.in_port = old_in_port;
681 do_xlate_actions(rule->wr.actions, rule->wr.n_actions, ctx);
688 xlate_output_action(struct wx_xlate_ctx *ctx,
689 const struct ofp_action_output *oao)
692 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
694 ctx->nf_output_iface = NF_OUT_DROP;
696 switch (ntohs(oao->port)) {
698 add_output_action(ctx, ctx->flow.in_port);
701 xlate_table_action(ctx, ctx->flow.in_port);
705 if (!ctx->wx->ofhooks->normal_cb(ctx->flow, ctx->packet,
707 &ctx->nf_output_iface,
709 COVERAGE_INC(wx_uninstallable);
710 ctx->may_set_up_flow = false;
714 /* fall through to flood for now */
717 add_output_group_action(ctx->out, WX_GROUP_FLOOD,
718 &ctx->nf_output_iface);
721 add_output_group_action(ctx->out, WX_GROUP_ALL, &ctx->nf_output_iface);
723 case OFPP_CONTROLLER:
724 add_controller_action(ctx->out, oao);
727 add_output_action(ctx, XFLOWP_LOCAL);
730 xflow_port = ofp_port_to_xflow_port(ntohs(oao->port));
731 if (xflow_port != ctx->flow.in_port) {
732 add_output_action(ctx, xflow_port);
737 if (prev_nf_output_iface == NF_OUT_FLOOD) {
738 ctx->nf_output_iface = NF_OUT_FLOOD;
739 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
740 ctx->nf_output_iface = prev_nf_output_iface;
741 } else if (prev_nf_output_iface != NF_OUT_DROP &&
742 ctx->nf_output_iface != NF_OUT_FLOOD) {
743 ctx->nf_output_iface = NF_OUT_MULTI;
747 /* If the final xflow action in 'ctx' is "pop priority", drop it, as an
748 * optimization, because we're going to add another action that sets the
749 * priority immediately after, or because there are no actions following the
752 remove_pop_action(struct wx_xlate_ctx *ctx)
754 size_t n = ctx->out->n_actions;
755 if (n > 0 && ctx->out->actions[n - 1].type == XFLOWAT_POP_PRIORITY) {
756 ctx->out->n_actions--;
761 xlate_enqueue_action(struct wx_xlate_ctx *ctx,
762 const struct ofp_action_enqueue *oae)
764 uint16_t ofp_port, xflow_port;
766 /* Figure out xflow output port. */
767 ofp_port = ntohs(oae->port);
768 if (ofp_port != OFPP_IN_PORT) {
769 xflow_port = ofp_port_to_xflow_port(ofp_port);
771 xflow_port = ctx->flow.in_port;
774 /* Add xflow actions. */
775 remove_pop_action(ctx);
776 xflow_actions_add(ctx->out, XFLOWAT_SET_PRIORITY)->priority.priority
777 = TC_H_MAKE(1, ntohl(oae->queue_id)); /* XXX */
778 add_output_action(ctx, xflow_port);
779 xflow_actions_add(ctx->out, XFLOWAT_POP_PRIORITY);
781 /* Update NetFlow output port. */
782 if (ctx->nf_output_iface == NF_OUT_DROP) {
783 ctx->nf_output_iface = xflow_port;
784 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
785 ctx->nf_output_iface = NF_OUT_MULTI;
790 xlate_nicira_action(struct wx_xlate_ctx *ctx,
791 const struct nx_action_header *nah)
793 const struct nx_action_resubmit *nar;
794 const struct nx_action_set_tunnel *nast;
795 union xflow_action *oa;
796 int subtype = ntohs(nah->subtype);
798 assert(nah->vendor == htonl(NX_VENDOR_ID));
801 nar = (const struct nx_action_resubmit *) nah;
802 xlate_table_action(ctx, ofp_port_to_xflow_port(ntohs(nar->in_port)));
805 case NXAST_SET_TUNNEL:
806 nast = (const struct nx_action_set_tunnel *) nah;
807 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TUNNEL);
808 ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id;
811 /* If you add a new action here that modifies flow data, don't forget to
812 * update the flow key in ctx->flow at the same time. */
815 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
821 do_xlate_actions(const union ofp_action *in, size_t n_in,
822 struct wx_xlate_ctx *ctx)
824 struct actions_iterator iter;
825 const union ofp_action *ia;
826 const struct wdp_port *port;
828 port = port_array_get(&ctx->wx->ports, ctx->flow.in_port);
829 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
830 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, stp_eth_addr)
831 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
832 /* Drop this flow. */
836 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
837 uint16_t type = ntohs(ia->type);
838 union xflow_action *oa;
842 xlate_output_action(ctx, &ia->output);
845 case OFPAT_SET_VLAN_VID:
846 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
847 oa->dl_tci.tci = ia->vlan_vid.vlan_vid & htons(VLAN_VID_MASK);
848 oa->dl_tci.mask = htons(VLAN_VID_MASK);
849 ctx->flow.dl_vlan = ia->vlan_vid.vlan_vid;
852 case OFPAT_SET_VLAN_PCP:
853 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
854 oa->dl_tci.tci = htons((ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT)
856 oa->dl_tci.mask = htons(VLAN_PCP_MASK);
858 if (ctx->flow.dl_vlan == htons(OFP_VLAN_NONE)) {
859 ctx->flow.dl_vlan = htons(0);
861 ctx->flow.dl_vlan_pcp = ia->vlan_pcp.vlan_pcp;
864 case OFPAT_STRIP_VLAN:
865 xflow_actions_add(ctx->out, XFLOWAT_STRIP_VLAN);
866 ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
867 ctx->flow.dl_vlan_pcp = 0;
870 case OFPAT_SET_DL_SRC:
871 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_SRC);
872 memcpy(oa->dl_addr.dl_addr,
873 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
874 memcpy(ctx->flow.dl_src,
875 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
878 case OFPAT_SET_DL_DST:
879 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_DST);
880 memcpy(oa->dl_addr.dl_addr,
881 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
882 memcpy(ctx->flow.dl_dst,
883 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
886 case OFPAT_SET_NW_SRC:
887 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_SRC);
888 ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
891 case OFPAT_SET_NW_DST:
892 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_DST);
893 ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
896 case OFPAT_SET_NW_TOS:
897 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_TOS);
898 ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
901 case OFPAT_SET_TP_SRC:
902 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_SRC);
903 ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
906 case OFPAT_SET_TP_DST:
907 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_DST);
908 ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
912 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
916 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
920 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
926 /* Returns true if 'flow' and 'actions' may be set up as a flow in the kernel.
927 * This is true most of the time, but we don't allow flows that would prevent
928 * DHCP replies from being seen by the local port to be set up in the
931 * We only need this, strictly speaking, when in-band control is turned on. */
933 wx_may_set_up(const flow_t *flow, const struct xflow_actions *actions)
935 if (flow->dl_type == htons(ETH_TYPE_IP)
936 && flow->nw_proto == IP_TYPE_UDP
937 && flow->tp_src == htons(DHCP_SERVER_PORT)
938 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
941 for (i = 0; i < actions->n_actions; i++) {
942 const struct xflow_action_output *oao = &actions->actions[i].output;
943 if (oao->type == XFLOWAT_OUTPUT && oao->port == XFLOWP_LOCAL) {
954 wx_xlate_actions(struct wx *wx, const union ofp_action *in, size_t n_in,
955 const flow_t *flow, const struct ofpbuf *packet,
956 struct xflow_actions *out, bool *may_set_up_flow)
958 //tag_type no_tags = 0;
959 struct wx_xlate_ctx ctx;
960 COVERAGE_INC(wx_ofp2xflow);
961 xflow_actions_init(out);
967 //ctx.tags = tags ? tags : &no_tags;
968 ctx.may_set_up_flow = true;
969 ctx.nf_output_iface = NF_OUT_DROP;
970 do_xlate_actions(in, n_in, &ctx);
971 remove_pop_action(&ctx);
973 if (may_set_up_flow) {
974 *may_set_up_flow = ctx.may_set_up_flow && wx_may_set_up(flow, out);
977 if (nf_output_iface) {
978 *nf_output_iface = ctx.nf_output_iface;
981 if (xflow_actions_overflow(out)) {
982 xflow_actions_init(out);
983 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
989 update_used(struct wx *wx)
991 struct xflow_flow *flows;
996 error = xfif_flow_list_all(wx->xfif, &flows, &n_flows);
1001 for (i = 0; i < n_flows; i++) {
1002 struct xflow_flow *f = &flows[i];
1003 struct wx_rule *rule;
1006 xflow_key_to_flow(&f->key, &flow);
1007 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, &flow));
1008 if (!rule || !rule->installed) {
1009 COVERAGE_INC(wx_unexpected_rule);
1010 xfif_flow_del(wx->xfif, f);
1014 wx_rule_update_time(wx, rule, &f->stats);
1015 wx_rule_account(wx, rule, f->stats.n_bytes);
1021 uninstall_idle_flow(struct wx *wx, struct wx_rule *rule)
1023 assert(rule->installed);
1024 assert(!rule->wr.cr.flow.wildcards);
1027 wx_rule_remove(wx, rule);
1029 wx_rule_uninstall(wx, rule);
1034 expire_rule(struct cls_rule *cls_rule, void *wx_)
1036 struct wx *wx = wx_;
1037 struct wx_rule *rule = wx_rule_cast(cls_rule);
1038 long long int hard_expire, idle_expire, expire, now;
1040 hard_expire = (rule->wr.hard_timeout
1041 ? rule->wr.created + rule->wr.hard_timeout * 1000
1043 idle_expire = (rule->wr.idle_timeout
1044 && (rule->super || list_is_empty(&rule->list))
1045 ? rule->used + rule->wr.idle_timeout * 1000
1047 expire = MIN(hard_expire, idle_expire);
1051 if (rule->installed && now >= rule->used + 5000) {
1052 uninstall_idle_flow(wx, rule);
1053 } else if (!rule->wr.cr.flow.wildcards) {
1054 //XXX active_timeout(wx, rule);
1060 COVERAGE_INC(wx_expired);
1062 /* Update stats. This code will be a no-op if the rule expired
1063 * due to an idle timeout. */
1064 if (rule->wr.cr.flow.wildcards) {
1065 struct wx_rule *subrule, *next;
1066 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
1067 wx_rule_remove(wx, subrule);
1070 wx_rule_uninstall(wx, rule);
1074 if (!wx_rule_is_hidden(rule)) {
1075 send_flow_removed(wx, rule, now,
1077 ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
1080 wx_rule_remove(wx, rule);
1083 struct revalidate_cbdata {
1085 bool revalidate_all; /* Revalidate all exact-match rules? */
1086 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
1087 //struct tag_set revalidate_set; /* Set of tags to revalidate. */
1091 revalidate_rule(struct wx *wx, struct wx_rule *rule)
1093 const flow_t *flow = &rule->wr.cr.flow;
1095 COVERAGE_INC(wx_revalidate_rule);
1097 struct wx_rule *super;
1098 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
1100 wx_rule_remove(wx, rule);
1102 } else if (super != rule->super) {
1103 COVERAGE_INC(wx_revalidate_moved);
1104 list_remove(&rule->list);
1105 list_push_back(&super->list, &rule->list);
1106 rule->super = super;
1107 rule->wr.hard_timeout = super->wr.hard_timeout;
1108 rule->wr.idle_timeout = super->wr.idle_timeout;
1109 rule->wr.created = super->wr.created;
1114 wx_rule_update_actions(wx, rule);
1119 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
1121 struct wx_rule *sub = wx_rule_cast(sub_);
1122 struct revalidate_cbdata *cbdata = cbdata_;
1124 if (cbdata->revalidate_all
1125 || (cbdata->revalidate_subrules && sub->super)
1126 /*|| (tag_set_intersects(&cbdata->revalidate_set, sub->tags))*/) {
1127 revalidate_rule(cbdata->wx, sub);
1132 wx_run_one(struct wx *wx)
1134 if (time_msec() >= wx->next_expiration) {
1135 COVERAGE_INC(wx_expiration);
1136 wx->next_expiration = time_msec() + 1000;
1139 classifier_for_each(&wx->cls, CLS_INC_ALL, expire_rule, wx);
1141 /* XXX account_checkpoint_cb */
1144 if (wx->need_revalidate /*|| !tag_set_is_empty(&p->revalidate_set)*/) {
1145 struct revalidate_cbdata cbdata;
1147 cbdata.revalidate_all = false;
1148 cbdata.revalidate_subrules = wx->need_revalidate;
1149 //cbdata.revalidate_set = wx->revalidate_set;
1150 //tag_set_init(&wx->revalidate_set);
1151 COVERAGE_INC(wx_revalidate);
1152 classifier_for_each(&wx->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
1153 wx->need_revalidate = false;
1162 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1169 wx_wait_one(struct wx *wx)
1171 if (wx->need_revalidate /*|| !tag_set_is_empty(&p->revalidate_set)*/) {
1172 poll_immediate_wake();
1173 } else if (wx->next_expiration != LLONG_MAX) {
1174 poll_timer_wait_until(wx->next_expiration);
1183 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1189 static int wx_flow_flush(struct wdp *);
1192 wx_enumerate(const struct wdp_class *wdp_class, struct svec *all_wdps)
1194 struct svec names = SVEC_EMPTY_INITIALIZER;
1195 int error = xf_enumerate_names(wdp_class->type, &names);
1196 svec_move(all_wdps, &names);
1201 wx_open(const struct wdp_class *wdp_class, const char *name, bool create,
1208 ? xfif_create_and_open(name, wdp_class->type, &xfif)
1209 : xfif_open(name, wdp_class->type, &xfif));
1213 wx = xzalloc(sizeof *wx);
1214 list_push_back(&all_wx, &wx->list_node);
1215 wdp_init(&wx->wdp, wdp_class, name, 0, 0);
1217 classifier_init(&wx->cls);
1218 wx->netdev_monitor = netdev_monitor_create();
1219 port_array_init(&wx->ports);
1220 shash_init(&wx->port_by_name);
1221 wx->next_expiration = time_msec() + 1000;
1232 wx_close(struct wdp *wdp)
1234 struct wx *wx = wx_cast(wdp);
1237 xfif_close(wx->xfif);
1238 classifier_destroy(&wx->cls);
1239 netdev_monitor_destroy(wx->netdev_monitor);
1240 list_remove(&wx->list_node);
1245 wx_get_all_names(const struct wdp *wdp, struct svec *all_names)
1247 struct wx *wx = wx_cast(wdp);
1249 return xfif_get_all_names(wx->xfif, all_names);
1253 wx_destroy(struct wdp *wdp)
1255 struct wx *wx = wx_cast(wdp);
1257 return xfif_delete(wx->xfif);
1261 wx_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
1263 struct wx *wx = wx_cast(wdp);
1264 struct ofp_switch_features *osf;
1266 unsigned int port_no;
1267 struct wdp_port *port;
1269 buf = ofpbuf_new(sizeof *osf);
1270 osf = ofpbuf_put_zeros(buf, sizeof *osf);
1272 osf->capabilities = htonl(OFPC_ARP_MATCH_IP);
1273 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1274 (1u << OFPAT_SET_VLAN_VID) |
1275 (1u << OFPAT_SET_VLAN_PCP) |
1276 (1u << OFPAT_STRIP_VLAN) |
1277 (1u << OFPAT_SET_DL_SRC) |
1278 (1u << OFPAT_SET_DL_DST) |
1279 (1u << OFPAT_SET_NW_SRC) |
1280 (1u << OFPAT_SET_NW_DST) |
1281 (1u << OFPAT_SET_NW_TOS) |
1282 (1u << OFPAT_SET_TP_SRC) |
1283 (1u << OFPAT_SET_TP_DST) |
1284 (1u << OFPAT_ENQUEUE));
1286 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1287 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1295 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
1297 struct wx_rule *rule = wx_rule_cast(cls_rule);
1298 int *n_subrules = n_subrules_;
1306 wx_get_stats(const struct wdp *wdp, struct wdp_stats *stats)
1308 struct wx *wx = wx_cast(wdp);
1309 struct xflow_stats xflow_stats;
1313 error = xfif_get_xf_stats(wx->xfif, &xflow_stats);
1316 classifier_for_each(&wx->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
1318 stats->exact.n_flows = classifier_count_exact(&wx->cls) - n_subrules;
1319 stats->exact.cur_capacity = xflow_stats.cur_capacity;
1320 stats->exact.max_capacity = MIN(WX_MAX_EXACT, xflow_stats.max_capacity);
1321 stats->exact.n_hit = xflow_stats.n_hit;
1322 stats->exact.n_missed = xflow_stats.n_missed;
1323 stats->exact.n_lost = xflow_stats.n_lost;
1325 stats->wild.n_flows = classifier_count_wild(&wx->cls);
1326 stats->wild.cur_capacity = WX_MAX_WILD;
1327 stats->wild.max_capacity = WX_MAX_WILD;
1328 stats->wild.n_hit = 0; /* XXX */
1329 stats->wild.n_missed = 0; /* XXX */
1330 stats->wild.n_lost = 0; /* XXX */
1332 stats->n_ports = xflow_stats.n_ports;
1333 stats->max_ports = xflow_stats.max_ports;
1335 stats->n_frags = xflow_stats.n_frags;
1337 stats->max_miss_queue = xflow_stats.max_miss_queue;
1338 stats->max_action_queue = xflow_stats.max_action_queue;
1339 stats->max_sflow_queue = xflow_stats.max_sflow_queue;
1345 wx_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
1347 struct wx *wx = wx_cast(wdp);
1349 return xfif_get_drop_frags(wx->xfif, drop_frags);
1353 wx_set_drop_frags(struct wdp *wdp, bool drop_frags)
1355 struct wx *wx = wx_cast(wdp);
1357 return xfif_set_drop_frags(wx->xfif, drop_frags);
1361 wx_port_add(struct wdp *wdp, const char *devname,
1362 bool internal, uint16_t *port_no)
1364 struct wx *wx = wx_cast(wdp);
1365 uint16_t xflow_flags = internal ? XFLOW_PORT_INTERNAL : 0;
1366 return xfif_port_add(wx->xfif, devname, xflow_flags, port_no);
1370 wx_port_del(struct wdp *wdp, uint16_t port_no)
1372 struct wx *wx = wx_cast(wdp);
1374 return xfif_port_del(wx->xfif, port_no);
1378 wx_answer_port_query(const struct wdp_port *port, struct wdp_port *portp)
1381 wdp_port_copy(portp, port);
1389 wx_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
1390 struct wdp_port *portp)
1392 struct wx *wx = wx_cast(wdp);
1393 const struct wdp_port *port;
1395 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1396 return wx_answer_port_query(port, portp);
1400 wx_port_query_by_name(const struct wdp *wdp, const char *devname,
1401 struct wdp_port *portp)
1403 struct wx *wx = wx_cast(wdp);
1405 return wx_answer_port_query(shash_find_data(&wx->port_by_name, devname),
1410 wx_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
1412 struct wx *wx = wx_cast(wdp);
1413 struct wdp_port *port;
1416 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1420 changes = config ^ port->opp.config;
1422 if (changes & OFPPC_PORT_DOWN) {
1424 if (config & OFPPC_PORT_DOWN) {
1425 error = netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1427 error = netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1430 port->opp.config ^= OFPPC_PORT_DOWN;
1434 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
1435 if (changes & REVALIDATE_BITS) {
1436 COVERAGE_INC(wx_costly_flags);
1437 port->opp.config ^= changes & REVALIDATE_BITS;
1438 wx->need_revalidate = true;
1440 #undef REVALIDATE_BITS
1442 if (changes & OFPPC_NO_FLOOD) {
1443 port->opp.config ^= OFPPC_NO_FLOOD;
1444 wx_port_refresh_groups(wx);
1447 if (changes & OFPPC_NO_PACKET_IN) {
1448 port->opp.config ^= OFPPC_NO_PACKET_IN;
1455 wx_port_list(const struct wdp *wdp, struct wdp_port **portsp, size_t *n_portsp)
1457 struct wx *wx = wx_cast(wdp);
1458 struct wdp_port *ports, *port;
1459 unsigned int port_no;
1462 *n_portsp = n_ports = port_array_count(&wx->ports);
1463 *portsp = ports = xmalloc(n_ports * sizeof *ports);
1465 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1466 wdp_port_copy(&ports[i++], port);
1468 assert(i == n_ports);
1474 wx_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
1476 struct wx *wx = wx_cast(wdp);
1482 while ((error = xfif_port_poll(wx->xfif, &devname)) != EAGAIN) {
1483 wx_port_process_change(wx, error, devname, cb, aux);
1484 if (error && error != ENOBUFS) {
1488 while ((error = netdev_monitor_poll(wx->netdev_monitor,
1489 &devname)) != EAGAIN) {
1490 wx_port_process_change(wx, error, devname, cb, aux);
1491 if (error && error != ENOBUFS) {
1499 wx_port_poll_wait(const struct wdp *wdp)
1501 struct wx *wx = wx_cast(wdp);
1503 xfif_port_poll_wait(wx->xfif);
1504 netdev_monitor_poll_wait(wx->netdev_monitor);
1508 static struct wdp_rule *
1509 wx_flow_get(const struct wdp *wdp, const flow_t *flow)
1511 struct wx *wx = wx_cast(wdp);
1512 struct wx_rule *rule;
1514 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, flow));
1515 return rule && !wx_rule_is_hidden(rule) ? &rule->wr : NULL;
1518 static struct wdp_rule *
1519 wx_flow_match(const struct wdp *wdp, const flow_t *flow)
1521 struct wx *wx = wx_cast(wdp);
1522 struct wx_rule *rule;
1524 rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
1526 if (wx_rule_is_hidden(rule)) {
1535 struct wx_for_each_thunk_aux {
1536 wdp_flow_cb_func *client_callback;
1541 wx_for_each_thunk(struct cls_rule *cls_rule, void *aux_)
1543 struct wx_for_each_thunk_aux *aux = aux_;
1544 struct wx_rule *rule = wx_rule_cast(cls_rule);
1546 if (!wx_rule_is_hidden(rule)) {
1547 aux->client_callback(&rule->wr, aux->client_aux);
1552 wx_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
1554 wdp_flow_cb_func *client_callback, void *client_aux)
1556 struct wx *wx = wx_cast(wdp);
1557 struct wx_for_each_thunk_aux aux;
1559 aux.client_callback = client_callback;
1560 aux.client_aux = client_aux;
1561 classifier_for_each_match(&wx->cls, target, include,
1562 wx_for_each_thunk, &aux);
1565 /* Obtains statistic counters for 'rule' within 'wx' and stores them into
1566 * '*stats'. If 'rule' is a wildcarded rule, the returned statistic include
1567 * statistics for all of 'rule''s subrules. */
1569 query_stats(struct wx *wx, struct wx_rule *rule, struct wdp_flow_stats *stats)
1571 struct wx_rule *subrule;
1572 struct xflow_flow *xflow_flows;
1573 size_t n_xflow_flows;
1575 /* Start from historical data for 'rule' itself that are no longer tracked
1576 * by the datapath. This counts, for example, subrules that have
1578 stats->n_packets = rule->packet_count;
1579 stats->n_bytes = rule->byte_count;
1580 stats->inserted = rule->wr.created;
1581 stats->used = LLONG_MIN;
1582 stats->tcp_flags = 0;
1585 /* Prepare to ask the datapath for statistics on 'rule', or if it is
1586 * wildcarded then on all of its subrules.
1588 * Also, add any statistics that are not tracked by the datapath for each
1589 * subrule. This includes, for example, statistics for packets that were
1590 * executed "by hand" by ofproto via xfif_execute() but must be accounted
1592 n_xflow_flows = rule->wr.cr.flow.wildcards ? list_size(&rule->list) : 1;
1593 xflow_flows = xzalloc(n_xflow_flows * sizeof *xflow_flows);
1594 if (rule->wr.cr.flow.wildcards) {
1596 LIST_FOR_EACH (subrule, struct wx_rule, list, &rule->list) {
1597 xflow_key_from_flow(&xflow_flows[i++].key, &subrule->wr.cr.flow);
1598 stats->n_packets += subrule->packet_count;
1599 stats->n_bytes += subrule->byte_count;
1602 xflow_key_from_flow(&xflow_flows[0].key, &rule->wr.cr.flow);
1605 /* Fetch up-to-date statistics from the datapath and add them in. */
1606 if (!xfif_flow_get_multiple(wx->xfif, xflow_flows, n_xflow_flows)) {
1608 for (i = 0; i < n_xflow_flows; i++) {
1609 struct xflow_flow *xflow_flow = &xflow_flows[i];
1612 stats->n_packets += xflow_flow->stats.n_packets;
1613 stats->n_bytes += xflow_flow->stats.n_bytes;
1614 used = xflow_flow_stats_to_msec(&xflow_flow->stats);
1615 if (used > stats->used) {
1617 if (xflow_flow->key.dl_type == htons(ETH_TYPE_IP)
1618 && xflow_flow->key.nw_proto == IP_TYPE_TCP) {
1619 stats->ip_tos = xflow_flow->stats.ip_tos;
1622 stats->tcp_flags |= xflow_flow->stats.tcp_flags;
1629 wx_flow_get_stats(const struct wdp *wdp,
1630 const struct wdp_rule *wdp_rule,
1631 struct wdp_flow_stats *stats)
1633 struct wx *wx = wx_cast(wdp);
1634 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1636 query_stats(wx, rule, stats);
1641 wx_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
1643 struct wx *wx = wx_cast(wdp);
1645 /* XXX overlap with a subrule? */
1646 return classifier_rule_overlaps(&wx->cls, flow);
1650 wx_flow_put(struct wdp *wdp, const struct wdp_flow_put *put,
1651 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
1653 struct wx *wx = wx_cast(wdp);
1654 struct wx_rule *rule;
1656 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, put->flow));
1657 if (rule && wx_rule_is_hidden(rule)) {
1662 if (!(put->flags & WDP_PUT_MODIFY)) {
1666 if (!(put->flags & WDP_PUT_CREATE)) {
1669 if ((put->flow->wildcards
1670 ? classifier_count_wild(&wx->cls) >= WX_MAX_WILD
1671 : classifier_count_exact(&wx->cls) >= WX_MAX_EXACT)) {
1672 /* XXX subrules should not count against exact-match limit */
1677 rule = wx_rule_create(NULL, put->actions, put->n_actions,
1678 put->idle_timeout, put->hard_timeout);
1679 cls_rule_from_flow(put->flow, &rule->wr.cr);
1680 wx_rule_insert(wx, rule, NULL, 0);
1684 memset(old_stats, 0, sizeof *old_stats);
1694 wx_flow_delete(struct wdp *wdp, struct wdp_rule *wdp_rule,
1695 struct wdp_flow_stats *final_stats)
1697 struct wx *wx = wx_cast(wdp);
1698 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1700 wx_rule_remove(wx, rule);
1702 memset(final_stats, 0, sizeof *final_stats); /* XXX */
1708 wx_flush_rule(struct cls_rule *cls_rule, void *wx_)
1710 struct wx_rule *rule = wx_rule_cast(cls_rule);
1711 struct wx *wx = wx_;
1713 /* Mark the flow as not installed, even though it might really be
1714 * installed, so that wx_rule_remove() doesn't bother trying to uninstall
1715 * it. There is no point in uninstalling it individually since we are
1716 * about to blow away all the flows with xfif_flow_flush(). */
1717 rule->installed = false;
1719 wx_rule_remove(wx, rule);
1723 wx_flow_flush(struct wdp *wdp)
1725 struct wx *wx = wx_cast(wdp);
1727 COVERAGE_INC(wx_flow_flush);
1728 classifier_for_each(&wx->cls, CLS_INC_ALL, wx_flush_rule, wx);
1729 xfif_flow_flush(wx->xfif);
1734 wx_execute(struct wdp *wdp, uint16_t in_port,
1735 const union ofp_action actions[], int n_actions,
1736 const struct ofpbuf *packet)
1738 struct wx *wx = wx_cast(wdp);
1739 struct xflow_actions xflow_actions;
1743 flow_extract((struct ofpbuf *) packet, 0, in_port, &flow);
1744 error = wx_xlate_actions(wx, actions, n_actions, &flow, packet,
1745 &xflow_actions, NULL);
1749 xfif_execute(wx->xfif, ofp_port_to_xflow_port(in_port),
1750 xflow_actions.actions, xflow_actions.n_actions, packet);
1755 wx_flow_inject(struct wdp *wdp, struct wdp_rule *wdp_rule,
1756 uint16_t in_port, const struct ofpbuf *packet)
1758 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1761 error = wx_execute(wdp, in_port, rule->wr.actions, rule->wr.n_actions,
1764 rule->packet_count++;
1765 rule->byte_count += packet->size;
1766 rule->used = time_msec();
1772 wx_recv_get_mask(const struct wdp *wdp, int *listen_mask)
1774 struct wx *wx = wx_cast(wdp);
1775 int xflow_listen_mask;
1778 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
1781 if (xflow_listen_mask & XFLOWL_MISS) {
1782 *listen_mask |= 1 << WDP_CHAN_MISS;
1784 if (xflow_listen_mask & XFLOWL_ACTION) {
1785 *listen_mask |= 1 << WDP_CHAN_ACTION;
1787 if (xflow_listen_mask & XFLOWL_SFLOW) {
1788 *listen_mask |= 1 << WDP_CHAN_SFLOW;
1795 wx_recv_set_mask(struct wdp *wdp, int listen_mask)
1797 struct wx *wx = wx_cast(wdp);
1798 int xflow_listen_mask;
1800 xflow_listen_mask = 0;
1801 if (listen_mask & (1 << WDP_CHAN_MISS)) {
1802 xflow_listen_mask |= XFLOWL_MISS;
1804 if (listen_mask & (1 << WDP_CHAN_ACTION)) {
1805 xflow_listen_mask |= XFLOWL_ACTION;
1807 if (listen_mask & (1 << WDP_CHAN_SFLOW)) {
1808 xflow_listen_mask |= XFLOWL_SFLOW;
1811 return xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
1815 wx_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
1817 struct wx *wx = wx_cast(wdp);
1819 return xfif_get_sflow_probability(wx->xfif, probability);
1823 wx_set_sflow_probability(struct wdp *wdp, uint32_t probability)
1825 struct wx *wx = wx_cast(wdp);
1827 return xfif_set_sflow_probability(wx->xfif, probability);
1831 wx_translate_xflow_msg(struct xflow_msg *msg, struct ofpbuf *payload,
1832 struct wdp_packet *packet)
1834 packet->in_port = xflow_port_to_ofp_port(msg->port);
1835 packet->send_len = 0;
1838 switch (msg->type) {
1839 case _XFLOWL_MISS_NR:
1840 packet->channel = WDP_CHAN_MISS;
1841 packet->payload = payload;
1842 packet->tun_id = msg->arg;
1845 case _XFLOWL_ACTION_NR:
1846 packet->channel = WDP_CHAN_ACTION;
1847 packet->payload = payload;
1848 packet->send_len = msg->arg;
1851 case _XFLOWL_SFLOW_NR:
1853 ofpbuf_delete(payload);
1857 VLOG_WARN_RL(&rl, "received XFLOW message of unexpected type %"PRIu32,
1859 ofpbuf_delete(payload);
1864 static const uint8_t *
1865 get_local_mac(const struct wx *wx)
1867 const struct wdp_port *port = port_array_get(&wx->ports, XFLOWP_LOCAL);
1868 return port ? port->opp.hw_addr : NULL;
1871 /* Returns true if 'packet' is a DHCP reply to the local port. Such a reply
1872 * should be sent to the local port regardless of the flow table.
1874 * We only need this, strictly speaking, when in-band control is turned on. */
1876 wx_is_local_dhcp_reply(const struct wx *wx,
1877 const flow_t *flow, const struct ofpbuf *packet)
1879 if (flow->dl_type == htons(ETH_TYPE_IP)
1880 && flow->nw_proto == IP_TYPE_UDP
1881 && flow->tp_src == htons(DHCP_SERVER_PORT)
1882 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
1885 const uint8_t *local_mac = get_local_mac(wx);
1886 struct dhcp_header *dhcp = ofpbuf_at(
1887 packet, (char *)packet->l7 - (char *)packet->data, sizeof *dhcp);
1888 return dhcp && local_mac && eth_addr_equals(dhcp->chaddr, local_mac);
1895 wx_explode_rule(struct wx *wx, struct xflow_msg *msg, struct ofpbuf *payload)
1897 struct wx_rule *rule;
1900 flow_extract(payload, 0, xflow_port_to_ofp_port(msg->port), &flow);
1902 if (wx_is_local_dhcp_reply(wx, &flow, payload)) {
1903 union xflow_action action;
1905 memset(&action, 0, sizeof(action));
1906 action.output.type = XFLOWAT_OUTPUT;
1907 action.output.port = XFLOWP_LOCAL;
1908 xfif_execute(wx->xfif, msg->port, &action, 1, payload);
1911 rule = wx_rule_lookup_valid(wx, &flow);
1916 if (rule->wr.cr.flow.wildcards) {
1917 rule = wx_rule_create_subrule(wx, rule, &flow);
1918 wx_rule_make_actions(wx, rule, payload);
1920 if (!rule->may_install) {
1921 /* The rule is not installable, that is, we need to process every
1922 * packet, so process the current packet and set its actions into
1924 wx_rule_make_actions(wx, rule, payload);
1926 /* XXX revalidate rule if it needs it */
1930 wx_rule_execute(wx, rule, payload, &flow);
1931 wx_rule_reinstall(wx, rule);
1937 wx_recv(struct wdp *wdp, struct wdp_packet *packet)
1939 struct wx *wx = wx_cast(wdp);
1942 /* XXX need to avoid 50*50 potential cost for caller. */
1943 for (i = 0; i < 50; i++) {
1944 struct xflow_msg *msg;
1948 error = xfif_recv(wx->xfif, &buf);
1953 msg = ofpbuf_pull(buf, sizeof *msg);
1954 if (msg->type != _XFLOWL_MISS_NR || !wx_explode_rule(wx, msg, buf)) {
1955 return wx_translate_xflow_msg(msg, buf, packet);
1963 wx_recv_wait(struct wdp *wdp)
1965 struct wx *wx = wx_cast(wdp);
1967 xfif_recv_wait(wx->xfif);
1970 static void wx_port_update(struct wx *, const char *devname,
1971 wdp_port_poll_cb_func *cb, void *aux);
1972 static void wx_port_reinit(struct wx *, wdp_port_poll_cb_func *cb, void *aux);
1975 wx_port_process_change(struct wx *wx, int error, char *devname,
1976 wdp_port_poll_cb_func *cb, void *aux)
1978 if (error == ENOBUFS) {
1979 wx_port_reinit(wx, cb, aux);
1980 } else if (!error) {
1981 wx_port_update(wx, devname, cb, aux);
1987 wx_port_refresh_group(struct wx *wx, unsigned int group)
1991 struct wdp_port *port;
1992 unsigned int port_no;
1994 assert(group == WX_GROUP_ALL || group == WX_GROUP_FLOOD);
1996 ports = xmalloc(port_array_count(&wx->ports) * sizeof *ports);
1998 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1999 if (group == WX_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
2000 ports[n_ports++] = port_no;
2003 xfif_port_group_set(wx->xfif, group, ports, n_ports);
2010 wx_port_refresh_groups(struct wx *wx)
2012 wx_port_refresh_group(wx, WX_GROUP_FLOOD);
2013 wx_port_refresh_group(wx, WX_GROUP_ALL);
2017 wx_port_reinit(struct wx *wx, wdp_port_poll_cb_func *cb, void *aux)
2019 struct svec devnames;
2020 struct wdp_port *wdp_port;
2021 unsigned int port_no;
2022 struct xflow_port *xflow_ports;
2023 size_t n_xflow_ports;
2026 svec_init(&devnames);
2027 PORT_ARRAY_FOR_EACH (wdp_port, &wx->ports, port_no) {
2028 svec_add (&devnames, (char *) wdp_port->opp.name);
2030 xfif_port_list(wx->xfif, &xflow_ports, &n_xflow_ports);
2031 for (i = 0; i < n_xflow_ports; i++) {
2032 svec_add(&devnames, xflow_ports[i].devname);
2036 svec_sort_unique(&devnames);
2037 for (i = 0; i < devnames.n; i++) {
2038 wx_port_update(wx, devnames.names[i], cb, aux);
2040 svec_destroy(&devnames);
2042 wx_port_refresh_groups(wx);
2045 static struct wdp_port *
2046 make_wdp_port(const struct xflow_port *xflow_port)
2048 struct netdev_options netdev_options;
2049 enum netdev_flags flags;
2050 struct wdp_port *wdp_port;
2051 struct netdev *netdev;
2055 memset(&netdev_options, 0, sizeof netdev_options);
2056 netdev_options.name = xflow_port->devname;
2057 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
2059 error = netdev_open(&netdev_options, &netdev);
2061 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
2062 "cannot be opened (%s)",
2063 xflow_port->devname, xflow_port->port,
2064 xflow_port->devname, strerror(error));
2068 wdp_port = xmalloc(sizeof *wdp_port);
2069 wdp_port->netdev = netdev;
2070 wdp_port->opp.port_no = xflow_port_to_ofp_port(xflow_port->port);
2071 netdev_get_etheraddr(netdev, wdp_port->opp.hw_addr);
2072 strncpy((char *) wdp_port->opp.name, xflow_port->devname,
2073 sizeof wdp_port->opp.name);
2074 wdp_port->opp.name[sizeof wdp_port->opp.name - 1] = '\0';
2076 netdev_get_flags(netdev, &flags);
2077 wdp_port->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
2079 netdev_get_carrier(netdev, &carrier);
2080 wdp_port->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
2082 netdev_get_features(netdev,
2083 &wdp_port->opp.curr, &wdp_port->opp.advertised,
2084 &wdp_port->opp.supported, &wdp_port->opp.peer);
2086 wdp_port->devname = xstrdup(xflow_port->devname);
2087 wdp_port->internal = (xflow_port->flags & XFLOW_PORT_INTERNAL) != 0;
2092 wx_port_conflicts(const struct wx *wx, const struct xflow_port *xflow_port)
2094 if (port_array_get(&wx->ports, xflow_port->port)) {
2095 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
2098 } else if (shash_find(&wx->port_by_name, xflow_port->devname)) {
2099 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
2100 xflow_port->devname);
2108 wdp_port_equal(const struct wdp_port *a_, const struct wdp_port *b_)
2110 const struct ofp_phy_port *a = &a_->opp;
2111 const struct ofp_phy_port *b = &b_->opp;
2113 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
2114 return (a->port_no == b->port_no
2115 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
2116 && !strcmp((char *) a->name, (char *) b->name)
2117 && a->state == b->state
2118 && a->config == b->config
2119 && a->curr == b->curr
2120 && a->advertised == b->advertised
2121 && a->supported == b->supported
2122 && a->peer == b->peer);
2126 wx_port_install(struct wx *wx, struct wdp_port *wdp_port)
2128 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2129 const char *netdev_name = (const char *) wdp_port->opp.name;
2131 netdev_monitor_add(wx->netdev_monitor, wdp_port->netdev);
2132 port_array_set(&wx->ports, xflow_port, wdp_port);
2133 shash_add(&wx->port_by_name, netdev_name, wdp_port);
2137 wx_port_remove(struct wx *wx, struct wdp_port *wdp_port)
2139 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2141 netdev_monitor_remove(wx->netdev_monitor, wdp_port->netdev);
2142 port_array_delete(&wx->ports, xflow_port);
2143 shash_delete(&wx->port_by_name,
2144 shash_find(&wx->port_by_name, (char *) wdp_port->opp.name));
2148 wx_port_free(struct wdp_port *wdp_port)
2151 netdev_close(wdp_port->netdev);
2157 wx_port_update(struct wx *wx, const char *devname,
2158 wdp_port_poll_cb_func *cb, void *aux)
2160 struct xflow_port xflow_port;
2161 struct wdp_port *old_wdp_port;
2162 struct wdp_port *new_wdp_port;
2165 COVERAGE_INC(wx_update_port);
2167 /* Query the datapath for port information. */
2168 error = xfif_port_query_by_name(wx->xfif, devname, &xflow_port);
2170 /* Find the old wdp_port. */
2171 old_wdp_port = shash_find_data(&wx->port_by_name, devname);
2173 if (!old_wdp_port) {
2174 /* There's no port named 'devname' but there might be a port with
2175 * the same port number. This could happen if a port is deleted
2176 * and then a new one added in its place very quickly, or if a port
2177 * is renamed. In the former case we want to send an OFPPR_DELETE
2178 * and an OFPPR_ADD, and in the latter case we want to send a
2179 * single OFPPR_MODIFY. We can distinguish the cases by comparing
2180 * the old port's ifindex against the new port, or perhaps less
2181 * reliably but more portably by comparing the old port's MAC
2182 * against the new port's MAC. However, this code isn't that smart
2183 * and always sends an OFPPR_MODIFY (XXX). */
2184 old_wdp_port = port_array_get(&wx->ports, xflow_port.port);
2186 } else if (error != ENOENT && error != ENODEV) {
2187 VLOG_WARN_RL(&rl, "xfif_port_query_by_name returned unexpected error "
2188 "%s", strerror(error));
2192 /* Create a new wdp_port. */
2193 new_wdp_port = !error ? make_wdp_port(&xflow_port) : NULL;
2195 /* Eliminate a few pathological cases. */
2196 if (!old_wdp_port && !new_wdp_port) {
2198 } else if (old_wdp_port && new_wdp_port) {
2199 /* Most of the 'config' bits are OpenFlow soft state, but
2200 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
2201 * OpenFlow bits from old_wdp_port. (make_wdp_port() only sets
2202 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
2203 new_wdp_port->opp.config |= old_wdp_port->opp.config & ~OFPPC_PORT_DOWN;
2205 if (wdp_port_equal(old_wdp_port, new_wdp_port)) {
2206 /* False alarm--no change. */
2207 wx_port_free(new_wdp_port);
2212 /* Now deal with the normal cases. */
2214 wx_port_remove(wx, old_wdp_port);
2217 wx_port_install(wx, new_wdp_port);
2221 if (!old_wdp_port) {
2222 (*cb)(&new_wdp_port->opp, OFPPR_ADD, aux);
2223 } else if (!new_wdp_port) {
2224 (*cb)(&old_wdp_port->opp, OFPPR_DELETE, aux);
2226 (*cb)(&new_wdp_port->opp, OFPPR_MODIFY, aux);
2229 /* Update port groups. */
2230 wx_port_refresh_groups(wx);
2233 wx_port_free(old_wdp_port);
2237 wx_port_init(struct wx *wx)
2239 struct xflow_port *ports;
2244 error = xfif_port_list(wx->xfif, &ports, &n_ports);
2249 for (i = 0; i < n_ports; i++) {
2250 const struct xflow_port *xflow_port = &ports[i];
2251 if (!wx_port_conflicts(wx, xflow_port)) {
2252 struct wdp_port *wdp_port = make_wdp_port(xflow_port);
2254 wx_port_install(wx, wdp_port);
2259 wx_port_refresh_groups(wx);
2264 wdp_xflow_register(void)
2266 static const struct wdp_class wdp_xflow_class = {
2281 wx_port_query_by_number,
2282 wx_port_query_by_name,
2289 wx_flow_for_each_match,
2299 wx_get_sflow_probability,
2300 wx_set_sflow_probability,
2305 static bool inited = false;
2318 xf_enumerate_types(&types);
2321 SVEC_FOR_EACH (i, type, &types) {
2322 struct wdp_class *class;
2324 class = xmalloc(sizeof *class);
2325 *class = wdp_xflow_class;
2326 class->type = xstrdup(type);
2331 if (!wdp_register_provider(class)) {
2336 svec_destroy(&types);