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
|
/*
* Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ox-stat.h"
#include "byte-order.h"
#include "openvswitch/ofp-errors.h"
#include "openvswitch/compiler.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "unaligned.h"
VLOG_DEFINE_THIS_MODULE(ox_stat);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
/* OXS header
* ==========
*
* The header is 32 bits long. It looks like this:
*
* |31 16 15 9 8 7 0
* +----------------------------------+---------------+-+------------------+
* | oxs_class | oxs_field |r| oxs_length |
* +----------------------------------+---------------+-+------------------+
*
* where r stands for oxs_reserved. It is followed by oxs_length bytes of
* payload (the statistic's value).
*
* Internally, we represent a standard OXS header as a 64-bit integer with the
* above information in the most-significant bits.
*
*
* Experimenter OXS
* ================
*
* The header is 64 bits long. It looks like the diagram above except that a
* 32-bit experimenter ID, which we call oxs_experimenter and which identifies
* a vendor, is inserted just before the payload. Experimenter OXSs are
* identified by an all-1-bits oxs_class (OFPXSC_EXPERIMENTER). The oxs_length
* value *includes* the experimenter ID, so that the real payload is only
* oxs_length - 4 bytes long.
*
* Internally, we represent an experimenter OXS header as a 64-bit integer with
* the standard header in the upper 32 bits and the experimenter ID in the
* lower 32 bits. (It would be more convenient to swap the positions of the
* two 32-bit words, but this would be more error-prone because experimenter
* OXSs are very rarely used, so accidentally passing one through a 32-bit type
* somewhere in the OVS code would be hard to find.)
*/
/* OXS Class IDs.
* The high order bit differentiate reserved classes from member classes.
* Classes 0x0000 to 0x7FFF are member classes, allocated by ONF.
* Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation.
*/
enum ofp_oxs_class {
OFPXSC_OPENFLOW_BASIC = 0x8002, /* Basic stats class for OpenFlow */
OFPXSC_EXPERIMENTER = 0xFFFF, /* Experimenter class */
};
/* Functions for extracting raw field values from OXS headers. */
static uint32_t oxs_experimenter(uint64_t header) { return header; }
static int oxs_class(uint64_t header) { return header >> 48; }
static int oxs_field(uint64_t header) { return (header >> 41) & 0x7f; }
static int oxs_length(uint64_t header) { return (header >> 32) & 0xff; }
static bool
is_experimenter_oxs(uint64_t header)
{
return oxs_class(header) == OFPXSC_EXPERIMENTER;
}
/* The OXS header "length" field is somewhat tricky:
*
* - For a standard OXS header, the length is the number of bytes of the
* payload, and the payload consists of just the value.
*
* - For an experimenter OXS header, the length is the number of bytes in
* the payload plus 4 (the length of the experimenter ID). That is, the
* experimenter ID is included in oxs_length.
*
* This function returns the length of the experimenter ID field in 'header'.
* That is, for an experimenter OXS (when an experimenter ID is present), it
* returns 4, and for a standard OXS (when no experimenter ID is present), it
* returns 0. */
static int
oxs_experimenter_len(uint64_t header)
{
return is_experimenter_oxs(header) ? 4 : 0;
}
/* Returns the number of bytes that follow the header for an OXS entry with the
* given 'header'. */
static int
oxs_payload_len(uint64_t header)
{
return oxs_length(header) - oxs_experimenter_len(header);
}
/* Returns the number of bytes in the header for an OXS entry with the given
* 'header'. */
static int
oxs_header_len(uint64_t header)
{
return 4 + oxs_experimenter_len(header);
}
/* Assembles an OXS header from its components. */
#define OXS_HEADER(EXPERIMENTER, CLASS, FIELD, LENGTH) \
(((uint64_t) (CLASS) << 48) | \
((uint64_t) (FIELD) << 41) | \
((uint64_t) (LENGTH) << 32) | \
(EXPERIMENTER))
#define OXS_HEADER_FMT "%#"PRIx32":%d:%d:%d"
#define OXS_HEADER_ARGS(HEADER) \
oxs_experimenter(HEADER), oxs_class(HEADER), oxs_field(HEADER), \
oxs_length(HEADER)
/* Currently defined OXS. */
#define OXS_OF_DURATION OXS_HEADER (0, 0x8002, OFPXST_OFB_DURATION, 8)
#define OXS_OF_IDLE_TIME OXS_HEADER (0, 0x8002, OFPXST_OFB_IDLE_TIME, 8)
#define OXS_OF_FLOW_COUNT OXS_HEADER (0, 0x8002, OFPXST_OFB_FLOW_COUNT, 4)
#define OXS_OF_PACKET_COUNT OXS_HEADER (0, 0x8002, OFPXST_OFB_PACKET_COUNT, 8)
#define OXS_OF_BYTE_COUNT OXS_HEADER (0, 0x8002, OFPXST_OFB_BYTE_COUNT, 8)
/* Header for a group of OXS statistics. */
struct ofp_oxs_stat {
ovs_be16 reserved; /* Must be zero. */
ovs_be16 length; /* Stats Length */
};
BUILD_ASSERT_DECL(sizeof(struct ofp_oxs_stat) == 4);
static int oxs_pull_header__(struct ofpbuf *b, uint64_t *header);
static enum ofperr oxs_pull_raw(const uint8_t *, unsigned int stat_len,
struct oxs_stats *, uint8_t *oxs_field_set);
static int
oxs_pull_header__(struct ofpbuf *b, uint64_t *header)
{
if (b->size < 4) {
goto bad_len;
}
*header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
if (is_experimenter_oxs(*header)) {
if (b->size < 8) {
goto bad_len;
}
*header = ntohll(get_unaligned_be64(b->data));
}
if (oxs_length(*header) < oxs_experimenter_len(*header)) {
VLOG_WARN_RL(&rl, "OXS header "OXS_HEADER_FMT" has invalid length %d "
"(minimum is %d)",
OXS_HEADER_ARGS(*header), oxs_length(*header),
oxs_header_len(*header));
goto error;
}
ofpbuf_pull(b, oxs_header_len(*header));
return 0;
bad_len:
VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXS entry",
b->size);
error:
*header = 0;
return OFPERR_OFPBMC_BAD_LEN;
}
static enum ofperr
oxs_pull_entry__(struct ofpbuf *b, struct oxs_stats *stats,
uint8_t *oxs_field_set)
{
uint64_t header;
enum ofperr error = oxs_pull_header__(b, &header);
if (error) {
return error;
}
unsigned int payload_len = oxs_payload_len(header);
const void *payload = ofpbuf_try_pull(b, payload_len);
if (!payload) {
return OFPERR_OFPBMC_BAD_LEN;
}
switch (header) {
case OXS_OF_DURATION: {
uint64_t duration = ntohll(get_unaligned_be64(payload));
stats->duration_sec = duration >> 32;
stats->duration_nsec = duration;
}
break;
case OXS_OF_IDLE_TIME:
stats->idle_age = ntohll(get_unaligned_be64(payload)) >> 32;
break;
case OXS_OF_PACKET_COUNT:
stats->packet_count = ntohll(get_unaligned_be64(payload));
break;
case OXS_OF_BYTE_COUNT:
stats->byte_count = ntohll(get_unaligned_be64(payload));
break;
case OXS_OF_FLOW_COUNT:
stats->flow_count = ntohl(get_unaligned_be32(payload));
break;
default:
/* Unknown header. */
return 0;
}
if (oxs_field_set
&& oxs_class(header) == OFPXSC_OPENFLOW_BASIC
&& oxs_field(header) < CHAR_BIT * sizeof *oxs_field_set) {
*oxs_field_set |= 1 << oxs_field(header);
}
return error;
}
static enum ofperr
oxs_pull_raw(const uint8_t * p, unsigned int stat_len,
struct oxs_stats *stats, uint8_t *oxs_field_set)
{
struct ofpbuf b = ofpbuf_const_initializer(p, stat_len);
while (b.size) {
const uint8_t *pos = b.data;
enum ofperr error = oxs_pull_entry__(&b, stats, oxs_field_set);
if (error && error != OFPERR_OFPBMC_BAD_FIELD) {
VLOG_DBG_RL(&rl, "error parsing OXS at offset %"PRIdPTR" "
"within match (%s)",
pos - p, ofperr_to_string(error));
return error;
}
}
return 0;
}
/* Retrieve struct ofp_oxs_stat from 'b', followed by the flow entry
* statistics in OXS format.
*
* Returns error if message parsing fails, otherwise returns zero . */
enum ofperr
oxs_pull_stat(struct ofpbuf *b, struct oxs_stats *stats,
uint16_t *statlen, uint8_t *oxs_field_set)
{
memset(stats, 0xff, sizeof *stats);
struct ofp_oxs_stat *oxs = b->data;
if (b->size < sizeof *oxs) {
return OFPERR_OFPBMC_BAD_LEN;
}
uint16_t stat_len = ntohs(oxs->length);
if (stat_len < sizeof *oxs) {
return OFPERR_OFPBMC_BAD_LEN;
}
uint8_t *p = ofpbuf_try_pull(b, ROUND_UP(stat_len, 8));
if (!p) {
VLOG_DBG_RL(&rl, "oxs length %u, rounded up to a "
"multiple of 8, is longer than space in message (max "
"length %" PRIu32 ")", stat_len, b->size);
return OFPERR_OFPBMC_BAD_LEN;
}
*statlen = ROUND_UP(stat_len, 8);
return oxs_pull_raw(p + sizeof *oxs, stat_len - sizeof *oxs, stats,
oxs_field_set);
}
static void
oxs_put__(struct ofpbuf *b, uint64_t header,
const void *value, size_t value_size)
{
if (is_experimenter_oxs(header)) {
ovs_be64 be64 = htonll(header);
ofpbuf_put(b, &be64, sizeof be64);
} else {
ovs_be32 be32 = htonl(header >> 32);
ofpbuf_put(b, &be32, sizeof be32);
}
ovs_assert(oxs_payload_len(header) == value_size);
ofpbuf_put(b, value, value_size);
}
static void
oxs_put32(struct ofpbuf *b, uint64_t header, uint32_t value_)
{
ovs_be32 value = htonl(value_);
oxs_put__(b, header, &value, sizeof value);
}
static void
oxs_put64(struct ofpbuf *b, uint64_t header, uint64_t value_)
{
ovs_be64 value = htonll(value_);
oxs_put__(b, header, &value, sizeof value);
}
/* Appends to 'b' an struct ofp_oxs_stat followed by the flow entry statistics
* in OXS format , plus enough zero bytes to pad the data appended out to a
* multiple of 8.
*
* Specify the OpenFlow version in use as 'version'.
*
* This function can cause 'b''s data to be reallocated.
*
* Returns the number of bytes appended to 'b', excluding the padding.Never
* returns zero. */
void
oxs_put_stats(struct ofpbuf *b, const struct oxs_stats *stats)
{
size_t start = b->size;
/* Put empty header. */
struct ofp_oxs_stat *oxs;
ofpbuf_put_zeros(b, sizeof *oxs);
/* Put stats. */
if (stats->duration_sec != UINT32_MAX) {
oxs_put64(b, OXS_OF_DURATION,
(((uint64_t) stats->duration_sec << 32)
| stats->duration_nsec));
}
if (stats->idle_age != UINT32_MAX) {
oxs_put64(b, OXS_OF_IDLE_TIME, (uint64_t) stats->idle_age << 32);
}
if (stats->packet_count != UINT64_MAX) {
oxs_put64(b, OXS_OF_PACKET_COUNT, stats->packet_count);
}
if (stats->byte_count != UINT64_MAX) {
oxs_put64(b, OXS_OF_BYTE_COUNT, stats->byte_count);
}
if (stats->flow_count != UINT32_MAX) {
oxs_put32(b, OXS_OF_FLOW_COUNT, stats->flow_count);
}
/* Fill in size in header, then pad to multiple of 8 bytes. */
oxs = ofpbuf_at(b, start, sizeof *oxs);
oxs->length = htons(b->size - start);
ofpbuf_put_zeros(b, PAD_SIZE(b->size - start, 8));
}
|