Support packets in flight
[samplevnf.git] / VNFs / DPPD-PROX / display_pkt_len.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include "prox_globals.h"
18 #include "display_pkt_len.h"
19 #include "stats_port.h"
20 #include "display.h"
21 #include "defaults.h"
22 #include "prox_port_cfg.h"
23 #include "clock.h"
24
25 static struct display_page display_page_pkt_len;
26 static struct display_column *port_col;
27 static struct display_column *name_col;
28 static struct display_column *type_col;
29 static struct display_column *stats_col[PKT_SIZE_COUNT];
30
31 const char *titles[] = {
32         "64B (#)",
33         "65-127B (#)",
34         "128-255B (#)",
35         "256-511B (#)",
36         "512-1023B (#)",
37         "1024-1522B (#)",
38         "1523B+ (#)",
39 };
40
41 static int port_disp[PROX_MAX_PORTS];
42 static int n_port_disp;
43
44 static void display_pkt_len_draw_frame(struct screen_state *screen_state)
45 {
46         n_port_disp = 0;
47         for (uint8_t i = 0; i < PROX_MAX_PORTS; ++i) {
48                 if (prox_port_cfg[i].active) {
49                         port_disp[n_port_disp++] = i;
50                 }
51         }
52
53         display_page_init(&display_page_pkt_len);
54
55         struct display_table *port_name = display_page_add_table(&display_page_pkt_len);
56
57         display_table_init(port_name, "Port");
58         port_col = display_table_add_col(port_name);
59         name_col = display_table_add_col(port_name);
60         type_col = display_table_add_col(port_name);
61
62         display_column_init(port_col, "ID", 4);
63         display_column_init(name_col, "Name", 8);
64         display_column_init(type_col, "Type", 7);
65
66         struct display_table *stats = display_page_add_table(&display_page_pkt_len);
67
68         if (screen_state->toggle == 0)
69                 display_table_init(stats, "Statistics per second");
70         else
71                 display_table_init(stats, "Total Statistics");
72
73         for (int i = 0; i < PKT_SIZE_COUNT; ++i) {
74                 stats_col[i] = display_table_add_col(stats);
75                 display_column_init(stats_col[i], titles[i], 13);
76         }
77
78         display_page_draw_frame(&display_page_pkt_len, n_port_disp);
79
80         for (uint8_t i = 0; i < n_port_disp; ++i) {
81                 const uint32_t port_id = port_disp[i];
82
83                 display_column_print(port_col, i, "%4u", port_id);
84                 display_column_print(name_col, i, "%8s", prox_port_cfg[port_id].names[0]);
85                 display_column_print(type_col, i, "%7s", prox_port_cfg[port_id].short_name);
86         }
87 }
88
89 static void display_pkt_len_draw_stats(struct screen_state *state)
90 {
91         for (uint8_t i = 0; i < n_port_disp; ++i) {
92                 const uint32_t port_id = port_disp[i];
93                 struct port_stats_sample *last = stats_get_port_stats_sample(port_id, 1);
94                 struct port_stats_sample *prev = stats_get_port_stats_sample(port_id, 0);
95
96                 uint64_t delta_t = last->tsc - prev->tsc;
97                 if (delta_t == 0) // This could happen if we just reset the screen => stats will be updated later
98                         continue;
99
100                 if (state->toggle == 0) {
101                         uint64_t diff;
102
103                         for (int j = 0; j < PKT_SIZE_COUNT; ++j) {
104                                 if (last->tx_pkt_size[j] == (uint64_t)-1) {
105                                         display_column_print(stats_col[j], i, "     ---     ");
106                                 } else {
107                                         diff = last->tx_pkt_size[j] - prev->tx_pkt_size[j];
108                                         display_column_print(stats_col[j], i, "%13lu", val_to_rate(diff, delta_t));
109                                 }
110                         }
111                 } else {
112                         for (int j = 0; j < PKT_SIZE_COUNT; ++j) {
113                                 if (last->tx_pkt_size[j] == (uint64_t)-1) {
114                                         display_column_print(stats_col[j], i, "     ---     ");
115                                 } else {
116                                         display_column_print(stats_col[j], i, "%13lu", last->tx_pkt_size[j]);
117                                 }
118                         }
119                 }
120         }
121 }
122
123 static int display_pkt_len_get_height(void)
124 {
125         return stats_get_n_ports();
126 }
127
128 static struct display_screen display_screen_pkt_len = {
129         .draw_frame = display_pkt_len_draw_frame,
130         .draw_stats = display_pkt_len_draw_stats,
131         .get_height = display_pkt_len_get_height,
132         .title = "pkt_len",
133 };
134
135 struct display_screen *display_pkt_len(void)
136 {
137         return &display_screen_pkt_len;
138 }