summaryrefslogtreecommitdiffstats
path: root/package/madwifi/patches/416-wprobe.patch
blob: 45307378297999b66c677e572161f4ec65ea8748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
--- /dev/null
+++ b/ath/ath_wprobe.c
@@ -0,0 +1,392 @@
+#include <net80211/ieee80211_node.h>
+#include <linux/wprobe.h>
+
+atomic_t cleanup_tasks = ATOMIC_INIT(0);
+
+enum wp_node_val {
+	WP_NODE_RSSI,
+	WP_NODE_SIGNAL,
+	WP_NODE_RX_RATE,
+	WP_NODE_TX_RATE,
+	WP_NODE_RETRANSMIT_200,
+	WP_NODE_RETRANSMIT_400,
+	WP_NODE_RETRANSMIT_800,
+	WP_NODE_RETRANSMIT_1600,
+};
+
+enum wp_global_val {
+	WP_GLOBAL_NOISE,
+	WP_GLOBAL_PHY_BUSY,
+	WP_GLOBAL_PHY_RX,
+	WP_GLOBAL_PHY_TX,
+	WP_GLOBAL_FRAMES,
+	WP_GLOBAL_PROBEREQ,
+};
+
+static struct wprobe_item ath_wprobe_globals[] = {
+	[WP_GLOBAL_NOISE] = {
+		.name = "noise",
+		.type = WPROBE_VAL_S16,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_GLOBAL_PHY_BUSY] = {
+		.name = "phy_busy",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_GLOBAL_PHY_RX] = {
+		.name = "phy_rx",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_GLOBAL_PHY_TX] = {
+		.name = "phy_tx",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_GLOBAL_FRAMES] = {
+		.name = "frames",
+		.type = WPROBE_VAL_U32,
+	},
+	[WP_GLOBAL_PROBEREQ] = {
+		.name = "probereq",
+		.type = WPROBE_VAL_U32,
+	},
+};
+
+static struct wprobe_item ath_wprobe_link[] = {
+	[WP_NODE_RSSI] = {
+		.name = "rssi",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_SIGNAL] = {
+		.name = "signal",
+		.type = WPROBE_VAL_S16,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_RX_RATE] = {
+		.name = "rx_rate",
+		.type = WPROBE_VAL_U16,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_TX_RATE] = {
+		.name = "tx_rate",
+		.type = WPROBE_VAL_U16,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_RETRANSMIT_200] = {
+		.name = "retransmit_200",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_RETRANSMIT_400] = {
+		.name = "retransmit_400",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_RETRANSMIT_800] = {
+		.name = "retransmit_800",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+	[WP_NODE_RETRANSMIT_1600] = {
+		.name = "retransmit_1600",
+		.type = WPROBE_VAL_U8,
+		.flags = WPROBE_F_KEEPSTAT
+	},
+};
+
+#define AR5K_MIBC       0x0040
+#define AR5K_MIBC_FREEZE	(1 << 1)
+#define AR5K_TXFC       0x80ec
+#define AR5K_RXFC       0x80f0
+#define AR5K_RXCLEAR    0x80f4
+#define AR5K_CYCLES     0x80f8
+
+#define READ_CLR(_ah, _reg) \
+	({ u32 __val = OS_REG_READ(_ah, _reg); OS_REG_WRITE(_ah, _reg, 0); __val; })
+
+static bool
+wprobe_disabled(void)
+{
+	return (!wprobe_add_iface || IS_ERR(wprobe_add_iface));
+}
+
+static int
+ath_wprobe_sync(struct wprobe_iface *dev, struct wprobe_link *l, struct wprobe_value *val, bool measure)
+{
+	struct ath_vap *avp = container_of(dev, struct ath_vap, av_wpif);
+	struct ieee80211vap *vap = &avp->av_vap;
+	struct ieee80211com *ic = vap->iv_ic;
+	struct ath_softc *sc = ic->ic_dev->priv;
+	struct ath_hal *ah = sc->sc_ah;
+	u32 cc, busy, rx, tx;
+	s16 noise;
+
+	if (l)
+		goto out;
+
+	OS_REG_WRITE(ah, AR5K_MIBC, AR5K_MIBC_FREEZE);
+	cc = READ_CLR(ah, AR5K_CYCLES);
+	busy = READ_CLR(ah, AR5K_RXCLEAR);
+	rx = READ_CLR(ah, AR5K_RXFC);
+	tx = READ_CLR(ah, AR5K_TXFC);
+	OS_REG_WRITE(ah, AR5K_MIBC, 0);
+	noise = ath_hal_get_channel_noise(sc->sc_ah, &(sc->sc_curchan));
+	ic->ic_channoise = noise;
+
+	WPROBE_FILL_BEGIN(val, ath_wprobe_globals);
+	if (cc & 0xf0000000) {
+		/* scale down if the counters are near max */
+		cc >>= 8;
+		busy >>= 8;
+		rx >>= 8;
+		tx >>= 8;
+	}
+	if (ah->ah_macType < 5212)
+		goto phy_skip;
+	if (!cc)
+		goto phy_skip;
+	if (busy > cc)
+		goto phy_skip;
+	if (rx > cc)
+		goto phy_skip;
+	if (tx > cc)
+		goto phy_skip;
+	busy = (busy * 100) / cc;
+	rx = (rx * 100) / cc;
+	tx = (tx * 100) / cc;
+	WPROBE_SET(WP_GLOBAL_PHY_BUSY, U8, busy);
+	WPROBE_SET(WP_GLOBAL_PHY_RX, U8, rx);
+	WPROBE_SET(WP_GLOBAL_PHY_TX, U8, tx);
+	WPROBE_SET(WP_GLOBAL_FRAMES, U32, avp->av_rxframes);
+	WPROBE_SET(WP_GLOBAL_PROBEREQ, U32, avp->av_rxprobereq);
+
+phy_skip:
+	WPROBE_SET(WP_GLOBAL_NOISE, S16, noise);
+	WPROBE_FILL_END();
+
+out:
+	return 0;
+}
+
+#undef AR5K_TXFC
+#undef AR5K_RXFC
+#undef AR5K_RXCLEAR
+#undef AR5K_CYCLES
+#undef AR5K_MIBC
+#undef AR5K_MIBC_FREEZE
+#undef READ_CLR
+
+static const struct wprobe_iface ath_wprobe_dev = {
+	.link_items = ath_wprobe_link,
+	.n_link_items = ARRAY_SIZE(ath_wprobe_link),
+	.global_items = ath_wprobe_globals,
+	.n_global_items = ARRAY_SIZE(ath_wprobe_globals),
+	.sync_data = ath_wprobe_sync,
+};
+
+static int
+ath_lookup_rateval(struct ieee80211_node *ni, int rate)
+{
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct ieee80211com *ic = vap->iv_ic;
+	struct ath_softc *sc = ic->ic_dev->priv;
+	const HAL_RATE_TABLE *rt = sc->sc_currates;
+
+	if ((!rt) || (rate < 0) || (rate >= ARRAY_SIZE(sc->sc_hwmap)))
+		return -1;
+
+	rate = sc->sc_hwmap[rate].ieeerate;
+	rate = sc->sc_rixmap[rate & IEEE80211_RATE_VAL];
+	if ((rate < 0) || (rate >= rt->rateCount))
+		return -1;
+
+	return rt->info[rate].rateKbps / 10;
+}
+
+static void
+ath_node_sample_rx(struct ieee80211_node *ni, struct ath_rx_status *rs)
+{
+	struct ath_node *an = ATH_NODE(ni);
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct ieee80211com *ic = vap->iv_ic;
+	struct wprobe_link *l = &an->an_wplink;
+	struct wprobe_value *v = l->val;
+	unsigned long flags;
+	int rate;
+
+	if (wprobe_disabled() || !an->an_wplink_active || !l->val)
+		return;
+
+	rate = ath_lookup_rateval(ni, rs->rs_rate);
+
+	spin_lock_irqsave(&l->iface->lock, flags);
+	WPROBE_FILL_BEGIN(v, ath_wprobe_link);
+	WPROBE_SET(WP_NODE_RSSI, U8, rs->rs_rssi);
+	WPROBE_SET(WP_NODE_SIGNAL, S16, ic->ic_channoise + rs->rs_rssi);
+	if ((rate > 0) && (rate <= 600000))
+		WPROBE_SET(WP_NODE_RX_RATE, U16, rate);
+	WPROBE_FILL_END();
+	wprobe_update_stats(l->iface, l);
+	spin_unlock_irqrestore(&l->iface->lock, flags);
+}
+
+static void
+ath_node_sample_tx(struct ieee80211_node *ni, struct ath_tx_status *ts, int len)
+{
+	struct ath_node *an = ATH_NODE(ni);
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct ieee80211com *ic = vap->iv_ic;
+	struct wprobe_link *l = &an->an_wplink;
+	struct wprobe_value *v = l->val;
+	unsigned long flags;
+	int rate, rexmit_counter;
+
+	if (wprobe_disabled() || !an->an_wplink_active || !l->val)
+		return;
+
+	rate = ath_lookup_rateval(ni, ts->ts_rate);
+
+	spin_lock_irqsave(&l->iface->lock, flags);
+	WPROBE_FILL_BEGIN(v, ath_wprobe_link);
+	WPROBE_SET(WP_NODE_RSSI, U8, ts->ts_rssi);
+	WPROBE_SET(WP_NODE_SIGNAL, S16, ic->ic_channoise + ts->ts_rssi);
+
+	if (len <= 200)
+		rexmit_counter = WP_NODE_RETRANSMIT_200;
+	else if (len <= 400)
+		rexmit_counter = WP_NODE_RETRANSMIT_400;
+	else if (len <= 800)
+		rexmit_counter = WP_NODE_RETRANSMIT_800;
+	else
+		rexmit_counter = WP_NODE_RETRANSMIT_1600;
+	WPROBE_SET(rexmit_counter, U8, ts->ts_longretry);
+
+	if ((rate > 0) && (rate <= 600000))
+		WPROBE_SET(WP_NODE_TX_RATE, U16, rate);
+	WPROBE_FILL_END();
+	wprobe_update_stats(l->iface, l);
+	spin_unlock_irqrestore(&l->iface->lock, flags);
+}
+
+static void
+ath_wprobe_report_rx(struct ieee80211vap *vap, struct sk_buff *skb)
+{
+	struct ieee80211_frame *wh = (struct ieee80211_frame *)skb->data;
+	struct ath_vap *avp;
+
+	if (wprobe_disabled())
+		return;
+
+	avp = ATH_VAP(vap);
+	avp->av_rxframes++;
+	if (wh->i_fc[0] == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
+		avp->av_rxprobereq++;
+}
+
+static void
+ath_wprobe_node_join(struct ieee80211vap *vap, struct ieee80211_node *ni)
+{
+	struct wprobe_iface *dev;
+	struct wprobe_link *l;
+	struct ath_vap *avp;
+	struct ath_node *an = ATH_NODE(ni);
+
+	if (wprobe_disabled() || an->an_wplink_active)
+		return;
+
+	avp = ATH_VAP(vap);
+	dev = &avp->av_wpif;
+	l = &an->an_wplink;
+
+	ieee80211_ref_node(ni);
+	wprobe_add_link(dev, l, ni->ni_macaddr);
+	an->an_wplink_active = 1;
+}
+
+static void
+ath_wprobe_do_node_leave(struct work_struct *work)
+{
+	struct ath_node *an = container_of(work, struct ath_node, an_destroy);
+	struct ieee80211_node *ni = &an->an_node;
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct wprobe_iface *dev;
+	struct wprobe_link *l;
+	struct ath_vap *avp;
+
+	avp = ATH_VAP(vap);
+	dev = &avp->av_wpif;
+	l = &an->an_wplink;
+
+	wprobe_remove_link(dev, l);
+	ieee80211_unref_node(&ni);
+	atomic_dec(&cleanup_tasks);
+}
+
+static void
+ath_wprobe_node_leave(struct ieee80211vap *vap, struct ieee80211_node *ni)
+{
+	struct ath_node *an = ATH_NODE(ni);
+
+	if (wprobe_disabled() || !an->an_wplink_active)
+		return;
+
+	atomic_inc(&cleanup_tasks);
+	an->an_wplink_active = 0;
+	IEEE80211_INIT_WORK(&an->an_destroy, ath_wprobe_do_node_leave);
+	schedule_work(&an->an_destroy);
+}
+
+static void
+ath_init_wprobe_dev(struct ath_vap *avp)
+{
+	struct ieee80211vap *vap = &avp->av_vap;
+	struct wprobe_iface *dev = &avp->av_wpif;
+
+	if (wprobe_disabled() || (vap->iv_opmode == IEEE80211_M_WDS))
+		return;
+
+	memcpy(dev, &ath_wprobe_dev, sizeof(struct wprobe_iface));
+	dev->addr = vap->iv_myaddr;
+	dev->name = vap->iv_dev->name;
+	wprobe_add_iface(dev);
+}
+
+static void
+ath_remove_wprobe_dev(struct ath_vap *avp)
+{
+	struct ieee80211vap *vap = &avp->av_vap;
+	struct ieee80211com *ic = vap->iv_ic;
+	struct ieee80211_node *ni;
+	struct wprobe_iface *dev = &avp->av_wpif;
+	struct wprobe_link *l;
+	struct ath_node *an;
+	unsigned long flags;
+
+	if (wprobe_disabled() || (vap->iv_opmode == IEEE80211_M_WDS))
+		return;
+
+restart:
+	rcu_read_lock();
+	list_for_each_entry_rcu(l, &dev->links, list) {
+		an = container_of(l, struct ath_node, an_wplink);
+
+		if (!an->an_wplink_active)
+			continue;
+
+		ni = &an->an_node;
+		ath_wprobe_node_leave(vap, ni);
+		rcu_read_unlock();
+		goto restart;
+	}
+	rcu_read_unlock();
+
+	/* wait for the cleanup tasks to finish */
+	while (atomic_read(&cleanup_tasks) != 0) {
+		schedule();
+	}
+
+	wprobe_remove_iface(dev);
+}
--- a/ath/if_ath.c
+++ b/ath/if_ath.c
@@ -400,6 +400,7 @@ static int countrycode = -1;
 static int maxvaps = -1;
 static int outdoor = -1;
 static int xchanmode = -1;
+#include "ath_wprobe.c"
 static int beacon_cal = 1;
 
 static const struct ath_hw_detect generic_hw_info = {
@@ -1525,6 +1526,7 @@ ath_vap_create(struct ieee80211com *ic, 
 		ath_hal_intrset(ah, sc->sc_imask);
 	}
 
+	ath_init_wprobe_dev(avp);
 	return vap;
 }
 
@@ -1605,6 +1607,7 @@ ath_vap_delete(struct ieee80211vap *vap)
 		decrease = 0;
 
 	ieee80211_vap_detach(vap);
+	ath_remove_wprobe_dev(ATH_VAP(vap));
 	/* NB: memory is reclaimed through dev->destructor callback */
 	if (decrease)
 		sc->sc_nvaps--;
@@ -5931,6 +5934,7 @@ ath_node_cleanup(struct ieee80211_node *
 	/* Clean up node-specific rate things - this currently appears to 
 	 * always be a no-op */
 	sc->sc_rc->ops->node_cleanup(sc, ATH_NODE(ni));
+	ath_wprobe_node_leave(ni->ni_vap, ni);
 
 	ATH_NODE_UAPSD_LOCK_IRQ(an);
 #ifdef IEEE80211_DEBUG_REFCNT
@@ -7001,6 +7005,8 @@ drop_micfail:
 				goto lookup_slowpath;
 			}
 			ATH_RSSI_LPF(ATH_NODE(ni)->an_avgrssi, rs->rs_rssi);
+			ath_node_sample_rx(ni, rs);
+			ath_wprobe_report_rx(ni->ni_vap, skb);
 			type = ieee80211_input(ni->ni_vap, ni, skb, rs->rs_rssi, bf->bf_tsf);
 			ieee80211_unref_node(&ni);
 		} else {
@@ -7011,15 +7017,22 @@ drop_micfail:
 
 lookup_slowpath:
 			vap = ieee80211_find_rxvap(ic, wh->i_addr1);
-			if (vap)
+			if (vap) {
+				ath_wprobe_report_rx(vap, skb);
 				ni = ieee80211_find_rxnode(ic, vap, wh);
-			else
+			} else {
+				TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
+					ath_wprobe_report_rx(vap, skb);
+				}
+				vap = NULL;
 				ni = NULL;
+			}
 
 			if (ni != NULL) {
 				ieee80211_keyix_t keyix;
 
 				ATH_RSSI_LPF(ATH_NODE(ni)->an_avgrssi, rs->rs_rssi);
+				ath_node_sample_rx(ni, rs);
 				type = ieee80211_input(vap, ni, skb, rs->rs_rssi, bf->bf_tsf);
 				/*
 				 * If the station has a key cache slot assigned
@@ -8599,6 +8612,7 @@ ath_tx_processq(struct ath_softc *sc, st
 				sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
 				ATH_RSSI_LPF(an->an_halstats.ns_avgtxrssi,
 					ts->ts_rssi);
+					ath_node_sample_tx(&an->an_node, ts, bf->bf_skb->len);
 				if (bf->bf_skb->priority == WME_AC_VO ||
 				    bf->bf_skb->priority == WME_AC_VI)
 					ni->ni_ic->ic_wme.wme_hipri_traffic++;
@@ -10090,6 +10104,7 @@ ath_newassoc(struct ieee80211_node *ni, 
 	struct ath_softc *sc = ic->ic_dev->priv;
 
 	sc->sc_rc->ops->newassoc(sc, ATH_NODE(ni), isnew);
+	ath_wprobe_node_join(ni->ni_vap, ni);
 
 	/* are we supporting compression? */
 	if (!(vap->iv_ath_cap & ni->ni_ath_flags & IEEE80211_NODE_COMP))
--- a/ath/if_athvar.h
+++ b/ath/if_athvar.h
@@ -46,6 +46,7 @@
 #include "ah_desc.h"
 #include "ah_os.h"
 #include "if_athioctl.h"
+#include <linux/wprobe.h>
 #include "net80211/ieee80211.h"		/* XXX for WME_NUM_AC */
 #include <asm/io.h>
 #include <linux/list.h>
@@ -352,6 +353,9 @@ typedef STAILQ_HEAD(, ath_buf) ath_bufhe
 /* driver-specific node state */
 struct ath_node {
 	struct ieee80211_node an_node;		/* base class */
+	struct wprobe_link an_wplink;
+	uint8_t an_wplink_active;
+	struct work_struct an_destroy;
 	u_int16_t an_decomp_index; 		/* decompression mask index */
 	u_int32_t an_avgrssi;			/* average rssi over all rx frames */
 	u_int8_t  an_prevdatarix;		/* rate ix of last data frame */
@@ -521,6 +525,9 @@ struct ath_vap {
 #else
 	unsigned int av_beacon_alloc;
 #endif
+	struct wprobe_iface av_wpif;
+	u32 av_rxframes;
+	u32 av_rxprobereq;
 };
 #define	ATH_VAP(_v)	((struct ath_vap *)(_v))