blob: f806ded1dd0a79d555a66180988144fecccedcc6 [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>
15
16#include <net/netlink.h>
17#include <net/genetlink.h>
18
19#include "datapath.h"
20#include "meter.h"
21
Andy Zhou96fbc132017-11-10 12:09:42 -080022static 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
33static 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 Zhangc7c4c442020-04-24 08:08:02 +080040static u32 meter_hash(struct dp_meter_instance *ti, u32 id)
41{
42 return id % ti->n_meters;
43}
44
Andy Zhou96fbc132017-11-10 12:09:42 -080045static void ovs_meter_free(struct dp_meter *meter)
46{
47 if (!meter)
48 return;
49
Wei Yongjun6dc14dc2017-11-14 06:27:12 +000050 kfree_rcu(meter, rcu);
Andy Zhou96fbc132017-11-10 12:09:42 -080051}
52
Andy Zhou96fbc132017-11-10 12:09:42 -080053/* Call with ovs_mutex or RCU read lock. */
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080054static struct dp_meter *lookup_meter(const struct dp_meter_table *tbl,
Andy Zhou96fbc132017-11-10 12:09:42 -080055 u32 meter_id)
56{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080057 struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti);
58 u32 hash = meter_hash(ti, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -080059 struct dp_meter *meter;
Andy Zhou96fbc132017-11-10 12:09:42 -080060
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080061 meter = rcu_dereference_ovsl(ti->dp_meters[hash]);
62 if (meter && likely(meter->id == meter_id))
63 return meter;
64
Andy Zhou96fbc132017-11-10 12:09:42 -080065 return NULL;
66}
67
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080068static struct dp_meter_instance *dp_meter_instance_alloc(const u32 size)
Andy Zhou96fbc132017-11-10 12:09:42 -080069{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080070 struct dp_meter_instance *ti;
Andy Zhou96fbc132017-11-10 12:09:42 -080071
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080072 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 Zhou96fbc132017-11-10 12:09:42 -080081}
82
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080083static void dp_meter_instance_free(struct dp_meter_instance *ti)
Andy Zhou96fbc132017-11-10 12:09:42 -080084{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +080085 kvfree(ti);
86}
87
88static 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
96static int
97dp_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
118static 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
127static 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
136static 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
156expand_err:
157 dp_meter_instance_remove(ti, meter);
158 tbl->count--;
159 return -ENOMEM;
160}
161
162static int detach_meter(struct dp_meter_table *tbl, struct dp_meter *meter)
163{
164 struct dp_meter_instance *ti;
165
Andy Zhou96fbc132017-11-10 12:09:42 -0800166 ASSERT_OVSL();
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800167 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
193out:
194 return 0;
195
196shrink_err:
197 dp_meter_instance_insert(ti, meter);
198 tbl->count++;
199 return -ENOMEM;
Andy Zhou96fbc132017-11-10 12:09:42 -0800200}
201
202static struct sk_buff *
203ovs_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. Silvab74912a2017-11-14 14:26:16 -0600216 if (!*ovs_reply_header) {
Andy Zhou96fbc132017-11-10 12:09:42 -0800217 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
225static 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 Kubecekae0be8d2019-04-26 11:13:06 +0200244 nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
Andy Zhou96fbc132017-11-10 12:09:42 -0800245 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 Kubecekae0be8d2019-04-26 11:13:06 +0200253 band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
Andy Zhou96fbc132017-11-10 12:09:42 -0800254 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;
263error:
264 return -EMSGSIZE;
265}
266
267static 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 Yongjun8a860c22017-11-14 06:20:16 +0000276 if (IS_ERR(reply))
Andy Zhou96fbc132017-11-10 12:09:42 -0800277 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 Kubecekae0be8d2019-04-26 11:13:06 +0200283 nla = nla_nest_start_noflag(reply, OVS_METER_ATTR_BANDS);
Andy Zhou96fbc132017-11-10 12:09:42 -0800284 if (!nla)
285 goto nla_put_failure;
286
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200287 band_nla = nla_nest_start_noflag(reply, OVS_BAND_ATTR_UNSPEC);
Andy Zhou96fbc132017-11-10 12:09:42 -0800288 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
299nla_put_failure:
300 nlmsg_free(reply);
301 err = -EMSGSIZE;
302 return err;
303}
304
305static 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. Silvac5c38992019-01-15 15:19:17 -0600323 meter = kzalloc(struct_size(meter, bands, n_bands), GFP_KERNEL);
Andy Zhou96fbc132017-11-10 12:09:42 -0800324 if (!meter)
325 return ERR_PTR(-ENOMEM);
326
Justin Pettit25432eb2018-07-28 15:26:01 -0700327 meter->id = nla_get_u32(a[OVS_METER_ATTR_ID]);
Andy Zhou96fbc132017-11-10 12:09:42 -0800328 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 Berg8cb08172019-04-26 14:07:28 +0200344 err = nla_parse_deprecated((struct nlattr **)&attr,
345 OVS_BAND_ATTR_MAX, nla_data(nla),
346 nla_len(nla), band_policy, NULL);
Andy Zhou96fbc132017-11-10 12:09:42 -0800347 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]);
zhanglipingddc502d2018-03-09 10:08:50 +0800359 if (band->rate == 0) {
360 err = -EINVAL;
361 goto exit_free_meter;
362 }
363
Andy Zhou96fbc132017-11-10 12:09:42 -0800364 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.
zhanglipingddc502d2018-03-09 10:08:50 +0800368 *
369 * Start with a full bucket.
Andy Zhou96fbc132017-11-10 12:09:42 -0800370 */
zhanglipingddc502d2018-03-09 10:08:50 +0800371 band->bucket = (band->burst_size + band->rate) * 1000;
372 band_max_delta_t = band->bucket / band->rate;
Andy Zhou96fbc132017-11-10 12:09:42 -0800373 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
380exit_free_meter:
381 kfree(meter);
382 return ERR_PTR(err);
383}
384
385static 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 Zhangc7c4c442020-04-24 08:08:02 +0800392 struct dp_meter_table *meter_tbl;
Andy Zhou96fbc132017-11-10 12:09:42 -0800393 struct datapath *dp;
394 int err;
395 u32 meter_id;
396 bool failed;
397
Justin Pettit25432eb2018-07-28 15:26:01 -0700398 if (!a[OVS_METER_ATTR_ID]) {
399 return -ENODEV;
400 }
401
Andy Zhou96fbc132017-11-10 12:09:42 -0800402 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 Zhangc7c4c442020-04-24 08:08:02 +0800420 meter_tbl = &dp->meter_tbl;
Andy Zhou96fbc132017-11-10 12:09:42 -0800421 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
422
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800423 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 Zhou96fbc132017-11-10 12:09:42 -0800432 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
453exit_unlock:
454 ovs_unlock();
455 nlmsg_free(reply);
456exit_free_meter:
457 kfree(meter);
458 return err;
459}
460
461static int ovs_meter_cmd_get(struct sk_buff *skb, struct genl_info *info)
462{
Andy Zhou96fbc132017-11-10 12:09:42 -0800463 struct ovs_header *ovs_header = info->userhdr;
464 struct ovs_header *ovs_reply_header;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800465 struct nlattr **a = info->attrs;
Andy Zhou96fbc132017-11-10 12:09:42 -0800466 struct dp_meter *meter;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800467 struct sk_buff *reply;
468 struct datapath *dp;
469 u32 meter_id;
470 int err;
Andy Zhou96fbc132017-11-10 12:09:42 -0800471
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 Zhangc7c4c442020-04-24 08:08:02 +0800491 meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800492 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
508exit_unlock:
509 ovs_unlock();
510 nlmsg_free(reply);
511 return err;
512}
513
514static int ovs_meter_cmd_del(struct sk_buff *skb, struct genl_info *info)
515{
Andy Zhou96fbc132017-11-10 12:09:42 -0800516 struct ovs_header *ovs_header = info->userhdr;
517 struct ovs_header *ovs_reply_header;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800518 struct nlattr **a = info->attrs;
Andy Zhou96fbc132017-11-10 12:09:42 -0800519 struct dp_meter *old_meter;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800520 struct sk_buff *reply;
521 struct datapath *dp;
522 u32 meter_id;
523 int err;
Andy Zhou96fbc132017-11-10 12:09:42 -0800524
525 if (!a[OVS_METER_ATTR_ID])
526 return -EINVAL;
Andy Zhou96fbc132017-11-10 12:09:42 -0800527
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 Zhangc7c4c442020-04-24 08:08:02 +0800541 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]);
542 old_meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800543 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 Zhangc7c4c442020-04-24 08:08:02 +0800548
549 err = detach_meter(&dp->meter_tbl, old_meter);
550 if (err)
551 goto exit_unlock;
Andy Zhou96fbc132017-11-10 12:09:42 -0800552 }
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800553
Andy Zhou96fbc132017-11-10 12:09:42 -0800554 ovs_unlock();
555 ovs_meter_free(old_meter);
556 genlmsg_end(reply, ovs_reply_header);
557 return genlmsg_reply(reply, info);
558
559exit_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 */
570bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb,
571 struct sw_flow_key *key, u32 meter_id)
572{
Andy Zhou96fbc132017-11-10 12:09:42 -0800573 long long int now_ms = div_u64(ktime_get_ns(), 1000 * 1000);
574 long long int long_delta_ms;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800575 struct dp_meter_band *band;
576 struct dp_meter *meter;
Andy Zhou96fbc132017-11-10 12:09:42 -0800577 int i, band_exceeded_max = -1;
578 u32 band_exceeded_rate = 0;
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800579 u32 delta_ms;
580 u32 cost;
Andy Zhou96fbc132017-11-10 12:09:42 -0800581
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800582 meter = lookup_meter(&dp->meter_tbl, meter_id);
Andy Zhou96fbc132017-11-10 12:09:42 -0800583 /* 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. Silva5b7789e2018-01-30 22:55:33 -0600621 max_bucket_size = (band->burst_size + band->rate) * 1000LL;
Andy Zhou96fbc132017-11-10 12:09:42 -0800622
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
652static struct genl_ops dp_meter_genl_ops[] = {
653 { .cmd = OVS_METER_CMD_FEATURES,
Johannes Bergef6243a2019-04-26 14:07:31 +0200654 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800655 .flags = 0, /* OK for unprivileged users. */
Andy Zhou96fbc132017-11-10 12:09:42 -0800656 .doit = ovs_meter_cmd_features
657 },
658 { .cmd = OVS_METER_CMD_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200659 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800660 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
661 * privilege.
662 */
Andy Zhou96fbc132017-11-10 12:09:42 -0800663 .doit = ovs_meter_cmd_set,
664 },
665 { .cmd = OVS_METER_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200666 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800667 .flags = 0, /* OK for unprivileged users. */
Andy Zhou96fbc132017-11-10 12:09:42 -0800668 .doit = ovs_meter_cmd_get,
669 },
670 { .cmd = OVS_METER_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200671 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Andy Zhou96fbc132017-11-10 12:09:42 -0800672 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
673 * privilege.
674 */
Andy Zhou96fbc132017-11-10 12:09:42 -0800675 .doit = ovs_meter_cmd_del
676 },
677};
678
679static const struct genl_multicast_group ovs_meter_multicast_group = {
680 .name = OVS_METER_MCGROUP,
681};
682
683struct 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 Berg3b0f31f2019-03-21 22:51:02 +0100688 .policy = meter_policy,
Andy Zhou96fbc132017-11-10 12:09:42 -0800689 .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
698int ovs_meters_init(struct datapath *dp)
699{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800700 struct dp_meter_table *tbl = &dp->meter_tbl;
701 struct dp_meter_instance *ti;
Andy Zhou96fbc132017-11-10 12:09:42 -0800702
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800703 ti = dp_meter_instance_alloc(DP_METER_ARRAY_SIZE_MIN);
704 if (!ti)
Andy Zhou96fbc132017-11-10 12:09:42 -0800705 return -ENOMEM;
706
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800707 rcu_assign_pointer(tbl->ti, ti);
708 tbl->count = 0;
Andy Zhou96fbc132017-11-10 12:09:42 -0800709
710 return 0;
711}
712
713void ovs_meters_exit(struct datapath *dp)
714{
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800715 struct dp_meter_table *tbl = &dp->meter_tbl;
716 struct dp_meter_instance *ti = rcu_dereference_raw(tbl->ti);
Andy Zhou96fbc132017-11-10 12:09:42 -0800717 int i;
718
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800719 for (i = 0; i < ti->n_meters; i++)
720 ovs_meter_free(ti->dp_meters[i]);
Andy Zhou96fbc132017-11-10 12:09:42 -0800721
Tonghao Zhangc7c4c442020-04-24 08:08:02 +0800722 dp_meter_instance_free(ti);
Andy Zhou96fbc132017-11-10 12:09:42 -0800723}