2 * Copyright (c) 2009, 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.
25 #include <netinet/in.h>
29 #include <sys/ioctl.h>
38 #include "xflow-util.h"
39 #include "ofp-print.h"
42 #include "poll-loop.h"
46 #include "xfif-provider.h"
49 #define THIS_MODULE VLM_xfif_netdev
51 /* Configuration parameters. */
52 enum { N_QUEUES = 2 }; /* Number of queues for xfif_recv(). */
53 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
54 enum { N_GROUPS = 16 }; /* Number of port groups. */
55 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
56 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
58 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
59 * headers to be aligned on a 4-byte boundary. */
60 enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
62 /* Datapath based on the network device interface from netdev.h. */
69 bool drop_frags; /* Drop all IP fragments, if true. */
70 struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
71 struct hmap flow_table; /* Flow table. */
72 struct xflow_port_group groups[N_GROUPS];
75 long long int n_frags; /* Number of dropped IP fragments. */
76 long long int n_hit; /* Number of flow table matches. */
77 long long int n_missed; /* Number of flow table misses. */
78 long long int n_lost; /* Number of misses not passed to client. */
82 struct xf_netdev_port *ports[MAX_PORTS];
83 struct list port_list;
87 /* A port in a netdev-based datapath. */
88 struct xf_netdev_port {
89 int port_no; /* Index into xf_netdev's 'ports'. */
90 struct list node; /* Element in xf_netdev's 'port_list'. */
91 struct netdev *netdev;
92 bool internal; /* Internal port (as XFLOW_PORT_INTERNAL)? */
95 /* A flow in xf_netdev's 'flow_table'. */
96 struct xf_netdev_flow {
97 struct hmap_node node; /* Element in xf_netdev's 'flow_table'. */
101 struct timeval used; /* Last used time, in milliseconds. */
102 long long int packet_count; /* Number of packets matched. */
103 long long int byte_count; /* Number of bytes matched. */
104 uint8_t ip_tos; /* IP TOS value. */
105 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
108 union xflow_action *actions;
109 unsigned int n_actions;
112 /* Interface to netdev-based datapath. */
115 struct xf_netdev *xf;
117 unsigned int xf_serial;
120 /* All netdev-based datapaths. */
121 static struct xf_netdev *xf_netdevs[256];
122 struct list xf_netdev_list = LIST_INITIALIZER(&xf_netdev_list);
123 enum { N_XF_NETDEVS = ARRAY_SIZE(xf_netdevs) };
125 /* Maximum port MTU seen so far. */
126 static int max_mtu = ETH_PAYLOAD_MAX;
128 static int get_port_by_number(struct xf_netdev *, uint16_t port_no,
129 struct xf_netdev_port **portp);
130 static int get_port_by_name(struct xf_netdev *, const char *devname,
131 struct xf_netdev_port **portp);
132 static void xf_netdev_free(struct xf_netdev *);
133 static void xf_netdev_flow_flush(struct xf_netdev *);
134 static int do_add_port(struct xf_netdev *, const char *devname, uint16_t flags,
136 static int do_del_port(struct xf_netdev *, uint16_t port_no);
137 static int xf_netdev_output_control(struct xf_netdev *, const struct ofpbuf *,
138 int queue_no, int port_no, uint32_t arg);
139 static int xf_netdev_execute_actions(struct xf_netdev *,
140 struct ofpbuf *, struct xflow_key *,
141 const union xflow_action *, int n);
143 static struct xfif_netdev *
144 xfif_netdev_cast(const struct xfif *xfif)
146 xfif_assert_class(xfif, &xfif_netdev_class);
147 return CONTAINER_OF(xfif, struct xfif_netdev, xfif);
150 static struct xf_netdev *
151 get_xf_netdev(const struct xfif *xfif)
153 return xfif_netdev_cast(xfif)->xf;
157 name_to_xf_idx(const char *name)
159 if (!strncmp(name, "xf", 2) && isdigit((unsigned char)name[2])) {
160 int xf_idx = atoi(name + 2);
161 if (xf_idx >= 0 && xf_idx < N_XF_NETDEVS) {
168 static struct xf_netdev *
169 find_xf_netdev(const char *name)
174 xf_idx = name_to_xf_idx(name);
176 return xf_netdevs[xf_idx];
179 for (i = 0; i < N_XF_NETDEVS; i++) {
180 struct xf_netdev *xf = xf_netdevs[i];
182 struct xf_netdev_port *port;
183 if (!get_port_by_name(xf, name, &port)) {
192 create_xfif_netdev(struct xf_netdev *xf)
194 struct xfif_netdev *xfif;
199 xfname = xasprintf("xf%d", xf->xf_idx);
200 xfif = xmalloc(sizeof *xfif);
201 xfif_init(&xfif->xfif, &xfif_netdev_class, xfname, xf->xf_idx, xf->xf_idx);
203 xfif->listen_mask = 0;
204 xfif->xf_serial = xf->serial;
211 create_xf_netdev(const char *name, int xf_idx, struct xfif **xfifp)
213 struct xf_netdev *xf;
217 if (xf_netdevs[xf_idx]) {
221 /* Create datapath. */
222 xf_netdevs[xf_idx] = xf = xzalloc(sizeof *xf);
223 list_push_back(&xf_netdev_list, &xf->node);
226 xf->drop_frags = false;
227 for (i = 0; i < N_QUEUES; i++) {
228 queue_init(&xf->queues[i]);
230 hmap_init(&xf->flow_table);
231 for (i = 0; i < N_GROUPS; i++) {
232 xf->groups[i].ports = NULL;
233 xf->groups[i].n_ports = 0;
234 xf->groups[i].group = i;
236 list_init(&xf->port_list);
237 error = do_add_port(xf, name, XFLOW_PORT_INTERNAL, XFLOWP_LOCAL);
243 *xfifp = create_xfif_netdev(xf);
248 xfif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
252 if (find_xf_netdev(name)) {
255 int xf_idx = name_to_xf_idx(name);
257 return create_xf_netdev(name, xf_idx, xfifp);
259 /* Scan for unused xf_idx number. */
260 for (xf_idx = 0; xf_idx < N_XF_NETDEVS; xf_idx++) {
261 int error = create_xf_netdev(name, xf_idx, xfifp);
262 if (error != EBUSY) {
267 /* All datapath numbers in use. */
272 struct xf_netdev *xf = find_xf_netdev(name);
274 *xfifp = create_xfif_netdev(xf);
283 xf_netdev_free(struct xf_netdev *xf)
287 xf_netdev_flow_flush(xf);
288 while (xf->n_ports > 0) {
289 struct xf_netdev_port *port = CONTAINER_OF(
290 xf->port_list.next, struct xf_netdev_port, node);
291 do_del_port(xf, port->port_no);
293 for (i = 0; i < N_QUEUES; i++) {
294 queue_destroy(&xf->queues[i]);
296 hmap_destroy(&xf->flow_table);
297 for (i = 0; i < N_GROUPS; i++) {
298 free(xf->groups[i].ports);
300 xf_netdevs[xf->xf_idx] = NULL;
301 list_remove(&xf->node);
306 xfif_netdev_close(struct xfif *xfif)
308 struct xf_netdev *xf = get_xf_netdev(xfif);
309 assert(xf->open_cnt > 0);
310 if (--xf->open_cnt == 0 && xf->destroyed) {
317 xfif_netdev_destroy(struct xfif *xfif)
319 struct xf_netdev *xf = get_xf_netdev(xfif);
320 xf->destroyed = true;
325 xfif_netdev_get_stats(const struct xfif *xfif, struct xflow_stats *stats)
327 struct xf_netdev *xf = get_xf_netdev(xfif);
328 memset(stats, 0, sizeof *stats);
329 stats->n_flows = hmap_count(&xf->flow_table);
330 stats->cur_capacity = hmap_capacity(&xf->flow_table);
331 stats->max_capacity = MAX_FLOWS;
332 stats->n_ports = xf->n_ports;
333 stats->max_ports = MAX_PORTS;
334 stats->max_groups = N_GROUPS;
335 stats->n_frags = xf->n_frags;
336 stats->n_hit = xf->n_hit;
337 stats->n_missed = xf->n_missed;
338 stats->n_lost = xf->n_lost;
339 stats->max_miss_queue = MAX_QUEUE_LEN;
340 stats->max_action_queue = MAX_QUEUE_LEN;
345 xfif_netdev_get_drop_frags(const struct xfif *xfif, bool *drop_fragsp)
347 struct xf_netdev *xf = get_xf_netdev(xfif);
348 *drop_fragsp = xf->drop_frags;
353 xfif_netdev_set_drop_frags(struct xfif *xfif, bool drop_frags)
355 struct xf_netdev *xf = get_xf_netdev(xfif);
356 xf->drop_frags = drop_frags;
361 do_add_port(struct xf_netdev *xf, const char *devname, uint16_t flags,
364 bool internal = (flags & XFLOW_PORT_INTERNAL) != 0;
365 struct xf_netdev_port *port;
366 struct netdev_options netdev_options;
367 struct netdev *netdev;
371 /* XXX reject devices already in some xf_netdev. */
373 /* Open and validate network device. */
374 memset(&netdev_options, 0, sizeof netdev_options);
375 netdev_options.name = devname;
376 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
377 netdev_options.may_create = true;
379 netdev_options.type = "tap";
381 netdev_options.may_open = true;
384 error = netdev_open(&netdev_options, &netdev);
388 /* XXX reject loopback devices */
389 /* XXX reject non-Ethernet devices */
391 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
393 netdev_close(netdev);
397 port = xmalloc(sizeof *port);
398 port->port_no = port_no;
399 port->netdev = netdev;
400 port->internal = internal;
402 netdev_get_mtu(netdev, &mtu);
407 list_push_back(&xf->port_list, &port->node);
408 xf->ports[port_no] = port;
416 xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
419 struct xf_netdev *xf = get_xf_netdev(xfif);
422 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
423 if (!xf->ports[port_no]) {
425 return do_add_port(xf, devname, flags, port_no);
432 xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
434 struct xf_netdev *xf = get_xf_netdev(xfif);
435 return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
439 is_valid_port_number(uint16_t port_no)
441 return port_no < MAX_PORTS;
445 get_port_by_number(struct xf_netdev *xf,
446 uint16_t port_no, struct xf_netdev_port **portp)
448 if (!is_valid_port_number(port_no)) {
452 *portp = xf->ports[port_no];
453 return *portp ? 0 : ENOENT;
458 get_port_by_name(struct xf_netdev *xf,
459 const char *devname, struct xf_netdev_port **portp)
461 struct xf_netdev_port *port;
463 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
464 if (!strcmp(netdev_get_name(port->netdev), devname)) {
473 do_del_port(struct xf_netdev *xf, uint16_t port_no)
475 struct xf_netdev_port *port;
479 error = get_port_by_number(xf, port_no, &port);
484 list_remove(&port->node);
485 xf->ports[port->port_no] = NULL;
489 name = xstrdup(netdev_get_name(port->netdev));
490 netdev_close(port->netdev);
499 answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
501 memset(xflow_port, 0, sizeof *xflow_port);
502 ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
503 sizeof xflow_port->devname);
504 xflow_port->port = port->port_no;
505 xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
509 xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
510 struct xflow_port *xflow_port)
512 struct xf_netdev *xf = get_xf_netdev(xfif);
513 struct xf_netdev_port *port;
516 error = get_port_by_number(xf, port_no, &port);
518 answer_port_query(port, xflow_port);
524 xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
525 struct xflow_port *xflow_port)
527 struct xf_netdev *xf = get_xf_netdev(xfif);
528 struct xf_netdev_port *port;
531 error = get_port_by_name(xf, devname, &port);
533 answer_port_query(port, xflow_port);
539 xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
541 hmap_remove(&xf->flow_table, &flow->node);
547 xf_netdev_flow_flush(struct xf_netdev *xf)
549 struct xf_netdev_flow *flow, *next;
551 HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
553 xf_netdev_free_flow(xf, flow);
558 xfif_netdev_flow_flush(struct xfif *xfif)
560 struct xf_netdev *xf = get_xf_netdev(xfif);
561 xf_netdev_flow_flush(xf);
566 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
568 struct xf_netdev *xf = get_xf_netdev(xfif);
569 struct xf_netdev_port *port;
573 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
574 struct xflow_port *xflow_port = &ports[i];
578 answer_port_query(port, xflow_port);
585 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
587 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
588 if (xfif->xf_serial != xfif->xf->serial) {
589 xfif->xf_serial = xfif->xf->serial;
597 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
599 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
600 if (xfif->xf_serial != xfif->xf->serial) {
601 poll_immediate_wake();
606 get_port_group(const struct xfif *xfif, int group_no,
607 struct xflow_port_group **groupp)
609 struct xf_netdev *xf = get_xf_netdev(xfif);
611 if (group_no >= 0 && group_no < N_GROUPS) {
612 *groupp = &xf->groups[group_no];
621 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
622 uint16_t ports[], int n)
624 struct xflow_port_group *group;
631 error = get_port_group(xfif, group_no, &group);
633 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
634 return group->n_ports;
641 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
642 const uint16_t ports[], int n)
644 struct xflow_port_group *group;
647 if (n < 0 || n > MAX_PORTS) {
651 error = get_port_group(xfif, group_no, &group);
654 group->ports = xmemdup(ports, n * sizeof *group->ports);
656 group->group = group_no;
661 static struct xf_netdev_flow *
662 xf_netdev_lookup_flow(const struct xf_netdev *xf,
663 const struct xflow_key *key)
665 struct xf_netdev_flow *flow;
667 HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
668 xflow_key_hash(key, 0), &xf->flow_table) {
669 if (xflow_key_equal(&flow->key, key)) {
677 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
678 struct xflow_flow *xflow_flow)
681 xflow_flow->key = flow->key;
682 xflow_flow->stats.n_packets = flow->packet_count;
683 xflow_flow->stats.n_bytes = flow->byte_count;
684 xflow_flow->stats.used_sec = flow->used.tv_sec;
685 xflow_flow->stats.used_nsec = flow->used.tv_usec * 1000;
686 xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
687 xflow_flow->stats.ip_tos = flow->ip_tos;
688 xflow_flow->stats.error = 0;
689 if (xflow_flow->n_actions > 0) {
690 unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
691 memcpy(xflow_flow->actions, flow->actions,
692 n * sizeof *xflow_flow->actions);
693 xflow_flow->n_actions = flow->n_actions;
696 if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
701 xflow_flow->stats.error = ENOENT;
706 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
708 struct xf_netdev *xf = get_xf_netdev(xfif);
711 for (i = 0; i < n; i++) {
712 struct xflow_flow *xflow_flow = &flows[i];
713 answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
714 xflow_flow->flags, xflow_flow);
720 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
726 for (i = 0; i < n_actions; i++) {
727 const union xflow_action *a = &actions[i];
730 if (a->output.port >= MAX_PORTS) {
735 case XFLOWAT_OUTPUT_GROUP:
737 if (a->output_group.group >= N_GROUPS) {
742 case XFLOWAT_CONTROLLER:
745 case XFLOWAT_SET_DL_TCI:
747 if (a->dl_tci.mask != htons(VLAN_VID_MASK)
748 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
749 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
752 if (a->dl_tci.tci & ~a->dl_tci.mask){
757 case XFLOWAT_SET_NW_TOS:
759 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
764 case XFLOWAT_STRIP_VLAN:
765 case XFLOWAT_SET_DL_SRC:
766 case XFLOWAT_SET_DL_DST:
767 case XFLOWAT_SET_NW_SRC:
768 case XFLOWAT_SET_NW_DST:
769 case XFLOWAT_SET_TP_SRC:
770 case XFLOWAT_SET_TP_DST:
782 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
788 if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
791 error = xfif_netdev_validate_actions(xflow_flow->actions,
792 xflow_flow->n_actions, &mutates);
797 n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
798 flow->actions = xrealloc(flow->actions, n_bytes);
799 flow->n_actions = xflow_flow->n_actions;
800 memcpy(flow->actions, xflow_flow->actions, n_bytes);
805 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
807 struct xf_netdev *xf = get_xf_netdev(xfif);
808 struct xf_netdev_flow *flow;
811 flow = xzalloc(sizeof *flow);
812 flow->key = xflow_flow->key;
814 error = set_flow_actions(flow, xflow_flow);
820 hmap_insert(&xf->flow_table, &flow->node,
821 xflow_key_hash(&flow->key, 0));
826 clear_stats(struct xf_netdev_flow *flow)
828 flow->used.tv_sec = 0;
829 flow->used.tv_usec = 0;
830 flow->packet_count = 0;
831 flow->byte_count = 0;
837 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
839 struct xf_netdev *xf = get_xf_netdev(xfif);
840 struct xf_netdev_flow *flow;
842 flow = xf_netdev_lookup_flow(xf, &put->flow.key);
844 if (put->flags & XFLOWPF_CREATE) {
845 if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
846 return add_flow(xfif, &put->flow);
854 if (put->flags & XFLOWPF_MODIFY) {
855 int error = set_flow_actions(flow, &put->flow);
856 if (!error && put->flags & XFLOWPF_ZERO_STATS) {
868 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
870 struct xf_netdev *xf = get_xf_netdev(xfif);
871 struct xf_netdev_flow *flow;
873 flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
875 answer_flow_query(flow, 0, xflow_flow);
876 xf_netdev_free_flow(xf, flow);
884 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
886 struct xf_netdev *xf = get_xf_netdev(xfif);
887 struct xf_netdev_flow *flow;
891 HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
895 answer_flow_query(flow, 0, &flows[i++]);
897 return hmap_count(&xf->flow_table);
901 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
902 const union xflow_action actions[], int n_actions,
903 const struct ofpbuf *packet)
905 struct xf_netdev *xf = get_xf_netdev(xfif);
908 struct xflow_key key;
912 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
916 error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
922 /* We need a deep copy of 'packet' since we're going to modify its
924 ofpbuf_init(©, XF_NETDEV_HEADROOM + packet->size);
925 copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
926 ofpbuf_put(©, packet->data, packet->size);
928 /* We still need a shallow copy of 'packet', even though we won't
929 * modify its data, because flow_extract() modifies packet->l2, etc.
930 * We could probably get away with modifying those but it's more polite
934 flow_extract(©, 0, in_port, &flow);
935 xflow_key_from_flow(&key, &flow);
936 error = xf_netdev_execute_actions(xf, ©, &key, actions, n_actions);
938 ofpbuf_uninit(©);
944 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
946 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
947 *listen_mask = xfif_netdev->listen_mask;
952 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
954 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
955 if (!(listen_mask & ~XFLOWL_ALL)) {
956 xfif_netdev->listen_mask = listen_mask;
963 static struct ovs_queue *
964 find_nonempty_queue(struct xfif *xfif)
966 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
967 struct xf_netdev *xf = get_xf_netdev(xfif);
968 int mask = xfif_netdev->listen_mask;
971 for (i = 0; i < N_QUEUES; i++) {
972 struct ovs_queue *q = &xf->queues[i];
973 if (q->n && mask & (1u << i)) {
981 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
983 struct ovs_queue *q = find_nonempty_queue(xfif);
985 *bufp = queue_pop_head(q);
993 xfif_netdev_recv_wait(struct xfif *xfif)
995 struct ovs_queue *q = find_nonempty_queue(xfif);
997 poll_immediate_wake();
999 /* No messages ready to be received, and xf_wait() will ensure that we
1000 * wake up to queue new messages, so there is nothing to do. */
1005 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1006 const struct xflow_key *key,
1007 const struct ofpbuf *packet)
1009 time_timeval(&flow->used);
1010 flow->packet_count++;
1011 flow->byte_count += packet->size;
1012 if (key->dl_type == htons(ETH_TYPE_IP)) {
1013 struct ip_header *nh = packet->l3;
1014 flow->ip_tos = nh->ip_tos;
1016 if (key->nw_proto == IPPROTO_TCP) {
1017 struct tcp_header *th = packet->l4;
1018 flow->tcp_ctl |= th->tcp_ctl;
1024 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1025 struct ofpbuf *packet)
1027 struct xf_netdev_flow *flow;
1028 struct xflow_key key;
1031 if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1035 xflow_key_from_flow(&key, &f);
1037 flow = xf_netdev_lookup_flow(xf, &key);
1039 xf_netdev_flow_used(flow, &key, packet);
1040 xf_netdev_execute_actions(xf, packet, &key,
1041 flow->actions, flow->n_actions);
1045 xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1052 struct ofpbuf packet;
1053 struct xf_netdev *xf;
1055 ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1056 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1057 struct xf_netdev_port *port;
1059 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1062 /* Reset packet contents. */
1063 packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1066 error = netdev_recv(port->netdev, &packet);
1068 xf_netdev_port_input(xf, port, &packet);
1069 } else if (error != EAGAIN) {
1070 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1071 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1072 netdev_get_name(port->netdev), strerror(error));
1076 ofpbuf_uninit(&packet);
1080 xf_netdev_wait(void)
1082 struct xf_netdev *xf;
1084 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1085 struct xf_netdev_port *port;
1086 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1087 netdev_recv_wait(port->netdev);
1093 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1095 xf_netdev_set_dl_tci(struct ofpbuf *packet, struct xflow_key *key,
1096 const struct xflow_action_dl_tci *a)
1098 struct vlan_eth_header *veh;
1102 veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1104 /* Insert new 802.1Q header. */
1105 struct eth_header *eh = packet->l2;
1106 struct vlan_eth_header tmp;
1107 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1108 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1109 tmp.veth_type = htons(ETH_TYPE_VLAN);
1110 tmp.veth_tci = htons(a->tci);
1111 tmp.veth_next_type = eh->eth_type;
1113 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1114 memcpy(veh, &tmp, sizeof tmp);
1115 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1118 key->dl_tci = veh->veth_tci | htons(XFLOW_TCI_PRESENT);
1122 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1124 struct vlan_eth_header *veh = packet->l2;
1125 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1126 struct eth_header tmp;
1128 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1129 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1130 tmp.eth_type = veh->veth_next_type;
1132 packet->size -= VLAN_HEADER_LEN;
1133 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1134 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1135 memcpy(packet->data, &tmp, sizeof tmp);
1137 key->dl_tci = htons(0);
1142 xf_netdev_set_dl_src(struct ofpbuf *packet, struct xflow_key *key,
1143 const uint8_t dl_addr[ETH_ADDR_LEN])
1145 struct eth_header *eh = packet->l2;
1146 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1147 memcpy(key->dl_src, dl_addr, sizeof key->dl_src);
1151 xf_netdev_set_dl_dst(struct ofpbuf *packet, struct xflow_key *key,
1152 const uint8_t dl_addr[ETH_ADDR_LEN])
1154 struct eth_header *eh = packet->l2;
1155 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1156 memcpy(key->dl_dst, dl_addr, sizeof key->dl_dst);
1160 xf_netdev_set_nw_addr(struct ofpbuf *packet, struct xflow_key *key,
1161 const struct xflow_action_nw_addr *a)
1163 if (key->dl_type == htons(ETH_TYPE_IP)) {
1164 struct ip_header *nh = packet->l3;
1167 field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1168 if (key->nw_proto == IP_TYPE_TCP) {
1169 struct tcp_header *th = packet->l4;
1170 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1171 } else if (key->nw_proto == IP_TYPE_UDP) {
1172 struct udp_header *uh = packet->l4;
1174 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1175 if (!uh->udp_csum) {
1176 uh->udp_csum = 0xffff;
1180 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1181 *field = a->nw_addr;
1183 if (a->type == XFLOWAT_SET_NW_SRC) {
1184 key->nw_src = a->type;
1186 key->nw_dst = a->type;
1192 xf_netdev_set_nw_tos(struct ofpbuf *packet, struct xflow_key *key,
1193 const struct xflow_action_nw_tos *a)
1195 if (key->dl_type == htons(ETH_TYPE_IP)) {
1196 struct ip_header *nh = packet->l3;
1197 uint8_t *field = &nh->ip_tos;
1199 /* Set the DSCP bits and preserve the ECN bits. */
1200 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1202 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1203 htons((uint16_t)a->nw_tos));
1205 key->nw_tos = a->nw_tos;
1210 xf_netdev_set_tp_port(struct ofpbuf *packet, struct xflow_key *key,
1211 const struct xflow_action_tp_port *a)
1213 if (key->dl_type == htons(ETH_TYPE_IP)) {
1215 if (key->nw_proto == IPPROTO_TCP) {
1216 struct tcp_header *th = packet->l4;
1217 field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1218 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1219 *field = a->tp_port;
1220 } else if (key->nw_proto == IPPROTO_UDP) {
1221 struct udp_header *uh = packet->l4;
1222 field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1223 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1224 *field = a->tp_port;
1229 if (a->type == XFLOWAT_SET_TP_SRC) {
1230 key->tp_src = a->tp_port;
1232 key->tp_dst = a->tp_port;
1238 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1241 struct xf_netdev_port *p = xf->ports[out_port];
1243 netdev_send(p->netdev, packet);
1248 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1249 struct ofpbuf *packet)
1251 struct xflow_port_group *g = &xf->groups[group];
1254 for (i = 0; i < g->n_ports; i++) {
1255 uint16_t out_port = g->ports[i];
1256 if (out_port != in_port) {
1257 xf_netdev_output_port(xf, packet, out_port);
1263 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1264 int queue_no, int port_no, uint32_t arg)
1266 struct ovs_queue *q = &xf->queues[queue_no];
1267 struct xflow_msg *header;
1271 if (q->n >= MAX_QUEUE_LEN) {
1276 msg_size = sizeof *header + packet->size;
1277 msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1278 header = ofpbuf_put_uninit(msg, sizeof *header);
1279 ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1280 header->type = queue_no;
1281 header->length = msg_size;
1282 header->port = port_no;
1284 ofpbuf_put(msg, packet->data, packet->size);
1285 queue_push_tail(q, msg);
1291 xf_netdev_execute_actions(struct xf_netdev *xf,
1292 struct ofpbuf *packet, struct xflow_key *key,
1293 const union xflow_action *actions, int n_actions)
1296 for (i = 0; i < n_actions; i++) {
1297 const union xflow_action *a = &actions[i];
1300 case XFLOWAT_OUTPUT:
1301 xf_netdev_output_port(xf, packet, a->output.port);
1304 case XFLOWAT_OUTPUT_GROUP:
1305 xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1309 case XFLOWAT_CONTROLLER:
1310 xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1311 key->in_port, a->controller.arg);
1314 case XFLOWAT_SET_DL_TCI:
1315 xf_netdev_set_dl_tci(packet, key, &a->dl_tci);
1318 case XFLOWAT_STRIP_VLAN:
1319 xf_netdev_strip_vlan(packet, key);
1322 case XFLOWAT_SET_DL_SRC:
1323 xf_netdev_set_dl_src(packet, key, a->dl_addr.dl_addr);
1326 case XFLOWAT_SET_DL_DST:
1327 xf_netdev_set_dl_dst(packet, key, a->dl_addr.dl_addr);
1330 case XFLOWAT_SET_NW_SRC:
1331 case XFLOWAT_SET_NW_DST:
1332 xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1335 case XFLOWAT_SET_NW_TOS:
1336 xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1339 case XFLOWAT_SET_TP_SRC:
1340 case XFLOWAT_SET_TP_DST:
1341 xf_netdev_set_tp_port(packet, key, &a->tp_port);
1348 const struct xfif_class xfif_netdev_class = {
1352 NULL, /* enumerate */
1355 NULL, /* get_all_names */
1356 xfif_netdev_destroy,
1357 xfif_netdev_get_stats,
1358 xfif_netdev_get_drop_frags,
1359 xfif_netdev_set_drop_frags,
1360 xfif_netdev_port_add,
1361 xfif_netdev_port_del,
1362 xfif_netdev_port_query_by_number,
1363 xfif_netdev_port_query_by_name,
1364 xfif_netdev_port_list,
1365 xfif_netdev_port_poll,
1366 xfif_netdev_port_poll_wait,
1367 xfif_netdev_port_group_get,
1368 xfif_netdev_port_group_set,
1369 xfif_netdev_flow_get,
1370 xfif_netdev_flow_put,
1371 xfif_netdev_flow_del,
1372 xfif_netdev_flow_flush,
1373 xfif_netdev_flow_list,
1374 xfif_netdev_execute,
1375 xfif_netdev_recv_get_mask,
1376 xfif_netdev_recv_set_mask,
1377 NULL, /* get_sflow_probability */
1378 NULL, /* set_sflow_probability */
1380 xfif_netdev_recv_wait,