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