2019-05-29 01:10:09 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-11-11 04:09:42 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Nicira, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/openvswitch.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rculist.h>
|
2020-04-24 08:08:03 +08:00
|
|
|
#include <linux/swap.h>
|
2017-11-11 04:09:42 +08:00
|
|
|
|
|
|
|
#include <net/netlink.h>
|
|
|
|
#include <net/genetlink.h>
|
|
|
|
|
|
|
|
#include "datapath.h"
|
|
|
|
#include "meter.h"
|
|
|
|
|
|
|
|
static const struct nla_policy meter_policy[OVS_METER_ATTR_MAX + 1] = {
|
|
|
|
[OVS_METER_ATTR_ID] = { .type = NLA_U32, },
|
|
|
|
[OVS_METER_ATTR_KBPS] = { .type = NLA_FLAG },
|
|
|
|
[OVS_METER_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
|
|
|
|
[OVS_METER_ATTR_BANDS] = { .type = NLA_NESTED },
|
|
|
|
[OVS_METER_ATTR_USED] = { .type = NLA_U64 },
|
|
|
|
[OVS_METER_ATTR_CLEAR] = { .type = NLA_FLAG },
|
|
|
|
[OVS_METER_ATTR_MAX_METERS] = { .type = NLA_U32 },
|
|
|
|
[OVS_METER_ATTR_MAX_BANDS] = { .type = NLA_U32 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct nla_policy band_policy[OVS_BAND_ATTR_MAX + 1] = {
|
|
|
|
[OVS_BAND_ATTR_TYPE] = { .type = NLA_U32, },
|
|
|
|
[OVS_BAND_ATTR_RATE] = { .type = NLA_U32, },
|
|
|
|
[OVS_BAND_ATTR_BURST] = { .type = NLA_U32, },
|
|
|
|
[OVS_BAND_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
|
|
|
|
};
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
static u32 meter_hash(struct dp_meter_instance *ti, u32 id)
|
|
|
|
{
|
|
|
|
return id % ti->n_meters;
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
static void ovs_meter_free(struct dp_meter *meter)
|
|
|
|
{
|
|
|
|
if (!meter)
|
|
|
|
return;
|
|
|
|
|
2017-11-14 14:27:12 +08:00
|
|
|
kfree_rcu(meter, rcu);
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with ovs_mutex or RCU read lock. */
|
2020-04-24 08:08:02 +08:00
|
|
|
static struct dp_meter *lookup_meter(const struct dp_meter_table *tbl,
|
2017-11-11 04:09:42 +08:00
|
|
|
u32 meter_id)
|
|
|
|
{
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
|
|
|
|
u32 hash = meter_hash(ti, meter_id);
|
2017-11-11 04:09:42 +08:00
|
|
|
struct dp_meter *meter;
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
meter = rcu_dereference_ovsl(ti->dp_meters[hash]);
|
|
|
|
if (meter && likely(meter->id == meter_id))
|
|
|
|
return meter;
|
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
static struct dp_meter_instance *dp_meter_instance_alloc(const u32 size)
|
|
|
|
{
|
|
|
|
struct dp_meter_instance *ti;
|
|
|
|
|
|
|
|
ti = kvzalloc(sizeof(*ti) +
|
|
|
|
sizeof(struct dp_meter *) * size,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!ti)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ti->n_meters = size;
|
|
|
|
|
|
|
|
return ti;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dp_meter_instance_free(struct dp_meter_instance *ti)
|
|
|
|
{
|
|
|
|
kvfree(ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dp_meter_instance_free_rcu(struct rcu_head *rcu)
|
|
|
|
{
|
|
|
|
struct dp_meter_instance *ti;
|
|
|
|
|
|
|
|
ti = container_of(rcu, struct dp_meter_instance, rcu);
|
|
|
|
kvfree(ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dp_meter_instance_realloc(struct dp_meter_table *tbl, u32 size)
|
|
|
|
{
|
|
|
|
struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
|
|
|
|
int n_meters = min(size, ti->n_meters);
|
|
|
|
struct dp_meter_instance *new_ti;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
new_ti = dp_meter_instance_alloc(size);
|
|
|
|
if (!new_ti)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < n_meters; i++)
|
2020-04-25 11:39:47 +08:00
|
|
|
if (rcu_dereference_ovsl(ti->dp_meters[i]))
|
|
|
|
new_ti->dp_meters[i] = ti->dp_meters[i];
|
2020-04-24 08:08:02 +08:00
|
|
|
|
|
|
|
rcu_assign_pointer(tbl->ti, new_ti);
|
|
|
|
call_rcu(&ti->rcu, dp_meter_instance_free_rcu);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dp_meter_instance_insert(struct dp_meter_instance *ti,
|
|
|
|
struct dp_meter *meter)
|
|
|
|
{
|
|
|
|
u32 hash;
|
|
|
|
|
|
|
|
hash = meter_hash(ti, meter->id);
|
|
|
|
rcu_assign_pointer(ti->dp_meters[hash], meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dp_meter_instance_remove(struct dp_meter_instance *ti,
|
|
|
|
struct dp_meter *meter)
|
2017-11-11 04:09:42 +08:00
|
|
|
{
|
2020-04-24 08:08:02 +08:00
|
|
|
u32 hash;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
hash = meter_hash(ti, meter->id);
|
|
|
|
RCU_INIT_POINTER(ti->dp_meters[hash], NULL);
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
static int attach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
|
2017-11-11 04:09:42 +08:00
|
|
|
{
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
|
|
|
|
u32 hash = meter_hash(ti, meter->id);
|
2020-04-24 08:08:03 +08:00
|
|
|
int err;
|
2020-04-24 08:08:02 +08:00
|
|
|
|
|
|
|
/* In generally, slots selected should be empty, because
|
|
|
|
* OvS uses id-pool to fetch a available id.
|
|
|
|
*/
|
|
|
|
if (unlikely(rcu_dereference_ovsl(ti->dp_meters[hash])))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
dp_meter_instance_insert(ti, meter);
|
|
|
|
|
|
|
|
/* That function is thread-safe. */
|
2020-04-24 08:08:03 +08:00
|
|
|
tbl->count++;
|
|
|
|
if (tbl->count >= tbl->max_meters_allowed) {
|
|
|
|
err = -EFBIG;
|
|
|
|
goto attach_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tbl->count >= ti->n_meters &&
|
|
|
|
dp_meter_instance_realloc(tbl, ti->n_meters * 2)) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto attach_err;
|
|
|
|
}
|
2020-04-24 08:08:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-04-24 08:08:03 +08:00
|
|
|
attach_err:
|
2020-04-24 08:08:02 +08:00
|
|
|
dp_meter_instance_remove(ti, meter);
|
|
|
|
tbl->count--;
|
2020-04-24 08:08:03 +08:00
|
|
|
return err;
|
2020-04-24 08:08:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int detach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
|
|
|
|
{
|
|
|
|
struct dp_meter_instance *ti;
|
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
ASSERT_OVSL();
|
2020-04-24 08:08:02 +08:00
|
|
|
if (!meter)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ti = rcu_dereference_ovsl(tbl->ti);
|
|
|
|
dp_meter_instance_remove(ti, meter);
|
|
|
|
|
|
|
|
tbl->count--;
|
|
|
|
|
|
|
|
/* Shrink the meter array if necessary. */
|
|
|
|
if (ti->n_meters > DP_METER_ARRAY_SIZE_MIN &&
|
|
|
|
tbl->count <= (ti->n_meters / 4)) {
|
|
|
|
int half_size = ti->n_meters / 2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Avoid hash collision, don't move slots to other place.
|
|
|
|
* Make sure there are no references of meters in array
|
|
|
|
* which will be released.
|
|
|
|
*/
|
|
|
|
for (i = half_size; i < ti->n_meters; i++)
|
|
|
|
if (rcu_dereference_ovsl(ti->dp_meters[i]))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (dp_meter_instance_realloc(tbl, half_size))
|
|
|
|
goto shrink_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
shrink_err:
|
|
|
|
dp_meter_instance_insert(ti, meter);
|
|
|
|
tbl->count++;
|
|
|
|
return -ENOMEM;
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *
|
|
|
|
ovs_meter_cmd_reply_start(struct genl_info *info, u8 cmd,
|
|
|
|
struct ovs_header **ovs_reply_header)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct ovs_header *ovs_header = info->userhdr;
|
|
|
|
|
|
|
|
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
*ovs_reply_header = genlmsg_put(skb, info->snd_portid,
|
|
|
|
info->snd_seq,
|
|
|
|
&dp_meter_genl_family, 0, cmd);
|
2017-11-15 04:26:16 +08:00
|
|
|
if (!*ovs_reply_header) {
|
2017-11-11 04:09:42 +08:00
|
|
|
nlmsg_free(skb);
|
|
|
|
return ERR_PTR(-EMSGSIZE);
|
|
|
|
}
|
|
|
|
(*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_meter_cmd_reply_stats(struct sk_buff *reply, u32 meter_id,
|
|
|
|
struct dp_meter *meter)
|
|
|
|
{
|
|
|
|
struct nlattr *nla;
|
|
|
|
struct dp_meter_band *band;
|
|
|
|
u16 i;
|
|
|
|
|
|
|
|
if (nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (nla_put(reply, OVS_METER_ATTR_STATS,
|
2020-04-24 08:08:04 +08:00
|
|
|
sizeof(struct ovs_flow_stats), &meter->stats))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (nla_put_u64_64bit(reply, OVS_METER_ATTR_USED, meter->used,
|
2017-11-11 04:09:42 +08:00
|
|
|
OVS_METER_ATTR_PAD))
|
|
|
|
goto error;
|
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!nla)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
band = meter->bands;
|
|
|
|
|
|
|
|
for (i = 0; i < meter->n_bands; ++i, ++band) {
|
|
|
|
struct nlattr *band_nla;
|
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!band_nla || nla_put(reply, OVS_BAND_ATTR_STATS,
|
|
|
|
sizeof(struct ovs_flow_stats),
|
|
|
|
&band->stats))
|
|
|
|
goto error;
|
|
|
|
nla_nest_end(reply, band_nla);
|
|
|
|
}
|
|
|
|
nla_nest_end(reply, nla);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
error:
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_meter_cmd_features(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
2020-04-24 08:08:03 +08:00
|
|
|
struct ovs_header *ovs_header = info->userhdr;
|
2017-11-11 04:09:42 +08:00
|
|
|
struct ovs_header *ovs_reply_header;
|
|
|
|
struct nlattr *nla, *band_nla;
|
2020-04-24 08:08:03 +08:00
|
|
|
struct sk_buff *reply;
|
|
|
|
struct datapath *dp;
|
|
|
|
int err = -EMSGSIZE;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
|
|
|
reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_FEATURES,
|
|
|
|
&ovs_reply_header);
|
2017-11-14 14:20:16 +08:00
|
|
|
if (IS_ERR(reply))
|
2017-11-11 04:09:42 +08:00
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
2020-04-24 08:08:03 +08:00
|
|
|
ovs_lock();
|
|
|
|
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
|
|
|
|
if (!dp) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nla_put_u32(reply, OVS_METER_ATTR_MAX_METERS,
|
|
|
|
dp->meter_tbl.max_meters_allowed))
|
|
|
|
goto exit_unlock;
|
|
|
|
|
|
|
|
ovs_unlock();
|
|
|
|
|
|
|
|
if (nla_put_u32(reply, OVS_METER_ATTR_MAX_BANDS, DP_MAX_BANDS))
|
2017-11-11 04:09:42 +08:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!nla)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!band_nla)
|
|
|
|
goto nla_put_failure;
|
|
|
|
/* Currently only DROP band type is supported. */
|
|
|
|
if (nla_put_u32(reply, OVS_BAND_ATTR_TYPE, OVS_METER_BAND_TYPE_DROP))
|
|
|
|
goto nla_put_failure;
|
|
|
|
nla_nest_end(reply, band_nla);
|
|
|
|
nla_nest_end(reply, nla);
|
|
|
|
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
2020-04-24 08:08:03 +08:00
|
|
|
exit_unlock:
|
|
|
|
ovs_unlock();
|
2017-11-11 04:09:42 +08:00
|
|
|
nla_put_failure:
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dp_meter *dp_meter_create(struct nlattr **a)
|
|
|
|
{
|
|
|
|
struct nlattr *nla;
|
|
|
|
int rem;
|
|
|
|
u16 n_bands = 0;
|
|
|
|
struct dp_meter *meter;
|
|
|
|
struct dp_meter_band *band;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Validate attributes, count the bands. */
|
|
|
|
if (!a[OVS_METER_ATTR_BANDS])
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem)
|
|
|
|
if (++n_bands > DP_MAX_BANDS)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
/* Allocate and set up the meter before locking anything. */
|
2019-01-16 05:19:17 +08:00
|
|
|
meter = kzalloc(struct_size(meter, bands, n_bands), GFP_KERNEL);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!meter)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2018-07-29 06:26:01 +08:00
|
|
|
meter->id = nla_get_u32(a[OVS_METER_ATTR_ID]);
|
2017-11-11 04:09:42 +08:00
|
|
|
meter->used = div_u64(ktime_get_ns(), 1000 * 1000);
|
|
|
|
meter->kbps = a[OVS_METER_ATTR_KBPS] ? 1 : 0;
|
|
|
|
meter->keep_stats = !a[OVS_METER_ATTR_CLEAR];
|
|
|
|
spin_lock_init(&meter->lock);
|
|
|
|
if (meter->keep_stats && a[OVS_METER_ATTR_STATS]) {
|
|
|
|
meter->stats = *(struct ovs_flow_stats *)
|
|
|
|
nla_data(a[OVS_METER_ATTR_STATS]);
|
|
|
|
}
|
|
|
|
meter->n_bands = n_bands;
|
|
|
|
|
|
|
|
/* Set up meter bands. */
|
|
|
|
band = meter->bands;
|
|
|
|
nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem) {
|
|
|
|
struct nlattr *attr[OVS_BAND_ATTR_MAX + 1];
|
|
|
|
u32 band_max_delta_t;
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 20:07:28 +08:00
|
|
|
err = nla_parse_deprecated((struct nlattr **)&attr,
|
|
|
|
OVS_BAND_ATTR_MAX, nla_data(nla),
|
|
|
|
nla_len(nla), band_policy, NULL);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (err)
|
|
|
|
goto exit_free_meter;
|
|
|
|
|
|
|
|
if (!attr[OVS_BAND_ATTR_TYPE] ||
|
|
|
|
!attr[OVS_BAND_ATTR_RATE] ||
|
|
|
|
!attr[OVS_BAND_ATTR_BURST]) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto exit_free_meter;
|
|
|
|
}
|
|
|
|
|
|
|
|
band->type = nla_get_u32(attr[OVS_BAND_ATTR_TYPE]);
|
|
|
|
band->rate = nla_get_u32(attr[OVS_BAND_ATTR_RATE]);
|
2018-03-09 10:08:50 +08:00
|
|
|
if (band->rate == 0) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto exit_free_meter;
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
band->burst_size = nla_get_u32(attr[OVS_BAND_ATTR_BURST]);
|
|
|
|
/* Figure out max delta_t that is enough to fill any bucket.
|
|
|
|
* Keep max_delta_t size to the bucket units:
|
|
|
|
* pkts => 1/1000 packets, kilobits => bits.
|
2018-03-09 10:08:50 +08:00
|
|
|
*
|
|
|
|
* Start with a full bucket.
|
2017-11-11 04:09:42 +08:00
|
|
|
*/
|
2020-04-24 08:08:06 +08:00
|
|
|
band->bucket = (band->burst_size + band->rate) * 1000ULL;
|
2020-04-25 11:39:48 +08:00
|
|
|
band_max_delta_t = div_u64(band->bucket, band->rate);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (band_max_delta_t > meter->max_delta_t)
|
|
|
|
meter->max_delta_t = band_max_delta_t;
|
|
|
|
band++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return meter;
|
|
|
|
|
|
|
|
exit_free_meter:
|
|
|
|
kfree(meter);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct dp_meter *meter, *old_meter;
|
|
|
|
struct sk_buff *reply;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
|
|
|
struct ovs_header *ovs_header = info->userhdr;
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_table *meter_tbl;
|
2017-11-11 04:09:42 +08:00
|
|
|
struct datapath *dp;
|
|
|
|
int err;
|
|
|
|
u32 meter_id;
|
|
|
|
bool failed;
|
|
|
|
|
2020-04-24 08:08:05 +08:00
|
|
|
if (!a[OVS_METER_ATTR_ID])
|
|
|
|
return -EINVAL;
|
2018-07-29 06:26:01 +08:00
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
meter = dp_meter_create(a);
|
|
|
|
if (IS_ERR_OR_NULL(meter))
|
|
|
|
return PTR_ERR(meter);
|
|
|
|
|
|
|
|
reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_SET,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply)) {
|
|
|
|
err = PTR_ERR(reply);
|
|
|
|
goto exit_free_meter;
|
|
|
|
}
|
|
|
|
|
|
|
|
ovs_lock();
|
|
|
|
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
|
|
|
|
if (!dp) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
meter_tbl = &dp->meter_tbl;
|
2017-11-11 04:09:42 +08:00
|
|
|
meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
old_meter = lookup_meter(meter_tbl, meter_id);
|
|
|
|
err = detach_meter(meter_tbl, old_meter);
|
|
|
|
if (err)
|
|
|
|
goto exit_unlock;
|
|
|
|
|
|
|
|
err = attach_meter(meter_tbl, meter);
|
|
|
|
if (err)
|
|
|
|
goto exit_unlock;
|
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
ovs_unlock();
|
|
|
|
|
|
|
|
/* Build response with the meter_id and stats from
|
|
|
|
* the old meter, if any.
|
|
|
|
*/
|
|
|
|
failed = nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id);
|
|
|
|
WARN_ON(failed);
|
|
|
|
if (old_meter) {
|
|
|
|
spin_lock_bh(&old_meter->lock);
|
|
|
|
if (old_meter->keep_stats) {
|
|
|
|
err = ovs_meter_cmd_reply_stats(reply, meter_id,
|
|
|
|
old_meter);
|
|
|
|
WARN_ON(err);
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&old_meter->lock);
|
|
|
|
ovs_meter_free(old_meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_unlock:
|
|
|
|
ovs_unlock();
|
|
|
|
nlmsg_free(reply);
|
|
|
|
exit_free_meter:
|
|
|
|
kfree(meter);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_meter_cmd_get(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct ovs_header *ovs_header = info->userhdr;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
2020-04-24 08:08:02 +08:00
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct dp_meter *meter;
|
|
|
|
struct sk_buff *reply;
|
2017-11-11 04:09:42 +08:00
|
|
|
struct datapath *dp;
|
2020-04-24 08:08:02 +08:00
|
|
|
u32 meter_id;
|
2017-11-11 04:09:42 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!a[OVS_METER_ATTR_ID])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
|
|
|
|
|
|
|
|
reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_GET,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply))
|
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
|
|
|
ovs_lock();
|
|
|
|
|
|
|
|
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
|
|
|
|
if (!dp) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate meter, copy stats. */
|
2020-04-24 08:08:02 +08:00
|
|
|
meter = lookup_meter(&dp->meter_tbl, meter_id);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (!meter) {
|
|
|
|
err = -ENOENT;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_bh(&meter->lock);
|
|
|
|
err = ovs_meter_cmd_reply_stats(reply, meter_id, meter);
|
|
|
|
spin_unlock_bh(&meter->lock);
|
|
|
|
if (err)
|
|
|
|
goto exit_unlock;
|
|
|
|
|
|
|
|
ovs_unlock();
|
|
|
|
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_unlock:
|
|
|
|
ovs_unlock();
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_meter_cmd_del(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct ovs_header *ovs_header = info->userhdr;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
2020-04-24 08:08:02 +08:00
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct dp_meter *old_meter;
|
|
|
|
struct sk_buff *reply;
|
2017-11-11 04:09:42 +08:00
|
|
|
struct datapath *dp;
|
2020-04-24 08:08:02 +08:00
|
|
|
u32 meter_id;
|
2017-11-11 04:09:42 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!a[OVS_METER_ATTR_ID])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_DEL,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply))
|
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
|
|
|
ovs_lock();
|
|
|
|
|
|
|
|
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
|
|
|
|
if (!dp) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto exit_unlock;
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
|
|
|
|
old_meter = lookup_meter(&dp->meter_tbl, meter_id);
|
2017-11-11 04:09:42 +08:00
|
|
|
if (old_meter) {
|
|
|
|
spin_lock_bh(&old_meter->lock);
|
|
|
|
err = ovs_meter_cmd_reply_stats(reply, meter_id, old_meter);
|
|
|
|
WARN_ON(err);
|
|
|
|
spin_unlock_bh(&old_meter->lock);
|
2020-04-24 08:08:02 +08:00
|
|
|
|
|
|
|
err = detach_meter(&dp->meter_tbl, old_meter);
|
|
|
|
if (err)
|
|
|
|
goto exit_unlock;
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|
2020-04-24 08:08:02 +08:00
|
|
|
|
2017-11-11 04:09:42 +08:00
|
|
|
ovs_unlock();
|
|
|
|
ovs_meter_free(old_meter);
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_unlock:
|
|
|
|
ovs_unlock();
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Meter action execution.
|
|
|
|
*
|
|
|
|
* Return true 'meter_id' drop band is triggered. The 'skb' should be
|
|
|
|
* dropped by the caller'.
|
|
|
|
*/
|
|
|
|
bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
|
|
|
|
struct sw_flow_key *key, u32 meter_id)
|
|
|
|
{
|
|
|
|
long long int now_ms = div_u64(ktime_get_ns(), 1000 * 1000);
|
|
|
|
long long int long_delta_ms;
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_band *band;
|
|
|
|
struct dp_meter *meter;
|
2017-11-11 04:09:42 +08:00
|
|
|
int i, band_exceeded_max = -1;
|
|
|
|
u32 band_exceeded_rate = 0;
|
2020-04-24 08:08:02 +08:00
|
|
|
u32 delta_ms;
|
|
|
|
u32 cost;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
meter = lookup_meter(&dp->meter_tbl, meter_id);
|
2017-11-11 04:09:42 +08:00
|
|
|
/* Do not drop the packet when there is no meter. */
|
|
|
|
if (!meter)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Lock the meter while using it. */
|
|
|
|
spin_lock(&meter->lock);
|
|
|
|
|
|
|
|
long_delta_ms = (now_ms - meter->used); /* ms */
|
|
|
|
|
|
|
|
/* Make sure delta_ms will not be too large, so that bucket will not
|
|
|
|
* wrap around below.
|
|
|
|
*/
|
|
|
|
delta_ms = (long_delta_ms > (long long int)meter->max_delta_t)
|
|
|
|
? meter->max_delta_t : (u32)long_delta_ms;
|
|
|
|
|
|
|
|
/* Update meter statistics.
|
|
|
|
*/
|
|
|
|
meter->used = now_ms;
|
|
|
|
meter->stats.n_packets += 1;
|
|
|
|
meter->stats.n_bytes += skb->len;
|
|
|
|
|
|
|
|
/* Bucket rate is either in kilobits per second, or in packets per
|
|
|
|
* second. We maintain the bucket in the units of either bits or
|
|
|
|
* 1/1000th of a packet, correspondingly.
|
|
|
|
* Then, when rate is multiplied with milliseconds, we get the
|
|
|
|
* bucket units:
|
|
|
|
* msec * kbps = bits, and
|
|
|
|
* msec * packets/sec = 1/1000 packets.
|
|
|
|
*
|
|
|
|
* 'cost' is the number of bucket units in this packet.
|
|
|
|
*/
|
|
|
|
cost = (meter->kbps) ? skb->len * 8 : 1000;
|
|
|
|
|
|
|
|
/* Update all bands and find the one hit with the highest rate. */
|
|
|
|
for (i = 0; i < meter->n_bands; ++i) {
|
|
|
|
long long int max_bucket_size;
|
|
|
|
|
|
|
|
band = &meter->bands[i];
|
2018-01-31 12:55:33 +08:00
|
|
|
max_bucket_size = (band->burst_size + band->rate) * 1000LL;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
|
|
|
band->bucket += delta_ms * band->rate;
|
|
|
|
if (band->bucket > max_bucket_size)
|
|
|
|
band->bucket = max_bucket_size;
|
|
|
|
|
|
|
|
if (band->bucket >= cost) {
|
|
|
|
band->bucket -= cost;
|
|
|
|
} else if (band->rate > band_exceeded_rate) {
|
|
|
|
band_exceeded_rate = band->rate;
|
|
|
|
band_exceeded_max = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (band_exceeded_max >= 0) {
|
|
|
|
/* Update band statistics. */
|
|
|
|
band = &meter->bands[band_exceeded_max];
|
|
|
|
band->stats.n_packets += 1;
|
|
|
|
band->stats.n_bytes += skb->len;
|
|
|
|
|
|
|
|
/* Drop band triggered, let the caller drop the 'skb'. */
|
|
|
|
if (band->type == OVS_METER_BAND_TYPE_DROP) {
|
|
|
|
spin_unlock(&meter->lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&meter->lock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:44:17 +08:00
|
|
|
static const struct genl_small_ops dp_meter_genl_ops[] = {
|
2017-11-11 04:09:42 +08:00
|
|
|
{ .cmd = OVS_METER_CMD_FEATURES,
|
2019-04-26 20:07:31 +08:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2017-11-11 04:09:42 +08:00
|
|
|
.flags = 0, /* OK for unprivileged users. */
|
|
|
|
.doit = ovs_meter_cmd_features
|
|
|
|
},
|
|
|
|
{ .cmd = OVS_METER_CMD_SET,
|
2019-04-26 20:07:31 +08:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2017-11-11 04:09:42 +08:00
|
|
|
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
|
|
|
|
* privilege.
|
|
|
|
*/
|
|
|
|
.doit = ovs_meter_cmd_set,
|
|
|
|
},
|
|
|
|
{ .cmd = OVS_METER_CMD_GET,
|
2019-04-26 20:07:31 +08:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2017-11-11 04:09:42 +08:00
|
|
|
.flags = 0, /* OK for unprivileged users. */
|
|
|
|
.doit = ovs_meter_cmd_get,
|
|
|
|
},
|
|
|
|
{ .cmd = OVS_METER_CMD_DEL,
|
2019-04-26 20:07:31 +08:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2017-11-11 04:09:42 +08:00
|
|
|
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
|
|
|
|
* privilege.
|
|
|
|
*/
|
|
|
|
.doit = ovs_meter_cmd_del
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct genl_multicast_group ovs_meter_multicast_group = {
|
|
|
|
.name = OVS_METER_MCGROUP,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct genl_family dp_meter_genl_family __ro_after_init = {
|
|
|
|
.hdrsize = sizeof(struct ovs_header),
|
|
|
|
.name = OVS_METER_FAMILY,
|
|
|
|
.version = OVS_METER_VERSION,
|
|
|
|
.maxattr = OVS_METER_ATTR_MAX,
|
genetlink: make policy common to family
Since maxattr is common, the policy can't really differ sanely,
so make it common as well.
The only user that did in fact manage to make a non-common policy
is taskstats, which has to be really careful about it (since it's
still using a common maxattr!). This is no longer supported, but
we can fake it using pre_doit.
This reduces the size of e.g. nl80211.o (which has lots of commands):
text data bss dec hex filename
398745 14323 2240 415308 6564c net/wireless/nl80211.o (before)
397913 14331 2240 414484 65314 net/wireless/nl80211.o (after)
--------------------------------
-832 +8 0 -824
Which is obviously just 8 bytes for each command, and an added 8
bytes for the new policy pointer. I'm not sure why the ops list is
counted as .text though.
Most of the code transformations were done using the following spatch:
@ops@
identifier OPS;
expression POLICY;
@@
struct genl_ops OPS[] = {
...,
{
- .policy = POLICY,
},
...
};
@@
identifier ops.OPS;
expression ops.POLICY;
identifier fam;
expression M;
@@
struct genl_family fam = {
.ops = OPS,
.maxattr = M,
+ .policy = POLICY,
...
};
This also gets rid of devlink_nl_cmd_region_read_dumpit() accessing
the cb->data as ops, which we want to change in a later genl patch.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-03-22 05:51:02 +08:00
|
|
|
.policy = meter_policy,
|
2017-11-11 04:09:42 +08:00
|
|
|
.netnsok = true,
|
|
|
|
.parallel_ops = true,
|
2020-10-03 05:49:54 +08:00
|
|
|
.small_ops = dp_meter_genl_ops,
|
|
|
|
.n_small_ops = ARRAY_SIZE(dp_meter_genl_ops),
|
2017-11-11 04:09:42 +08:00
|
|
|
.mcgrps = &ovs_meter_multicast_group,
|
|
|
|
.n_mcgrps = 1,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
int ovs_meters_init(struct datapath *dp)
|
|
|
|
{
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_table *tbl = &dp->meter_tbl;
|
|
|
|
struct dp_meter_instance *ti;
|
2020-04-24 08:08:03 +08:00
|
|
|
unsigned long free_mem_bytes;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
ti = dp_meter_instance_alloc(DP_METER_ARRAY_SIZE_MIN);
|
|
|
|
if (!ti)
|
2017-11-11 04:09:42 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-04-24 08:08:03 +08:00
|
|
|
/* Allow meters in a datapath to use ~3.12% of physical memory. */
|
|
|
|
free_mem_bytes = nr_free_buffer_pages() * (PAGE_SIZE >> 5);
|
|
|
|
tbl->max_meters_allowed = min(free_mem_bytes / sizeof(struct dp_meter),
|
|
|
|
DP_METER_NUM_MAX);
|
|
|
|
if (!tbl->max_meters_allowed)
|
|
|
|
goto out_err;
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
rcu_assign_pointer(tbl->ti, ti);
|
|
|
|
tbl->count = 0;
|
2017-11-11 04:09:42 +08:00
|
|
|
|
|
|
|
return 0;
|
2020-04-24 08:08:03 +08:00
|
|
|
|
|
|
|
out_err:
|
|
|
|
dp_meter_instance_free(ti);
|
|
|
|
return -ENOMEM;
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovs_meters_exit(struct datapath *dp)
|
|
|
|
{
|
2020-04-24 08:08:02 +08:00
|
|
|
struct dp_meter_table *tbl = &dp->meter_tbl;
|
|
|
|
struct dp_meter_instance *ti = rcu_dereference_raw(tbl->ti);
|
2017-11-11 04:09:42 +08:00
|
|
|
int i;
|
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
for (i = 0; i < ti->n_meters; i++)
|
2020-04-25 11:39:47 +08:00
|
|
|
ovs_meter_free(rcu_dereference_raw(ti->dp_meters[i]));
|
2017-11-11 04:09:42 +08:00
|
|
|
|
2020-04-24 08:08:02 +08:00
|
|
|
dp_meter_instance_free(ti);
|
2017-11-11 04:09:42 +08:00
|
|
|
}
|