Coverage Report

Created: 2023-03-26 07:42

/src/openvswitch/lib/ofp-ipfix.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2008-2017 Nicira, Inc.
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 <config.h>
18
#include "openvswitch/ofp-ipfix.h"
19
#include <stdlib.h>
20
#include "byte-order.h"
21
#include "openflow/nicira-ext.h"
22
#include "openvswitch/ofp-errors.h"
23
#include "openvswitch/ofp-msgs.h"
24
#include "openvswitch/ofpbuf.h"
25
#include "util.h"
26
27
static void
28
ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats *ois,
29
                            struct nx_ipfix_stats_reply *reply)
30
0
{
31
0
    reply->collector_set_id = htonl(ois->collector_set_id);
32
0
    reply->total_flows = htonll(ois->total_flows);
33
0
    reply->current_flows = htonll(ois->current_flows);
34
0
    reply->pkts = htonll(ois->pkts);
35
0
    reply->ipv4_pkts = htonll(ois->ipv4_pkts);
36
0
    reply->ipv6_pkts = htonll(ois->ipv6_pkts);
37
0
    reply->error_pkts = htonll(ois->error_pkts);
38
0
    reply->ipv4_error_pkts = htonll(ois->ipv4_error_pkts);
39
0
    reply->ipv6_error_pkts = htonll(ois->ipv6_error_pkts);
40
0
    reply->tx_pkts = htonll(ois->tx_pkts);
41
0
    reply->tx_errors = htonll(ois->tx_errors);
42
0
    memset(reply->pad, 0, sizeof reply->pad);
43
0
}
44
45
/* Encode a ipfix stat for 'ois' and append it to 'replies'. */
46
void
47
ofputil_append_ipfix_stat(struct ovs_list *replies,
48
                         const struct ofputil_ipfix_stats *ois)
49
0
{
50
0
    struct nx_ipfix_stats_reply *reply = ofpmp_append(replies, sizeof *reply);
51
0
    ofputil_ipfix_stats_to_reply(ois, reply);
52
0
}
53
54
static enum ofperr
55
ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats *is,
56
                            const struct nx_ipfix_stats_reply *reply)
57
6.87k
{
58
6.87k
    is->collector_set_id = ntohl(reply->collector_set_id);
59
6.87k
    is->total_flows = ntohll(reply->total_flows);
60
6.87k
    is->current_flows = ntohll(reply->current_flows);
61
6.87k
    is->pkts = ntohll(reply->pkts);
62
6.87k
    is->ipv4_pkts = ntohll(reply->ipv4_pkts);
63
6.87k
    is->ipv6_pkts = ntohll(reply->ipv6_pkts);
64
6.87k
    is->error_pkts = ntohll(reply->error_pkts);
65
6.87k
    is->ipv4_error_pkts = ntohll(reply->ipv4_error_pkts);
66
6.87k
    is->ipv6_error_pkts = ntohll(reply->ipv6_error_pkts);
67
6.87k
    is->tx_pkts = ntohll(reply->tx_pkts);
68
6.87k
    is->tx_errors = ntohll(reply->tx_errors);
69
70
6.87k
    return 0;
71
6.87k
}
72
73
int
74
ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats *is, struct ofpbuf *msg)
75
8.65k
{
76
8.65k
    enum ofperr error;
77
8.65k
    enum ofpraw raw;
78
79
8.65k
    memset(is, 0xFF, sizeof (*is));
80
81
8.65k
    error = (msg->header ? ofpraw_decode(&raw, msg->header)
82
8.65k
             : ofpraw_pull(&raw, msg));
83
8.65k
    if (error) {
84
0
        return error;
85
0
    }
86
87
8.65k
    if (!msg->size) {
88
1.78k
        return EOF;
89
6.87k
    } else if (raw == OFPRAW_NXST_IPFIX_BRIDGE_REPLY ||
90
6.87k
               raw == OFPRAW_NXST_IPFIX_FLOW_REPLY) {
91
6.87k
        struct nx_ipfix_stats_reply *reply;
92
93
6.87k
        reply = ofpbuf_try_pull(msg, sizeof *reply);
94
6.87k
        return ofputil_ipfix_stats_from_nx(is, reply);
95
6.87k
    } else {
96
0
        OVS_NOT_REACHED();
97
0
    }
98
8.65k
}
99
100
101
/* Returns the number of ipfix stats elements in
102
 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
103
 * message 'oh'. */
104
size_t
105
ofputil_count_ipfix_stats(const struct ofp_header *oh)
106
1.42k
{
107
1.42k
    struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
108
1.42k
    ofpraw_pull_assert(&b);
109
110
1.42k
    return b.size / sizeof(struct ofputil_ipfix_stats);
111
1.42k
}
112
113
static void
114
print_ipfix_stat(struct ds *string, const char *leader, uint64_t stat,
115
                 int more)
116
68.7k
{
117
68.7k
    ds_put_cstr(string, leader);
118
68.7k
    if (stat != UINT64_MAX) {
119
53.6k
        ds_put_format(string, "%"PRIu64, stat);
120
53.6k
    } else {
121
15.0k
        ds_put_char(string, '?');
122
15.0k
    }
123
68.7k
    if (more) {
124
55.0k
        ds_put_cstr(string, ", ");
125
55.0k
    } else {
126
13.7k
        ds_put_cstr(string, "\n");
127
13.7k
    }
128
68.7k
}
129
130
static void
131
format_ipfix_stats(struct ds *string, const struct ofputil_ipfix_stats *is,
132
                   int indent)
133
6.87k
{
134
6.87k
    print_ipfix_stat(string, "flows=", is->total_flows, 1);
135
6.87k
    print_ipfix_stat(string, "current flows=", is->current_flows, 1);
136
6.87k
    print_ipfix_stat(string, "sampled pkts=", is->pkts, 1);
137
6.87k
    print_ipfix_stat(string, "ipv4 ok=", is->ipv4_pkts, 1);
138
6.87k
    print_ipfix_stat(string, "ipv6 ok=", is->ipv6_pkts, 1);
139
6.87k
    print_ipfix_stat(string, "tx pkts=", is->tx_pkts, 0);
140
6.87k
    ds_put_char_multiple(string, ' ', indent);
141
6.87k
    print_ipfix_stat(string, "pkts errs=", is->error_pkts, 1);
142
6.87k
    print_ipfix_stat(string, "ipv4 errs=", is->ipv4_error_pkts, 1);
143
6.87k
    print_ipfix_stat(string, "ipv6 errs=", is->ipv6_error_pkts, 1);
144
6.87k
    print_ipfix_stat(string, "tx errs=", is->tx_errors, 0);
145
6.87k
}
146
147
void
148
ofputil_format_ipfix_stats_bridge(struct ds *string,
149
                                  const struct ofputil_ipfix_stats *is)
150
358
{
151
358
    ds_put_cstr(string, "\n  bridge ipfix: ");
152
358
    format_ipfix_stats(string, is, 16);
153
358
}
154
155
void
156
ofputil_format_ipfix_stats_flow(struct ds *string,
157
                                const struct ofputil_ipfix_stats *is)
158
6.51k
{
159
6.51k
    ds_put_cstr(string, "  id");
160
6.51k
    ds_put_format(string, " %3"PRIuSIZE": ", (size_t) is->collector_set_id);
161
6.51k
    format_ipfix_stats(string, is, 10);
162
6.51k
}