Skip to content

Commit

Permalink
Merge branch 'Openvswitch-meter-action'
Browse files Browse the repository at this point in the history
Andy Zhou says:

====================
Openvswitch meter action

This patch series is the first attempt to add openvswitch
meter support. We have previously experimented with adding
metering support in nftables. However 1) It was not clear
how to expose a named nftables object cleanly, and 2)
the logic that implements metering is quite small, < 100 lines
of code.

With those two observations, it seems cleaner to add meter
support in the openvswitch module directly.

---

    v1(RFC)->v2:  remove unused code improve locking
		  and other review comments
    v2 -> v3:     rebase
    v3 -> v4:     fix undefined "__udivdi3" references on 32 bit builds.
                  use div_u64() instead.
    v4 -> v5:     rebase
====================

Acked-by: Pravin B Shelar <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
davem330 committed Nov 13, 2017
2 parents aef1e0d + cd8a6c3 commit fd9080a
Show file tree
Hide file tree
Showing 8 changed files with 772 additions and 31 deletions.
54 changes: 54 additions & 0 deletions include/uapi/linux/openvswitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ struct ovs_action_push_eth {
* @OVS_ACTION_ATTR_CT_CLEAR: Clear conntrack state from the packet.
* @OVS_ACTION_ATTR_PUSH_NSH: push NSH header to the packet.
* @OVS_ACTION_ATTR_POP_NSH: pop the outermost NSH header off the packet.
* @OVS_ACTION_ATTR_METER: Run packet through a meter, which may drop the
* packet, or modify the packet (e.g., change the DSCP field).
*
* Only a single header can be set with a single %OVS_ACTION_ATTR_SET. Not all
* fields within a header are modifiable, e.g. the IPv4 protocol and fragment
Expand Down Expand Up @@ -870,6 +872,7 @@ enum ovs_action_attr {
OVS_ACTION_ATTR_CT_CLEAR, /* No argument. */
OVS_ACTION_ATTR_PUSH_NSH, /* Nested OVS_NSH_KEY_ATTR_*. */
OVS_ACTION_ATTR_POP_NSH, /* No argument. */
OVS_ACTION_ATTR_METER, /* u32 meter ID. */

__OVS_ACTION_ATTR_MAX, /* Nothing past this will be accepted
* from userspace. */
Expand All @@ -883,4 +886,55 @@ enum ovs_action_attr {

#define OVS_ACTION_ATTR_MAX (__OVS_ACTION_ATTR_MAX - 1)

/* Meters. */
#define OVS_METER_FAMILY "ovs_meter"
#define OVS_METER_MCGROUP "ovs_meter"
#define OVS_METER_VERSION 0x1

enum ovs_meter_cmd {
OVS_METER_CMD_UNSPEC,
OVS_METER_CMD_FEATURES, /* Get features supported by the datapath. */
OVS_METER_CMD_SET, /* Add or modify a meter. */
OVS_METER_CMD_DEL, /* Delete a meter. */
OVS_METER_CMD_GET /* Get meter stats. */
};

enum ovs_meter_attr {
OVS_METER_ATTR_UNSPEC,
OVS_METER_ATTR_ID, /* u32 meter ID within datapath. */
OVS_METER_ATTR_KBPS, /* No argument. If set, units in kilobits
* per second. Otherwise, units in
* packets per second.
*/
OVS_METER_ATTR_STATS, /* struct ovs_flow_stats for the meter. */
OVS_METER_ATTR_BANDS, /* Nested attributes for meter bands. */
OVS_METER_ATTR_USED, /* u64 msecs last used in monotonic time. */
OVS_METER_ATTR_CLEAR, /* Flag to clear stats, used. */
OVS_METER_ATTR_MAX_METERS, /* u32 number of meters supported. */
OVS_METER_ATTR_MAX_BANDS, /* u32 max number of bands per meter. */
OVS_METER_ATTR_PAD,
__OVS_METER_ATTR_MAX
};

#define OVS_METER_ATTR_MAX (__OVS_METER_ATTR_MAX - 1)

enum ovs_band_attr {
OVS_BAND_ATTR_UNSPEC,
OVS_BAND_ATTR_TYPE, /* u32 OVS_METER_BAND_TYPE_* constant. */
OVS_BAND_ATTR_RATE, /* u32 band rate in meter units (see above). */
OVS_BAND_ATTR_BURST, /* u32 burst size in meter units. */
OVS_BAND_ATTR_STATS, /* struct ovs_flow_stats for the band. */
__OVS_BAND_ATTR_MAX
};

#define OVS_BAND_ATTR_MAX (__OVS_BAND_ATTR_MAX - 1)

enum ovs_meter_band_type {
OVS_METER_BAND_TYPE_UNSPEC,
OVS_METER_BAND_TYPE_DROP, /* Drop exceeding packets. */
__OVS_METER_BAND_TYPE_MAX
};

#define OVS_METER_BAND_TYPE_MAX (__OVS_METER_BAND_TYPE_MAX - 1)

#endif /* _LINUX_OPENVSWITCH_H */
1 change: 1 addition & 0 deletions net/openvswitch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ openvswitch-y := \
flow.o \
flow_netlink.o \
flow_table.o \
meter.o \
vport.o \
vport-internal_dev.o \
vport-netdev.o
Expand Down
6 changes: 6 additions & 0 deletions net/openvswitch/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,12 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
case OVS_ACTION_ATTR_POP_NSH:
err = pop_nsh(skb, key);
break;

case OVS_ACTION_ATTR_METER:
if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) {
consume_skb(skb);
return 0;
}
}

if (unlikely(err)) {
Expand Down
43 changes: 12 additions & 31 deletions net/openvswitch/datapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "flow.h"
#include "flow_table.h"
#include "flow_netlink.h"
#include "meter.h"
#include "vport-internal_dev.h"
#include "vport-netdev.h"

Expand Down Expand Up @@ -142,35 +143,6 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
const struct dp_upcall_info *,
uint32_t cutlen);

/* Must be called with rcu_read_lock. */
static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
{
struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);

if (dev) {
struct vport *vport = ovs_internal_dev_get_vport(dev);
if (vport)
return vport->dp;
}

return NULL;
}

/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
* returned dp pointer valid.
*/
static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
{
struct datapath *dp;

WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
rcu_read_lock();
dp = get_dp_rcu(net, dp_ifindex);
rcu_read_unlock();

return dp;
}

/* Must be called with rcu_read_lock or ovs_mutex. */
const char *ovs_dp_name(const struct datapath *dp)
{
Expand Down Expand Up @@ -203,6 +175,7 @@ static void destroy_dp_rcu(struct rcu_head *rcu)
ovs_flow_tbl_destroy(&dp->table);
free_percpu(dp->stats_percpu);
kfree(dp->ports);
ovs_meters_exit(dp);
kfree(dp);
}

Expand Down Expand Up @@ -1601,6 +1574,10 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
INIT_HLIST_HEAD(&dp->ports[i]);

err = ovs_meters_init(dp);
if (err)
goto err_destroy_ports_array;

/* Set up our datapath device. */
parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
parms.type = OVS_VPORT_TYPE_INTERNAL;
Expand Down Expand Up @@ -1629,7 +1606,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
ovs_dp_reset_user_features(skb, info);
}

goto err_destroy_ports_array;
goto err_destroy_meters;
}

err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
Expand All @@ -1644,8 +1621,10 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
ovs_notify(&dp_datapath_genl_family, reply, info);
return 0;

err_destroy_ports_array:
err_destroy_meters:
ovs_unlock();
ovs_meters_exit(dp);
err_destroy_ports_array:
kfree(dp->ports);
err_destroy_percpu:
free_percpu(dp->stats_percpu);
Expand Down Expand Up @@ -2294,6 +2273,7 @@ static struct genl_family * const dp_genl_families[] = {
&dp_vport_genl_family,
&dp_flow_genl_family,
&dp_packet_genl_family,
&dp_meter_genl_family,
};

static void dp_unregister_genl(int n_families)
Expand Down Expand Up @@ -2474,3 +2454,4 @@ MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
35 changes: 35 additions & 0 deletions net/openvswitch/datapath.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "conntrack.h"
#include "flow.h"
#include "flow_table.h"
#include "meter.h"
#include "vport-internal_dev.h"

#define DP_MAX_PORTS USHRT_MAX
#define DP_VPORT_HASH_BUCKETS 1024
Expand Down Expand Up @@ -91,6 +93,9 @@ struct datapath {
u32 user_features;

u32 max_headroom;

/* Switch meters. */
struct hlist_head *meters;
};

/**
Expand Down Expand Up @@ -190,6 +195,36 @@ static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_n
return ovs_lookup_vport(dp, port_no);
}

/* Must be called with rcu_read_lock. */
static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
{
struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);

if (dev) {
struct vport *vport = ovs_internal_dev_get_vport(dev);

if (vport)
return vport->dp;
}

return NULL;
}

/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
* returned dp pointer valid.
*/
static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
{
struct datapath *dp;

WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
rcu_read_lock();
dp = get_dp_rcu(net, dp_ifindex);
rcu_read_unlock();

return dp;
}

extern struct notifier_block ovs_dp_device_notifier;
extern struct genl_family dp_vport_genl_family;

Expand Down
6 changes: 6 additions & 0 deletions net/openvswitch/flow_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ static bool actions_may_change_flow(const struct nlattr *actions)
case OVS_ACTION_ATTR_SAMPLE:
case OVS_ACTION_ATTR_SET:
case OVS_ACTION_ATTR_SET_MASKED:
case OVS_ACTION_ATTR_METER:
default:
return true;
}
Expand Down Expand Up @@ -2844,6 +2845,7 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
[OVS_ACTION_ATTR_POP_ETH] = 0,
[OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
[OVS_ACTION_ATTR_POP_NSH] = 0,
[OVS_ACTION_ATTR_METER] = sizeof(u32),
};
const struct ovs_action_push_vlan *vlan;
int type = nla_type(a);
Expand Down Expand Up @@ -3029,6 +3031,10 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
break;
}

case OVS_ACTION_ATTR_METER:
/* Non-existent meters are simply ignored. */
break;

default:
OVS_NLERR(log, "Unknown Action type %d", type);
return -EINVAL;
Expand Down
Loading

0 comments on commit fd9080a

Please sign in to comment.