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