blob: 896b8f5bc885350980a9547fc921583515dff7ec [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Andy Zhou96fbc132017-11-10 12:09:42 -08002/*
3 * Copyright (c) 2017 Nicira, Inc.
Andy Zhou96fbc132017-11-10 12:09:42 -08004 */
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>
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +080015#include <linux/swap.h>
Andy Zhou96fbc132017-11-10 12:09:42 -080016
17#include <net/netlink.h>
18#include <net/genetlink.h>
19
20#include "datapath.h"
21#include "meter.h"
22
Andy Zhou96fbc132017-11-10 12:09:42 -080023static const struct nla_policy meter_policy[OVS_METER_ATTR_MAX + 1] = {
24 [OVS_METER_ATTR_ID] = { .type = NLA_U32, },
25 [OVS_METER_ATTR_KBPS] = { .type = NLA_FLAG },
26 [OVS_METER_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
27 [OVS_METER_ATTR_BANDS] = { .type = NLA_NESTED },
28 [OVS_METER_ATTR_USED] = { .type = NLA_U64 },
29 [OVS_METER_ATTR_CLEAR] = { .type = NLA_FLAG },
30 [OVS_METER_ATTR_MAX_METERS] = { .type = NLA_U32 },
31 [OVS_METER_ATTR_MAX_BANDS] = { .type = NLA_U32 },
32};
33
34static const struct nla_policy band_policy[OVS_BAND_ATTR_MAX + 1] = {
35 [OVS_BAND_ATTR_TYPE] = { .type = NLA_U32, },
36 [OVS_BAND_ATTR_RATE] = { .type = NLA_U32, },
37 [OVS_BAND_ATTR_BURST] = { .type = NLA_U32, },
38 [OVS_BAND_ATTR_STATS] = { .len = sizeof(struct ovs_flow_stats) },
39};
40
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080041static u32 meter_hash(struct dp_meter_instance *ti, u32 id)
42{
43 return id % ti->n_meters;
44}
45
Andy Zhou96fbc132017-11-10 12:09:42 -080046static void ovs_meter_free(struct dp_meter *meter)
47{
48 if (!meter)
49 return;
50
Wei Yongjun6dc14dc2017-11-14 06:27:12 +000051 kfree_rcu(meter, rcu);
Andy Zhou96fbc132017-11-10 12:09:42 -080052}
53
Andy Zhou96fbc132017-11-10 12:09:42 -080054/* Call with ovs_mutex or RCU read lock. */
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080055static struct dp_meter *lookup_meter(const struct dp_meter_table *tbl,
Andy Zhou96fbc132017-11-10 12:09:42 -080056 u32 meter_id)
57{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080058 struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
59 u32 hash = meter_hash(ti, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -080060 struct dp_meter *meter;
Andy Zhou96fbc132017-11-10 12:09:42 -080061
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080062 meter = rcu_dereference_ovsl(ti->dp_meters[hash]);
63 if (meter && likely(meter->id == meter_id))
64 return meter;
65
Andy Zhou96fbc132017-11-10 12:09:42 -080066 return NULL;
67}
68
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080069static struct dp_meter_instance *dp_meter_instance_alloc(const u32 size)
Andy Zhou96fbc132017-11-10 12:09:42 -080070{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080071 struct dp_meter_instance *ti;
Andy Zhou96fbc132017-11-10 12:09:42 -080072
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080073 ti = kvzalloc(sizeof(*ti) +
74 sizeof(struct dp_meter *) * size,
75 GFP_KERNEL);
76 if (!ti)
77 return NULL;
78
79 ti->n_meters = size;
80
81 return ti;
Andy Zhou96fbc132017-11-10 12:09:42 -080082}
83
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080084static void dp_meter_instance_free(struct dp_meter_instance *ti)
Andy Zhou96fbc132017-11-10 12:09:42 -080085{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080086 kvfree(ti);
87}
88
89static void dp_meter_instance_free_rcu(struct rcu_head *rcu)
90{
91 struct dp_meter_instance *ti;
92
93 ti = container_of(rcu, struct dp_meter_instance, rcu);
94 kvfree(ti);
95}
96
97static int
98dp_meter_instance_realloc(struct dp_meter_table *tbl, u32 size)
99{
100 struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
101 int n_meters = min(size, ti->n_meters);
102 struct dp_meter_instance *new_ti;
103 int i;
104
105 new_ti = dp_meter_instance_alloc(size);
106 if (!new_ti)
107 return -ENOMEM;
108
109 for (i = 0; i < n_meters; i++)
Tonghao Zhang4b36a0d2020-04-25 11:39:47 +0800110 if (rcu_dereference_ovsl(ti->dp_meters[i]))
111 new_ti->dp_meters[i] = ti->dp_meters[i];
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800112
113 rcu_assign_pointer(tbl->ti, new_ti);
114 call_rcu(&ti->rcu, dp_meter_instance_free_rcu);
115
116 return 0;
117}
118
119static void dp_meter_instance_insert(struct dp_meter_instance *ti,
120 struct dp_meter *meter)
121{
122 u32 hash;
123
124 hash = meter_hash(ti, meter->id);
125 rcu_assign_pointer(ti->dp_meters[hash], meter);
126}
127
128static void dp_meter_instance_remove(struct dp_meter_instance *ti,
129 struct dp_meter *meter)
130{
131 u32 hash;
132
133 hash = meter_hash(ti, meter->id);
134 RCU_INIT_POINTER(ti->dp_meters[hash], NULL);
135}
136
137static int attach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
138{
139 struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
140 u32 hash = meter_hash(ti, meter->id);
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800141 int err;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800142
143 /* In generally, slots selected should be empty, because
144 * OvS uses id-pool to fetch a available id.
145 */
146 if (unlikely(rcu_dereference_ovsl(ti->dp_meters[hash])))
147 return -EBUSY;
148
149 dp_meter_instance_insert(ti, meter);
150
151 /* That function is thread-safe. */
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800152 tbl->count++;
153 if (tbl->count >= tbl->max_meters_allowed) {
154 err = -EFBIG;
155 goto attach_err;
156 }
157
158 if (tbl->count >= ti->n_meters &&
159 dp_meter_instance_realloc(tbl, ti->n_meters * 2)) {
160 err = -ENOMEM;
161 goto attach_err;
162 }
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800163
164 return 0;
165
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800166attach_err:
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800167 dp_meter_instance_remove(ti, meter);
168 tbl->count--;
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800169 return err;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800170}
171
172static int detach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
173{
174 struct dp_meter_instance *ti;
175
Andy Zhou96fbc132017-11-10 12:09:42 -0800176 ASSERT_OVSL();
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800177 if (!meter)
178 return 0;
179
180 ti = rcu_dereference_ovsl(tbl->ti);
181 dp_meter_instance_remove(ti, meter);
182
183 tbl->count--;
184
185 /* Shrink the meter array if necessary. */
186 if (ti->n_meters > DP_METER_ARRAY_SIZE_MIN &&
187 tbl->count <= (ti->n_meters / 4)) {
188 int half_size = ti->n_meters / 2;
189 int i;
190
191 /* Avoid hash collision, don't move slots to other place.
192 * Make sure there are no references of meters in array
193 * which will be released.
194 */
195 for (i = half_size; i < ti->n_meters; i++)
196 if (rcu_dereference_ovsl(ti->dp_meters[i]))
197 goto out;
198
199 if (dp_meter_instance_realloc(tbl, half_size))
200 goto shrink_err;
201 }
202
203out:
204 return 0;
205
206shrink_err:
207 dp_meter_instance_insert(ti, meter);
208 tbl->count++;
209 return -ENOMEM;
Andy Zhou96fbc132017-11-10 12:09:42 -0800210}
211
212static struct sk_buff *
213ovs_meter_cmd_reply_start(struct genl_info *info, u8 cmd,
214 struct ovs_header **ovs_reply_header)
215{
216 struct sk_buff *skb;
217 struct ovs_header *ovs_header = info->userhdr;
218
219 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
220 if (!skb)
221 return ERR_PTR(-ENOMEM);
222
223 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
224 info->snd_seq,
225 &dp_meter_genl_family, 0, cmd);
Gustavo A. R. Silvab74912a2017-11-14 14:26:16 -0600226 if (!*ovs_reply_header) {
Andy Zhou96fbc132017-11-10 12:09:42 -0800227 nlmsg_free(skb);
228 return ERR_PTR(-EMSGSIZE);
229 }
230 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
231
232 return skb;
233}
234
235static int ovs_meter_cmd_reply_stats(struct sk_buff *reply, u32 meter_id,
236 struct dp_meter *meter)
237{
238 struct nlattr *nla;
239 struct dp_meter_band *band;
240 u16 i;
241
242 if (nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id))
243 goto error;
244
Andy Zhou96fbc132017-11-10 12:09:42 -0800245 if (nla_put(reply, OVS_METER_ATTR_STATS,
Tonghao Zhanga8e38732020-04-24 08:08:04 +0800246 sizeof(struct ovs_flow_stats), &meter->stats))
247 goto error;
248
249 if (nla_put_u64_64bit(reply, OVS_METER_ATTR_USED, meter->used,
Andy Zhou96fbc132017-11-10 12:09:42 -0800250 OVS_METER_ATTR_PAD))
251 goto error;
252
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200253 nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
Andy Zhou96fbc132017-11-10 12:09:42 -0800254 if (!nla)
255 goto error;
256
257 band = meter->bands;
258
259 for (i = 0; i < meter->n_bands; ++i, ++band) {
260 struct nlattr *band_nla;
261
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200262 band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
Andy Zhou96fbc132017-11-10 12:09:42 -0800263 if (!band_nla || nla_put(reply, OVS_BAND_ATTR_STATS,
264 sizeof(struct ovs_flow_stats),
265 &band->stats))
266 goto error;
267 nla_nest_end(reply, band_nla);
268 }
269 nla_nest_end(reply, nla);
270
271 return 0;
272error:
273 return -EMSGSIZE;
274}
275
276static int ovs_meter_cmd_features(struct sk_buff *skb, struct genl_info *info)
277{
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800278 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou96fbc132017-11-10 12:09:42 -0800279 struct ovs_header *ovs_reply_header;
280 struct nlattr *nla, *band_nla;
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800281 struct sk_buff *reply;
282 struct datapath *dp;
283 int err = -EMSGSIZE;
Andy Zhou96fbc132017-11-10 12:09:42 -0800284
285 reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_FEATURES,
286 &ovs_reply_header);
Wei Yongjun8a860c22017-11-14 06:20:16 +0000287 if (IS_ERR(reply))
Andy Zhou96fbc132017-11-10 12:09:42 -0800288 return PTR_ERR(reply);
289
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800290 ovs_lock();
291 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
292 if (!dp) {
293 err = -ENODEV;
294 goto exit_unlock;
295 }
296
297 if (nla_put_u32(reply, OVS_METER_ATTR_MAX_METERS,
298 dp->meter_tbl.max_meters_allowed))
299 goto exit_unlock;
300
301 ovs_unlock();
302
303 if (nla_put_u32(reply, OVS_METER_ATTR_MAX_BANDS, DP_MAX_BANDS))
Andy Zhou96fbc132017-11-10 12:09:42 -0800304 goto nla_put_failure;
305
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200306 nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
Andy Zhou96fbc132017-11-10 12:09:42 -0800307 if (!nla)
308 goto nla_put_failure;
309
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200310 band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
Andy Zhou96fbc132017-11-10 12:09:42 -0800311 if (!band_nla)
312 goto nla_put_failure;
313 /* Currently only DROP band type is supported. */
314 if (nla_put_u32(reply, OVS_BAND_ATTR_TYPE, OVS_METER_BAND_TYPE_DROP))
315 goto nla_put_failure;
316 nla_nest_end(reply, band_nla);
317 nla_nest_end(reply, nla);
318
319 genlmsg_end(reply, ovs_reply_header);
320 return genlmsg_reply(reply, info);
321
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800322exit_unlock:
323 ovs_unlock();
Andy Zhou96fbc132017-11-10 12:09:42 -0800324nla_put_failure:
325 nlmsg_free(reply);
Andy Zhou96fbc132017-11-10 12:09:42 -0800326 return err;
327}
328
329static struct dp_meter *dp_meter_create(struct nlattr **a)
330{
331 struct nlattr *nla;
332 int rem;
333 u16 n_bands = 0;
334 struct dp_meter *meter;
335 struct dp_meter_band *band;
336 int err;
337
338 /* Validate attributes, count the bands. */
339 if (!a[OVS_METER_ATTR_BANDS])
340 return ERR_PTR(-EINVAL);
341
342 nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem)
343 if (++n_bands > DP_MAX_BANDS)
344 return ERR_PTR(-EINVAL);
345
346 /* Allocate and set up the meter before locking anything. */
Gustavo A. R. Silvac5c38992019-01-15 15:19:17 -0600347 meter = kzalloc(struct_size(meter, bands, n_bands), GFP_KERNEL);
Andy Zhou96fbc132017-11-10 12:09:42 -0800348 if (!meter)
349 return ERR_PTR(-ENOMEM);
350
Justin Pettit25432eb2018-07-28 15:26:01 -0700351 meter->id = nla_get_u32(a[OVS_METER_ATTR_ID]);
Andy Zhou96fbc132017-11-10 12:09:42 -0800352 meter->used = div_u64(ktime_get_ns(), 1000 * 1000);
353 meter->kbps = a[OVS_METER_ATTR_KBPS] ? 1 : 0;
354 meter->keep_stats = !a[OVS_METER_ATTR_CLEAR];
355 spin_lock_init(&meter->lock);
356 if (meter->keep_stats && a[OVS_METER_ATTR_STATS]) {
357 meter->stats = *(struct ovs_flow_stats *)
358 nla_data(a[OVS_METER_ATTR_STATS]);
359 }
360 meter->n_bands = n_bands;
361
362 /* Set up meter bands. */
363 band = meter->bands;
364 nla_for_each_nested(nla, a[OVS_METER_ATTR_BANDS], rem) {
365 struct nlattr *attr[OVS_BAND_ATTR_MAX + 1];
366 u32 band_max_delta_t;
367
Johannes Berg8cb08172019-04-26 14:07:28 +0200368 err = nla_parse_deprecated((struct nlattr **)&attr,
369 OVS_BAND_ATTR_MAX, nla_data(nla),
370 nla_len(nla), band_policy, NULL);
Andy Zhou96fbc132017-11-10 12:09:42 -0800371 if (err)
372 goto exit_free_meter;
373
374 if (!attr[OVS_BAND_ATTR_TYPE] ||
375 !attr[OVS_BAND_ATTR_RATE] ||
376 !attr[OVS_BAND_ATTR_BURST]) {
377 err = -EINVAL;
378 goto exit_free_meter;
379 }
380
381 band->type = nla_get_u32(attr[OVS_BAND_ATTR_TYPE]);
382 band->rate = nla_get_u32(attr[OVS_BAND_ATTR_RATE]);
zhanglipingddc502d2018-03-09 10:08:50 +0800383 if (band->rate == 0) {
384 err = -EINVAL;
385 goto exit_free_meter;
386 }
387
Andy Zhou96fbc132017-11-10 12:09:42 -0800388 band->burst_size = nla_get_u32(attr[OVS_BAND_ATTR_BURST]);
389 /* Figure out max delta_t that is enough to fill any bucket.
390 * Keep max_delta_t size to the bucket units:
391 * pkts => 1/1000 packets, kilobits => bits.
zhanglipingddc502d2018-03-09 10:08:50 +0800392 *
393 * Start with a full bucket.
Andy Zhou96fbc132017-11-10 12:09:42 -0800394 */
Ilya Maximets7d742b52021-04-21 15:57:47 +0200395 band->bucket = band->burst_size * 1000ULL;
Tonghao Zhang659d4582020-04-25 11:39:48 +0800396 band_max_delta_t = div_u64(band->bucket, band->rate);
Andy Zhou96fbc132017-11-10 12:09:42 -0800397 if (band_max_delta_t > meter->max_delta_t)
398 meter->max_delta_t = band_max_delta_t;
399 band++;
400 }
401
402 return meter;
403
404exit_free_meter:
405 kfree(meter);
406 return ERR_PTR(err);
407}
408
409static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
410{
411 struct nlattr **a = info->attrs;
412 struct dp_meter *meter, *old_meter;
413 struct sk_buff *reply;
414 struct ovs_header *ovs_reply_header;
415 struct ovs_header *ovs_header = info->userhdr;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800416 struct dp_meter_table *meter_tbl;
Andy Zhou96fbc132017-11-10 12:09:42 -0800417 struct datapath *dp;
418 int err;
419 u32 meter_id;
420 bool failed;
421
Tonghao Zhangc773500892020-04-24 08:08:05 +0800422 if (!a[OVS_METER_ATTR_ID])
423 return -EINVAL;
Justin Pettit25432eb2018-07-28 15:26:01 -0700424
Andy Zhou96fbc132017-11-10 12:09:42 -0800425 meter = dp_meter_create(a);
YueHaibing92f9e232020-10-31 14:01:53 +0800426 if (IS_ERR(meter))
Andy Zhou96fbc132017-11-10 12:09:42 -0800427 return PTR_ERR(meter);
428
429 reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_SET,
430 &ovs_reply_header);
431 if (IS_ERR(reply)) {
432 err = PTR_ERR(reply);
433 goto exit_free_meter;
434 }
435
436 ovs_lock();
437 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
438 if (!dp) {
439 err = -ENODEV;
440 goto exit_unlock;
441 }
442
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800443 meter_tbl = &dp->meter_tbl;
Andy Zhou96fbc132017-11-10 12:09:42 -0800444 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
445
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800446 old_meter = lookup_meter(meter_tbl, meter_id);
447 err = detach_meter(meter_tbl, old_meter);
448 if (err)
449 goto exit_unlock;
450
451 err = attach_meter(meter_tbl, meter);
452 if (err)
453 goto exit_unlock;
454
Andy Zhou96fbc132017-11-10 12:09:42 -0800455 ovs_unlock();
456
457 /* Build response with the meter_id and stats from
458 * the old meter, if any.
459 */
460 failed = nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id);
461 WARN_ON(failed);
462 if (old_meter) {
463 spin_lock_bh(&old_meter->lock);
464 if (old_meter->keep_stats) {
465 err = ovs_meter_cmd_reply_stats(reply, meter_id,
466 old_meter);
467 WARN_ON(err);
468 }
469 spin_unlock_bh(&old_meter->lock);
470 ovs_meter_free(old_meter);
471 }
472
473 genlmsg_end(reply, ovs_reply_header);
474 return genlmsg_reply(reply, info);
475
476exit_unlock:
477 ovs_unlock();
478 nlmsg_free(reply);
479exit_free_meter:
480 kfree(meter);
481 return err;
482}
483
484static int ovs_meter_cmd_get(struct sk_buff *skb, struct genl_info *info)
485{
Andy Zhou96fbc132017-11-10 12:09:42 -0800486 struct ovs_header *ovs_header = info->userhdr;
487 struct ovs_header *ovs_reply_header;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800488 struct nlattr **a = info->attrs;
Andy Zhou96fbc132017-11-10 12:09:42 -0800489 struct dp_meter *meter;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800490 struct sk_buff *reply;
491 struct datapath *dp;
492 u32 meter_id;
493 int err;
Andy Zhou96fbc132017-11-10 12:09:42 -0800494
495 if (!a[OVS_METER_ATTR_ID])
496 return -EINVAL;
497
498 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
499
500 reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_GET,
501 &ovs_reply_header);
502 if (IS_ERR(reply))
503 return PTR_ERR(reply);
504
505 ovs_lock();
506
507 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
508 if (!dp) {
509 err = -ENODEV;
510 goto exit_unlock;
511 }
512
513 /* Locate meter, copy stats. */
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800514 meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800515 if (!meter) {
516 err = -ENOENT;
517 goto exit_unlock;
518 }
519
520 spin_lock_bh(&meter->lock);
521 err = ovs_meter_cmd_reply_stats(reply, meter_id, meter);
522 spin_unlock_bh(&meter->lock);
523 if (err)
524 goto exit_unlock;
525
526 ovs_unlock();
527
528 genlmsg_end(reply, ovs_reply_header);
529 return genlmsg_reply(reply, info);
530
531exit_unlock:
532 ovs_unlock();
533 nlmsg_free(reply);
534 return err;
535}
536
537static int ovs_meter_cmd_del(struct sk_buff *skb, struct genl_info *info)
538{
Andy Zhou96fbc132017-11-10 12:09:42 -0800539 struct ovs_header *ovs_header = info->userhdr;
540 struct ovs_header *ovs_reply_header;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800541 struct nlattr **a = info->attrs;
Andy Zhou96fbc132017-11-10 12:09:42 -0800542 struct dp_meter *old_meter;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800543 struct sk_buff *reply;
544 struct datapath *dp;
545 u32 meter_id;
546 int err;
Andy Zhou96fbc132017-11-10 12:09:42 -0800547
548 if (!a[OVS_METER_ATTR_ID])
549 return -EINVAL;
Andy Zhou96fbc132017-11-10 12:09:42 -0800550
551 reply = ovs_meter_cmd_reply_start(info, OVS_METER_CMD_DEL,
552 &ovs_reply_header);
553 if (IS_ERR(reply))
554 return PTR_ERR(reply);
555
556 ovs_lock();
557
558 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
559 if (!dp) {
560 err = -ENODEV;
561 goto exit_unlock;
562 }
563
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800564 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
565 old_meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800566 if (old_meter) {
567 spin_lock_bh(&old_meter->lock);
568 err = ovs_meter_cmd_reply_stats(reply, meter_id, old_meter);
569 WARN_ON(err);
570 spin_unlock_bh(&old_meter->lock);
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800571
572 err = detach_meter(&dp->meter_tbl, old_meter);
573 if (err)
574 goto exit_unlock;
Andy Zhou96fbc132017-11-10 12:09:42 -0800575 }
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800576
Andy Zhou96fbc132017-11-10 12:09:42 -0800577 ovs_unlock();
578 ovs_meter_free(old_meter);
579 genlmsg_end(reply, ovs_reply_header);
580 return genlmsg_reply(reply, info);
581
582exit_unlock:
583 ovs_unlock();
584 nlmsg_free(reply);
585 return err;
586}
587
588/* Meter action execution.
589 *
590 * Return true 'meter_id' drop band is triggered. The 'skb' should be
591 * dropped by the caller'.
592 */
593bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
594 struct sw_flow_key *key, u32 meter_id)
595{
Andy Zhou96fbc132017-11-10 12:09:42 -0800596 long long int now_ms = div_u64(ktime_get_ns(), 1000 * 1000);
597 long long int long_delta_ms;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800598 struct dp_meter_band *band;
599 struct dp_meter *meter;
Andy Zhou96fbc132017-11-10 12:09:42 -0800600 int i, band_exceeded_max = -1;
601 u32 band_exceeded_rate = 0;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800602 u32 delta_ms;
603 u32 cost;
Andy Zhou96fbc132017-11-10 12:09:42 -0800604
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800605 meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800606 /* Do not drop the packet when there is no meter. */
607 if (!meter)
608 return false;
609
610 /* Lock the meter while using it. */
611 spin_lock(&meter->lock);
612
613 long_delta_ms = (now_ms - meter->used); /* ms */
Tao Liue4df1b02021-05-13 21:08:00 +0800614 if (long_delta_ms < 0) {
615 /* This condition means that we have several threads fighting
616 * for a meter lock, and the one who received the packets a
617 * bit later wins. Assuming that all racing threads received
618 * packets at the same time to avoid overflow.
619 */
620 long_delta_ms = 0;
621 }
Andy Zhou96fbc132017-11-10 12:09:42 -0800622
623 /* Make sure delta_ms will not be too large, so that bucket will not
624 * wrap around below.
625 */
626 delta_ms = (long_delta_ms > (long long int)meter->max_delta_t)
627 ? meter->max_delta_t : (u32)long_delta_ms;
628
629 /* Update meter statistics.
630 */
631 meter->used = now_ms;
632 meter->stats.n_packets += 1;
633 meter->stats.n_bytes += skb->len;
634
635 /* Bucket rate is either in kilobits per second, or in packets per
636 * second. We maintain the bucket in the units of either bits or
637 * 1/1000th of a packet, correspondingly.
638 * Then, when rate is multiplied with milliseconds, we get the
639 * bucket units:
640 * msec * kbps = bits, and
641 * msec * packets/sec = 1/1000 packets.
642 *
643 * 'cost' is the number of bucket units in this packet.
644 */
645 cost = (meter->kbps) ? skb->len * 8 : 1000;
646
647 /* Update all bands and find the one hit with the highest rate. */
648 for (i = 0; i < meter->n_bands; ++i) {
649 long long int max_bucket_size;
650
651 band = &meter->bands[i];
Ilya Maximets7d742b52021-04-21 15:57:47 +0200652 max_bucket_size = band->burst_size * 1000LL;
Andy Zhou96fbc132017-11-10 12:09:42 -0800653
654 band->bucket += delta_ms * band->rate;
655 if (band->bucket > max_bucket_size)
656 band->bucket = max_bucket_size;
657
658 if (band->bucket >= cost) {
659 band->bucket -= cost;
660 } else if (band->rate > band_exceeded_rate) {
661 band_exceeded_rate = band->rate;
662 band_exceeded_max = i;
663 }
664 }
665
666 if (band_exceeded_max >= 0) {
667 /* Update band statistics. */
668 band = &meter->bands[band_exceeded_max];
669 band->stats.n_packets += 1;
670 band->stats.n_bytes += skb->len;
671
672 /* Drop band triggered, let the caller drop the 'skb'. */
673 if (band->type == OVS_METER_BAND_TYPE_DROP) {
674 spin_unlock(&meter->lock);
675 return true;
676 }
677 }
678
679 spin_unlock(&meter->lock);
680 return false;
681}
682
Rikard Falkebornb980b312020-10-05 01:44:17 +0200683static const struct genl_small_ops dp_meter_genl_ops[] = {
Andy Zhou96fbc132017-11-10 12:09:42 -0800684 { .cmd = OVS_METER_CMD_FEATURES,
Johannes Bergef6243a2019-04-26 14:07:31 +0200685 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800686 .flags = 0, /* OK for unprivileged users. */
Andy Zhou96fbc132017-11-10 12:09:42 -0800687 .doit = ovs_meter_cmd_features
688 },
689 { .cmd = OVS_METER_CMD_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200690 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800691 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
692 * privilege.
693 */
Andy Zhou96fbc132017-11-10 12:09:42 -0800694 .doit = ovs_meter_cmd_set,
695 },
696 { .cmd = OVS_METER_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800698 .flags = 0, /* OK for unprivileged users. */
Andy Zhou96fbc132017-11-10 12:09:42 -0800699 .doit = ovs_meter_cmd_get,
700 },
701 { .cmd = OVS_METER_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200702 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800703 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
704 * privilege.
705 */
Andy Zhou96fbc132017-11-10 12:09:42 -0800706 .doit = ovs_meter_cmd_del
707 },
708};
709
710static const struct genl_multicast_group ovs_meter_multicast_group = {
711 .name = OVS_METER_MCGROUP,
712};
713
714struct genl_family dp_meter_genl_family __ro_after_init = {
715 .hdrsize = sizeof(struct ovs_header),
716 .name = OVS_METER_FAMILY,
717 .version = OVS_METER_VERSION,
718 .maxattr = OVS_METER_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100719 .policy = meter_policy,
Andy Zhou96fbc132017-11-10 12:09:42 -0800720 .netnsok = true,
721 .parallel_ops = true,
Jakub Kicinski66a9b922020-10-02 14:49:54 -0700722 .small_ops = dp_meter_genl_ops,
723 .n_small_ops = ARRAY_SIZE(dp_meter_genl_ops),
Andy Zhou96fbc132017-11-10 12:09:42 -0800724 .mcgrps = &ovs_meter_multicast_group,
725 .n_mcgrps = 1,
726 .module = THIS_MODULE,
727};
728
729int ovs_meters_init(struct datapath *dp)
730{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800731 struct dp_meter_table *tbl = &dp->meter_tbl;
732 struct dp_meter_instance *ti;
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800733 unsigned long free_mem_bytes;
Andy Zhou96fbc132017-11-10 12:09:42 -0800734
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800735 ti = dp_meter_instance_alloc(DP_METER_ARRAY_SIZE_MIN);
736 if (!ti)
Andy Zhou96fbc132017-11-10 12:09:42 -0800737 return -ENOMEM;
738
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800739 /* Allow meters in a datapath to use ~3.12% of physical memory. */
740 free_mem_bytes = nr_free_buffer_pages() * (PAGE_SIZE >> 5);
741 tbl->max_meters_allowed = min(free_mem_bytes / sizeof(struct dp_meter),
742 DP_METER_NUM_MAX);
743 if (!tbl->max_meters_allowed)
744 goto out_err;
745
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800746 rcu_assign_pointer(tbl->ti, ti);
747 tbl->count = 0;
Andy Zhou96fbc132017-11-10 12:09:42 -0800748
749 return 0;
Tonghao Zhangeb58eeb2020-04-24 08:08:03 +0800750
751out_err:
752 dp_meter_instance_free(ti);
753 return -ENOMEM;
Andy Zhou96fbc132017-11-10 12:09:42 -0800754}
755
756void ovs_meters_exit(struct datapath *dp)
757{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800758 struct dp_meter_table *tbl = &dp->meter_tbl;
759 struct dp_meter_instance *ti = rcu_dereference_raw(tbl->ti);
Andy Zhou96fbc132017-11-10 12:09:42 -0800760 int i;
761
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800762 for (i = 0; i < ti->n_meters; i++)
Tonghao Zhang4b36a0d2020-04-25 11:39:47 +0800763 ovs_meter_free(rcu_dereference_raw(ti->dp_meters[i]));
Andy Zhou96fbc132017-11-10 12:09:42 -0800764
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800765 dp_meter_instance_free(ti);
Andy Zhou96fbc132017-11-10 12:09:42 -0800766}