]> git.proxmox.com Git - ovs.git/blame - lib/ofp-print.c
Switch many macros from using CONTAINER_OF to using OBJECT_CONTAINING.
[ovs.git] / lib / ofp-print.c
CommitLineData
064af421 1/*
67a4917b 2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "ofp-print.h"
064af421
BP
19
20#include <errno.h>
21#include <inttypes.h>
7f3adc00 22#include <sys/types.h>
064af421
BP
23#include <netinet/in.h>
24#include <sys/wait.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <ctype.h>
28
29#include "compiler.h"
30#include "dynamic-string.h"
31#include "flow.h"
32#include "ofpbuf.h"
33#include "openflow/openflow.h"
34#include "openflow/nicira-ext.h"
35#include "packets.h"
36#include "pcap.h"
37#include "util.h"
e5f1d483 38#include "xtoxll.h"
064af421
BP
39
40static void ofp_print_port_name(struct ds *string, uint16_t port);
064af421
BP
41
42/* Returns a string that represents the contents of the Ethernet frame in the
43 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
44 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
45 * bytes were captured).
46 *
47 * The caller must free the returned string.
48 *
49 * This starts and kills a tcpdump subprocess so it's quite expensive. */
50char *
67a4917b 51ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
064af421
BP
52{
53 struct ds ds = DS_EMPTY_INITIALIZER;
54 struct ofpbuf buf;
55
56 char command[128];
57 FILE *pcap;
58 FILE *tcpdump;
59 int status;
60 int c;
61
62 buf.data = (void *) data;
63 buf.size = len;
64
65 pcap = tmpfile();
66 if (!pcap) {
67 ovs_error(errno, "tmpfile");
68 return xstrdup("<error>");
69 }
70 pcap_write_header(pcap);
71 pcap_write(pcap, &buf);
72 fflush(pcap);
73 if (ferror(pcap)) {
74 ovs_error(errno, "error writing temporary file");
75 }
76 rewind(pcap);
77
78 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
79 fileno(pcap));
80 tcpdump = popen(command, "r");
81 fclose(pcap);
82 if (!tcpdump) {
83 ovs_error(errno, "exec(\"%s\")", command);
84 return xstrdup("<error>");
85 }
86
87 while ((c = getc(tcpdump)) != EOF) {
88 ds_put_char(&ds, c);
89 }
90
91 status = pclose(tcpdump);
92 if (WIFEXITED(status)) {
93 if (WEXITSTATUS(status))
94 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
95 } else if (WIFSIGNALED(status)) {
d295e8e9 96 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
064af421
BP
97 }
98 return ds_cstr(&ds);
99}
100
101/* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
102 * at the given 'verbosity' level. */
103static void
104ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
105{
106 const struct ofp_packet_in *op = oh;
107 size_t data_len;
108
109 ds_put_format(string, " total_len=%"PRIu16" in_port=",
110 ntohs(op->total_len));
111 ofp_print_port_name(string, ntohs(op->in_port));
112
113 if (op->reason == OFPR_ACTION)
114 ds_put_cstr(string, " (via action)");
115 else if (op->reason != OFPR_NO_MATCH)
116 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
117
118 data_len = len - offsetof(struct ofp_packet_in, data);
119 ds_put_format(string, " data_len=%zu", data_len);
120 if (htonl(op->buffer_id) == UINT32_MAX) {
121 ds_put_format(string, " (unbuffered)");
122 if (ntohs(op->total_len) != data_len)
123 ds_put_format(string, " (***total_len != data_len***)");
124 } else {
125 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
126 if (ntohs(op->total_len) < data_len)
127 ds_put_format(string, " (***total_len < data_len***)");
128 }
129 ds_put_char(string, '\n');
130
131 if (verbosity > 0) {
132 flow_t flow;
133 struct ofpbuf packet;
134 struct ofp_match match;
135 packet.data = (void *) op->data;
136 packet.size = data_len;
659586ef
JG
137 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
138 flow_to_match(&flow, 0, false, &match);
064af421
BP
139 ofp_print_match(string, &match, verbosity);
140 ds_put_char(string, '\n');
141 }
142 if (verbosity > 1) {
143 char *packet = ofp_packet_to_string(op->data, data_len,
d295e8e9 144 ntohs(op->total_len));
064af421
BP
145 ds_put_cstr(string, packet);
146 free(packet);
147 }
148}
149
d295e8e9 150static void ofp_print_port_name(struct ds *string, uint16_t port)
064af421
BP
151{
152 const char *name;
153 switch (port) {
154 case OFPP_IN_PORT:
155 name = "IN_PORT";
156 break;
157 case OFPP_TABLE:
158 name = "TABLE";
159 break;
160 case OFPP_NORMAL:
161 name = "NORMAL";
162 break;
163 case OFPP_FLOOD:
164 name = "FLOOD";
165 break;
166 case OFPP_ALL:
167 name = "ALL";
168 break;
169 case OFPP_CONTROLLER:
170 name = "CONTROLLER";
171 break;
172 case OFPP_LOCAL:
173 name = "LOCAL";
174 break;
175 case OFPP_NONE:
176 name = "NONE";
177 break;
178 default:
179 ds_put_format(string, "%"PRIu16, port);
180 return;
181 }
182 ds_put_cstr(string, name);
183}
184
185static void
186ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
187{
188 switch (ntohs(nah->subtype)) {
189 case NXAST_RESUBMIT: {
190 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
191 ds_put_format(string, "resubmit:");
192 ofp_print_port_name(string, ntohs(nar->in_port));
193 break;
194 }
195
659586ef
JG
196 case NXAST_SET_TUNNEL: {
197 const struct nx_action_set_tunnel *nast =
198 (struct nx_action_set_tunnel *)nah;
199 ds_put_format(string, "set_tunnel:0x%08"PRIx32, ntohl(nast->tun_id));
200 break;
201 }
202
a77d89b8
BP
203 case NXAST_DROP_SPOOFED_ARP:
204 ds_put_cstr(string, "drop_spoofed_arp");
205 break;
206
064af421 207 default:
a77d89b8 208 ds_put_format(string, "***unknown Nicira action:%d***",
064af421
BP
209 ntohs(nah->subtype));
210 }
211}
212
213static int
d295e8e9
JP
214ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
215 size_t actions_len)
064af421
BP
216{
217 uint16_t type;
218 size_t len;
219
220 struct openflow_action {
221 size_t min_size;
222 size_t max_size;
223 };
224
225 const struct openflow_action of_actions[] = {
226 [OFPAT_OUTPUT] = {
227 sizeof(struct ofp_action_output),
228 sizeof(struct ofp_action_output),
229 },
230 [OFPAT_SET_VLAN_VID] = {
231 sizeof(struct ofp_action_vlan_vid),
232 sizeof(struct ofp_action_vlan_vid),
233 },
234 [OFPAT_SET_VLAN_PCP] = {
235 sizeof(struct ofp_action_vlan_pcp),
236 sizeof(struct ofp_action_vlan_pcp),
237 },
238 [OFPAT_STRIP_VLAN] = {
239 sizeof(struct ofp_action_header),
240 sizeof(struct ofp_action_header),
241 },
242 [OFPAT_SET_DL_SRC] = {
243 sizeof(struct ofp_action_dl_addr),
244 sizeof(struct ofp_action_dl_addr),
245 },
246 [OFPAT_SET_DL_DST] = {
247 sizeof(struct ofp_action_dl_addr),
248 sizeof(struct ofp_action_dl_addr),
249 },
250 [OFPAT_SET_NW_SRC] = {
251 sizeof(struct ofp_action_nw_addr),
252 sizeof(struct ofp_action_nw_addr),
253 },
254 [OFPAT_SET_NW_DST] = {
255 sizeof(struct ofp_action_nw_addr),
256 sizeof(struct ofp_action_nw_addr),
257 },
959a2ecd
JP
258 [OFPAT_SET_NW_TOS] = {
259 sizeof(struct ofp_action_nw_tos),
260 sizeof(struct ofp_action_nw_tos),
261 },
064af421
BP
262 [OFPAT_SET_TP_SRC] = {
263 sizeof(struct ofp_action_tp_port),
264 sizeof(struct ofp_action_tp_port),
265 },
266 [OFPAT_SET_TP_DST] = {
267 sizeof(struct ofp_action_tp_port),
268 sizeof(struct ofp_action_tp_port),
269 }
270 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
271 };
272
273 if (actions_len < sizeof *ah) {
274 ds_put_format(string, "***action array too short for next action***\n");
275 return -1;
276 }
277
278 type = ntohs(ah->type);
279 len = ntohs(ah->len);
280 if (actions_len < len) {
281 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
282 return -1;
283 }
284
285 if ((len % 8) != 0) {
d295e8e9 286 ds_put_format(string,
064af421
BP
287 "***action %"PRIu16" length not a multiple of 8***\n",
288 type);
289 return -1;
290 }
291
292 if (type < ARRAY_SIZE(of_actions)) {
293 const struct openflow_action *act = &of_actions[type];
294 if ((len < act->min_size) || (len > act->max_size)) {
d295e8e9 295 ds_put_format(string,
064af421
BP
296 "***action %"PRIu16" wrong length: %zu***\n", type, len);
297 return -1;
298 }
299 }
d295e8e9 300
064af421
BP
301 switch (type) {
302 case OFPAT_OUTPUT: {
303 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
d295e8e9 304 uint16_t port = ntohs(oa->port);
064af421
BP
305 if (port < OFPP_MAX) {
306 ds_put_format(string, "output:%"PRIu16, port);
307 } else {
308 ofp_print_port_name(string, port);
309 if (port == OFPP_CONTROLLER) {
310 if (oa->max_len) {
311 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
312 } else {
313 ds_put_cstr(string, ":all");
314 }
315 }
316 }
317 break;
318 }
319
c4d279ab
BP
320 case OFPAT_ENQUEUE: {
321 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
322 unsigned int port = ntohs(ea->port);
323 unsigned int queue_id = ntohl(ea->queue_id);
324 ds_put_format(string, "enqueue:");
325 if (port != OFPP_IN_PORT) {
326 ds_put_format(string, "%u", port);
327 } else {
328 ds_put_cstr(string, "IN_PORT");
329 }
330 ds_put_format(string, "q%u", queue_id);
331 break;
332 }
333
064af421
BP
334 case OFPAT_SET_VLAN_VID: {
335 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
336 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
337 break;
338 }
339
340 case OFPAT_SET_VLAN_PCP: {
341 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
342 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
343 break;
344 }
345
346 case OFPAT_STRIP_VLAN:
347 ds_put_cstr(string, "strip_vlan");
348 break;
349
350 case OFPAT_SET_DL_SRC: {
351 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 352 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
064af421
BP
353 ETH_ADDR_ARGS(da->dl_addr));
354 break;
355 }
356
357 case OFPAT_SET_DL_DST: {
358 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 359 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
064af421
BP
360 ETH_ADDR_ARGS(da->dl_addr));
361 break;
362 }
363
364 case OFPAT_SET_NW_SRC: {
365 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
366 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
367 break;
368 }
369
370 case OFPAT_SET_NW_DST: {
371 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
372 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
373 break;
374 }
375
959a2ecd
JP
376 case OFPAT_SET_NW_TOS: {
377 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
378 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
379 break;
380 }
381
064af421
BP
382 case OFPAT_SET_TP_SRC: {
383 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
384 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
385 break;
386 }
387
388 case OFPAT_SET_TP_DST: {
389 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
390 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
391 break;
392 }
393
394 case OFPAT_VENDOR: {
d295e8e9 395 struct ofp_action_vendor_header *avh
064af421
BP
396 = (struct ofp_action_vendor_header *)ah;
397 if (len < sizeof *avh) {
398 ds_put_format(string, "***ofpat_vendor truncated***\n");
399 return -1;
400 }
401 if (avh->vendor == htonl(NX_VENDOR_ID)) {
402 ofp_print_nx_action(string, (struct nx_action_header *)avh);
403 } else {
404 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
405 }
406 break;
407 }
408
409 default:
410 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
411 break;
412 }
413
414 return len;
415}
416
d295e8e9 417void
064af421 418ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
d295e8e9 419 size_t actions_len)
064af421
BP
420{
421 uint8_t *p = (uint8_t *)action;
422 int len = 0;
423
424 ds_put_cstr(string, "actions=");
425 if (!actions_len) {
426 ds_put_cstr(string, "drop");
427 }
428 while (actions_len > 0) {
429 if (len) {
430 ds_put_cstr(string, ",");
431 }
d295e8e9 432 len = ofp_print_action(string, (struct ofp_action_header *)p,
064af421
BP
433 actions_len);
434 if (len < 0) {
435 return;
436 }
437 p += len;
438 actions_len -= len;
439 }
440}
441
442/* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
443 * at the given 'verbosity' level. */
444static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
d295e8e9 445 int verbosity)
064af421
BP
446{
447 const struct ofp_packet_out *opo = oh;
448 size_t actions_len = ntohs(opo->actions_len);
449
450 ds_put_cstr(string, " in_port=");
451 ofp_print_port_name(string, ntohs(opo->in_port));
452
453 ds_put_format(string, " actions_len=%zu ", actions_len);
454 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
455 ds_put_format(string, "***packet too short for action length***\n");
456 return;
457 }
458 ofp_print_actions(string, opo->actions, actions_len);
459
460 if (ntohl(opo->buffer_id) == UINT32_MAX) {
461 int data_len = len - sizeof *opo - actions_len;
462 ds_put_format(string, " data_len=%d", data_len);
463 if (verbosity > 0 && len > sizeof *opo) {
464 char *packet = ofp_packet_to_string(
465 (uint8_t *)opo->actions + actions_len, data_len, data_len);
466 ds_put_char(string, '\n');
467 ds_put_cstr(string, packet);
468 free(packet);
469 }
470 } else {
471 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
472 }
473 ds_put_char(string, '\n');
474}
475
476/* qsort comparison function. */
477static int
478compare_ports(const void *a_, const void *b_)
479{
480 const struct ofp_phy_port *a = a_;
481 const struct ofp_phy_port *b = b_;
482 uint16_t ap = ntohs(a->port_no);
483 uint16_t bp = ntohs(b->port_no);
484
485 return ap < bp ? -1 : ap > bp;
486}
487
488static void ofp_print_port_features(struct ds *string, uint32_t features)
489{
490 if (features == 0) {
491 ds_put_cstr(string, "Unsupported\n");
492 return;
493 }
494 if (features & OFPPF_10MB_HD) {
495 ds_put_cstr(string, "10MB-HD ");
496 }
497 if (features & OFPPF_10MB_FD) {
498 ds_put_cstr(string, "10MB-FD ");
499 }
500 if (features & OFPPF_100MB_HD) {
501 ds_put_cstr(string, "100MB-HD ");
502 }
503 if (features & OFPPF_100MB_FD) {
504 ds_put_cstr(string, "100MB-FD ");
505 }
506 if (features & OFPPF_1GB_HD) {
507 ds_put_cstr(string, "1GB-HD ");
508 }
509 if (features & OFPPF_1GB_FD) {
510 ds_put_cstr(string, "1GB-FD ");
511 }
512 if (features & OFPPF_10GB_FD) {
513 ds_put_cstr(string, "10GB-FD ");
514 }
515 if (features & OFPPF_COPPER) {
516 ds_put_cstr(string, "COPPER ");
517 }
518 if (features & OFPPF_FIBER) {
519 ds_put_cstr(string, "FIBER ");
520 }
521 if (features & OFPPF_AUTONEG) {
522 ds_put_cstr(string, "AUTO_NEG ");
523 }
524 if (features & OFPPF_PAUSE) {
525 ds_put_cstr(string, "AUTO_PAUSE ");
526 }
527 if (features & OFPPF_PAUSE_ASYM) {
528 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
529 }
530 ds_put_char(string, '\n');
531}
532
533static void
534ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
535{
536 uint8_t name[OFP_MAX_PORT_NAME_LEN];
537 int j;
538
539 memcpy(name, port->name, sizeof name);
540 for (j = 0; j < sizeof name - 1; j++) {
541 if (!isprint(name[j])) {
542 break;
543 }
544 }
545 name[j] = '\0';
546
547 ds_put_char(string, ' ');
548 ofp_print_port_name(string, ntohs(port->port_no));
549 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
550 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
551 ntohl(port->state));
552 if (port->curr) {
553 ds_put_format(string, " current: ");
554 ofp_print_port_features(string, ntohl(port->curr));
555 }
556 if (port->advertised) {
557 ds_put_format(string, " advertised: ");
558 ofp_print_port_features(string, ntohl(port->advertised));
559 }
560 if (port->supported) {
561 ds_put_format(string, " supported: ");
562 ofp_print_port_features(string, ntohl(port->supported));
563 }
564 if (port->peer) {
565 ds_put_format(string, " peer: ");
566 ofp_print_port_features(string, ntohl(port->peer));
567 }
568}
569
570/* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
571 * 'string' at the given 'verbosity' level. */
572static void
573ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
67a4917b 574 int verbosity OVS_UNUSED)
064af421
BP
575{
576 const struct ofp_switch_features *osf = oh;
577 struct ofp_phy_port *port_list;
578 int n_ports;
579 int i;
580
d295e8e9 581 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
064af421
BP
582 osf->header.version, ntohll(osf->datapath_id));
583 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
584 ntohl(osf->n_buffers));
585 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
586 ntohl(osf->capabilities), ntohl(osf->actions));
587
588 if (ntohs(osf->header.length) >= sizeof *osf) {
589 len = MIN(len, ntohs(osf->header.length));
590 }
591 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
592
d295e8e9 593 port_list = xmemdup(osf->ports, len - sizeof *osf);
064af421
BP
594 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
595 for (i = 0; i < n_ports; i++) {
596 ofp_print_phy_port(string, &port_list[i]);
597 }
598 free(port_list);
599}
600
601/* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
602 * at the given 'verbosity' level. */
603static void
67a4917b
BP
604ofp_print_switch_config(struct ds *string, const void *oh,
605 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
064af421
BP
606{
607 const struct ofp_switch_config *osc = oh;
608 uint16_t flags;
609
610 flags = ntohs(osc->flags);
064af421
BP
611 if (flags) {
612 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
613 }
614
615 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
616}
617
618static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 619 int verbosity, const char *format, ...)
064af421
BP
620 __attribute__((format(printf, 5, 6)));
621
622static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 623 int verbosity, const char *format, ...)
064af421
BP
624{
625 if (is_wild && verbosity < 2) {
626 return;
627 }
628 ds_put_cstr(string, leader);
629 if (!is_wild) {
630 va_list args;
631
632 va_start(args, format);
633 ds_put_format_valist(string, format, args);
634 va_end(args);
635 } else {
636 ds_put_char(string, '*');
637 }
638 ds_put_char(string, ',');
639}
640
641static void
642print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
643 uint32_t wild_bits, int verbosity)
644{
645 if (wild_bits >= 32 && verbosity < 2) {
646 return;
647 }
648 ds_put_cstr(string, leader);
649 if (wild_bits < 32) {
650 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
651 if (wild_bits) {
652 ds_put_format(string, "/%d", 32 - wild_bits);
653 }
654 } else {
655 ds_put_char(string, '*');
656 }
657 ds_put_char(string, ',');
658}
659
4f2cad2c 660void
064af421
BP
661ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
662{
663 char *s = ofp_match_to_string(om, verbosity);
664 ds_put_cstr(f, s);
665 free(s);
666}
667
668char *
669ofp_match_to_string(const struct ofp_match *om, int verbosity)
670{
671 struct ds f = DS_EMPTY_INITIALIZER;
672 uint32_t w = ntohl(om->wildcards);
673 bool skip_type = false;
674 bool skip_proto = false;
675
676 if (!(w & OFPFW_DL_TYPE)) {
677 skip_type = true;
678 if (om->dl_type == htons(ETH_TYPE_IP)) {
679 if (!(w & OFPFW_NW_PROTO)) {
680 skip_proto = true;
681 if (om->nw_proto == IP_TYPE_ICMP) {
682 ds_put_cstr(&f, "icmp,");
683 } else if (om->nw_proto == IP_TYPE_TCP) {
684 ds_put_cstr(&f, "tcp,");
685 } else if (om->nw_proto == IP_TYPE_UDP) {
686 ds_put_cstr(&f, "udp,");
687 } else {
688 ds_put_cstr(&f, "ip,");
689 skip_proto = false;
690 }
691 } else {
692 ds_put_cstr(&f, "ip,");
693 }
694 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
695 ds_put_cstr(&f, "arp,");
696 } else {
697 skip_type = false;
698 }
699 }
659586ef
JG
700 if (w & NXFW_TUN_ID) {
701 ds_put_cstr(&f, "tun_id_wild,");
702 }
064af421
BP
703 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
704 "%d", ntohs(om->in_port));
705 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
6a1f89c8 706 "%d", ntohs(om->dl_vlan));
959a2ecd
JP
707 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
708 "%d", om->dl_vlan_pcp);
064af421
BP
709 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
710 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
711 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
712 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
713 if (!skip_type) {
714 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
715 "0x%04x", ntohs(om->dl_type));
716 }
717 print_ip_netmask(&f, "nw_src=", om->nw_src,
718 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
719 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
720 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
721 if (!skip_proto) {
fb892732
JP
722 if (om->dl_type == htons(ETH_TYPE_ARP)) {
723 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
724 "%u", om->nw_proto);
725 } else {
726 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
727 "%u", om->nw_proto);
834377ea
JP
728 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
729 "%u", om->nw_tos);
fb892732 730 }
064af421
BP
731 }
732 if (om->nw_proto == IP_TYPE_ICMP) {
733 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
734 "%d", ntohs(om->icmp_type));
735 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
736 "%d", ntohs(om->icmp_code));
737 } else {
738 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
739 "%d", ntohs(om->tp_src));
740 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
741 "%d", ntohs(om->tp_dst));
742 }
743 return ds_cstr(&f);
744}
745
746/* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
747 * at the given 'verbosity' level. */
748static void
d295e8e9 749ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
064af421
BP
750 int verbosity)
751{
752 const struct ofp_flow_mod *ofm = oh;
753
754 ofp_print_match(string, &ofm->match, verbosity);
755 switch (ntohs(ofm->command)) {
756 case OFPFC_ADD:
757 ds_put_cstr(string, " ADD: ");
758 break;
759 case OFPFC_MODIFY:
760 ds_put_cstr(string, " MOD: ");
761 break;
762 case OFPFC_MODIFY_STRICT:
763 ds_put_cstr(string, " MOD_STRICT: ");
764 break;
765 case OFPFC_DELETE:
766 ds_put_cstr(string, " DEL: ");
767 break;
768 case OFPFC_DELETE_STRICT:
769 ds_put_cstr(string, " DEL_STRICT: ");
770 break;
771 default:
772 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
773 }
8cce2125 774 ds_put_format(string, "cookie:0x%"PRIx64" idle:%d hard:%d pri:%d "
d295e8e9 775 "buf:%#x flags:%"PRIx16" ", ntohll(ofm->cookie),
064af421
BP
776 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
777 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
ca069229 778 ntohl(ofm->buffer_id), ntohs(ofm->flags));
064af421
BP
779 ofp_print_actions(string, ofm->actions,
780 len - offsetof(struct ofp_flow_mod, actions));
781 ds_put_char(string, '\n');
782}
783
ca069229 784/* Pretty-print the OFPT_FLOW_REMOVED packet of 'len' bytes at 'oh' to 'string'
064af421
BP
785 * at the given 'verbosity' level. */
786static void
d295e8e9 787ofp_print_flow_removed(struct ds *string, const void *oh,
67a4917b 788 size_t len OVS_UNUSED, int verbosity)
064af421 789{
ca069229 790 const struct ofp_flow_removed *ofr = oh;
064af421 791
ca069229 792 ofp_print_match(string, &ofr->match, verbosity);
064af421 793 ds_put_cstr(string, " reason=");
ca069229
JP
794 switch (ofr->reason) {
795 case OFPRR_IDLE_TIMEOUT:
064af421
BP
796 ds_put_cstr(string, "idle");
797 break;
ca069229 798 case OFPRR_HARD_TIMEOUT:
064af421
BP
799 ds_put_cstr(string, "hard");
800 break;
ca069229
JP
801 case OFPRR_DELETE:
802 ds_put_cstr(string, "delete");
803 break;
064af421 804 default:
ca069229 805 ds_put_format(string, "**%"PRIu8"**", ofr->reason);
064af421
BP
806 break;
807 }
d295e8e9 808 ds_put_format(string,
8cce2125 809 " cookie0x%"PRIx64" pri%"PRIu16" secs%"PRIu32" nsecs%"PRIu32
d295e8e9 810 " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
26c3f94a 811 ntohll(ofr->cookie),
ca069229 812 ofr->match.wildcards ? ntohs(ofr->priority) : (uint16_t)-1,
26c3f94a 813 ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
d295e8e9 814 ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
26c3f94a 815 ntohll(ofr->byte_count));
064af421
BP
816}
817
818static void
67a4917b
BP
819ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
820 int verbosity OVS_UNUSED)
064af421
BP
821{
822 const struct ofp_port_mod *opm = oh;
823
824 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
d295e8e9 825 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
064af421
BP
826 ntohl(opm->config), ntohl(opm->mask));
827 ds_put_format(string, " advertise: ");
828 if (opm->advertise) {
829 ofp_print_port_features(string, ntohl(opm->advertise));
830 } else {
831 ds_put_format(string, "UNCHANGED\n");
832 }
833}
834
835struct error_type {
836 int type;
837 int code;
838 const char *name;
839};
840
841static const struct error_type error_types[] = {
842#define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
843#define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
844 ERROR_TYPE(OFPET_HELLO_FAILED),
845 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
49bdc010 846 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
064af421
BP
847
848 ERROR_TYPE(OFPET_BAD_REQUEST),
849 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
850 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
851 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
b4785c1e
JP
852 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
853 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
49bdc010
JP
854 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
855 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
b4785c1e 856 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
49bdc010 857 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
064af421
BP
858
859 ERROR_TYPE(OFPET_BAD_ACTION),
860 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
861 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
862 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
863 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
864 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
b4785c1e 865 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
49bdc010 866 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
b4785c1e 867 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
064af421
BP
868
869 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
b4785c1e 870 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
49bdc010
JP
871 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
872 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
873 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
b4785c1e
JP
874 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
875
876 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
877 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
878 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
064af421
BP
879};
880#define N_ERROR_TYPES ARRAY_SIZE(error_types)
881
882static const char *
883lookup_error_type(int type)
884{
885 const struct error_type *t;
886
887 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
888 if (t->type == type && t->code == -1) {
889 return t->name;
890 }
891 }
892 return "?";
893}
894
895static const char *
896lookup_error_code(int type, int code)
897{
898 const struct error_type *t;
899
900 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
901 if (t->type == type && t->code == code) {
902 return t->name;
903 }
904 }
905 return "?";
906}
907
908/* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
909 * at the given 'verbosity' level. */
910static void
d295e8e9 911ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
67a4917b 912 int verbosity OVS_UNUSED)
064af421
BP
913{
914 const struct ofp_error_msg *oem = oh;
915 int type = ntohs(oem->type);
916 int code = ntohs(oem->code);
917 char *s;
918
919 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
920 type, lookup_error_type(type),
921 code, lookup_error_code(type, code));
922
923 switch (type) {
924 case OFPET_HELLO_FAILED:
925 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
926 break;
927
928 case OFPET_BAD_REQUEST:
929 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
930 ds_put_cstr(string, s);
931 free(s);
932 break;
933
934 default:
935 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
936 break;
937 }
938}
939
940/* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
941 * at the given 'verbosity' level. */
942static void
67a4917b
BP
943ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
944 int verbosity OVS_UNUSED)
064af421
BP
945{
946 const struct ofp_port_status *ops = oh;
947
948 if (ops->reason == OFPPR_ADD) {
949 ds_put_format(string, " ADD:");
950 } else if (ops->reason == OFPPR_DELETE) {
951 ds_put_format(string, " DEL:");
952 } else if (ops->reason == OFPPR_MODIFY) {
953 ds_put_format(string, " MOD:");
954 }
955
956 ofp_print_phy_port(string, &ops->desc);
957}
958
959static void
67a4917b
BP
960ofp_desc_stats_reply(struct ds *string, const void *body,
961 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
064af421
BP
962{
963 const struct ofp_desc_stats *ods = body;
964
d295e8e9 965 ds_put_format(string, "Manufacturer: %.*s\n",
dd70b475
JP
966 (int) sizeof ods->mfr_desc, ods->mfr_desc);
967 ds_put_format(string, "Hardware: %.*s\n",
968 (int) sizeof ods->hw_desc, ods->hw_desc);
969 ds_put_format(string, "Software: %.*s\n",
970 (int) sizeof ods->sw_desc, ods->sw_desc);
971 ds_put_format(string, "Serial Num: %.*s\n",
972 (int) sizeof ods->serial_num, ods->serial_num);
973 ds_put_format(string, "DP Description: %.*s\n",
974 (int) sizeof ods->dp_desc, ods->dp_desc);
064af421
BP
975}
976
977static void
67a4917b
BP
978ofp_flow_stats_request(struct ds *string, const void *oh,
979 size_t len OVS_UNUSED, int verbosity)
064af421
BP
980{
981 const struct ofp_flow_stats_request *fsr = oh;
982
983 if (fsr->table_id == 0xff) {
984 ds_put_format(string, " table_id=any, ");
985 } else {
986 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
987 }
988
989 ofp_print_match(string, &fsr->match, verbosity);
990}
991
992static void
993ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
994 int verbosity)
995{
996 const char *body = body_;
997 const char *pos = body;
998 for (;;) {
999 const struct ofp_flow_stats *fs;
1000 ptrdiff_t bytes_left = body + len - pos;
1001 size_t length;
1002
1003 if (bytes_left < sizeof *fs) {
1004 if (bytes_left != 0) {
1005 ds_put_format(string, " ***%td leftover bytes at end***",
1006 bytes_left);
1007 }
1008 break;
1009 }
1010
1011 fs = (const void *) pos;
1012 length = ntohs(fs->length);
1013 if (length < sizeof *fs) {
1014 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1015 length, sizeof *fs);
1016 break;
1017 } else if (length > bytes_left) {
1018 ds_put_format(string,
1019 " ***length=%zu but only %td bytes left***",
1020 length, bytes_left);
1021 break;
1022 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1023 ds_put_format(string,
1024 " ***length=%zu has %zu bytes leftover in "
1025 "final action***",
1026 length,
1027 (length - sizeof *fs) % sizeof fs->actions[0]);
1028 break;
1029 }
1030
8cce2125 1031 ds_put_format(string, " cookie=0x%"PRIx64", ", ntohll(fs->cookie));
d295e8e9 1032 ds_put_format(string, "duration_sec=%"PRIu32"s, ",
26c3f94a 1033 ntohl(fs->duration_sec));
d295e8e9 1034 ds_put_format(string, "duration_nsec=%"PRIu32"ns, ",
26c3f94a 1035 ntohl(fs->duration_nsec));
064af421 1036 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
d295e8e9 1037 ds_put_format(string, "priority=%"PRIu16", ",
064af421
BP
1038 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1039 ds_put_format(string, "n_packets=%"PRIu64", ",
1040 ntohll(fs->packet_count));
1041 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1042 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1043 ds_put_format(string, "idle_timeout=%"PRIu16",",
1044 ntohs(fs->idle_timeout));
1045 }
1046 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1047 ds_put_format(string, "hard_timeout=%"PRIu16",",
1048 ntohs(fs->hard_timeout));
1049 }
1050 ofp_print_match(string, &fs->match, verbosity);
1051 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1052 ds_put_char(string, '\n');
1053
1054 pos += length;
1055 }
1056}
1057
1058static void
1059ofp_aggregate_stats_request(struct ds *string, const void *oh,
67a4917b 1060 size_t len OVS_UNUSED, int verbosity)
064af421
BP
1061{
1062 const struct ofp_aggregate_stats_request *asr = oh;
1063
1064 if (asr->table_id == 0xff) {
1065 ds_put_format(string, " table_id=any, ");
1066 } else {
1067 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1068 }
1069
1070 ofp_print_match(string, &asr->match, verbosity);
1071}
1072
1073static void
1074ofp_aggregate_stats_reply(struct ds *string, const void *body_,
67a4917b 1075 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
064af421
BP
1076{
1077 const struct ofp_aggregate_stats_reply *asr = body_;
1078
1079 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1080 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1081 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1082}
1083
d295e8e9 1084static void print_port_stat(struct ds *string, const char *leader,
064af421
BP
1085 uint64_t stat, int more)
1086{
1087 ds_put_cstr(string, leader);
1088 if (stat != -1) {
1089 ds_put_format(string, "%"PRIu64, stat);
1090 } else {
1091 ds_put_char(string, '?');
1092 }
1093 if (more) {
1094 ds_put_cstr(string, ", ");
1095 } else {
1096 ds_put_cstr(string, "\n");
1097 }
1098}
1099
abaad8cf
JP
1100static void
1101ofp_port_stats_request(struct ds *string, const void *body_,
1102 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1103{
1104 const struct ofp_port_stats_request *psr = body_;
1105 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1106}
1107
064af421
BP
1108static void
1109ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1110 int verbosity)
1111{
1112 const struct ofp_port_stats *ps = body;
1113 size_t n = len / sizeof *ps;
1114 ds_put_format(string, " %zu ports\n", n);
1115 if (verbosity < 1) {
1116 return;
1117 }
1118
1119 for (; n--; ps++) {
1120 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1121
1122 ds_put_cstr(string, "rx ");
1123 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1124 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1125 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1126 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1127 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1128 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1129 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1130
1131 ds_put_cstr(string, " tx ");
1132 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1133 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1134 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1135 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1136 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1137 }
1138}
1139
1140static void
1141ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1142 int verbosity)
1143{
1144 const struct ofp_table_stats *ts = body;
1145 size_t n = len / sizeof *ts;
1146 ds_put_format(string, " %zu tables\n", n);
1147 if (verbosity < 1) {
1148 return;
1149 }
1150
1151 for (; n--; ts++) {
1152 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1153 strncpy(name, ts->name, sizeof name);
1154 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1155
1156 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1157 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1158 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1159 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1160 ds_put_cstr(string, " ");
d295e8e9 1161 ds_put_format(string, "lookup=%"PRIu64", ",
064af421
BP
1162 ntohll(ts->lookup_count));
1163 ds_put_format(string, "matched=%"PRIu64"\n",
1164 ntohll(ts->matched_count));
1165 }
1166}
1167
1168static void
1169vendor_stat(struct ds *string, const void *body, size_t len,
67a4917b 1170 int verbosity OVS_UNUSED)
064af421
BP
1171{
1172 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1173 ds_put_format(string, " %zu bytes additional data",
1174 len - sizeof(uint32_t));
1175}
1176
1177enum stats_direction {
1178 REQUEST,
1179 REPLY
1180};
1181
1182static void
1183print_stats(struct ds *string, int type, const void *body, size_t body_len,
1184 int verbosity, enum stats_direction direction)
1185{
1186 struct stats_msg {
1187 size_t min_body, max_body;
1188 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1189 };
1190
1191 struct stats_type {
1192 int type;
1193 const char *name;
1194 struct stats_msg request;
1195 struct stats_msg reply;
1196 };
1197
1198 static const struct stats_type stats_types[] = {
1199 {
1200 OFPST_DESC,
1201 "description",
1202 { 0, 0, NULL },
1203 { 0, SIZE_MAX, ofp_desc_stats_reply },
1204 },
1205 {
1206 OFPST_FLOW,
1207 "flow",
1208 { sizeof(struct ofp_flow_stats_request),
1209 sizeof(struct ofp_flow_stats_request),
1210 ofp_flow_stats_request },
1211 { 0, SIZE_MAX, ofp_flow_stats_reply },
1212 },
1213 {
1214 OFPST_AGGREGATE,
1215 "aggregate",
1216 { sizeof(struct ofp_aggregate_stats_request),
1217 sizeof(struct ofp_aggregate_stats_request),
1218 ofp_aggregate_stats_request },
1219 { sizeof(struct ofp_aggregate_stats_reply),
1220 sizeof(struct ofp_aggregate_stats_reply),
1221 ofp_aggregate_stats_reply },
1222 },
1223 {
1224 OFPST_TABLE,
1225 "table",
1226 { 0, 0, NULL },
1227 { 0, SIZE_MAX, ofp_table_stats_reply },
1228 },
1229 {
1230 OFPST_PORT,
1231 "port",
d295e8e9
JP
1232 { sizeof(struct ofp_port_stats_request),
1233 sizeof(struct ofp_port_stats_request),
abaad8cf 1234 ofp_port_stats_request },
064af421
BP
1235 { 0, SIZE_MAX, ofp_port_stats_reply },
1236 },
1237 {
1238 OFPST_VENDOR,
1239 "vendor-specific",
1240 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1241 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1242 },
1243 {
1244 -1,
1245 "unknown",
1246 { 0, 0, NULL, },
1247 { 0, 0, NULL, },
1248 },
1249 };
1250
1251 const struct stats_type *s;
1252 const struct stats_msg *m;
1253
1254 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1255 ds_put_format(string, " ***unknown type %d***", type);
1256 return;
1257 }
1258 for (s = stats_types; s->type >= 0; s++) {
1259 if (s->type == type) {
1260 break;
1261 }
1262 }
1263 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1264
1265 m = direction == REQUEST ? &s->request : &s->reply;
1266 if (body_len < m->min_body || body_len > m->max_body) {
1267 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1268 body_len, m->min_body, m->max_body);
1269 return;
1270 }
1271 if (m->printer) {
1272 m->printer(string, body, body_len, verbosity);
1273 }
1274}
1275
1276static void
1277ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1278{
1279 const struct ofp_stats_request *srq = oh;
1280
1281 if (srq->flags) {
1282 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1283 ntohs(srq->flags));
1284 }
1285
1286 print_stats(string, ntohs(srq->type), srq->body,
1287 len - offsetof(struct ofp_stats_request, body),
1288 verbosity, REQUEST);
1289}
1290
1291static void
1292ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1293{
1294 const struct ofp_stats_reply *srp = oh;
1295
1296 ds_put_cstr(string, " flags=");
1297 if (!srp->flags) {
1298 ds_put_cstr(string, "none");
1299 } else {
1300 uint16_t flags = ntohs(srp->flags);
1301 if (flags & OFPSF_REPLY_MORE) {
1302 ds_put_cstr(string, "[more]");
1303 flags &= ~OFPSF_REPLY_MORE;
1304 }
1305 if (flags) {
1306 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1307 }
1308 }
1309
1310 print_stats(string, ntohs(srp->type), srp->body,
1311 len - offsetof(struct ofp_stats_reply, body),
1312 verbosity, REPLY);
1313}
1314
1315static void
1316ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1317{
1318 const struct ofp_header *hdr = oh;
1319
1320 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1321 if (verbosity > 1) {
d295e8e9 1322 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
064af421
BP
1323 }
1324}
1325
1326struct openflow_packet {
1327 uint8_t type;
1328 const char *name;
1329 size_t min_size;
1330 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1331};
1332
1333static const struct openflow_packet packets[] = {
1334 {
1335 OFPT_HELLO,
1336 "hello",
1337 sizeof (struct ofp_header),
1338 NULL,
1339 },
1340 {
1341 OFPT_FEATURES_REQUEST,
1342 "features_request",
1343 sizeof (struct ofp_header),
1344 NULL,
1345 },
1346 {
1347 OFPT_FEATURES_REPLY,
1348 "features_reply",
1349 sizeof (struct ofp_switch_features),
1350 ofp_print_switch_features,
1351 },
1352 {
1353 OFPT_GET_CONFIG_REQUEST,
1354 "get_config_request",
1355 sizeof (struct ofp_header),
1356 NULL,
1357 },
1358 {
1359 OFPT_GET_CONFIG_REPLY,
1360 "get_config_reply",
1361 sizeof (struct ofp_switch_config),
1362 ofp_print_switch_config,
1363 },
1364 {
1365 OFPT_SET_CONFIG,
1366 "set_config",
1367 sizeof (struct ofp_switch_config),
1368 ofp_print_switch_config,
1369 },
1370 {
1371 OFPT_PACKET_IN,
1372 "packet_in",
1373 offsetof(struct ofp_packet_in, data),
1374 ofp_packet_in,
1375 },
1376 {
1377 OFPT_PACKET_OUT,
1378 "packet_out",
1379 sizeof (struct ofp_packet_out),
1380 ofp_packet_out,
1381 },
1382 {
1383 OFPT_FLOW_MOD,
1384 "flow_mod",
1385 sizeof (struct ofp_flow_mod),
1386 ofp_print_flow_mod,
1387 },
1388 {
ca069229
JP
1389 OFPT_FLOW_REMOVED,
1390 "flow_removed",
1391 sizeof (struct ofp_flow_removed),
1392 ofp_print_flow_removed,
064af421
BP
1393 },
1394 {
1395 OFPT_PORT_MOD,
1396 "port_mod",
1397 sizeof (struct ofp_port_mod),
1398 ofp_print_port_mod,
1399 },
1400 {
1401 OFPT_PORT_STATUS,
1402 "port_status",
1403 sizeof (struct ofp_port_status),
1404 ofp_print_port_status
1405 },
1406 {
1407 OFPT_ERROR,
1408 "error_msg",
1409 sizeof (struct ofp_error_msg),
1410 ofp_print_error_msg,
1411 },
1412 {
1413 OFPT_STATS_REQUEST,
1414 "stats_request",
1415 sizeof (struct ofp_stats_request),
1416 ofp_stats_request,
1417 },
1418 {
1419 OFPT_STATS_REPLY,
1420 "stats_reply",
1421 sizeof (struct ofp_stats_reply),
1422 ofp_stats_reply,
1423 },
1424 {
1425 OFPT_ECHO_REQUEST,
1426 "echo_request",
1427 sizeof (struct ofp_header),
1428 ofp_echo,
1429 },
1430 {
1431 OFPT_ECHO_REPLY,
1432 "echo_reply",
1433 sizeof (struct ofp_header),
1434 ofp_echo,
1435 },
1436 {
1437 OFPT_VENDOR,
1438 "vendor",
1439 sizeof (struct ofp_vendor_header),
1440 NULL,
1441 },
246e61ea
JP
1442 {
1443 OFPT_BARRIER_REQUEST,
1444 "barrier_request",
1445 sizeof (struct ofp_header),
1446 NULL,
1447 },
1448 {
1449 OFPT_BARRIER_REPLY,
1450 "barrier_reply",
1451 sizeof (struct ofp_header),
1452 NULL,
1453 }
064af421
BP
1454};
1455
1456/* Composes and returns a string representing the OpenFlow packet of 'len'
1457 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1458 * verbosity and higher numbers increase verbosity. The caller is responsible
1459 * for freeing the string. */
1460char *
1461ofp_to_string(const void *oh_, size_t len, int verbosity)
1462{
1463 struct ds string = DS_EMPTY_INITIALIZER;
1464 const struct ofp_header *oh = oh_;
1465 const struct openflow_packet *pkt;
1466
1467 if (len < sizeof(struct ofp_header)) {
1468 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1469 ds_put_hex_dump(&string, oh, len, 0, true);
1470 return ds_cstr(&string);
1471 } else if (oh->version != OFP_VERSION) {
1472 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1473 ds_put_hex_dump(&string, oh, len, 0, true);
1474 return ds_cstr(&string);
1475 }
1476
1477 for (pkt = packets; ; pkt++) {
1478 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1479 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1480 oh->type);
1481 ds_put_hex_dump(&string, oh, len, 0, true);
1482 return ds_cstr(&string);
1483 } else if (oh->type == pkt->type) {
1484 break;
1485 }
1486 }
1487
1488 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1489
1490 if (ntohs(oh->length) > len)
1491 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1492 len, ntohs(oh->length));
1493 else if (ntohs(oh->length) < len) {
1494 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1495 ntohs(oh->length), len);
1496 len = ntohs(oh->length);
1497 }
1498
1499 if (len < pkt->min_size) {
1500 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1501 len, pkt->min_size);
1502 } else if (!pkt->printer) {
1503 if (len > sizeof *oh) {
1504 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
d295e8e9 1505 ntohs(oh->length));
064af421
BP
1506 }
1507 } else {
1508 pkt->printer(&string, oh, len, verbosity);
1509 }
1510 if (verbosity >= 3) {
1511 ds_put_hex_dump(&string, oh, len, 0, true);
1512 }
1513 if (string.string[string.length - 1] != '\n') {
1514 ds_put_char(&string, '\n');
1515 }
1516 return ds_cstr(&string);
1517}
1518
1519/* Returns the name for the specified OpenFlow message type as a string,
1520 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1521 * hex number, e.g. "0x55".
1522 *
1523 * The caller must free the returned string when it is no longer needed. */
1524char *
1525ofp_message_type_to_string(uint8_t type)
1526{
1527 struct ds s = DS_EMPTY_INITIALIZER;
1528 const struct openflow_packet *pkt;
1529 for (pkt = packets; ; pkt++) {
1530 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1531 ds_put_format(&s, "0x%02"PRIx8, type);
1532 break;
1533 } else if (type == pkt->type) {
1534 const char *p;
1535
1536 ds_put_cstr(&s, "OFPT_");
1537 for (p = pkt->name; *p; p++) {
1538 ds_put_char(&s, toupper((unsigned char) *p));
1539 }
1540 break;
1541 }
1542 }
1543 return ds_cstr(&s);
1544}
1545\f
1546static void
d295e8e9 1547print_and_free(FILE *stream, char *string)
064af421
BP
1548{
1549 fputs(string, stream);
1550 free(string);
1551}
1552
1553/* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1554 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1555 * numbers increase verbosity. */
1556void
1557ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1558{
1559 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1560}
1561
1562/* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1563 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1564 * the Ethernet frame (of which 'len' bytes were captured).
1565 *
1566 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1567void
1568ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1569{
1570 print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1571}