^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2017 Nicira, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/openvswitch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "datapath.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "meter.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const struct nla_policy meter_policy[OVS_METER_ATTR_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) [OVS_METER_ATTR_ID] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) [OVS_METER_ATTR_KBPS] = { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) [OVS_METER_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) [OVS_METER_ATTR_BANDS] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) [OVS_METER_ATTR_USED] = { .type = NLA_U64 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) [OVS_METER_ATTR_CLEAR] = { .type = NLA_FLAG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) [OVS_METER_ATTR_MAX_METERS] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) [OVS_METER_ATTR_MAX_BANDS] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const struct nla_policy band_policy[OVS_BAND_ATTR_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) [OVS_BAND_ATTR_TYPE] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) [OVS_BAND_ATTR_RATE] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) [OVS_BAND_ATTR_BURST] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) [OVS_BAND_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static u32 meter_hash(struct dp_meter_instance *ti, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return id % ti->n_meters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void ovs_meter_free(struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) kfree_rcu(meter, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Call with ovs_mutex or RCU read lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static struct dp_meter *lookup_meter(const struct dp_meter_table *tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u32 meter_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 hash = meter_hash(ti, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct dp_meter *meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) meter = rcu_dereference_ovsl(ti->dp_meters[hash]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (meter && likely(meter->id == meter_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct dp_meter_instance *dp_meter_instance_alloc(const u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct dp_meter_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ti = kvzalloc(sizeof(*ti) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sizeof(struct dp_meter *) * size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ti->n_meters = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static void dp_meter_instance_free(struct dp_meter_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) kvfree(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void dp_meter_instance_free_rcu(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct dp_meter_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ti = container_of(rcu, struct dp_meter_instance, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) kvfree(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) dp_meter_instance_realloc(struct dp_meter_table *tbl, u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int n_meters = min(size, ti->n_meters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct dp_meter_instance *new_ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) new_ti = dp_meter_instance_alloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!new_ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) for (i = 0; i < n_meters; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (rcu_dereference_ovsl(ti->dp_meters[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) new_ti->dp_meters[i] = ti->dp_meters[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rcu_assign_pointer(tbl->ti, new_ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) call_rcu(&ti->rcu, dp_meter_instance_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void dp_meter_instance_insert(struct dp_meter_instance *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) hash = meter_hash(ti, meter->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rcu_assign_pointer(ti->dp_meters[hash], meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void dp_meter_instance_remove(struct dp_meter_instance *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) hash = meter_hash(ti, meter->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) RCU_INIT_POINTER(ti->dp_meters[hash], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int attach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) u32 hash = meter_hash(ti, meter->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* In generally, slots selected should be empty, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * OvS uses id-pool to fetch a available id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (unlikely(rcu_dereference_ovsl(ti->dp_meters[hash])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dp_meter_instance_insert(ti, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* That function is thread-safe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tbl->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (tbl->count >= tbl->max_meters_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto attach_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (tbl->count >= ti->n_meters &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dp_meter_instance_realloc(tbl, ti->n_meters * 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto attach_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) attach_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dp_meter_instance_remove(ti, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) tbl->count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int detach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct dp_meter_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ASSERT_OVSL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ti = rcu_dereference_ovsl(tbl->ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dp_meter_instance_remove(ti, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) tbl->count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Shrink the meter array if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (ti->n_meters > DP_METER_ARRAY_SIZE_MIN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) tbl->count <= (ti->n_meters / 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int half_size = ti->n_meters / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Avoid hash collision, don't move slots to other place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Make sure there are no references of meters in array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * which will be released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) for (i = half_size; i < ti->n_meters; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (rcu_dereference_ovsl(ti->dp_meters[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (dp_meter_instance_realloc(tbl, half_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) goto shrink_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) shrink_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) dp_meter_instance_insert(ti, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) tbl->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static struct sk_buff *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ovs_meter_cmd_reply_start(struct genl_info *info, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct ovs_header **ovs_reply_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct ovs_header *ovs_header = info->userhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) info->snd_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) &dp_meter_genl_family, 0, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!*ovs_reply_header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) nlmsg_free(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ERR_PTR(-EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int ovs_meter_cmd_reply_stats(struct sk_buff *reply, u32 meter_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct dp_meter *meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct dp_meter_band *band;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) u16 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (nla_put(reply, OVS_METER_ATTR_STATS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) sizeof(struct ovs_flow_stats), &meter->stats))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (nla_put_u64_64bit(reply, OVS_METER_ATTR_USED, meter->used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) OVS_METER_ATTR_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) band = meter->bands;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) for (i = 0; i < meter->n_bands; ++i, ++band) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct nlattr *band_nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!band_nla || nla_put(reply, OVS_BAND_ATTR_STATS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) sizeof(struct ovs_flow_stats),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) &band->stats))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) nla_nest_end(reply, band_nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) nla_nest_end(reply, nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int ovs_meter_cmd_features(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct ovs_header *ovs_header = info->userhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct ovs_header *ovs_reply_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct nlattr *nla, *band_nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct sk_buff *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct datapath *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_FEATURES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) &ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (IS_ERR(reply))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return PTR_ERR(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) ovs_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (nla_put_u32(reply, OVS_METER_ATTR_MAX_METERS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) dp->meter_tbl.max_meters_allowed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (nla_put_u32(reply, OVS_METER_ATTR_MAX_BANDS, DP_MAX_BANDS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!band_nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Currently only DROP band type is supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (nla_put_u32(reply, OVS_BAND_ATTR_TYPE, OVS_METER_BAND_TYPE_DROP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) nla_nest_end(reply, band_nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) nla_nest_end(reply, nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) genlmsg_end(reply, ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return genlmsg_reply(reply, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) nlmsg_free(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static struct dp_meter *dp_meter_create(struct nlattr **a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) u16 n_bands = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct dp_meter *meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct dp_meter_band *band;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* Validate attributes, count the bands. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!a[OVS_METER_ATTR_BANDS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (++n_bands > DP_MAX_BANDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* Allocate and set up the meter before locking anything. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) meter = kzalloc(struct_size(meter, bands, n_bands), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) meter->id = nla_get_u32(a[OVS_METER_ATTR_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) meter->used = div_u64(ktime_get_ns(), 1000 * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) meter->kbps = a[OVS_METER_ATTR_KBPS] ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) meter->keep_stats = !a[OVS_METER_ATTR_CLEAR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) spin_lock_init(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (meter->keep_stats && a[OVS_METER_ATTR_STATS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) meter->stats = *(struct ovs_flow_stats *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) nla_data(a[OVS_METER_ATTR_STATS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) meter->n_bands = n_bands;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* Set up meter bands. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) band = meter->bands;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct nlattr *attr[OVS_BAND_ATTR_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) u32 band_max_delta_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) err = nla_parse_deprecated((struct nlattr **)&attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) OVS_BAND_ATTR_MAX, nla_data(nla),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) nla_len(nla), band_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto exit_free_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (!attr[OVS_BAND_ATTR_TYPE] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) !attr[OVS_BAND_ATTR_RATE] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) !attr[OVS_BAND_ATTR_BURST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto exit_free_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) band->type = nla_get_u32(attr[OVS_BAND_ATTR_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) band->rate = nla_get_u32(attr[OVS_BAND_ATTR_RATE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (band->rate == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) goto exit_free_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) band->burst_size = nla_get_u32(attr[OVS_BAND_ATTR_BURST]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* Figure out max delta_t that is enough to fill any bucket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * Keep max_delta_t size to the bucket units:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * pkts => 1/1000 packets, kilobits => bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * Start with a full bucket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) band->bucket = (band->burst_size + band->rate) * 1000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) band_max_delta_t = div_u64(band->bucket, band->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (band_max_delta_t > meter->max_delta_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) meter->max_delta_t = band_max_delta_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) band++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) exit_free_meter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) kfree(meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct nlattr **a = info->attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct dp_meter *meter, *old_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct sk_buff *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct ovs_header *ovs_reply_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct ovs_header *ovs_header = info->userhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct dp_meter_table *meter_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct datapath *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) u32 meter_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) bool failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (!a[OVS_METER_ATTR_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) meter = dp_meter_create(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (IS_ERR_OR_NULL(meter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return PTR_ERR(meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) &ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (IS_ERR(reply)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) err = PTR_ERR(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto exit_free_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ovs_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) meter_tbl = &dp->meter_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) old_meter = lookup_meter(meter_tbl, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) err = detach_meter(meter_tbl, old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) err = attach_meter(meter_tbl, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* Build response with the meter_id and stats from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * the old meter, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) failed = nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) WARN_ON(failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (old_meter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) spin_lock_bh(&old_meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (old_meter->keep_stats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) err = ovs_meter_cmd_reply_stats(reply, meter_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) WARN_ON(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) spin_unlock_bh(&old_meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) ovs_meter_free(old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) genlmsg_end(reply, ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return genlmsg_reply(reply, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) nlmsg_free(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) exit_free_meter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) kfree(meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int ovs_meter_cmd_get(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct ovs_header *ovs_header = info->userhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct ovs_header *ovs_reply_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct nlattr **a = info->attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct dp_meter *meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct sk_buff *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct datapath *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) u32 meter_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!a[OVS_METER_ATTR_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) &ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (IS_ERR(reply))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return PTR_ERR(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) ovs_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /* Locate meter, copy stats. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) meter = lookup_meter(&dp->meter_tbl, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!meter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) spin_lock_bh(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = ovs_meter_cmd_reply_stats(reply, meter_id, meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) spin_unlock_bh(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) genlmsg_end(reply, ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return genlmsg_reply(reply, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) nlmsg_free(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static int ovs_meter_cmd_del(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct ovs_header *ovs_header = info->userhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct ovs_header *ovs_reply_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct nlattr **a = info->attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct dp_meter *old_meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct sk_buff *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) struct datapath *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) u32 meter_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (!a[OVS_METER_ATTR_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_DEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) &ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (IS_ERR(reply))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return PTR_ERR(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ovs_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) old_meter = lookup_meter(&dp->meter_tbl, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (old_meter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) spin_lock_bh(&old_meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err = ovs_meter_cmd_reply_stats(reply, meter_id, old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) WARN_ON(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) spin_unlock_bh(&old_meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) err = detach_meter(&dp->meter_tbl, old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) ovs_meter_free(old_meter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) genlmsg_end(reply, ovs_reply_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return genlmsg_reply(reply, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) ovs_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) nlmsg_free(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /* Meter action execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * Return true 'meter_id' drop band is triggered. The 'skb' should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * dropped by the caller'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct sw_flow_key *key, u32 meter_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) long long int now_ms = div_u64(ktime_get_ns(), 1000 * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) long long int long_delta_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct dp_meter_band *band;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct dp_meter *meter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int i, band_exceeded_max = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) u32 band_exceeded_rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) u32 delta_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) u32 cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) meter = lookup_meter(&dp->meter_tbl, meter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) /* Do not drop the packet when there is no meter. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (!meter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* Lock the meter while using it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) spin_lock(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) long_delta_ms = (now_ms - meter->used); /* ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (long_delta_ms < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /* This condition means that we have several threads fighting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * for a meter lock, and the one who received the packets a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * bit later wins. Assuming that all racing threads received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * packets at the same time to avoid overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) long_delta_ms = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /* Make sure delta_ms will not be too large, so that bucket will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * wrap around below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) delta_ms = (long_delta_ms > (long long int)meter->max_delta_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) ? meter->max_delta_t : (u32)long_delta_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /* Update meter statistics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) meter->used = now_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) meter->stats.n_packets += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) meter->stats.n_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /* Bucket rate is either in kilobits per second, or in packets per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * second. We maintain the bucket in the units of either bits or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * 1/1000th of a packet, correspondingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * Then, when rate is multiplied with milliseconds, we get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * bucket units:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * msec * kbps = bits, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * msec * packets/sec = 1/1000 packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * 'cost' is the number of bucket units in this packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) cost = (meter->kbps) ? skb->len * 8 : 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) /* Update all bands and find the one hit with the highest rate. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) for (i = 0; i < meter->n_bands; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) long long int max_bucket_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) band = &meter->bands[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) max_bucket_size = (band->burst_size + band->rate) * 1000LL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) band->bucket += delta_ms * band->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (band->bucket > max_bucket_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) band->bucket = max_bucket_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (band->bucket >= cost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) band->bucket -= cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) } else if (band->rate > band_exceeded_rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) band_exceeded_rate = band->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) band_exceeded_max = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (band_exceeded_max >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* Update band statistics. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) band = &meter->bands[band_exceeded_max];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) band->stats.n_packets += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) band->stats.n_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* Drop band triggered, let the caller drop the 'skb'. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (band->type == OVS_METER_BAND_TYPE_DROP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) spin_unlock(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) spin_unlock(&meter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static const struct genl_small_ops dp_meter_genl_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) { .cmd = OVS_METER_CMD_FEATURES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) .flags = 0, /* OK for unprivileged users. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) .doit = ovs_meter_cmd_features
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) { .cmd = OVS_METER_CMD_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * privilege.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) .doit = ovs_meter_cmd_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) { .cmd = OVS_METER_CMD_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) .flags = 0, /* OK for unprivileged users. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) .doit = ovs_meter_cmd_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) { .cmd = OVS_METER_CMD_DEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * privilege.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) .doit = ovs_meter_cmd_del
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static const struct genl_multicast_group ovs_meter_multicast_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) .name = OVS_METER_MCGROUP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct genl_family dp_meter_genl_family __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) .hdrsize = sizeof(struct ovs_header),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) .name = OVS_METER_FAMILY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) .version = OVS_METER_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) .maxattr = OVS_METER_ATTR_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) .policy = meter_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) .netnsok = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) .parallel_ops = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) .small_ops = dp_meter_genl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) .n_small_ops = ARRAY_SIZE(dp_meter_genl_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) .mcgrps = &ovs_meter_multicast_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) .n_mcgrps = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) int ovs_meters_init(struct datapath *dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct dp_meter_table *tbl = &dp->meter_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct dp_meter_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) unsigned long free_mem_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) ti = dp_meter_instance_alloc(DP_METER_ARRAY_SIZE_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (!ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) /* Allow meters in a datapath to use ~3.12% of physical memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) free_mem_bytes = nr_free_buffer_pages() * (PAGE_SIZE >> 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) tbl->max_meters_allowed = min(free_mem_bytes / sizeof(struct dp_meter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) DP_METER_NUM_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (!tbl->max_meters_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) rcu_assign_pointer(tbl->ti, ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) tbl->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) dp_meter_instance_free(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) void ovs_meters_exit(struct datapath *dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) struct dp_meter_table *tbl = &dp->meter_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct dp_meter_instance *ti = rcu_dereference_raw(tbl->ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) for (i = 0; i < ti->n_meters; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) ovs_meter_free(rcu_dereference_raw(ti->dp_meters[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) dp_meter_instance_free(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }