X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fdpif-netdev.c;h=c1c339e955bfcb5de7df8182db572e3f6234d983;hb=86dc65011b820698dab5f1e4cb5083d20f14aad4;hp=14b919275d2f9d190175a5a39f533fec68b540aa;hpb=f180c2e2ccdfc36801685dc487748b570652da68;p=sliver-openvswitch.git diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 14b919275..c1c339e95 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -47,6 +47,7 @@ #include "ofpbuf.h" #include "packets.h" #include "poll-loop.h" +#include "random.h" #include "shash.h" #include "timeval.h" #include "util.h" @@ -91,7 +92,6 @@ struct dp_netdev { long long int n_lost; /* Number of misses not passed to client. */ /* Ports. */ - int n_ports; struct dp_netdev_port *ports[MAX_PORTS]; struct list port_list; unsigned int serial; @@ -204,7 +204,7 @@ create_dp_netdev(const char *name, const struct dpif_class *class, } hmap_init(&dp->flow_table); list_init(&dp->port_list); - error = do_add_port(dp, name, "internal", ODPP_LOCAL); + error = do_add_port(dp, name, "internal", OVSP_LOCAL); if (error) { dp_netdev_free(dp); return error; @@ -265,10 +265,10 @@ dp_netdev_purge_queues(struct dp_netdev *dp) static void dp_netdev_free(struct dp_netdev *dp) { + struct dp_netdev_port *port, *next; + dp_netdev_flow_flush(dp); - while (dp->n_ports > 0) { - struct dp_netdev_port *port = CONTAINER_OF( - dp->port_list.next, struct dp_netdev_port, node); + LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) { do_del_port(dp, port->port_no); } dp_netdev_purge_queues(dp); @@ -298,10 +298,9 @@ dpif_netdev_destroy(struct dpif *dpif) } static int -dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats) +dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats) { struct dp_netdev *dp = get_dp_netdev(dpif); - memset(stats, 0, sizeof *stats); stats->n_flows = hmap_count(&dp->flow_table); stats->n_frags = dp->n_frags; stats->n_hit = dp->n_hit; @@ -331,14 +330,13 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type, uint16_t port_no) { struct dp_netdev_port *port; - struct netdev_options netdev_options; struct netdev *netdev; bool internal; int mtu; int error; /* XXX reject devices already in some dp_netdev. */ - if (type[0] == '\0' || !strcmp(type, "system")) { + if (type[0] == '\0' || !strcmp(type, "system") || !strcmp(type, "dummy")) { internal = false; } else if (!strcmp(type, "internal")) { internal = true; @@ -348,22 +346,27 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type, } /* Open and validate network device. */ - memset(&netdev_options, 0, sizeof netdev_options); - netdev_options.name = devname; - netdev_options.ethertype = NETDEV_ETH_TYPE_ANY; if (dp->class == &dpif_dummy_class) { - netdev_options.type = "dummy"; + type = "dummy"; } else if (internal) { - netdev_options.type = "tap"; + type = "tap"; } - error = netdev_open(&netdev_options, &netdev); + error = netdev_open(devname, type, &netdev); if (error) { return error; } /* XXX reject loopback devices */ /* XXX reject non-Ethernet devices */ + error = netdev_listen(netdev); + if (error) { + VLOG_ERR("%s: cannot receive packets on this network device (%s)", + devname, strerror(errno)); + netdev_close(netdev); + return error; + } + error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false); if (error) { netdev_close(netdev); @@ -375,14 +378,13 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type, port->netdev = netdev; port->internal = internal; - netdev_get_mtu(netdev, &mtu); - if (mtu != INT_MAX && mtu > max_mtu) { + error = netdev_get_mtu(netdev, &mtu); + if (!error) { max_mtu = mtu; } list_push_back(&dp->port_list, &port->node); dp->ports[port_no] = port; - dp->n_ports++; dp->serial++; return 0; @@ -409,7 +411,7 @@ static int dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no) { struct dp_netdev *dp = get_dp_netdev(dpif); - return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no); + return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no); } static bool @@ -460,7 +462,6 @@ do_del_port(struct dp_netdev *dp, uint16_t port_no) list_remove(&port->node); dp->ports[port->port_no] = NULL; - dp->n_ports--; dp->serial++; name = xstrdup(netdev_get_name(port->netdev)); @@ -653,6 +654,12 @@ dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len, return EINVAL; } + if (flow->in_port < OFPP_MAX + ? flow->in_port >= MAX_PORTS + : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) { + return EINVAL; + } + return 0; } @@ -685,78 +692,10 @@ dpif_netdev_flow_get(const struct dpif *dpif, return 0; } -static int -dpif_netdev_validate_actions(const struct nlattr *actions, - size_t actions_len, bool *mutates) -{ - const struct nlattr *a; - unsigned int left; - - *mutates = false; - NL_ATTR_FOR_EACH (a, left, actions, actions_len) { - uint16_t type = nl_attr_type(a); - int len = odp_action_len(type); - - if (len != nl_attr_get_size(a)) { - return EINVAL; - } - - switch (type) { - case ODP_ACTION_ATTR_OUTPUT: - if (nl_attr_get_u32(a) >= MAX_PORTS) { - return EINVAL; - } - break; - - case ODP_ACTION_ATTR_USERSPACE: - break; - - case ODP_ACTION_ATTR_SET_DL_TCI: - *mutates = true; - if (nl_attr_get_be16(a) & htons(VLAN_CFI)) { - return EINVAL; - } - break; - - case ODP_ACTION_ATTR_SET_NW_TOS: - *mutates = true; - if (nl_attr_get_u8(a) & IP_ECN_MASK) { - return EINVAL; - } - break; - - case ODP_ACTION_ATTR_STRIP_VLAN: - case ODP_ACTION_ATTR_SET_DL_SRC: - case ODP_ACTION_ATTR_SET_DL_DST: - case ODP_ACTION_ATTR_SET_NW_SRC: - case ODP_ACTION_ATTR_SET_NW_DST: - case ODP_ACTION_ATTR_SET_TP_SRC: - case ODP_ACTION_ATTR_SET_TP_DST: - *mutates = true; - break; - - case ODP_ACTION_ATTR_SET_TUNNEL: - case ODP_ACTION_ATTR_SET_PRIORITY: - case ODP_ACTION_ATTR_POP_PRIORITY: - default: - return EOPNOTSUPP; - } - } - return 0; -} - static int set_flow_actions(struct dp_netdev_flow *flow, const struct nlattr *actions, size_t actions_len) { - bool mutates; - int error; - - error = dpif_netdev_validate_actions(actions, actions_len, &mutates); - if (error) { - return error; - } - flow->actions = xrealloc(flow->actions, actions_len); flow->actions_len = actions_len; memcpy(flow->actions, actions, actions_len); @@ -950,7 +889,6 @@ dpif_netdev_execute(struct dpif *dpif, { struct dp_netdev *dp = get_dp_netdev(dpif); struct ofpbuf copy; - bool mutates; struct flow key; int error; @@ -958,32 +896,19 @@ dpif_netdev_execute(struct dpif *dpif, return EINVAL; } - error = dpif_netdev_validate_actions(actions, actions_len, &mutates); - if (error) { - return error; - } - - if (mutates) { - /* We need a deep copy of 'packet' since we're going to modify its - * data. */ - ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size); - ofpbuf_reserve(©, DP_NETDEV_HEADROOM); - ofpbuf_put(©, packet->data, packet->size); - } else { - /* We still need a shallow copy of 'packet', even though we won't - * modify its data, because flow_extract() modifies packet->l2, etc. - * We could probably get away with modifying those but it's more polite - * if we don't. */ - copy = *packet; - } + /* Make a deep copy of 'packet', because we might modify its data. */ + ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size); + ofpbuf_reserve(©, DP_NETDEV_HEADROOM); + ofpbuf_put(©, packet->data, packet->size); flow_extract(©, 0, -1, &key); - dpif_netdev_flow_from_nlattrs(key_attrs, key_len, &key); - - error = dp_netdev_execute_actions(dp, ©, &key, actions, actions_len); - if (mutates) { - ofpbuf_uninit(©); + error = dpif_netdev_flow_from_nlattrs(key_attrs, key_len, &key); + if (!error) { + error = dp_netdev_execute_actions(dp, ©, &key, + actions, actions_len); } + + ofpbuf_uninit(©); return error; } @@ -1133,7 +1058,7 @@ dpif_netdev_wait(struct dpif *dpif) } static void -dp_netdev_strip_vlan(struct ofpbuf *packet) +dp_netdev_pop_vlan(struct ofpbuf *packet) { struct vlan_eth_header *veh = packet->l2; if (packet->size >= sizeof *veh @@ -1180,7 +1105,7 @@ dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key, uint16_t type = nl_attr_type(a); ovs_be32 *field; - field = type == ODP_ACTION_ATTR_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst; + field = type == OVS_ACTION_ATTR_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst; if (key->nw_proto == IPPROTO_TCP && packet->l7) { struct tcp_header *th = packet->l4; th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip); @@ -1226,13 +1151,13 @@ dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key, if (key->nw_proto == IPPROTO_TCP && packet->l7) { struct tcp_header *th = packet->l4; - field = (type == ODP_ACTION_ATTR_SET_TP_SRC + field = (type == OVS_ACTION_ATTR_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst); th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port); *field = port; } else if (key->nw_proto == IPPROTO_UDP && packet->l7) { struct udp_header *uh = packet->l4; - field = (type == ODP_ACTION_ATTR_SET_TP_SRC + field = (type == OVS_ACTION_ATTR_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst); uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port); *field = port; @@ -1285,6 +1210,40 @@ dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet, return 0; } +static void +dp_netdev_sample(struct dp_netdev *dp, + struct ofpbuf *packet, struct flow *key, + const struct nlattr *action) +{ + const struct nlattr *subactions = NULL; + const struct nlattr *a; + size_t left; + + NL_NESTED_FOR_EACH_UNSAFE (a, left, action) { + int type = nl_attr_type(a); + + switch ((enum ovs_sample_attr) type) { + case OVS_SAMPLE_ATTR_PROBABILITY: + if (random_uint32() >= nl_attr_get_u32(a)) { + return; + } + break; + + case OVS_SAMPLE_ATTR_ACTIONS: + subactions = a; + break; + + case OVS_SAMPLE_ATTR_UNSPEC: + case __OVS_SAMPLE_ATTR_MAX: + default: + NOT_REACHED(); + } + } + + dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions), + nl_attr_get_size(subactions)); +} + static int dp_netdev_execute_actions(struct dp_netdev *dp, struct ofpbuf *packet, struct flow *key, @@ -1295,45 +1254,61 @@ dp_netdev_execute_actions(struct dp_netdev *dp, unsigned int left; NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) { - switch (nl_attr_type(a)) { - case ODP_ACTION_ATTR_OUTPUT: + int type = nl_attr_type(a); + + switch ((enum ovs_action_type) type) { + case OVS_ACTION_ATTR_OUTPUT: dp_netdev_output_port(dp, packet, nl_attr_get_u32(a)); break; - case ODP_ACTION_ATTR_USERSPACE: + case OVS_ACTION_ATTR_USERSPACE: dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, nl_attr_get_u64(a)); break; - case ODP_ACTION_ATTR_SET_DL_TCI: - eth_set_vlan_tci(packet, nl_attr_get_be16(a)); + case OVS_ACTION_ATTR_PUSH_VLAN: + eth_push_vlan(packet, nl_attr_get_be16(a)); break; - case ODP_ACTION_ATTR_STRIP_VLAN: - dp_netdev_strip_vlan(packet); + case OVS_ACTION_ATTR_POP_VLAN: + dp_netdev_pop_vlan(packet); break; - case ODP_ACTION_ATTR_SET_DL_SRC: + case OVS_ACTION_ATTR_SET_DL_SRC: dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN)); break; - case ODP_ACTION_ATTR_SET_DL_DST: + case OVS_ACTION_ATTR_SET_DL_DST: dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN)); break; - case ODP_ACTION_ATTR_SET_NW_SRC: - case ODP_ACTION_ATTR_SET_NW_DST: + case OVS_ACTION_ATTR_SET_NW_SRC: + case OVS_ACTION_ATTR_SET_NW_DST: dp_netdev_set_nw_addr(packet, key, a); break; - case ODP_ACTION_ATTR_SET_NW_TOS: + case OVS_ACTION_ATTR_SET_NW_TOS: dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a)); break; - case ODP_ACTION_ATTR_SET_TP_SRC: - case ODP_ACTION_ATTR_SET_TP_DST: + case OVS_ACTION_ATTR_SET_TP_SRC: + case OVS_ACTION_ATTR_SET_TP_DST: dp_netdev_set_tp_port(packet, key, a); break; + + case OVS_ACTION_ATTR_SAMPLE: + dp_netdev_sample(dp, packet, key, a); + break; + + case OVS_ACTION_ATTR_SET_TUNNEL: + case OVS_ACTION_ATTR_SET_PRIORITY: + case OVS_ACTION_ATTR_POP_PRIORITY: + /* not implemented */ + break; + + case OVS_ACTION_ATTR_UNSPEC: + case __OVS_ACTION_ATTR_MAX: + NOT_REACHED(); } } return 0; @@ -1370,8 +1345,6 @@ const struct dpif_class dpif_netdev_class = { dpif_netdev_execute, dpif_netdev_recv_get_mask, dpif_netdev_recv_set_mask, - NULL, /* get_sflow_probability */ - NULL, /* set_sflow_probability */ NULL, /* queue_to_priority */ dpif_netdev_recv, dpif_netdev_recv_wait,