Make sure that the Capture/Start menue item will always use the
[obnox/wireshark/wip.git] / tap-rlcltestat.c
1 /* tap-rlclte_stat.c
2  * Copyright 2011 Martin Mathieson
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35
36 #include <string.h>
37 #include <epan/packet.h>
38 #include <epan/packet_info.h>
39 #include <epan/tap.h>
40 #include <epan/stat_cmd_args.h>
41 #include <epan/dissectors/packet-rlc-lte.h>
42
43
44 enum {
45     UEID_COLUMN,
46     UL_FRAMES_COLUMN,
47     UL_BYTES_COLUMN,
48     UL_BW_COLUMN,
49     UL_ACKS_COLUMN,
50     UL_NACKS_COLUMN,
51     UL_MISSING_COLUMN,
52     DL_FRAMES_COLUMN,
53     DL_BYTES_COLUMN,
54     DL_BW_COLUMN,
55     DL_ACKS_COLUMN,
56     DL_NACKS_COLUMN,
57     DL_MISSING_COLUMN,
58     NUM_UE_COLUMNS
59 };
60
61 static const gchar *ue_titles[] = { " UEId",
62                                     "UL Frames", "UL Bytes", "   UL Mbs", "UL ACKs", "UL NACKs", "UL Missed",
63                                     "DL Frames", "DL Bytes", "   DL Mbs", "UL ACKs", "DL NACKs", "DL Missed"};
64
65 /* Stats for one UE */
66 typedef struct rlc_lte_row_data {
67     /* Key for matching this row */
68     guint16  ueid;
69
70     gboolean is_predefined_data;
71
72     guint32  UL_frames;
73     guint32  UL_total_bytes;
74     nstime_t UL_time_start;
75     nstime_t UL_time_stop;
76     guint32  UL_total_acks;
77     guint32  UL_total_nacks;
78     guint32  UL_total_missing;
79
80     guint32  DL_frames;
81     guint32  DL_total_bytes;
82     nstime_t DL_time_start;
83     nstime_t DL_time_stop;
84     guint32  DL_total_acks;
85     guint32  DL_total_nacks;
86     guint32  DL_total_missing;
87
88 } rlc_lte_row_data;
89
90
91 /* Common channel stats */
92 typedef struct rlc_lte_common_stats {
93     guint32 bcch_frames;
94     guint32 bcch_bytes;
95     guint32 pcch_frames;
96     guint32 pcch_bytes;
97 } rlc_lte_common_stats;
98
99
100 /* One row/UE in the UE table */
101 typedef struct rlc_lte_ep {
102     struct rlc_lte_ep* next;
103     struct rlc_lte_row_data stats;
104 } rlc_lte_ep_t;
105
106
107 /* Used to keep track of all RLC LTE statistics */
108 typedef struct rlc_lte_stat_t {
109     rlc_lte_ep_t  *ep_list;
110     guint32       total_frames;
111
112     /* Common stats */
113     rlc_lte_common_stats common_stats;
114 } rlc_lte_stat_t;
115
116
117
118 /* Reset RLC stats */
119 static void
120 rlc_lte_stat_reset(void *phs)
121 {
122     rlc_lte_stat_t* rlc_lte_stat = (rlc_lte_stat_t *)phs;
123     rlc_lte_ep_t* list = rlc_lte_stat->ep_list;
124
125     rlc_lte_stat->total_frames = 0;
126     memset(&rlc_lte_stat->common_stats, 0, sizeof(rlc_lte_common_stats));
127
128     if (!list) {
129         return;
130     }
131
132     rlc_lte_stat->ep_list = NULL;
133 }
134
135
136 /* Allocate a rlc_lte_ep_t struct to store info for new UE */
137 static rlc_lte_ep_t* alloc_rlc_lte_ep(struct rlc_lte_tap_info *si, packet_info *pinfo _U_)
138 {
139     rlc_lte_ep_t* ep;
140
141     if (!si) {
142         return NULL;
143     }
144
145     if (!(ep = g_malloc(sizeof(rlc_lte_ep_t)))) {
146         return NULL;
147     }
148
149     /* Copy SI data into ep->stats */
150     ep->stats.ueid = si->ueid;
151
152     /* Counts for new UE are all 0 */
153     ep->stats.UL_frames = 0;
154     ep->stats.DL_frames = 0;
155     ep->stats.UL_total_bytes = 0;
156     ep->stats.DL_total_bytes = 0;
157     memset(&ep->stats.DL_time_start, 0, sizeof(nstime_t));
158     memset(&ep->stats.DL_time_stop, 0, sizeof(nstime_t));
159     ep->stats.UL_total_nacks = 0;
160     ep->stats.DL_total_nacks = 0;
161     ep->stats.UL_total_missing = 0;
162     ep->stats.DL_total_missing = 0;
163
164     return ep;
165 }
166
167
168 /* Process stat struct for a RLC LTE frame */
169 static int
170 rlc_lte_stat_packet(void *phs, packet_info *pinfo, epan_dissect_t *edt _U_,
171                     const void *phi)
172 {
173     /* Get reference to stats struct */
174     rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
175     rlc_lte_ep_t *tmp = NULL, *te = NULL;
176
177     /* Cast tap info struct */
178     struct rlc_lte_tap_info *si = (struct rlc_lte_tap_info *)phi;
179
180     /* Need this */
181     if (!hs) {
182         return 0;
183     }
184
185     /* Inc top-level frame count */
186     hs->total_frames++;
187
188     /* Common channel stats */
189     switch (si->channelType) {
190         case CHANNEL_TYPE_BCCH:
191             hs->common_stats.bcch_frames++;
192             hs->common_stats.bcch_bytes += si->pduLength;
193             return 1;
194
195         case CHANNEL_TYPE_PCCH:
196             hs->common_stats.pcch_frames++;
197             hs->common_stats.pcch_bytes += si->pduLength;
198             return 1;
199
200         default:
201             break;
202     }
203
204     /* For per-UE data, must create a new row if none already existing */
205     if (!hs->ep_list) {
206         /* Allocate new list */
207         hs->ep_list = alloc_rlc_lte_ep(si, pinfo);
208         /* Make it the first/only entry */
209         te = hs->ep_list;
210     } else {
211         /* Look among existing rows for this UEId */
212         for (tmp = hs->ep_list; (tmp != NULL); tmp = tmp->next) {
213             if (tmp->stats.ueid == si->ueid) {
214                 te = tmp;
215                 break;
216             }
217         }
218
219         /* Not found among existing, so create a new one anyway */
220         if (te == NULL) {
221             if ((te = alloc_rlc_lte_ep(si, pinfo))) {
222                 /* Add new item to end of list */
223                 rlc_lte_ep_t *p = hs->ep_list;
224                 while (p->next) {
225                     p = p->next;
226                 }
227                 p->next = te;
228                 te->next = NULL;
229             }
230         }
231     }
232
233     /* Really should have a row pointer by now */
234     if (!te) {
235         return 0;
236     }
237
238     /* Update entry with details from si */
239     te->stats.ueid = si->ueid;
240
241     /* Top-level traffic stats */
242     if (si->direction == DIRECTION_UPLINK) {
243         /* Update time range */
244         if (te->stats.UL_frames == 0) {
245             te->stats.UL_time_start = si->time;
246         }
247         te->stats.UL_time_stop = si->time;
248
249         te->stats.UL_frames++;
250         te->stats.UL_total_bytes += si->pduLength;
251     }
252     else {
253         /* Update time range */
254         if (te->stats.DL_frames == 0) {
255             te->stats.DL_time_start = si->time;
256         }
257         te->stats.DL_time_stop = si->time;
258
259         te->stats.DL_frames++;
260         te->stats.DL_total_bytes += si->pduLength;
261     }
262
263
264     if (si->direction == DIRECTION_UPLINK) {
265         if (si->isControlPDU) {
266             te->stats.UL_total_acks++;
267         }
268         te->stats.UL_total_nacks += si->noOfNACKs;
269         te->stats.UL_total_missing += si->missingSNs;
270     }
271     else {
272         if (si->isControlPDU) {
273             te->stats.DL_total_acks++;
274         }
275         te->stats.DL_total_nacks += si->noOfNACKs;
276         te->stats.DL_total_missing += si->missingSNs;
277     }
278
279     return 1;
280 }
281
282
283 /* Calculate and return a bandwidth figure, in Mbs */
284 static float calculate_bw(nstime_t *start_time, nstime_t *stop_time, guint32 bytes)
285 {
286     if (memcmp(start_time, stop_time, sizeof(nstime_t)) != 0) {
287         float elapsed_ms = (((float)stop_time->secs - (float)start_time->secs) * 1000) +
288                            (((float)stop_time->nsecs - (float)start_time->nsecs) / 1000000);
289         return ((bytes * 8) / elapsed_ms) / 1000;
290     }
291     else {
292         return 0.0;
293     }
294 }
295
296
297
298
299 /* (Re)draw RLC stats */
300 static void
301 rlc_lte_stat_draw(void *phs)
302 {
303     guint16 number_of_ues = 0;
304     gint i;
305
306     /* Look up the statistics struct */
307     rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
308     rlc_lte_ep_t* list = hs->ep_list, *tmp = 0;
309
310     /* Common channel data */
311     printf("Common Data:\n");
312     printf("==============\n");
313     printf("BCCH Frames: %u   BCCH Bytes: %u   PCCH Frames: %u   PCCH Bytes: %u\n\n",
314            hs->common_stats.bcch_frames, hs->common_stats.bcch_bytes,
315            hs->common_stats.pcch_frames, hs->common_stats.pcch_bytes);
316
317     /* Per-UE table entries */
318     
319
320     /* Set title that shows how many UEs currently in table */
321     for (tmp = list; (tmp!=NULL); tmp=tmp->next, number_of_ues++);
322     printf("Per UE Data - %u UEs (%u frames)\n", number_of_ues, hs->total_frames);
323     printf("==========================================\n");
324
325     /* Show column titles */
326     for (i=0; i < NUM_UE_COLUMNS; i++) {
327         printf("%s  ", ue_titles[i]);
328     }
329     printf("\n");
330
331     /* For each row/UE in the model */
332     for (tmp = list; tmp; tmp=tmp->next) {
333         /* Calculate bandwidth */
334         float UL_bw = calculate_bw(&tmp->stats.UL_time_start,
335                                    &tmp->stats.UL_time_stop,
336                                    tmp->stats.UL_total_bytes);
337         float DL_bw = calculate_bw(&tmp->stats.DL_time_start,
338                                    &tmp->stats.DL_time_stop,
339                                    tmp->stats.DL_total_bytes);
340
341         printf("%5u %10u %9u %10f %8u %9u %10u %10u %9u %10f %8u %9u %10u\n",
342                tmp->stats.ueid,
343                tmp->stats.UL_frames,
344                tmp->stats.UL_total_bytes, UL_bw,
345                tmp->stats.UL_total_acks,
346                tmp->stats.UL_total_nacks,
347                tmp->stats.UL_total_missing,
348                tmp->stats.DL_frames,
349                tmp->stats.DL_total_bytes, DL_bw,
350                tmp->stats.DL_total_acks,
351                tmp->stats.DL_total_nacks,
352                tmp->stats.DL_total_missing);
353     }
354 }
355
356
357
358
359 /* Create a new RLC LTE stats struct */
360 static void rlc_lte_stat_init(const char *optarg, void *userdata _U_)
361 {
362     rlc_lte_stat_t    *hs;
363     const char        *filter = NULL;
364     GString           *error_string;
365
366     /* Check for a filter string */
367     if (strncmp(optarg, "rlc-lte,stat,", 13) == 0) {
368         /* Skip those characters from filter to display */
369         filter = optarg + 13;
370     }
371     else {
372         /* No filter */
373         filter = NULL;
374     }
375
376     /* Create top-level struct */
377     hs = g_malloc(sizeof(rlc_lte_stat_t));
378     memset(hs, 0,  sizeof(rlc_lte_stat_t));
379     hs->ep_list = NULL;
380
381
382     /**********************************************/
383     /* Register the tap listener                  */
384     /**********************************************/
385
386     error_string = register_tap_listener("rlc-lte", hs,
387                                          filter, 0,
388                                          rlc_lte_stat_reset,
389                                          rlc_lte_stat_packet,
390                                          rlc_lte_stat_draw);
391     if (error_string) {
392         g_string_free(error_string, TRUE);
393         g_free(hs);
394         exit(1);
395     }
396
397 }
398
399
400 /* Register this tap listener (need void on own so line register function found) */
401 void
402 register_tap_listener_rlc_lte_stat(void)
403 {
404     register_stat_cmd_arg("rlc-lte,stat", rlc_lte_stat_init, NULL);
405 }
406