2 * wprobe-core.c: Wireless probe interface core
3 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/rcupdate.h>
22 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
23 #include <linux/rculist.h>
25 #include <linux/list.h>
27 #include <linux/skbuff.h>
28 #include <linux/wprobe.h>
29 #include <linux/math64.h>
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
34 #define list_for_each_rcu __list_for_each_rcu
37 #define WPROBE_MIN_INTERVAL 100 /* minimum measurement interval in msecs */
39 static struct list_head wprobe_if
;
40 static spinlock_t wprobe_lock
;
42 static struct genl_family wprobe_fam
= {
43 .id
= GENL_ID_GENERATE
,
47 /* only the first set of attributes is used for queries */
48 .maxattr
= WPROBE_ATTR_LAST
,
51 static void wprobe_update_stats(struct wprobe_iface
*dev
, struct wprobe_link
*l
);
52 static int wprobe_sync_data(struct wprobe_iface
*dev
, struct wprobe_link
*l
, bool query
);
55 wprobe_add_link(struct wprobe_iface
*s
, struct wprobe_link
*l
, const char *addr
)
59 INIT_LIST_HEAD(&l
->list
);
60 l
->val
= kzalloc(sizeof(struct wprobe_value
) * s
->n_link_items
, GFP_ATOMIC
);
65 memcpy(&l
->addr
, addr
, ETH_ALEN
);
66 spin_lock_irqsave(&wprobe_lock
, flags
);
67 list_add_tail_rcu(&l
->list
, &s
->links
);
68 spin_unlock_irqrestore(&wprobe_lock
, flags
);
72 EXPORT_SYMBOL(wprobe_add_link
);
75 wprobe_remove_link(struct wprobe_iface
*s
, struct wprobe_link
*l
)
79 spin_lock_irqsave(&wprobe_lock
, flags
);
80 list_del_rcu(&l
->list
);
81 spin_unlock_irqrestore(&wprobe_lock
, flags
);
85 EXPORT_SYMBOL(wprobe_remove_link
);
88 wprobe_measure_timer(unsigned long data
)
90 struct wprobe_iface
*dev
= (struct wprobe_iface
*) data
;
92 /* set next measurement interval */
93 mod_timer(&dev
->measure_timer
, jiffies
+
94 msecs_to_jiffies(dev
->measure_interval
));
96 /* perform measurement */
97 wprobe_sync_data(dev
, NULL
, false);
101 wprobe_add_iface(struct wprobe_iface
*s
)
106 /* reset only wprobe private area */
107 memset(&s
->list
, 0, sizeof(struct wprobe_iface
) - offsetof(struct wprobe_iface
, list
));
110 INIT_LIST_HEAD(&s
->list
);
111 INIT_LIST_HEAD(&s
->links
);
112 setup_timer(&s
->measure_timer
, wprobe_measure_timer
, (unsigned long) s
);
114 vsize
= max(s
->n_link_items
, s
->n_global_items
);
115 s
->val
= kzalloc(sizeof(struct wprobe_value
) * vsize
, GFP_ATOMIC
);
119 s
->query_val
= kzalloc(sizeof(struct wprobe_value
) * vsize
, GFP_ATOMIC
);
123 /* initialize defaults to be able to handle overflow,
124 * user space will need to handle this if it keeps an
125 * internal histogram */
127 s
->scale_max
= (1 << 31);
132 spin_lock_irqsave(&wprobe_lock
, flags
);
133 list_add_rcu(&s
->list
, &wprobe_if
);
134 spin_unlock_irqrestore(&wprobe_lock
, flags
);
143 EXPORT_SYMBOL(wprobe_add_iface
);
146 wprobe_remove_iface(struct wprobe_iface
*s
)
150 BUG_ON(!list_empty(&s
->links
));
152 del_timer_sync(&s
->measure_timer
);
153 spin_lock_irqsave(&wprobe_lock
, flags
);
154 list_del_rcu(&s
->list
);
155 spin_unlock_irqrestore(&wprobe_lock
, flags
);
157 /* wait for all queries to finish before freeing the
158 * temporary value storage buffer */
164 EXPORT_SYMBOL(wprobe_remove_iface
);
166 static struct wprobe_iface
*
167 wprobe_get_dev(struct nlattr
*attr
)
169 struct wprobe_iface
*dev
= NULL
;
170 struct wprobe_iface
*p
;
177 name
= nla_data(attr
);
178 list_for_each_entry_rcu(p
, &wprobe_if
, list
) {
180 if (strcmp(name
, p
->name
) != 0)
191 wprobe_sync_data(struct wprobe_iface
*dev
, struct wprobe_link
*l
, bool query
)
193 struct wprobe_value
*val
;
198 n
= dev
->n_link_items
;
201 n
= dev
->n_global_items
;
205 spin_lock_irqsave(&dev
->lock
, flags
);
206 err
= dev
->sync_data(dev
, l
, val
, !query
);
211 memcpy(dev
->query_val
, val
, sizeof(struct wprobe_value
) * n
);
213 wprobe_update_stats(dev
, l
);
215 spin_unlock_irqrestore(&dev
->lock
, flags
);
218 EXPORT_SYMBOL(wprobe_sync_data
);
221 wprobe_scale_stats(struct wprobe_iface
*dev
, const struct wprobe_item
*item
,
222 struct wprobe_value
*val
, int n
)
224 u64 scale_ts
= jiffies_64
;
227 for (i
= 0; i
< n
; i
++) {
228 if (!(item
[i
].flags
& WPROBE_F_KEEPSTAT
))
231 if (val
[i
].n
<= dev
->scale_min
)
234 /* FIXME: div_s64 seems to be very imprecise here, even when
235 * the values are scaled up */
236 val
[i
].s
*= dev
->scale_m
;
237 val
[i
].s
= div_s64(val
[i
].s
, dev
->scale_d
);
239 val
[i
].ss
*= dev
->scale_m
;
240 val
[i
].ss
= div_s64(val
[i
].ss
, dev
->scale_d
);
242 val
[i
].n
= (val
[i
].n
* dev
->scale_m
) / dev
->scale_d
;
243 val
[i
].scale_timestamp
= scale_ts
;
249 wprobe_update_stats(struct wprobe_iface
*dev
, struct wprobe_link
*l
)
251 const struct wprobe_item
*item
;
252 struct wprobe_value
*val
;
253 bool scale_stats
= false;
257 n
= dev
->n_link_items
;
258 item
= dev
->link_items
;
261 n
= dev
->n_global_items
;
262 item
= dev
->global_items
;
266 /* process statistics */
267 for (i
= 0; i
< n
; i
++) {
274 if ((item
[i
].flags
& WPROBE_F_KEEPSTAT
) &&
275 (dev
->scale_max
> 0) && (val
[i
].n
> dev
->scale_max
)) {
279 switch(item
[i
].type
) {
310 val
[i
].pending
= false;
313 wprobe_scale_stats(dev
, item
, val
, n
);
315 EXPORT_SYMBOL(wprobe_update_stats
);
317 static const struct nla_policy wprobe_policy
[WPROBE_ATTR_LAST
+1] = {
318 [WPROBE_ATTR_INTERFACE
] = { .type
= NLA_NUL_STRING
},
319 [WPROBE_ATTR_MAC
] = { .type
= NLA_STRING
},
320 [WPROBE_ATTR_FLAGS
] = { .type
= NLA_U32
},
323 [WPROBE_ATTR_INTERVAL
] = { .type
= NLA_MSECS
},
324 [WPROBE_ATTR_SAMPLES_MIN
] = { .type
= NLA_U32
},
325 [WPROBE_ATTR_SAMPLES_MAX
] = { .type
= NLA_U32
},
326 [WPROBE_ATTR_SAMPLES_SCALE_M
] = { .type
= NLA_U32
},
327 [WPROBE_ATTR_SAMPLES_SCALE_D
] = { .type
= NLA_U32
},
331 wprobe_check_ptr(struct list_head
*list
, struct list_head
*ptr
)
335 list_for_each_rcu(p
, list
) {
343 wprobe_send_item_value(struct sk_buff
*msg
, struct netlink_callback
*cb
,
344 struct wprobe_iface
*dev
, struct wprobe_link
*l
,
345 const struct wprobe_item
*item
,
348 struct genlmsghdr
*hdr
;
349 struct wprobe_value
*val
= dev
->query_val
;
350 u64 time
= val
[i
].last
- val
[i
].first
;
352 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
353 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_INFO
);
355 NLA_PUT_U32(msg
, WPROBE_ATTR_ID
, i
);
356 NLA_PUT_U32(msg
, WPROBE_ATTR_FLAGS
, flags
);
357 NLA_PUT_U8(msg
, WPROBE_ATTR_TYPE
, item
[i
].type
);
358 NLA_PUT_U64(msg
, WPROBE_ATTR_DURATION
, time
);
360 switch(item
[i
].type
) {
363 NLA_PUT_U8(msg
, item
[i
].type
, val
[i
].U8
);
367 NLA_PUT_U16(msg
, item
[i
].type
, val
[i
].U16
);
371 NLA_PUT_U32(msg
, item
[i
].type
, val
[i
].U32
);
375 NLA_PUT_U64(msg
, item
[i
].type
, val
[i
].U64
);
377 case WPROBE_VAL_STRING
:
379 NLA_PUT_STRING(msg
, item
[i
].type
, val
[i
].STRING
);
381 NLA_PUT_STRING(msg
, item
[i
].type
, "");
382 /* bypass avg/stdev */
385 /* skip unknown values */
388 if (item
[i
].flags
& WPROBE_F_KEEPSTAT
) {
389 NLA_PUT_U64(msg
, WPROBE_VAL_SUM
, val
[i
].s
);
390 NLA_PUT_U64(msg
, WPROBE_VAL_SUM_SQ
, val
[i
].ss
);
391 NLA_PUT_U32(msg
, WPROBE_VAL_SAMPLES
, (u32
) val
[i
].n
);
392 NLA_PUT_MSECS(msg
, WPROBE_VAL_SCALE_TIME
, val
[i
].scale_timestamp
);
395 genlmsg_end(msg
, hdr
);
399 genlmsg_cancel(msg
, hdr
);
404 wprobe_send_item_info(struct sk_buff
*msg
, struct netlink_callback
*cb
,
405 struct wprobe_iface
*dev
,
406 const struct wprobe_item
*item
, int i
)
408 struct genlmsghdr
*hdr
;
410 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
411 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_LIST
);
413 if ((i
== 0) && (dev
->addr
!= NULL
))
414 NLA_PUT(msg
, WPROBE_ATTR_MAC
, 6, dev
->addr
);
415 NLA_PUT_U32(msg
, WPROBE_ATTR_ID
, (u32
) i
);
416 NLA_PUT_STRING(msg
, WPROBE_ATTR_NAME
, item
[i
].name
);
417 NLA_PUT_U8(msg
, WPROBE_ATTR_TYPE
, item
[i
].type
);
418 NLA_PUT_U32(msg
, WPROBE_ATTR_FLAGS
, item
[i
].flags
);
419 genlmsg_end(msg
, hdr
);
423 genlmsg_cancel(msg
, hdr
);
428 static struct wprobe_link
*
429 wprobe_find_link(struct wprobe_iface
*dev
, const char *mac
)
431 struct wprobe_link
*l
;
433 list_for_each_entry_rcu(l
, &dev
->links
, list
) {
434 if (!memcmp(l
->addr
, mac
, 6))
441 wprobe_dump_link(struct sk_buff
*msg
, struct wprobe_link
*l
, struct netlink_callback
*cb
)
443 struct genlmsghdr
*hdr
;
445 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
446 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_LINKS
);
450 NLA_PUT(msg
, WPROBE_ATTR_MAC
, 6, l
->addr
);
451 genlmsg_end(msg
, hdr
);
455 genlmsg_cancel(msg
, hdr
);
460 wprobe_dump_links(struct sk_buff
*skb
, struct netlink_callback
*cb
)
462 struct wprobe_iface
*dev
= (struct wprobe_iface
*)cb
->args
[0];
463 struct wprobe_link
*l
;
468 err
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ wprobe_fam
.hdrsize
,
469 wprobe_fam
.attrbuf
, wprobe_fam
.maxattr
, wprobe_policy
);
473 dev
= wprobe_get_dev(wprobe_fam
.attrbuf
[WPROBE_ATTR_INTERFACE
]);
479 cb
->args
[0] = (long) dev
;
481 if (!wprobe_check_ptr(&wprobe_if
, &dev
->list
)) {
488 list_for_each_entry_rcu(l
, &dev
->links
, list
) {
492 if (unlikely(!wprobe_dump_link(skb
, l
, cb
)))
504 #define WPROBE_F_LINK (1 << 31) /* for internal use */
506 wprobe_dump_info(struct sk_buff
*skb
, struct netlink_callback
*cb
)
508 struct wprobe_iface
*dev
= (struct wprobe_iface
*)cb
->args
[0];
509 struct wprobe_link
*l
= (struct wprobe_link
*)cb
->args
[1];
510 struct wprobe_value
*val
;
511 const struct wprobe_item
*item
;
512 struct genlmsghdr
*hdr
;
514 int cmd
, n
, i
= cb
->args
[3];
515 u32 vflags
= cb
->args
[2];
518 hdr
= (struct genlmsghdr
*)nlmsg_data(cb
->nlh
);
521 /* since the attribute value list might be too big for a single netlink
522 * message, the device, link and offset get stored in the netlink callback.
523 * if this is the first request, we need to do the full lookup for the device.
525 * access to the device and link structure is synchronized through rcu.
529 err
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ wprobe_fam
.hdrsize
,
530 wprobe_fam
.attrbuf
, wprobe_fam
.maxattr
, wprobe_policy
);
535 dev
= wprobe_get_dev(wprobe_fam
.attrbuf
[WPROBE_ATTR_INTERFACE
]);
539 if (cmd
== WPROBE_CMD_GET_INFO
) {
540 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]) {
541 l
= wprobe_find_link(dev
, nla_data(wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]));
549 item
= dev
->link_items
;
550 n
= dev
->n_link_items
;
553 item
= dev
->global_items
;
554 n
= dev
->n_global_items
;
558 /* sync data and move to temp storage for the query */
559 spin_lock_irqsave(&dev
->lock
, flags
);
560 err
= wprobe_sync_data(dev
, l
, true);
562 memcpy(dev
->query_val
, val
, n
* sizeof(struct wprobe_value
));
563 spin_unlock_irqrestore(&dev
->lock
, flags
);
569 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_FLAGS
])
570 vflags
|= nla_get_u32(wprobe_fam
.attrbuf
[WPROBE_ATTR_FLAGS
]);
572 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
])
573 vflags
|= WPROBE_F_LINK
;
575 cb
->args
[0] = (long) dev
;
576 cb
->args
[1] = (long) l
;
577 cb
->args
[2] = vflags
;
580 /* when pulling pointers from the callback, validate them
581 * against the list using rcu to make sure that we won't
582 * dereference pointers to free'd memory after the last
585 if (!wprobe_check_ptr(&wprobe_if
, &dev
->list
))
588 if (l
&& !wprobe_check_ptr(&dev
->links
, &l
->list
))
592 if (vflags
& WPROBE_F_LINK
) {
593 item
= dev
->link_items
;
594 n
= dev
->n_link_items
;
596 item
= dev
->global_items
;
597 n
= dev
->n_global_items
;
602 case WPROBE_CMD_GET_INFO
:
604 if (!wprobe_send_item_value(skb
, cb
, dev
, l
, item
, i
, vflags
))
609 case WPROBE_CMD_GET_LIST
:
611 if (!wprobe_send_item_info(skb
, cb
, dev
, item
, i
))
630 wprobe_update_auto_measurement(struct wprobe_iface
*dev
, u32 interval
)
632 if (interval
&& (interval
< WPROBE_MIN_INTERVAL
))
635 if (!interval
&& dev
->measure_interval
)
636 del_timer_sync(&dev
->measure_timer
);
638 dev
->measure_interval
= interval
;
642 /* kick of a new measurement immediately */
643 mod_timer(&dev
->measure_timer
, jiffies
+ 1);
649 wprobe_measure(struct sk_buff
*skb
, struct genl_info
*info
)
651 struct wprobe_iface
*dev
;
652 struct wprobe_link
*l
= NULL
;
656 dev
= wprobe_get_dev(info
->attrs
[WPROBE_ATTR_INTERFACE
]);
660 if (info
->attrs
[WPROBE_ATTR_MAC
]) {
661 l
= wprobe_find_link(dev
, nla_data(wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]));
666 err
= wprobe_sync_data(dev
, l
, false);
674 wprobe_set_config(struct sk_buff
*skb
, struct genl_info
*info
)
676 struct wprobe_iface
*dev
;
679 u32 scale_min
, scale_max
;
680 u32 scale_m
, scale_d
;
683 dev
= wprobe_get_dev(info
->attrs
[WPROBE_ATTR_INTERFACE
]);
688 spin_lock_irqsave(&dev
->lock
, flags
);
689 if (info
->attrs
[WPROBE_ATTR_MAC
]) {
690 /* not supported yet */
694 if (info
->attrs
[WPROBE_ATTR_SAMPLES_MIN
] ||
695 info
->attrs
[WPROBE_ATTR_SAMPLES_MAX
]) {
696 if (info
->attrs
[WPROBE_ATTR_SAMPLES_MIN
])
697 scale_min
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_MIN
]);
699 scale_min
= dev
->scale_min
;
701 if (info
->attrs
[WPROBE_ATTR_SAMPLES_MAX
])
702 scale_max
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_MAX
]);
704 scale_max
= dev
->scale_max
;
706 if ((!scale_min
&& !scale_max
) ||
707 (scale_min
&& scale_max
&& (scale_min
< scale_max
))) {
708 dev
->scale_min
= scale_min
;
709 dev
->scale_max
= scale_max
;
715 if (info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_M
] &&
716 info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_D
]) {
718 scale_m
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_M
]);
719 scale_d
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_D
]);
721 if (!scale_d
|| (scale_m
> scale_d
))
724 dev
->scale_m
= scale_m
;
725 dev
->scale_d
= scale_d
;
729 if (info
->attrs
[WPROBE_ATTR_INTERVAL
]) {
730 /* change of measurement interval requested */
731 err
= wprobe_update_auto_measurement(dev
,
732 (u32
) nla_get_u64(info
->attrs
[WPROBE_ATTR_INTERVAL
]));
736 spin_unlock_irqrestore(&dev
->lock
, flags
);
742 static struct genl_ops wprobe_ops
[] = {
744 .cmd
= WPROBE_CMD_GET_INFO
,
745 .dumpit
= wprobe_dump_info
,
746 .policy
= wprobe_policy
,
749 .cmd
= WPROBE_CMD_GET_LIST
,
750 .dumpit
= wprobe_dump_info
,
751 .policy
= wprobe_policy
,
754 .cmd
= WPROBE_CMD_MEASURE
,
755 .doit
= wprobe_measure
,
756 .policy
= wprobe_policy
,
759 .cmd
= WPROBE_CMD_GET_LINKS
,
760 .dumpit
= wprobe_dump_links
,
761 .policy
= wprobe_policy
,
764 .cmd
= WPROBE_CMD_CONFIG
,
765 .doit
= wprobe_set_config
,
772 BUG_ON(!list_empty(&wprobe_if
));
773 genl_unregister_family(&wprobe_fam
);
782 spin_lock_init(&wprobe_lock
);
783 INIT_LIST_HEAD(&wprobe_if
);
785 err
= genl_register_family(&wprobe_fam
);
789 for (i
= 0; i
< ARRAY_SIZE(wprobe_ops
); i
++) {
790 err
= genl_register_ops(&wprobe_fam
, &wprobe_ops
[i
]);
798 genl_unregister_family(&wprobe_fam
);
802 module_init(wprobe_init
);
803 module_exit(wprobe_exit
);
804 MODULE_LICENSE("GPL");