Try to fix the compile errors on OS X:
[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", "DL 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_acks = 0;
160     ep->stats.DL_total_acks = 0;
161     ep->stats.UL_total_nacks = 0;
162     ep->stats.DL_total_nacks = 0;
163     ep->stats.UL_total_missing = 0;
164     ep->stats.DL_total_missing = 0;
165
166     ep->next = NULL;
167
168     return ep;
169 }
170
171
172 /* Process stat struct for a RLC LTE frame */
173 static int
174 rlc_lte_stat_packet(void *phs, packet_info *pinfo, epan_dissect_t *edt _U_,
175                     const void *phi)
176 {
177     /* Get reference to stats struct */
178     rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
179     rlc_lte_ep_t *tmp = NULL, *te = NULL;
180
181     /* Cast tap info struct */
182     struct rlc_lte_tap_info *si = (struct rlc_lte_tap_info *)phi;
183
184     /* Need this */
185     if (!hs) {
186         return 0;
187     }
188
189     /* Inc top-level frame count */
190     hs->total_frames++;
191
192     /* Common channel stats */
193     switch (si->channelType) {
194         case CHANNEL_TYPE_BCCH_BCH:
195         case CHANNEL_TYPE_BCCH_DL_SCH:
196             hs->common_stats.bcch_frames++;
197             hs->common_stats.bcch_bytes += si->pduLength;
198             return 1;
199
200         case CHANNEL_TYPE_PCCH:
201             hs->common_stats.pcch_frames++;
202             hs->common_stats.pcch_bytes += si->pduLength;
203             return 1;
204
205         default:
206             break;
207     }
208
209     /* For per-UE data, must create a new row if none already existing */
210     if (!hs->ep_list) {
211         /* Allocate new list */
212         hs->ep_list = alloc_rlc_lte_ep(si, pinfo);
213         /* Make it the first/only entry */
214         te = hs->ep_list;
215     } else {
216         /* Look among existing rows for this UEId */
217         for (tmp = hs->ep_list; (tmp != NULL); tmp = tmp->next) {
218             if (tmp->stats.ueid == si->ueid) {
219                 te = tmp;
220                 break;
221             }
222         }
223
224         /* Not found among existing, so create a new one anyway */
225         if (te == NULL) {
226             if ((te = alloc_rlc_lte_ep(si, pinfo))) {
227                 /* Add new item to end of list */
228                 rlc_lte_ep_t *p = hs->ep_list;
229                 while (p->next) {
230                     p = p->next;
231                 }
232                 p->next = te;
233                 te->next = NULL;
234             }
235         }
236     }
237
238     /* Really should have a row pointer by now */
239     if (!te) {
240         return 0;
241     }
242
243     /* Update entry with details from si */
244     te->stats.ueid = si->ueid;
245
246     /* Top-level traffic stats */
247     if (si->direction == DIRECTION_UPLINK) {
248         /* Update time range */
249         if (te->stats.UL_frames == 0) {
250             te->stats.UL_time_start = si->time;
251         }
252         te->stats.UL_time_stop = si->time;
253
254         te->stats.UL_frames++;
255         te->stats.UL_total_bytes += si->pduLength;
256     }
257     else {
258         /* Update time range */
259         if (te->stats.DL_frames == 0) {
260             te->stats.DL_time_start = si->time;
261         }
262         te->stats.DL_time_stop = si->time;
263
264         te->stats.DL_frames++;
265         te->stats.DL_total_bytes += si->pduLength;
266     }
267
268
269     if (si->direction == DIRECTION_UPLINK) {
270         if (si->isControlPDU) {
271             te->stats.UL_total_acks++;
272         }
273         te->stats.UL_total_nacks += si->noOfNACKs;
274         te->stats.UL_total_missing += si->missingSNs;
275     }
276     else {
277         if (si->isControlPDU) {
278             te->stats.DL_total_acks++;
279         }
280         te->stats.DL_total_nacks += si->noOfNACKs;
281         te->stats.DL_total_missing += si->missingSNs;
282     }
283
284     return 1;
285 }
286
287
288 /* Calculate and return a bandwidth figure, in Mbs */
289 static float calculate_bw(nstime_t *start_time, nstime_t *stop_time, guint32 bytes)
290 {
291     if (memcmp(start_time, stop_time, sizeof(nstime_t)) != 0) {
292         float elapsed_ms = (((float)stop_time->secs - (float)start_time->secs) * 1000) +
293                            (((float)stop_time->nsecs - (float)start_time->nsecs) / 1000000);
294         return ((bytes * 8) / elapsed_ms) / 1000;
295     }
296     else {
297         return 0.0;
298     }
299 }
300
301
302
303
304 /* (Re)draw RLC stats */
305 static void
306 rlc_lte_stat_draw(void *phs)
307 {
308     guint16 number_of_ues = 0;
309     gint i;
310
311     /* Look up the statistics struct */
312     rlc_lte_stat_t *hs = (rlc_lte_stat_t *)phs;
313     rlc_lte_ep_t* list = hs->ep_list, *tmp = 0;
314
315     /* Common channel data */
316     printf("Common Data:\n");
317     printf("==============\n");
318     printf("BCCH Frames: %u   BCCH Bytes: %u   PCCH Frames: %u   PCCH Bytes: %u\n\n",
319            hs->common_stats.bcch_frames, hs->common_stats.bcch_bytes,
320            hs->common_stats.pcch_frames, hs->common_stats.pcch_bytes);
321
322     /* Per-UE table entries */
323     
324
325     /* Set title that shows how many UEs currently in table */
326     for (tmp = list; (tmp!=NULL); tmp=tmp->next, number_of_ues++);
327     printf("Per UE Data - %u UEs (%u frames)\n", number_of_ues, hs->total_frames);
328     printf("==========================================\n");
329
330     /* Show column titles */
331     for (i=0; i < NUM_UE_COLUMNS; i++) {
332         printf("%s  ", ue_titles[i]);
333     }
334     printf("\n");
335
336     /* For each row/UE in the model */
337     for (tmp = list; tmp; tmp=tmp->next) {
338         /* Calculate bandwidth */
339         float UL_bw = calculate_bw(&tmp->stats.UL_time_start,
340                                    &tmp->stats.UL_time_stop,
341                                    tmp->stats.UL_total_bytes);
342         float DL_bw = calculate_bw(&tmp->stats.DL_time_start,
343                                    &tmp->stats.DL_time_stop,
344                                    tmp->stats.DL_total_bytes);
345
346         printf("%5u %10u %9u %10f %8u %9u %10u %10u %9u %10f %8u %9u %10u\n",
347                tmp->stats.ueid,
348                tmp->stats.UL_frames,
349                tmp->stats.UL_total_bytes, UL_bw,
350                tmp->stats.UL_total_acks,
351                tmp->stats.UL_total_nacks,
352                tmp->stats.UL_total_missing,
353                tmp->stats.DL_frames,
354                tmp->stats.DL_total_bytes, DL_bw,
355                tmp->stats.DL_total_acks,
356                tmp->stats.DL_total_nacks,
357                tmp->stats.DL_total_missing);
358     }
359 }
360
361
362
363
364 /* Create a new RLC LTE stats struct */
365 static void rlc_lte_stat_init(const char *optarg, void *userdata _U_)
366 {
367     rlc_lte_stat_t    *hs;
368     const char        *filter = NULL;
369     GString           *error_string;
370
371     /* Check for a filter string */
372     if (strncmp(optarg, "rlc-lte,stat,", 13) == 0) {
373         /* Skip those characters from filter to display */
374         filter = optarg + 13;
375     }
376     else {
377         /* No filter */
378         filter = NULL;
379     }
380
381     /* Create top-level struct */
382     hs = g_malloc(sizeof(rlc_lte_stat_t));
383     memset(hs, 0,  sizeof(rlc_lte_stat_t));
384     hs->ep_list = NULL;
385
386
387     /**********************************************/
388     /* Register the tap listener                  */
389     /**********************************************/
390
391     error_string = register_tap_listener("rlc-lte", hs,
392                                          filter, 0,
393                                          rlc_lte_stat_reset,
394                                          rlc_lte_stat_packet,
395                                          rlc_lte_stat_draw);
396     if (error_string) {
397         g_string_free(error_string, TRUE);
398         g_free(hs);
399         exit(1);
400     }
401
402 }
403
404
405 /* Register this tap listener (need void on own so line register function found) */
406 void
407 register_tap_listener_rlc_lte_stat(void)
408 {
409     register_stat_cmd_arg("rlc-lte,stat", rlc_lte_stat_init, NULL);
410 }
411