2 * Copyright (c) 2017 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/skbuff.h>
14 #include <linux/kernel.h>
15 #include <linux/openvswitch.h>
16 #include <linux/netlink.h>
17 #include <linux/rculist.h>
19 #include <net/netlink.h>
20 #include <net/genetlink.h>
25 #define METER_HASH_BUCKETS 1024
27 static const struct nla_policy meter_policy
[OVS_METER_ATTR_MAX
+ 1] = {
28 [OVS_METER_ATTR_ID
] = { .type
= NLA_U32
, },
29 [OVS_METER_ATTR_KBPS
] = { .type
= NLA_FLAG
},
30 [OVS_METER_ATTR_STATS
] = { .len
= sizeof(struct ovs_flow_stats
) },
31 [OVS_METER_ATTR_BANDS
] = { .type
= NLA_NESTED
},
32 [OVS_METER_ATTR_USED
] = { .type
= NLA_U64
},
33 [OVS_METER_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
34 [OVS_METER_ATTR_MAX_METERS
] = { .type
= NLA_U32
},
35 [OVS_METER_ATTR_MAX_BANDS
] = { .type
= NLA_U32
},
38 static const struct nla_policy band_policy
[OVS_BAND_ATTR_MAX
+ 1] = {
39 [OVS_BAND_ATTR_TYPE
] = { .type
= NLA_U32
, },
40 [OVS_BAND_ATTR_RATE
] = { .type
= NLA_U32
, },
41 [OVS_BAND_ATTR_BURST
] = { .type
= NLA_U32
, },
42 [OVS_BAND_ATTR_STATS
] = { .len
= sizeof(struct ovs_flow_stats
) },
45 static void ovs_meter_free(struct dp_meter
*meter
)
50 kfree_rcu(meter
, rcu
);
53 static struct hlist_head
*meter_hash_bucket(const struct datapath
*dp
,
56 return &dp
->meters
[meter_id
& (METER_HASH_BUCKETS
- 1)];
59 /* Call with ovs_mutex or RCU read lock. */
60 static struct dp_meter
*lookup_meter(const struct datapath
*dp
,
63 struct dp_meter
*meter
;
64 struct hlist_head
*head
;
66 head
= meter_hash_bucket(dp
, meter_id
);
67 hlist_for_each_entry_rcu(meter
, head
, dp_hash_node
) {
68 if (meter
->id
== meter_id
)
74 static void attach_meter(struct datapath
*dp
, struct dp_meter
*meter
)
76 struct hlist_head
*head
= meter_hash_bucket(dp
, meter
->id
);
78 hlist_add_head_rcu(&meter
->dp_hash_node
, head
);
81 static void detach_meter(struct dp_meter
*meter
)
85 hlist_del_rcu(&meter
->dp_hash_node
);
88 static struct sk_buff
*
89 ovs_meter_cmd_reply_start(struct genl_info
*info
, u8 cmd
,
90 struct ovs_header
**ovs_reply_header
)
93 struct ovs_header
*ovs_header
= info
->userhdr
;
95 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
97 return ERR_PTR(-ENOMEM
);
99 *ovs_reply_header
= genlmsg_put(skb
, info
->snd_portid
,
101 &dp_meter_genl_family
, 0, cmd
);
102 if (!*ovs_reply_header
) {
104 return ERR_PTR(-EMSGSIZE
);
106 (*ovs_reply_header
)->dp_ifindex
= ovs_header
->dp_ifindex
;
111 static int ovs_meter_cmd_reply_stats(struct sk_buff
*reply
, u32 meter_id
,
112 struct dp_meter
*meter
)
115 struct dp_meter_band
*band
;
118 if (nla_put_u32(reply
, OVS_METER_ATTR_ID
, meter_id
))
124 if (nla_put(reply
, OVS_METER_ATTR_STATS
,
125 sizeof(struct ovs_flow_stats
), &meter
->stats
) ||
126 nla_put_u64_64bit(reply
, OVS_METER_ATTR_USED
, meter
->used
,
130 nla
= nla_nest_start(reply
, OVS_METER_ATTR_BANDS
);
136 for (i
= 0; i
< meter
->n_bands
; ++i
, ++band
) {
137 struct nlattr
*band_nla
;
139 band_nla
= nla_nest_start(reply
, OVS_BAND_ATTR_UNSPEC
);
140 if (!band_nla
|| nla_put(reply
, OVS_BAND_ATTR_STATS
,
141 sizeof(struct ovs_flow_stats
),
144 nla_nest_end(reply
, band_nla
);
146 nla_nest_end(reply
, nla
);
153 static int ovs_meter_cmd_features(struct sk_buff
*skb
, struct genl_info
*info
)
155 struct sk_buff
*reply
;
156 struct ovs_header
*ovs_reply_header
;
157 struct nlattr
*nla
, *band_nla
;
160 reply
= ovs_meter_cmd_reply_start(info
, OVS_METER_CMD_FEATURES
,
163 return PTR_ERR(reply
);
165 if (nla_put_u32(reply
, OVS_METER_ATTR_MAX_METERS
, U32_MAX
) ||
166 nla_put_u32(reply
, OVS_METER_ATTR_MAX_BANDS
, DP_MAX_BANDS
))
167 goto nla_put_failure
;
169 nla
= nla_nest_start(reply
, OVS_METER_ATTR_BANDS
);
171 goto nla_put_failure
;
173 band_nla
= nla_nest_start(reply
, OVS_BAND_ATTR_UNSPEC
);
175 goto nla_put_failure
;
176 /* Currently only DROP band type is supported. */
177 if (nla_put_u32(reply
, OVS_BAND_ATTR_TYPE
, OVS_METER_BAND_TYPE_DROP
))
178 goto nla_put_failure
;
179 nla_nest_end(reply
, band_nla
);
180 nla_nest_end(reply
, nla
);
182 genlmsg_end(reply
, ovs_reply_header
);
183 return genlmsg_reply(reply
, info
);
191 static struct dp_meter
*dp_meter_create(struct nlattr
**a
)
196 struct dp_meter
*meter
;
197 struct dp_meter_band
*band
;
200 /* Validate attributes, count the bands. */
201 if (!a
[OVS_METER_ATTR_BANDS
])
202 return ERR_PTR(-EINVAL
);
204 nla_for_each_nested(nla
, a
[OVS_METER_ATTR_BANDS
], rem
)
205 if (++n_bands
> DP_MAX_BANDS
)
206 return ERR_PTR(-EINVAL
);
208 /* Allocate and set up the meter before locking anything. */
209 meter
= kzalloc(n_bands
* sizeof(struct dp_meter_band
) +
210 sizeof(*meter
), GFP_KERNEL
);
212 return ERR_PTR(-ENOMEM
);
214 meter
->used
= div_u64(ktime_get_ns(), 1000 * 1000);
215 meter
->kbps
= a
[OVS_METER_ATTR_KBPS
] ? 1 : 0;
216 meter
->keep_stats
= !a
[OVS_METER_ATTR_CLEAR
];
217 spin_lock_init(&meter
->lock
);
218 if (meter
->keep_stats
&& a
[OVS_METER_ATTR_STATS
]) {
219 meter
->stats
= *(struct ovs_flow_stats
*)
220 nla_data(a
[OVS_METER_ATTR_STATS
]);
222 meter
->n_bands
= n_bands
;
224 /* Set up meter bands. */
226 nla_for_each_nested(nla
, a
[OVS_METER_ATTR_BANDS
], rem
) {
227 struct nlattr
*attr
[OVS_BAND_ATTR_MAX
+ 1];
228 u32 band_max_delta_t
;
230 err
= nla_parse((struct nlattr
**)&attr
, OVS_BAND_ATTR_MAX
,
231 nla_data(nla
), nla_len(nla
), band_policy
,
234 goto exit_free_meter
;
236 if (!attr
[OVS_BAND_ATTR_TYPE
] ||
237 !attr
[OVS_BAND_ATTR_RATE
] ||
238 !attr
[OVS_BAND_ATTR_BURST
]) {
240 goto exit_free_meter
;
243 band
->type
= nla_get_u32(attr
[OVS_BAND_ATTR_TYPE
]);
244 band
->rate
= nla_get_u32(attr
[OVS_BAND_ATTR_RATE
]);
245 band
->burst_size
= nla_get_u32(attr
[OVS_BAND_ATTR_BURST
]);
246 /* Figure out max delta_t that is enough to fill any bucket.
247 * Keep max_delta_t size to the bucket units:
248 * pkts => 1/1000 packets, kilobits => bits.
250 band_max_delta_t
= (band
->burst_size
+ band
->rate
) * 1000;
251 /* Start with a full bucket. */
252 band
->bucket
= band_max_delta_t
;
253 if (band_max_delta_t
> meter
->max_delta_t
)
254 meter
->max_delta_t
= band_max_delta_t
;
265 static int ovs_meter_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
267 struct nlattr
**a
= info
->attrs
;
268 struct dp_meter
*meter
, *old_meter
;
269 struct sk_buff
*reply
;
270 struct ovs_header
*ovs_reply_header
;
271 struct ovs_header
*ovs_header
= info
->userhdr
;
277 meter
= dp_meter_create(a
);
278 if (IS_ERR_OR_NULL(meter
))
279 return PTR_ERR(meter
);
281 reply
= ovs_meter_cmd_reply_start(info
, OVS_METER_CMD_SET
,
284 err
= PTR_ERR(reply
);
285 goto exit_free_meter
;
289 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
295 if (!a
[OVS_METER_ATTR_ID
]) {
300 meter_id
= nla_get_u32(a
[OVS_METER_ATTR_ID
]);
302 /* Cannot fail after this. */
303 old_meter
= lookup_meter(dp
, meter_id
);
304 detach_meter(old_meter
);
305 attach_meter(dp
, meter
);
308 /* Build response with the meter_id and stats from
309 * the old meter, if any.
311 failed
= nla_put_u32(reply
, OVS_METER_ATTR_ID
, meter_id
);
314 spin_lock_bh(&old_meter
->lock
);
315 if (old_meter
->keep_stats
) {
316 err
= ovs_meter_cmd_reply_stats(reply
, meter_id
,
320 spin_unlock_bh(&old_meter
->lock
);
321 ovs_meter_free(old_meter
);
324 genlmsg_end(reply
, ovs_reply_header
);
325 return genlmsg_reply(reply
, info
);
335 static int ovs_meter_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
337 struct nlattr
**a
= info
->attrs
;
339 struct ovs_header
*ovs_header
= info
->userhdr
;
340 struct ovs_header
*ovs_reply_header
;
343 struct sk_buff
*reply
;
344 struct dp_meter
*meter
;
346 if (!a
[OVS_METER_ATTR_ID
])
349 meter_id
= nla_get_u32(a
[OVS_METER_ATTR_ID
]);
351 reply
= ovs_meter_cmd_reply_start(info
, OVS_METER_CMD_GET
,
354 return PTR_ERR(reply
);
358 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
364 /* Locate meter, copy stats. */
365 meter
= lookup_meter(dp
, meter_id
);
371 spin_lock_bh(&meter
->lock
);
372 err
= ovs_meter_cmd_reply_stats(reply
, meter_id
, meter
);
373 spin_unlock_bh(&meter
->lock
);
379 genlmsg_end(reply
, ovs_reply_header
);
380 return genlmsg_reply(reply
, info
);
388 static int ovs_meter_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
390 struct nlattr
**a
= info
->attrs
;
392 struct ovs_header
*ovs_header
= info
->userhdr
;
393 struct ovs_header
*ovs_reply_header
;
396 struct sk_buff
*reply
;
397 struct dp_meter
*old_meter
;
399 if (!a
[OVS_METER_ATTR_ID
])
401 meter_id
= nla_get_u32(a
[OVS_METER_ATTR_ID
]);
403 reply
= ovs_meter_cmd_reply_start(info
, OVS_METER_CMD_DEL
,
406 return PTR_ERR(reply
);
410 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
416 old_meter
= lookup_meter(dp
, meter_id
);
418 spin_lock_bh(&old_meter
->lock
);
419 err
= ovs_meter_cmd_reply_stats(reply
, meter_id
, old_meter
);
421 spin_unlock_bh(&old_meter
->lock
);
422 detach_meter(old_meter
);
425 ovs_meter_free(old_meter
);
426 genlmsg_end(reply
, ovs_reply_header
);
427 return genlmsg_reply(reply
, info
);
435 /* Meter action execution.
437 * Return true 'meter_id' drop band is triggered. The 'skb' should be
438 * dropped by the caller'.
440 bool ovs_meter_execute(struct datapath
*dp
, struct sk_buff
*skb
,
441 struct sw_flow_key
*key
, u32 meter_id
)
443 struct dp_meter
*meter
;
444 struct dp_meter_band
*band
;
445 long long int now_ms
= div_u64(ktime_get_ns(), 1000 * 1000);
446 long long int long_delta_ms
;
449 int i
, band_exceeded_max
= -1;
450 u32 band_exceeded_rate
= 0;
452 meter
= lookup_meter(dp
, meter_id
);
453 /* Do not drop the packet when there is no meter. */
457 /* Lock the meter while using it. */
458 spin_lock(&meter
->lock
);
460 long_delta_ms
= (now_ms
- meter
->used
); /* ms */
462 /* Make sure delta_ms will not be too large, so that bucket will not
465 delta_ms
= (long_delta_ms
> (long long int)meter
->max_delta_t
)
466 ? meter
->max_delta_t
: (u32
)long_delta_ms
;
468 /* Update meter statistics.
470 meter
->used
= now_ms
;
471 meter
->stats
.n_packets
+= 1;
472 meter
->stats
.n_bytes
+= skb
->len
;
474 /* Bucket rate is either in kilobits per second, or in packets per
475 * second. We maintain the bucket in the units of either bits or
476 * 1/1000th of a packet, correspondingly.
477 * Then, when rate is multiplied with milliseconds, we get the
479 * msec * kbps = bits, and
480 * msec * packets/sec = 1/1000 packets.
482 * 'cost' is the number of bucket units in this packet.
484 cost
= (meter
->kbps
) ? skb
->len
* 8 : 1000;
486 /* Update all bands and find the one hit with the highest rate. */
487 for (i
= 0; i
< meter
->n_bands
; ++i
) {
488 long long int max_bucket_size
;
490 band
= &meter
->bands
[i
];
491 max_bucket_size
= (band
->burst_size
+ band
->rate
) * 1000LL;
493 band
->bucket
+= delta_ms
* band
->rate
;
494 if (band
->bucket
> max_bucket_size
)
495 band
->bucket
= max_bucket_size
;
497 if (band
->bucket
>= cost
) {
498 band
->bucket
-= cost
;
499 } else if (band
->rate
> band_exceeded_rate
) {
500 band_exceeded_rate
= band
->rate
;
501 band_exceeded_max
= i
;
505 if (band_exceeded_max
>= 0) {
506 /* Update band statistics. */
507 band
= &meter
->bands
[band_exceeded_max
];
508 band
->stats
.n_packets
+= 1;
509 band
->stats
.n_bytes
+= skb
->len
;
511 /* Drop band triggered, let the caller drop the 'skb'. */
512 if (band
->type
== OVS_METER_BAND_TYPE_DROP
) {
513 spin_unlock(&meter
->lock
);
518 spin_unlock(&meter
->lock
);
522 static struct genl_ops dp_meter_genl_ops
[] = {
523 { .cmd
= OVS_METER_CMD_FEATURES
,
524 .flags
= 0, /* OK for unprivileged users. */
525 .policy
= meter_policy
,
526 .doit
= ovs_meter_cmd_features
528 { .cmd
= OVS_METER_CMD_SET
,
529 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
532 .policy
= meter_policy
,
533 .doit
= ovs_meter_cmd_set
,
535 { .cmd
= OVS_METER_CMD_GET
,
536 .flags
= 0, /* OK for unprivileged users. */
537 .policy
= meter_policy
,
538 .doit
= ovs_meter_cmd_get
,
540 { .cmd
= OVS_METER_CMD_DEL
,
541 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN
544 .policy
= meter_policy
,
545 .doit
= ovs_meter_cmd_del
549 static const struct genl_multicast_group ovs_meter_multicast_group
= {
550 .name
= OVS_METER_MCGROUP
,
553 struct genl_family dp_meter_genl_family __ro_after_init
= {
554 .hdrsize
= sizeof(struct ovs_header
),
555 .name
= OVS_METER_FAMILY
,
556 .version
= OVS_METER_VERSION
,
557 .maxattr
= OVS_METER_ATTR_MAX
,
559 .parallel_ops
= true,
560 .ops
= dp_meter_genl_ops
,
561 .n_ops
= ARRAY_SIZE(dp_meter_genl_ops
),
562 .mcgrps
= &ovs_meter_multicast_group
,
564 .module
= THIS_MODULE
,
567 int ovs_meters_init(struct datapath
*dp
)
571 dp
->meters
= kmalloc_array(METER_HASH_BUCKETS
,
572 sizeof(struct hlist_head
), GFP_KERNEL
);
577 for (i
= 0; i
< METER_HASH_BUCKETS
; i
++)
578 INIT_HLIST_HEAD(&dp
->meters
[i
]);
583 void ovs_meters_exit(struct datapath
*dp
)
587 for (i
= 0; i
< METER_HASH_BUCKETS
; i
++) {
588 struct hlist_head
*head
= &dp
->meters
[i
];
589 struct dp_meter
*meter
;
590 struct hlist_node
*n
;
592 hlist_for_each_entry_safe(meter
, n
, head
, dp_hash_node
)