ofp-util: Simplify OpenFlow 1.0 ofp_match normalization.
[openvswitch] / lib / ofp-util.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-print.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include "autopath.h"
23 #include "byte-order.h"
24 #include "classifier.h"
25 #include "dynamic-string.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-errors.h"
29 #include "ofp-util.h"
30 #include "ofpbuf.h"
31 #include "packets.h"
32 #include "random.h"
33 #include "unaligned.h"
34 #include "type-props.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(ofp_util);
38
39 static void normalize_match(struct ofp_match *);
40
41 /* Rate limit for OpenFlow message parse errors.  These always indicate a bug
42  * in the peer and so there's not much point in showing a lot of them. */
43 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
44
45 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
46  * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
47  * is wildcarded.
48  *
49  * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
50  * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
51  * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
52  * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
53  * wildcarded. */
54 ovs_be32
55 ofputil_wcbits_to_netmask(int wcbits)
56 {
57     wcbits &= 0x3f;
58     return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
59 }
60
61 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
62  * that it wildcards.  'netmask' must be a CIDR netmask (see ip_is_cidr()). */
63 int
64 ofputil_netmask_to_wcbits(ovs_be32 netmask)
65 {
66     assert(ip_is_cidr(netmask));
67 #if __GNUC__ >= 4
68     return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
69 #else
70     int wcbits;
71
72     for (wcbits = 32; netmask; wcbits--) {
73         netmask &= netmask - 1;
74     }
75
76     return wcbits;
77 #endif
78 }
79
80 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
81  * name. */
82 #define WC_INVARIANT_LIST \
83     WC_INVARIANT_BIT(IN_PORT) \
84     WC_INVARIANT_BIT(DL_SRC) \
85     WC_INVARIANT_BIT(DL_DST) \
86     WC_INVARIANT_BIT(DL_TYPE) \
87     WC_INVARIANT_BIT(NW_PROTO) \
88     WC_INVARIANT_BIT(TP_SRC) \
89     WC_INVARIANT_BIT(TP_DST)
90
91 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
92  * actually have the same names and values. */
93 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
94     WC_INVARIANT_LIST
95 #undef WC_INVARIANT_BIT
96
97 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
98  * OR'd together. */
99 enum {
100     WC_INVARIANTS = 0
101 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
102     WC_INVARIANT_LIST
103 #undef WC_INVARIANT_BIT
104 };
105
106 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
107  * 'priority'. */
108 void
109 ofputil_cls_rule_from_match(const struct ofp_match *match,
110                             unsigned int priority, struct cls_rule *rule)
111 {
112     struct flow_wildcards *wc = &rule->wc;
113     unsigned int ofpfw;
114     ovs_be16 vid, pcp;
115
116     /* Initialize rule->priority. */
117     ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
118     rule->priority = !ofpfw ? UINT16_MAX : priority;
119
120     /* Initialize most of rule->wc. */
121     flow_wildcards_init_catchall(wc);
122     wc->wildcards = ofpfw & WC_INVARIANTS;
123
124     /* Wildcard fields that aren't defined by ofp_match or tun_id. */
125     wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
126
127     if (ofpfw & OFPFW_NW_TOS) {
128         wc->wildcards |= FWW_NW_TOS;
129     }
130     wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
131     wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
132
133     if (ofpfw & OFPFW_DL_DST) {
134         /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
135          * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
136          * and FWW_ETH_MCAST. */
137         wc->wildcards |= FWW_ETH_MCAST;
138     }
139
140     /* Initialize most of rule->flow. */
141     rule->flow.nw_src = match->nw_src;
142     rule->flow.nw_dst = match->nw_dst;
143     rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
144                      : ntohs(match->in_port));
145     rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
146     rule->flow.tp_src = match->tp_src;
147     rule->flow.tp_dst = match->tp_dst;
148     memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
149     memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
150     rule->flow.nw_tos = match->nw_tos;
151     rule->flow.nw_proto = match->nw_proto;
152
153     /* Translate VLANs. */
154     vid = match->dl_vlan & htons(VLAN_VID_MASK);
155     pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
156     switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
157     case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
158         /* Wildcard everything. */
159         rule->flow.vlan_tci = htons(0);
160         rule->wc.vlan_tci_mask = htons(0);
161         break;
162
163     case OFPFW_DL_VLAN_PCP:
164         if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
165             /* Match only packets without 802.1Q header. */
166             rule->flow.vlan_tci = htons(0);
167             rule->wc.vlan_tci_mask = htons(0xffff);
168         } else {
169             /* Wildcard PCP, specific VID. */
170             rule->flow.vlan_tci = vid | htons(VLAN_CFI);
171             rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
172         }
173         break;
174
175     case OFPFW_DL_VLAN:
176         /* Wildcard VID, specific PCP. */
177         rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
178         rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
179         break;
180
181     case 0:
182         if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
183             /* This case is odd, since we can't have a specific PCP without an
184              * 802.1Q header.  However, older versions of OVS treated this as
185              * matching packets withut an 802.1Q header, so we do here too. */
186             rule->flow.vlan_tci = htons(0);
187             rule->wc.vlan_tci_mask = htons(0xffff);
188         } else {
189             /* Specific VID and PCP. */
190             rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
191             rule->wc.vlan_tci_mask = htons(0xffff);
192         }
193         break;
194     }
195
196     /* Clean up. */
197     cls_rule_zero_wildcarded_fields(rule);
198 }
199
200 /* Convert 'rule' into the OpenFlow match structure 'match'. */
201 void
202 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
203 {
204     const struct flow_wildcards *wc = &rule->wc;
205     unsigned int ofpfw;
206
207     /* Figure out most OpenFlow wildcards. */
208     ofpfw = wc->wildcards & WC_INVARIANTS;
209     ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
210     ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
211     if (wc->wildcards & FWW_NW_TOS) {
212         ofpfw |= OFPFW_NW_TOS;
213     }
214
215     /* Translate VLANs. */
216     match->dl_vlan = htons(0);
217     match->dl_vlan_pcp = 0;
218     if (rule->wc.vlan_tci_mask == htons(0)) {
219         ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
220     } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
221                && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
222         match->dl_vlan = htons(OFP_VLAN_NONE);
223     } else {
224         if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
225             ofpfw |= OFPFW_DL_VLAN;
226         } else {
227             match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
228         }
229
230         if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
231             ofpfw |= OFPFW_DL_VLAN_PCP;
232         } else {
233             match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
234         }
235     }
236
237     /* Compose most of the match structure. */
238     match->wildcards = htonl(ofpfw);
239     match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
240                            : rule->flow.in_port);
241     memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
242     memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
243     match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
244     match->nw_src = rule->flow.nw_src;
245     match->nw_dst = rule->flow.nw_dst;
246     match->nw_tos = rule->flow.nw_tos;
247     match->nw_proto = rule->flow.nw_proto;
248     match->tp_src = rule->flow.tp_src;
249     match->tp_dst = rule->flow.tp_dst;
250     memset(match->pad1, '\0', sizeof match->pad1);
251     memset(match->pad2, '\0', sizeof match->pad2);
252 }
253
254 /* Given a 'dl_type' value in the format used in struct flow, returns the
255  * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
256 ovs_be16
257 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
258 {
259     return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
260             ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
261             : flow_dl_type);
262 }
263
264 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
265  * structure, returns the corresponding 'dl_type' value for use in struct
266  * flow. */
267 ovs_be16
268 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
269 {
270     return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
271             ? htons(FLOW_DL_TYPE_NONE)
272             : ofp_dl_type);
273 }
274
275 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
276 static ovs_be32
277 alloc_xid(void)
278 {
279     static uint32_t next_xid = 1;
280     return htonl(next_xid++);
281 }
282 \f
283 /* Basic parsing of OpenFlow messages. */
284
285 struct ofputil_msg_type {
286     enum ofputil_msg_code code; /* OFPUTIL_*. */
287     uint32_t value;             /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
288     const char *name;           /* e.g. "OFPT_FLOW_REMOVED". */
289     unsigned int min_size;      /* Minimum total message size in bytes. */
290     /* 0 if 'min_size' is the exact size that the message must be.  Otherwise,
291      * the message may exceed 'min_size' by an even multiple of this value. */
292     unsigned int extra_multiple;
293 };
294
295 struct ofputil_msg_category {
296     const char *name;           /* e.g. "OpenFlow message" */
297     const struct ofputil_msg_type *types;
298     size_t n_types;
299     int missing_error;          /* ofp_mkerr() value for missing type. */
300 };
301
302 static bool
303 ofputil_length_ok(const struct ofputil_msg_category *cat,
304                   const struct ofputil_msg_type *type,
305                   unsigned int size)
306 {
307     switch (type->extra_multiple) {
308     case 0:
309         if (size != type->min_size) {
310             VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
311                          "length %u (expected length %u)",
312                          cat->name, type->name, size, type->min_size);
313             return false;
314         }
315         return true;
316
317     case 1:
318         if (size < type->min_size) {
319             VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
320                          "length %u (expected length at least %u bytes)",
321                          cat->name, type->name, size, type->min_size);
322             return false;
323         }
324         return true;
325
326     default:
327         if (size < type->min_size
328             || (size - type->min_size) % type->extra_multiple) {
329             VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
330                          "length %u (must be exactly %u bytes or longer "
331                          "by an integer multiple of %u bytes)",
332                          cat->name, type->name, size,
333                          type->min_size, type->extra_multiple);
334             return false;
335         }
336         return true;
337     }
338 }
339
340 static int
341 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
342                                 uint32_t value, unsigned int size,
343                                 const struct ofputil_msg_type **typep)
344 {
345     const struct ofputil_msg_type *type;
346
347     for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
348         if (type->value == value) {
349             if (!ofputil_length_ok(cat, type, size)) {
350                 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
351             }
352             *typep = type;
353             return 0;
354         }
355     }
356
357     VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
358                  cat->name, value);
359     return cat->missing_error;
360 }
361
362 static int
363 ofputil_decode_vendor(const struct ofp_header *oh,
364                       const struct ofputil_msg_type **typep)
365 {
366     static const struct ofputil_msg_type nxt_messages[] = {
367         { OFPUTIL_NXT_ROLE_REQUEST,
368           NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
369           sizeof(struct nx_role_request), 0 },
370
371         { OFPUTIL_NXT_ROLE_REPLY,
372           NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
373           sizeof(struct nx_role_request), 0 },
374
375         { OFPUTIL_NXT_SET_FLOW_FORMAT,
376           NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
377           sizeof(struct nxt_set_flow_format), 0 },
378
379         { OFPUTIL_NXT_FLOW_MOD,
380           NXT_FLOW_MOD, "NXT_FLOW_MOD",
381           sizeof(struct nx_flow_mod), 8 },
382
383         { OFPUTIL_NXT_FLOW_REMOVED,
384           NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
385           sizeof(struct nx_flow_removed), 8 },
386     };
387
388     static const struct ofputil_msg_category nxt_category = {
389         "Nicira extension message",
390         nxt_messages, ARRAY_SIZE(nxt_messages),
391         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
392     };
393
394     const struct ofp_vendor_header *ovh;
395     const struct nicira_header *nh;
396
397     ovh = (const struct ofp_vendor_header *) oh;
398     if (ovh->vendor != htonl(NX_VENDOR_ID)) {
399         VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
400                      "vendor %"PRIx32, ntohl(ovh->vendor));
401         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
402     }
403
404     if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
405         VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
406                      "length %u (expected at least %zu)",
407                      ntohs(ovh->header.length), sizeof(struct nicira_header));
408         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
409     }
410
411     nh = (const struct nicira_header *) oh;
412     return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
413                                            ntohs(oh->length), typep);
414 }
415
416 static int
417 check_nxstats_msg(const struct ofp_header *oh)
418 {
419     const struct ofp_stats_request *osr;
420     ovs_be32 vendor;
421
422     osr = (const struct ofp_stats_request *) oh;
423
424     memcpy(&vendor, osr->body, sizeof vendor);
425     if (vendor != htonl(NX_VENDOR_ID)) {
426         VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
427                      "unknown vendor %"PRIx32, ntohl(vendor));
428         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
429     }
430
431     if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
432         VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
433         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
434     }
435
436     return 0;
437 }
438
439 static int
440 ofputil_decode_nxst_request(const struct ofp_header *oh,
441                             const struct ofputil_msg_type **typep)
442 {
443     static const struct ofputil_msg_type nxst_requests[] = {
444         { OFPUTIL_NXST_FLOW_REQUEST,
445           NXST_FLOW, "NXST_FLOW request",
446           sizeof(struct nx_flow_stats_request), 8 },
447
448         { OFPUTIL_NXST_AGGREGATE_REQUEST,
449           NXST_AGGREGATE, "NXST_AGGREGATE request",
450           sizeof(struct nx_aggregate_stats_request), 8 },
451     };
452
453     static const struct ofputil_msg_category nxst_request_category = {
454         "Nicira extension statistics request",
455         nxst_requests, ARRAY_SIZE(nxst_requests),
456         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
457     };
458
459     const struct nicira_stats_msg *nsm;
460     int error;
461
462     error = check_nxstats_msg(oh);
463     if (error) {
464         return error;
465     }
466
467     nsm = (struct nicira_stats_msg *) oh;
468     return ofputil_lookup_openflow_message(&nxst_request_category,
469                                            ntohl(nsm->subtype),
470                                            ntohs(oh->length), typep);
471 }
472
473 static int
474 ofputil_decode_nxst_reply(const struct ofp_header *oh,
475                           const struct ofputil_msg_type **typep)
476 {
477     static const struct ofputil_msg_type nxst_replies[] = {
478         { OFPUTIL_NXST_FLOW_REPLY,
479           NXST_FLOW, "NXST_FLOW reply",
480           sizeof(struct nicira_stats_msg), 8 },
481
482         { OFPUTIL_NXST_AGGREGATE_REPLY,
483           NXST_AGGREGATE, "NXST_AGGREGATE reply",
484           sizeof(struct nx_aggregate_stats_reply), 0 },
485     };
486
487     static const struct ofputil_msg_category nxst_reply_category = {
488         "Nicira extension statistics reply",
489         nxst_replies, ARRAY_SIZE(nxst_replies),
490         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
491     };
492
493     const struct nicira_stats_msg *nsm;
494     int error;
495
496     error = check_nxstats_msg(oh);
497     if (error) {
498         return error;
499     }
500
501     nsm = (struct nicira_stats_msg *) oh;
502     return ofputil_lookup_openflow_message(&nxst_reply_category,
503                                            ntohl(nsm->subtype),
504                                            ntohs(oh->length), typep);
505 }
506
507 static int
508 ofputil_decode_ofpst_request(const struct ofp_header *oh,
509                              const struct ofputil_msg_type **typep)
510 {
511     enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
512     static const struct ofputil_msg_type ofpst_requests[] = {
513         { OFPUTIL_OFPST_DESC_REQUEST,
514           OFPST_DESC, "OFPST_DESC request",
515           OSR_SIZE, 0 },
516
517         { OFPUTIL_OFPST_FLOW_REQUEST,
518           OFPST_FLOW, "OFPST_FLOW request",
519           OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
520
521         { OFPUTIL_OFPST_AGGREGATE_REQUEST,
522           OFPST_AGGREGATE, "OFPST_AGGREGATE request",
523           OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
524
525         { OFPUTIL_OFPST_TABLE_REQUEST,
526           OFPST_TABLE, "OFPST_TABLE request",
527           OSR_SIZE, 0 },
528
529         { OFPUTIL_OFPST_PORT_REQUEST,
530           OFPST_PORT, "OFPST_PORT request",
531           OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
532
533         { OFPUTIL_OFPST_QUEUE_REQUEST,
534           OFPST_QUEUE, "OFPST_QUEUE request",
535           OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
536
537         { 0,
538           OFPST_VENDOR, "OFPST_VENDOR request",
539           OSR_SIZE + sizeof(uint32_t), 1 },
540     };
541
542     static const struct ofputil_msg_category ofpst_request_category = {
543         "OpenFlow statistics",
544         ofpst_requests, ARRAY_SIZE(ofpst_requests),
545         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
546     };
547
548     const struct ofp_stats_request *osr;
549     int error;
550
551     osr = (const struct ofp_stats_request *) oh;
552     error = ofputil_lookup_openflow_message(&ofpst_request_category,
553                                             ntohs(osr->type),
554                                             ntohs(oh->length), typep);
555     if (!error && osr->type == htons(OFPST_VENDOR)) {
556         error = ofputil_decode_nxst_request(oh, typep);
557     }
558     return error;
559 }
560
561 static int
562 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
563                            const struct ofputil_msg_type **typep)
564 {
565     enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
566     static const struct ofputil_msg_type ofpst_replies[] = {
567         { OFPUTIL_OFPST_DESC_REPLY,
568           OFPST_DESC, "OFPST_DESC reply",
569           OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
570
571         { OFPUTIL_OFPST_FLOW_REPLY,
572           OFPST_FLOW, "OFPST_FLOW reply",
573           OSR_SIZE, 1 },
574
575         { OFPUTIL_OFPST_AGGREGATE_REPLY,
576           OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
577           OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
578
579         { OFPUTIL_OFPST_TABLE_REPLY,
580           OFPST_TABLE, "OFPST_TABLE reply",
581           OSR_SIZE, sizeof(struct ofp_table_stats) },
582
583         { OFPUTIL_OFPST_PORT_REPLY,
584           OFPST_PORT, "OFPST_PORT reply",
585           OSR_SIZE, sizeof(struct ofp_port_stats) },
586
587         { OFPUTIL_OFPST_QUEUE_REPLY,
588           OFPST_QUEUE, "OFPST_QUEUE reply",
589           OSR_SIZE, sizeof(struct ofp_queue_stats) },
590
591         { 0,
592           OFPST_VENDOR, "OFPST_VENDOR reply",
593           OSR_SIZE + sizeof(uint32_t), 1 },
594     };
595
596     static const struct ofputil_msg_category ofpst_reply_category = {
597         "OpenFlow statistics",
598         ofpst_replies, ARRAY_SIZE(ofpst_replies),
599         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
600     };
601
602     const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
603     int error;
604
605     error = ofputil_lookup_openflow_message(&ofpst_reply_category,
606                                            ntohs(osr->type),
607                                            ntohs(oh->length), typep);
608     if (!error && osr->type == htons(OFPST_VENDOR)) {
609         error = ofputil_decode_nxst_reply(oh, typep);
610     }
611     return error;
612 }
613
614 /* Decodes the message type represented by 'oh'.  Returns 0 if successful or
615  * an OpenFlow error code constructed with ofp_mkerr() on failure.  Either
616  * way, stores in '*typep' a type structure that can be inspected with the
617  * ofputil_msg_type_*() functions.
618  *
619  * oh->length must indicate the correct length of the message (and must be at
620  * least sizeof(struct ofp_header)).
621  *
622  * Success indicates that 'oh' is at least as long as the minimum-length
623  * message of its type. */
624 int
625 ofputil_decode_msg_type(const struct ofp_header *oh,
626                         const struct ofputil_msg_type **typep)
627 {
628     static const struct ofputil_msg_type ofpt_messages[] = {
629         { OFPUTIL_OFPT_HELLO,
630           OFPT_HELLO, "OFPT_HELLO",
631           sizeof(struct ofp_hello), 1 },
632
633         { OFPUTIL_OFPT_ERROR,
634           OFPT_ERROR, "OFPT_ERROR",
635           sizeof(struct ofp_error_msg), 1 },
636
637         { OFPUTIL_OFPT_ECHO_REQUEST,
638           OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
639           sizeof(struct ofp_header), 1 },
640
641         { OFPUTIL_OFPT_ECHO_REPLY,
642           OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
643           sizeof(struct ofp_header), 1 },
644
645         { OFPUTIL_OFPT_FEATURES_REQUEST,
646           OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
647           sizeof(struct ofp_header), 0 },
648
649         { OFPUTIL_OFPT_FEATURES_REPLY,
650           OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
651           sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
652
653         { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
654           OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
655           sizeof(struct ofp_header), 0 },
656
657         { OFPUTIL_OFPT_GET_CONFIG_REPLY,
658           OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
659           sizeof(struct ofp_switch_config), 0 },
660
661         { OFPUTIL_OFPT_SET_CONFIG,
662           OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
663           sizeof(struct ofp_switch_config), 0 },
664
665         { OFPUTIL_OFPT_PACKET_IN,
666           OFPT_PACKET_IN, "OFPT_PACKET_IN",
667           offsetof(struct ofp_packet_in, data), 1 },
668
669         { OFPUTIL_OFPT_FLOW_REMOVED,
670           OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
671           sizeof(struct ofp_flow_removed), 0 },
672
673         { OFPUTIL_OFPT_PORT_STATUS,
674           OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
675           sizeof(struct ofp_port_status), 0 },
676
677         { OFPUTIL_OFPT_PACKET_OUT,
678           OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
679           sizeof(struct ofp_packet_out), 1 },
680
681         { OFPUTIL_OFPT_FLOW_MOD,
682           OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
683           sizeof(struct ofp_flow_mod), 1 },
684
685         { OFPUTIL_OFPT_PORT_MOD,
686           OFPT_PORT_MOD, "OFPT_PORT_MOD",
687           sizeof(struct ofp_port_mod), 0 },
688
689         { 0,
690           OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
691           sizeof(struct ofp_stats_request), 1 },
692
693         { 0,
694           OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
695           sizeof(struct ofp_stats_reply), 1 },
696
697         { OFPUTIL_OFPT_BARRIER_REQUEST,
698           OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
699           sizeof(struct ofp_header), 0 },
700
701         { OFPUTIL_OFPT_BARRIER_REPLY,
702           OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
703           sizeof(struct ofp_header), 0 },
704
705         { 0,
706           OFPT_VENDOR, "OFPT_VENDOR",
707           sizeof(struct ofp_vendor_header), 1 },
708     };
709
710     static const struct ofputil_msg_category ofpt_category = {
711         "OpenFlow message",
712         ofpt_messages, ARRAY_SIZE(ofpt_messages),
713         OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
714     };
715
716     int error;
717
718     error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
719                                             ntohs(oh->length), typep);
720     if (!error) {
721         switch (oh->type) {
722         case OFPT_VENDOR:
723             error = ofputil_decode_vendor(oh, typep);
724             break;
725
726         case OFPT_STATS_REQUEST:
727             error = ofputil_decode_ofpst_request(oh, typep);
728             break;
729
730         case OFPT_STATS_REPLY:
731             error = ofputil_decode_ofpst_reply(oh, typep);
732
733         default:
734             break;
735         }
736     }
737     if (error) {
738         static const struct ofputil_msg_type ofputil_invalid_type = {
739             OFPUTIL_INVALID,
740             0, "OFPUTIL_INVALID",
741             0, 0
742         };
743
744         *typep = &ofputil_invalid_type;
745     }
746     return error;
747 }
748
749 /* Returns an OFPUTIL_* message type code for 'type'. */
750 enum ofputil_msg_code
751 ofputil_msg_type_code(const struct ofputil_msg_type *type)
752 {
753     return type->code;
754 }
755 \f
756 /* Flow formats. */
757
758 bool
759 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
760 {
761     switch (flow_format) {
762     case NXFF_OPENFLOW10:
763     case NXFF_NXM:
764         return true;
765     }
766
767     return false;
768 }
769
770 const char *
771 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
772 {
773     switch (flow_format) {
774     case NXFF_OPENFLOW10:
775         return "openflow10";
776     case NXFF_NXM:
777         return "nxm";
778     default:
779         NOT_REACHED();
780     }
781 }
782
783 int
784 ofputil_flow_format_from_string(const char *s)
785 {
786     return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
787             : !strcmp(s, "nxm") ? NXFF_NXM
788             : -1);
789 }
790
791 static bool
792 regs_fully_wildcarded(const struct flow_wildcards *wc)
793 {
794     int i;
795
796     for (i = 0; i < FLOW_N_REGS; i++) {
797         if (wc->reg_masks[i] != 0) {
798             return false;
799         }
800     }
801     return true;
802 }
803
804 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
805  * (e.g. to add or remove a flow).  Only NXM can handle tunnel IDs, registers,
806  * or fixing the Ethernet multicast bit.  Otherwise, it's better to use
807  * NXFF_OPENFLOW10 for backward compatibility. */
808 enum nx_flow_format
809 ofputil_min_flow_format(const struct cls_rule *rule)
810 {
811     const struct flow_wildcards *wc = &rule->wc;
812
813     /* Only NXM supports separately wildcards the Ethernet multicast bit. */
814     if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
815         return NXFF_NXM;
816     }
817
818     /* Only NXM supports matching ARP hardware addresses. */
819     if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
820         return NXFF_NXM;
821     }
822
823     /* Only NXM supports matching IPv6 traffic. */
824     if (!(wc->wildcards & FWW_DL_TYPE)
825             && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
826         return NXFF_NXM;
827     }
828
829     /* Only NXM supports matching registers. */
830     if (!regs_fully_wildcarded(wc)) {
831         return NXFF_NXM;
832     }
833
834     /* Only NXM supports matching tun_id. */
835     if (wc->tun_id_mask != htonll(0)) {
836         return NXFF_NXM;
837     }
838
839     /* Other formats can express this rule. */
840     return NXFF_OPENFLOW10;
841 }
842
843 /* Returns an OpenFlow message that can be used to set the flow format to
844  * 'flow_format'.  */
845 struct ofpbuf *
846 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
847 {
848     struct nxt_set_flow_format *sff;
849     struct ofpbuf *msg;
850
851     sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
852     sff->format = htonl(flow_format);
853
854     return msg;
855 }
856
857 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
858  * flow_mod in 'fm'.  Returns 0 if successful, otherwise an OpenFlow error
859  * code.
860  *
861  * Does not validate the flow_mod actions. */
862 int
863 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh)
864 {
865     const struct ofputil_msg_type *type;
866     struct ofpbuf b;
867
868     ofpbuf_use_const(&b, oh, ntohs(oh->length));
869
870     ofputil_decode_msg_type(oh, &type);
871     if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
872         /* Standard OpenFlow flow_mod. */
873         struct ofp_match match, orig_match;
874         const struct ofp_flow_mod *ofm;
875         int error;
876
877         /* Dissect the message. */
878         ofm = ofpbuf_pull(&b, sizeof *ofm);
879         error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
880         if (error) {
881             return error;
882         }
883
884         /* Normalize ofm->match.  If normalization actually changes anything,
885          * then log the differences. */
886         match = ofm->match;
887         match.pad1[0] = match.pad2[0] = 0;
888         orig_match = match;
889         normalize_match(&match);
890         if (memcmp(&match, &orig_match, sizeof orig_match)) {
891             if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
892                 char *old = ofp_match_to_literal_string(&orig_match);
893                 char *new = ofp_match_to_literal_string(&match);
894                 VLOG_INFO("normalization changed ofp_match, details:");
895                 VLOG_INFO(" pre: %s", old);
896                 VLOG_INFO("post: %s", new);
897                 free(old);
898                 free(new);
899             }
900         }
901
902         /* Translate the message. */
903         ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), &fm->cr);
904         fm->cookie = ofm->cookie;
905         fm->command = ntohs(ofm->command);
906         fm->idle_timeout = ntohs(ofm->idle_timeout);
907         fm->hard_timeout = ntohs(ofm->hard_timeout);
908         fm->buffer_id = ntohl(ofm->buffer_id);
909         fm->out_port = ntohs(ofm->out_port);
910         fm->flags = ntohs(ofm->flags);
911     } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
912         /* Nicira extended flow_mod. */
913         const struct nx_flow_mod *nfm;
914         int error;
915
916         /* Dissect the message. */
917         nfm = ofpbuf_pull(&b, sizeof *nfm);
918         error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
919                               &fm->cr);
920         if (error) {
921             return error;
922         }
923         error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
924         if (error) {
925             return error;
926         }
927
928         /* Translate the message. */
929         fm->cookie = nfm->cookie;
930         fm->command = ntohs(nfm->command);
931         fm->idle_timeout = ntohs(nfm->idle_timeout);
932         fm->hard_timeout = ntohs(nfm->hard_timeout);
933         fm->buffer_id = ntohl(nfm->buffer_id);
934         fm->out_port = ntohs(nfm->out_port);
935         fm->flags = ntohs(nfm->flags);
936     } else {
937         NOT_REACHED();
938     }
939
940     return 0;
941 }
942
943 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
944  * 'flow_format' and returns the message. */
945 struct ofpbuf *
946 ofputil_encode_flow_mod(const struct flow_mod *fm,
947                         enum nx_flow_format flow_format)
948 {
949     size_t actions_len = fm->n_actions * sizeof *fm->actions;
950     struct ofpbuf *msg;
951
952     if (flow_format == NXFF_OPENFLOW10) {
953         struct ofp_flow_mod *ofm;
954
955         msg = ofpbuf_new(sizeof *ofm + actions_len);
956         ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
957         ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
958         ofm->cookie = fm->cookie;
959         ofm->command = htons(fm->command);
960         ofm->idle_timeout = htons(fm->idle_timeout);
961         ofm->hard_timeout = htons(fm->hard_timeout);
962         ofm->priority = htons(fm->cr.priority);
963         ofm->buffer_id = htonl(fm->buffer_id);
964         ofm->out_port = htons(fm->out_port);
965         ofm->flags = htons(fm->flags);
966     } else if (flow_format == NXFF_NXM) {
967         struct nx_flow_mod *nfm;
968         int match_len;
969
970         msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
971         put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
972         match_len = nx_put_match(msg, &fm->cr);
973
974         nfm = msg->data;
975         nfm->cookie = fm->cookie;
976         nfm->command = htons(fm->command);
977         nfm->idle_timeout = htons(fm->idle_timeout);
978         nfm->hard_timeout = htons(fm->hard_timeout);
979         nfm->priority = htons(fm->cr.priority);
980         nfm->buffer_id = htonl(fm->buffer_id);
981         nfm->out_port = htons(fm->out_port);
982         nfm->flags = htons(fm->flags);
983         nfm->match_len = htons(match_len);
984     } else {
985         NOT_REACHED();
986     }
987
988     ofpbuf_put(msg, fm->actions, actions_len);
989     update_openflow_length(msg);
990     return msg;
991 }
992
993 static int
994 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
995                                   const struct ofp_header *oh,
996                                   bool aggregate)
997 {
998     const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
999
1000     fsr->aggregate = aggregate;
1001     ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1002     fsr->out_port = ntohs(ofsr->out_port);
1003     fsr->table_id = ofsr->table_id;
1004
1005     return 0;
1006 }
1007
1008 static int
1009 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1010                                  const struct ofp_header *oh,
1011                                  bool aggregate)
1012 {
1013     const struct nx_flow_stats_request *nfsr;
1014     struct ofpbuf b;
1015     int error;
1016
1017     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1018
1019     nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1020     error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1021     if (error) {
1022         return error;
1023     }
1024     if (b.size) {
1025         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1026     }
1027
1028     fsr->aggregate = aggregate;
1029     fsr->out_port = ntohs(nfsr->out_port);
1030     fsr->table_id = nfsr->table_id;
1031
1032     return 0;
1033 }
1034
1035 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1036  * request 'oh', into an abstract flow_stats_request in 'fsr'.  Returns 0 if
1037  * successful, otherwise an OpenFlow error code. */
1038 int
1039 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1040                                   const struct ofp_header *oh)
1041 {
1042     const struct ofputil_msg_type *type;
1043     struct ofpbuf b;
1044     int code;
1045
1046     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1047
1048     ofputil_decode_msg_type(oh, &type);
1049     code = ofputil_msg_type_code(type);
1050     switch (code) {
1051     case OFPUTIL_OFPST_FLOW_REQUEST:
1052         return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1053
1054     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1055         return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1056
1057     case OFPUTIL_NXST_FLOW_REQUEST:
1058         return ofputil_decode_nxst_flow_request(fsr, oh, false);
1059
1060     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1061         return ofputil_decode_nxst_flow_request(fsr, oh, true);
1062
1063     default:
1064         /* Hey, the caller lied. */
1065         NOT_REACHED();
1066     }
1067 }
1068
1069 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1070  * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1071  * 'flow_format', and returns the message. */
1072 struct ofpbuf *
1073 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1074                                   enum nx_flow_format flow_format)
1075 {
1076     struct ofpbuf *msg;
1077
1078     if (flow_format == NXFF_OPENFLOW10) {
1079         struct ofp_flow_stats_request *ofsr;
1080         int type;
1081
1082         BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1083                           == sizeof(struct ofp_aggregate_stats_request));
1084
1085         type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1086         ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1087         ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1088         ofsr->table_id = fsr->table_id;
1089         ofsr->out_port = htons(fsr->out_port);
1090     } else if (flow_format == NXFF_NXM) {
1091         struct nx_flow_stats_request *nfsr;
1092         int match_len;
1093         int subtype;
1094
1095         subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1096         ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1097         match_len = nx_put_match(msg, &fsr->match);
1098
1099         nfsr = msg->data;
1100         nfsr->out_port = htons(fsr->out_port);
1101         nfsr->match_len = htons(match_len);
1102         nfsr->table_id = fsr->table_id;
1103     } else {
1104         NOT_REACHED();
1105     }
1106
1107     return msg;
1108 }
1109
1110 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1111  * ofputil_flow_stats in 'fs'.
1112  *
1113  * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1114  * OpenFlow message.  Calling this function multiple times for a single 'msg'
1115  * iterates through the replies.  The caller must initially leave 'msg''s layer
1116  * pointers null and not modify them between calls.
1117  *
1118  * Returns 0 if successful, EOF if no replies were left in this 'msg',
1119  * otherwise a positive errno value. */
1120 int
1121 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1122                                 struct ofpbuf *msg)
1123 {
1124     const struct ofputil_msg_type *type;
1125     int code;
1126
1127     ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1128     code = ofputil_msg_type_code(type);
1129     if (!msg->l2) {
1130         msg->l2 = msg->data;
1131         if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1132             ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
1133         } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1134             ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1135         } else {
1136             NOT_REACHED();
1137         }
1138     }
1139
1140     if (!msg->size) {
1141         return EOF;
1142     } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1143         const struct ofp_flow_stats *ofs;
1144         size_t length;
1145
1146         ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1147         if (!ofs) {
1148             VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1149                          "bytes at end", msg->size);
1150             return EINVAL;
1151         }
1152
1153         length = ntohs(ofs->length);
1154         if (length < sizeof *ofs) {
1155             VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1156                          "length %zu", length);
1157             return EINVAL;
1158         }
1159
1160         if (ofputil_pull_actions(msg, length - sizeof *ofs,
1161                                  &fs->actions, &fs->n_actions)) {
1162             return EINVAL;
1163         }
1164
1165         fs->cookie = get_32aligned_be64(&ofs->cookie);
1166         ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1167                                     &fs->rule);
1168         fs->table_id = ofs->table_id;
1169         fs->duration_sec = ntohl(ofs->duration_sec);
1170         fs->duration_nsec = ntohl(ofs->duration_nsec);
1171         fs->idle_timeout = ntohs(ofs->idle_timeout);
1172         fs->hard_timeout = ntohs(ofs->hard_timeout);
1173         fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1174         fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1175     } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1176         const struct nx_flow_stats *nfs;
1177         size_t match_len, length;
1178
1179         nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1180         if (!nfs) {
1181             VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1182                          "bytes at end", msg->size);
1183             return EINVAL;
1184         }
1185
1186         length = ntohs(nfs->length);
1187         match_len = ntohs(nfs->match_len);
1188         if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1189             VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1190                          "claims invalid length %zu", match_len, length);
1191             return EINVAL;
1192         }
1193         if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1194             return EINVAL;
1195         }
1196
1197         if (ofputil_pull_actions(msg,
1198                                  length - sizeof *nfs - ROUND_UP(match_len, 8),
1199                                  &fs->actions, &fs->n_actions)) {
1200             return EINVAL;
1201         }
1202
1203         fs->cookie = nfs->cookie;
1204         fs->table_id = nfs->table_id;
1205         fs->duration_sec = ntohl(nfs->duration_sec);
1206         fs->duration_nsec = ntohl(nfs->duration_nsec);
1207         fs->idle_timeout = ntohs(nfs->idle_timeout);
1208         fs->hard_timeout = ntohs(nfs->hard_timeout);
1209         fs->packet_count = ntohll(nfs->packet_count);
1210         fs->byte_count = ntohll(nfs->byte_count);
1211     } else {
1212         NOT_REACHED();
1213     }
1214
1215     return 0;
1216 }
1217
1218 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1219  * abstract ofputil_flow_removed in 'fr'.  Returns 0 if successful, otherwise
1220  * an OpenFlow error code. */
1221 int
1222 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1223                             const struct ofp_header *oh)
1224 {
1225     const struct ofputil_msg_type *type;
1226     enum ofputil_msg_code code;
1227
1228     ofputil_decode_msg_type(oh, &type);
1229     code = ofputil_msg_type_code(type);
1230     if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1231         const struct ofp_flow_removed *ofr;
1232
1233         ofr = (const struct ofp_flow_removed *) oh;
1234         ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1235                                     &fr->rule);
1236         fr->cookie = ofr->cookie;
1237         fr->reason = ofr->reason;
1238         fr->duration_sec = ntohl(ofr->duration_sec);
1239         fr->duration_nsec = ntohl(ofr->duration_nsec);
1240         fr->idle_timeout = ntohs(ofr->idle_timeout);
1241         fr->packet_count = ntohll(ofr->packet_count);
1242         fr->byte_count = ntohll(ofr->byte_count);
1243     } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1244         struct nx_flow_removed *nfr;
1245         struct ofpbuf b;
1246         int error;
1247
1248         ofpbuf_use_const(&b, oh, ntohs(oh->length));
1249
1250         nfr = ofpbuf_pull(&b, sizeof *nfr);
1251         error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1252                               &fr->rule);
1253         if (error) {
1254             return error;
1255         }
1256         if (b.size) {
1257             return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1258         }
1259
1260         fr->cookie = nfr->cookie;
1261         fr->reason = nfr->reason;
1262         fr->duration_sec = ntohl(nfr->duration_sec);
1263         fr->duration_nsec = ntohl(nfr->duration_nsec);
1264         fr->idle_timeout = ntohs(nfr->idle_timeout);
1265         fr->packet_count = ntohll(nfr->packet_count);
1266         fr->byte_count = ntohll(nfr->byte_count);
1267     } else {
1268         NOT_REACHED();
1269     }
1270
1271     return 0;
1272 }
1273
1274 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1275  * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1276  * message. */
1277 struct ofpbuf *
1278 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1279                             enum nx_flow_format flow_format)
1280 {
1281     struct ofpbuf *msg;
1282
1283     if (flow_format == NXFF_OPENFLOW10) {
1284         struct ofp_flow_removed *ofr;
1285
1286         ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1287                                 &msg);
1288         ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1289         ofr->priority = htons(fr->rule.priority);
1290         ofr->reason = fr->reason;
1291         ofr->duration_sec = htonl(fr->duration_sec);
1292         ofr->duration_nsec = htonl(fr->duration_nsec);
1293         ofr->idle_timeout = htons(fr->idle_timeout);
1294         ofr->packet_count = htonll(fr->packet_count);
1295         ofr->byte_count = htonll(fr->byte_count);
1296     } else if (flow_format == NXFF_NXM) {
1297         struct nx_flow_removed *nfr;
1298         int match_len;
1299
1300         make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1301         match_len = nx_put_match(msg, &fr->rule);
1302
1303         nfr = msg->data;
1304         nfr->cookie = fr->cookie;
1305         nfr->priority = htons(fr->rule.priority);
1306         nfr->reason = fr->reason;
1307         nfr->duration_sec = htonl(fr->duration_sec);
1308         nfr->duration_nsec = htonl(fr->duration_nsec);
1309         nfr->idle_timeout = htons(fr->idle_timeout);
1310         nfr->match_len = htons(match_len);
1311         nfr->packet_count = htonll(fr->packet_count);
1312         nfr->byte_count = htonll(fr->byte_count);
1313     } else {
1314         NOT_REACHED();
1315     }
1316
1317     return msg;
1318 }
1319
1320 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1321  * and returns the message.
1322  *
1323  * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1324  * returned ofpbuf.
1325  *
1326  * If 'rw_packet' is nonnull, then it must contain the same data as
1327  * pin->packet.  'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1328  * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1329  * and then ofputil_encode_packet_in() returns 'rw_packet'.  If 'rw_packet' has
1330  * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1331  * than ofputil_encode_packet_in() because it does not copy the packet
1332  * payload. */
1333 struct ofpbuf *
1334 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1335                         struct ofpbuf *rw_packet)
1336 {
1337     int total_len = pin->packet->size;
1338     struct ofp_packet_in *opi;
1339
1340     if (rw_packet) {
1341         if (pin->send_len < rw_packet->size) {
1342             rw_packet->size = pin->send_len;
1343         }
1344     } else {
1345         rw_packet = ofpbuf_clone_data_with_headroom(
1346             pin->packet->data, MIN(pin->send_len, pin->packet->size),
1347             offsetof(struct ofp_packet_in, data));
1348     }
1349
1350     /* Add OFPT_PACKET_IN. */
1351     opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1352     opi->header.version = OFP_VERSION;
1353     opi->header.type = OFPT_PACKET_IN;
1354     opi->total_len = htons(total_len);
1355     opi->in_port = htons(pin->in_port);
1356     opi->reason = pin->reason;
1357     opi->buffer_id = htonl(pin->buffer_id);
1358     update_openflow_length(rw_packet);
1359
1360     return rw_packet;
1361 }
1362
1363 /* Returns a string representing the message type of 'type'.  The string is the
1364  * enumeration constant for the type, e.g. "OFPT_HELLO".  For statistics
1365  * messages, the constant is followed by "request" or "reply",
1366  * e.g. "OFPST_AGGREGATE reply". */
1367 const char *
1368 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1369 {
1370     return type->name;
1371 }
1372 \f
1373 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1374  * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1375  * an arbitrary transaction id.  Allocated bytes beyond the header, if any, are
1376  * zeroed.
1377  *
1378  * The caller is responsible for freeing '*bufferp' when it is no longer
1379  * needed.
1380  *
1381  * The OpenFlow header length is initially set to 'openflow_len'; if the
1382  * message is later extended, the length should be updated with
1383  * update_openflow_length() before sending.
1384  *
1385  * Returns the header. */
1386 void *
1387 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1388 {
1389     *bufferp = ofpbuf_new(openflow_len);
1390     return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1391 }
1392
1393 /* Similar to make_openflow() but creates a Nicira vendor extension message
1394  * with the specific 'subtype'.  'subtype' should be in host byte order. */
1395 void *
1396 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1397 {
1398     return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1399 }
1400
1401 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1402  * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1403  * transaction id 'xid'.  Allocated bytes beyond the header, if any, are
1404  * zeroed.
1405  *
1406  * The caller is responsible for freeing '*bufferp' when it is no longer
1407  * needed.
1408  *
1409  * The OpenFlow header length is initially set to 'openflow_len'; if the
1410  * message is later extended, the length should be updated with
1411  * update_openflow_length() before sending.
1412  *
1413  * Returns the header. */
1414 void *
1415 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1416                   struct ofpbuf **bufferp)
1417 {
1418     *bufferp = ofpbuf_new(openflow_len);
1419     return put_openflow_xid(openflow_len, type, xid, *bufferp);
1420 }
1421
1422 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1423  * with the specific 'subtype'.  'subtype' should be in host byte order. */
1424 void *
1425 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1426                struct ofpbuf **bufferp)
1427 {
1428     *bufferp = ofpbuf_new(openflow_len);
1429     return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1430 }
1431
1432 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1433  * with the given 'type' and an arbitrary transaction id.  Allocated bytes
1434  * beyond the header, if any, are zeroed.
1435  *
1436  * The OpenFlow header length is initially set to 'openflow_len'; if the
1437  * message is later extended, the length should be updated with
1438  * update_openflow_length() before sending.
1439  *
1440  * Returns the header. */
1441 void *
1442 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1443 {
1444     return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1445 }
1446
1447 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1448  * with the given 'type' and an transaction id 'xid'.  Allocated bytes beyond
1449  * the header, if any, are zeroed.
1450  *
1451  * The OpenFlow header length is initially set to 'openflow_len'; if the
1452  * message is later extended, the length should be updated with
1453  * update_openflow_length() before sending.
1454  *
1455  * Returns the header. */
1456 void *
1457 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1458                  struct ofpbuf *buffer)
1459 {
1460     struct ofp_header *oh;
1461
1462     assert(openflow_len >= sizeof *oh);
1463     assert(openflow_len <= UINT16_MAX);
1464
1465     oh = ofpbuf_put_uninit(buffer, openflow_len);
1466     oh->version = OFP_VERSION;
1467     oh->type = type;
1468     oh->length = htons(openflow_len);
1469     oh->xid = xid;
1470     memset(oh + 1, 0, openflow_len - sizeof *oh);
1471     return oh;
1472 }
1473
1474 /* Similar to put_openflow() but append a Nicira vendor extension message with
1475  * the specific 'subtype'.  'subtype' should be in host byte order. */
1476 void *
1477 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1478 {
1479     return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1480 }
1481
1482 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1483  * with the specific 'subtype'.  'subtype' should be in host byte order. */
1484 void *
1485 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1486               struct ofpbuf *buffer)
1487 {
1488     struct nicira_header *nxh;
1489
1490     nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1491     nxh->vendor = htonl(NX_VENDOR_ID);
1492     nxh->subtype = htonl(subtype);
1493     return nxh;
1494 }
1495
1496 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1497  * 'buffer->size'. */
1498 void
1499 update_openflow_length(struct ofpbuf *buffer)
1500 {
1501     struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1502     oh->length = htons(buffer->size);
1503 }
1504
1505 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1506  * space allocated for the 'body' member.  Returns the first byte of the 'body'
1507  * member. */
1508 void *
1509 ofputil_make_stats_request(size_t body_len, uint16_t type,
1510                            struct ofpbuf **bufferp)
1511 {
1512     struct ofp_stats_request *osr;
1513     osr = make_openflow((offsetof(struct ofp_stats_request, body)
1514                         + body_len), OFPT_STATS_REQUEST, bufferp);
1515     osr->type = htons(type);
1516     osr->flags = htons(0);
1517     return osr->body;
1518 }
1519
1520 /* Creates a stats request message with Nicira as vendor and the given
1521  * 'subtype', of total length 'openflow_len'.  Returns the message. */
1522 void *
1523 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1524                              struct ofpbuf **bufferp)
1525 {
1526     struct nicira_stats_msg *nsm;
1527
1528     nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1529     nsm->type = htons(OFPST_VENDOR);
1530     nsm->flags = htons(0);
1531     nsm->vendor = htonl(NX_VENDOR_ID);
1532     nsm->subtype = htonl(subtype);
1533     return nsm;
1534 }
1535
1536 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1537  * ofp_stats_reply in 'oh'. */
1538 const void *
1539 ofputil_stats_body(const struct ofp_header *oh)
1540 {
1541     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1542     return ((const struct ofp_stats_request *) oh)->body;
1543 }
1544
1545 /* Returns the length of the 'body' member of the ofp_stats_request or
1546  * ofp_stats_reply in 'oh'. */
1547 size_t
1548 ofputil_stats_body_len(const struct ofp_header *oh)
1549 {
1550     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1551     return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1552 }
1553
1554 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1555 const void *
1556 ofputil_nxstats_body(const struct ofp_header *oh)
1557 {
1558     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1559     return ((const struct nicira_stats_msg *) oh) + 1;
1560 }
1561
1562 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1563 size_t
1564 ofputil_nxstats_body_len(const struct ofp_header *oh)
1565 {
1566     assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1567     return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1568 }
1569
1570 struct ofpbuf *
1571 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1572               size_t actions_len)
1573 {
1574     struct ofp_flow_mod *ofm;
1575     size_t size = sizeof *ofm + actions_len;
1576     struct ofpbuf *out = ofpbuf_new(size);
1577     ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1578     ofm->header.version = OFP_VERSION;
1579     ofm->header.type = OFPT_FLOW_MOD;
1580     ofm->header.length = htons(size);
1581     ofm->cookie = 0;
1582     ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1583     ofputil_cls_rule_to_match(rule, &ofm->match);
1584     ofm->command = htons(command);
1585     return out;
1586 }
1587
1588 struct ofpbuf *
1589 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1590               uint16_t idle_timeout, size_t actions_len)
1591 {
1592     struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1593     struct ofp_flow_mod *ofm = out->data;
1594     ofm->idle_timeout = htons(idle_timeout);
1595     ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1596     ofm->buffer_id = htonl(buffer_id);
1597     return out;
1598 }
1599
1600 struct ofpbuf *
1601 make_del_flow(const struct cls_rule *rule)
1602 {
1603     struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1604     struct ofp_flow_mod *ofm = out->data;
1605     ofm->out_port = htons(OFPP_NONE);
1606     return out;
1607 }
1608
1609 struct ofpbuf *
1610 make_add_simple_flow(const struct cls_rule *rule,
1611                      uint32_t buffer_id, uint16_t out_port,
1612                      uint16_t idle_timeout)
1613 {
1614     if (out_port != OFPP_NONE) {
1615         struct ofp_action_output *oao;
1616         struct ofpbuf *buffer;
1617
1618         buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1619         oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1620         oao->type = htons(OFPAT_OUTPUT);
1621         oao->len = htons(sizeof *oao);
1622         oao->port = htons(out_port);
1623         return buffer;
1624     } else {
1625         return make_add_flow(rule, buffer_id, idle_timeout, 0);
1626     }
1627 }
1628
1629 struct ofpbuf *
1630 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1631                const struct ofpbuf *payload, int max_send_len)
1632 {
1633     struct ofp_packet_in *opi;
1634     struct ofpbuf *buf;
1635     int send_len;
1636
1637     send_len = MIN(max_send_len, payload->size);
1638     buf = ofpbuf_new(sizeof *opi + send_len);
1639     opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1640                            OFPT_PACKET_IN, 0, buf);
1641     opi->buffer_id = htonl(buffer_id);
1642     opi->total_len = htons(payload->size);
1643     opi->in_port = htons(in_port);
1644     opi->reason = reason;
1645     ofpbuf_put(buf, payload->data, send_len);
1646     update_openflow_length(buf);
1647
1648     return buf;
1649 }
1650
1651 struct ofpbuf *
1652 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1653                 uint16_t in_port,
1654                 const struct ofp_action_header *actions, size_t n_actions)
1655 {
1656     size_t actions_len = n_actions * sizeof *actions;
1657     struct ofp_packet_out *opo;
1658     size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1659     struct ofpbuf *out = ofpbuf_new(size);
1660
1661     opo = ofpbuf_put_uninit(out, sizeof *opo);
1662     opo->header.version = OFP_VERSION;
1663     opo->header.type = OFPT_PACKET_OUT;
1664     opo->header.length = htons(size);
1665     opo->header.xid = htonl(0);
1666     opo->buffer_id = htonl(buffer_id);
1667     opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1668     opo->actions_len = htons(actions_len);
1669     ofpbuf_put(out, actions, actions_len);
1670     if (packet) {
1671         ofpbuf_put(out, packet->data, packet->size);
1672     }
1673     return out;
1674 }
1675
1676 struct ofpbuf *
1677 make_unbuffered_packet_out(const struct ofpbuf *packet,
1678                            uint16_t in_port, uint16_t out_port)
1679 {
1680     struct ofp_action_output action;
1681     action.type = htons(OFPAT_OUTPUT);
1682     action.len = htons(sizeof action);
1683     action.port = htons(out_port);
1684     return make_packet_out(packet, UINT32_MAX, in_port,
1685                            (struct ofp_action_header *) &action, 1);
1686 }
1687
1688 struct ofpbuf *
1689 make_buffered_packet_out(uint32_t buffer_id,
1690                          uint16_t in_port, uint16_t out_port)
1691 {
1692     if (out_port != OFPP_NONE) {
1693         struct ofp_action_output action;
1694         action.type = htons(OFPAT_OUTPUT);
1695         action.len = htons(sizeof action);
1696         action.port = htons(out_port);
1697         return make_packet_out(NULL, buffer_id, in_port,
1698                                (struct ofp_action_header *) &action, 1);
1699     } else {
1700         return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1701     }
1702 }
1703
1704 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1705 struct ofpbuf *
1706 make_echo_request(void)
1707 {
1708     struct ofp_header *rq;
1709     struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1710     rq = ofpbuf_put_uninit(out, sizeof *rq);
1711     rq->version = OFP_VERSION;
1712     rq->type = OFPT_ECHO_REQUEST;
1713     rq->length = htons(sizeof *rq);
1714     rq->xid = htonl(0);
1715     return out;
1716 }
1717
1718 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1719  * OFPT_ECHO_REQUEST message in 'rq'. */
1720 struct ofpbuf *
1721 make_echo_reply(const struct ofp_header *rq)
1722 {
1723     size_t size = ntohs(rq->length);
1724     struct ofpbuf *out = ofpbuf_new(size);
1725     struct ofp_header *reply = ofpbuf_put(out, rq, size);
1726     reply->type = OFPT_ECHO_REPLY;
1727     return out;
1728 }
1729
1730 /* Converts the members of 'opp' from host to network byte order. */
1731 void
1732 hton_ofp_phy_port(struct ofp_phy_port *opp)
1733 {
1734     opp->port_no = htons(opp->port_no);
1735     opp->config = htonl(opp->config);
1736     opp->state = htonl(opp->state);
1737     opp->curr = htonl(opp->curr);
1738     opp->advertised = htonl(opp->advertised);
1739     opp->supported = htonl(opp->supported);
1740     opp->peer = htonl(opp->peer);
1741 }
1742
1743 static int
1744 check_action_exact_len(const union ofp_action *a, unsigned int len,
1745                        unsigned int required_len)
1746 {
1747     if (len != required_len) {
1748         VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1749                      "%"PRIu16" (must be %u)\n",
1750                      ntohs(a->type), ntohs(a->header.len), required_len);
1751         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1752     }
1753     return 0;
1754 }
1755
1756 static int
1757 check_nx_action_exact_len(const struct nx_action_header *a,
1758                           unsigned int len, unsigned int required_len)
1759 {
1760     if (len != required_len) {
1761         VLOG_WARN_RL(&bad_ofmsg_rl,
1762                      "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1763                      "(must be %u)\n",
1764                      ntohs(a->subtype), ntohs(a->len), required_len);
1765         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1766     }
1767     return 0;
1768 }
1769
1770 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1771  * that the switch will never have more than 'max_ports' ports.  Returns 0 if
1772  * 'port' is valid, otherwise an ofp_mkerr() return code. */
1773 static int
1774 check_output_port(uint16_t port, int max_ports)
1775 {
1776     switch (port) {
1777     case OFPP_IN_PORT:
1778     case OFPP_TABLE:
1779     case OFPP_NORMAL:
1780     case OFPP_FLOOD:
1781     case OFPP_ALL:
1782     case OFPP_CONTROLLER:
1783     case OFPP_LOCAL:
1784         return 0;
1785
1786     default:
1787         if (port < max_ports) {
1788             return 0;
1789         }
1790         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1791         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1792     }
1793 }
1794
1795 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1796  * will never have more than 'max_ports' ports.  Returns 0 if 'port' is valid,
1797  * otherwise an ofp_mkerr() return code. */
1798 static int
1799 check_enqueue_action(const union ofp_action *a, unsigned int len,
1800                      int max_ports)
1801 {
1802     const struct ofp_action_enqueue *oae;
1803     uint16_t port;
1804     int error;
1805
1806     error = check_action_exact_len(a, len, 16);
1807     if (error) {
1808         return error;
1809     }
1810
1811     oae = (const struct ofp_action_enqueue *) a;
1812     port = ntohs(oae->port);
1813     if (port < max_ports || port == OFPP_IN_PORT) {
1814         return 0;
1815     }
1816     VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1817     return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1818 }
1819
1820 static int
1821 check_nicira_action(const union ofp_action *a, unsigned int len,
1822                     const struct flow *flow)
1823 {
1824     const struct nx_action_header *nah;
1825     int subtype;
1826     int error;
1827
1828     if (len < 16) {
1829         VLOG_WARN_RL(&bad_ofmsg_rl,
1830                      "Nicira vendor action only %u bytes", len);
1831         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1832     }
1833     nah = (const struct nx_action_header *) a;
1834
1835     subtype = ntohs(nah->subtype);
1836     if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1837         /* This is necessary because enum nx_action_subtype may be an
1838          * 8-bit type, so the cast below throws away the top 8 bits. */
1839         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1840     }
1841
1842     switch ((enum nx_action_subtype) subtype) {
1843     case NXAST_RESUBMIT:
1844     case NXAST_SET_TUNNEL:
1845     case NXAST_DROP_SPOOFED_ARP:
1846     case NXAST_SET_QUEUE:
1847     case NXAST_POP_QUEUE:
1848         return check_nx_action_exact_len(nah, len, 16);
1849
1850     case NXAST_REG_MOVE:
1851         error = check_nx_action_exact_len(nah, len,
1852                                           sizeof(struct nx_action_reg_move));
1853         if (error) {
1854             return error;
1855         }
1856         return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1857
1858     case NXAST_REG_LOAD:
1859         error = check_nx_action_exact_len(nah, len,
1860                                           sizeof(struct nx_action_reg_load));
1861         if (error) {
1862             return error;
1863         }
1864         return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1865
1866     case NXAST_NOTE:
1867         return 0;
1868
1869     case NXAST_SET_TUNNEL64:
1870         return check_nx_action_exact_len(
1871             nah, len, sizeof(struct nx_action_set_tunnel64));
1872
1873     case NXAST_MULTIPATH:
1874         error = check_nx_action_exact_len(
1875             nah, len, sizeof(struct nx_action_multipath));
1876         if (error) {
1877             return error;
1878         }
1879         return multipath_check((const struct nx_action_multipath *) a);
1880
1881     case NXAST_AUTOPATH:
1882         error = check_nx_action_exact_len(
1883             nah, len, sizeof(struct nx_action_autopath));
1884         if (error) {
1885             return error;
1886         }
1887         return autopath_check((const struct nx_action_autopath *) a);
1888
1889     case NXAST_SNAT__OBSOLETE:
1890     default:
1891         VLOG_WARN_RL(&bad_ofmsg_rl,
1892                      "unknown Nicira vendor action subtype %d", subtype);
1893         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1894     }
1895 }
1896
1897 static int
1898 check_action(const union ofp_action *a, unsigned int len,
1899              const struct flow *flow, int max_ports)
1900 {
1901     enum ofp_action_type type = ntohs(a->type);
1902     int error;
1903
1904     switch (type) {
1905     case OFPAT_OUTPUT:
1906         error = check_action_exact_len(a, len, 8);
1907         if (error) {
1908             return error;
1909         }
1910         return check_output_port(ntohs(a->output.port), max_ports);
1911
1912     case OFPAT_SET_VLAN_VID:
1913         error = check_action_exact_len(a, len, 8);
1914         if (error) {
1915             return error;
1916         }
1917         if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1918             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1919         }
1920         return 0;
1921
1922     case OFPAT_SET_VLAN_PCP:
1923         error = check_action_exact_len(a, len, 8);
1924         if (error) {
1925             return error;
1926         }
1927         if (a->vlan_pcp.vlan_pcp & ~7) {
1928             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1929         }
1930         return 0;
1931
1932     case OFPAT_STRIP_VLAN:
1933     case OFPAT_SET_NW_SRC:
1934     case OFPAT_SET_NW_DST:
1935     case OFPAT_SET_NW_TOS:
1936     case OFPAT_SET_TP_SRC:
1937     case OFPAT_SET_TP_DST:
1938         return check_action_exact_len(a, len, 8);
1939
1940     case OFPAT_SET_DL_SRC:
1941     case OFPAT_SET_DL_DST:
1942         return check_action_exact_len(a, len, 16);
1943
1944     case OFPAT_VENDOR:
1945         return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1946                 ? check_nicira_action(a, len, flow)
1947                 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1948
1949     case OFPAT_ENQUEUE:
1950         return check_enqueue_action(a, len, max_ports);
1951
1952     default:
1953         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1954         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1955     }
1956 }
1957
1958 int
1959 validate_actions(const union ofp_action *actions, size_t n_actions,
1960                  const struct flow *flow, int max_ports)
1961 {
1962     size_t i;
1963
1964     for (i = 0; i < n_actions; ) {
1965         const union ofp_action *a = &actions[i];
1966         unsigned int len = ntohs(a->header.len);
1967         unsigned int n_slots = len / OFP_ACTION_ALIGN;
1968         unsigned int slots_left = &actions[n_actions] - a;
1969         int error;
1970
1971         if (n_slots > slots_left) {
1972             VLOG_WARN_RL(&bad_ofmsg_rl,
1973                          "action requires %u slots but only %u remain",
1974                          n_slots, slots_left);
1975             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1976         } else if (!len) {
1977             VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
1978             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1979         } else if (len % OFP_ACTION_ALIGN) {
1980             VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1981                          "of %d", len, OFP_ACTION_ALIGN);
1982             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1983         }
1984
1985         error = check_action(a, len, flow, max_ports);
1986         if (error) {
1987             return error;
1988         }
1989         i += n_slots;
1990     }
1991     return 0;
1992 }
1993
1994 /* Returns true if 'action' outputs to 'port' (which must be in network byte
1995  * order), false otherwise. */
1996 bool
1997 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1998 {
1999     switch (ntohs(action->type)) {
2000     case OFPAT_OUTPUT:
2001         return action->output.port == port;
2002     case OFPAT_ENQUEUE:
2003         return ((const struct ofp_action_enqueue *) action)->port == port;
2004     default:
2005         return false;
2006     }
2007 }
2008
2009 /* The set of actions must either come from a trusted source or have been
2010  * previously validated with validate_actions(). */
2011 const union ofp_action *
2012 actions_first(struct actions_iterator *iter,
2013               const union ofp_action *oa, size_t n_actions)
2014 {
2015     iter->pos = oa;
2016     iter->end = oa + n_actions;
2017     return actions_next(iter);
2018 }
2019
2020 const union ofp_action *
2021 actions_next(struct actions_iterator *iter)
2022 {
2023     if (iter->pos != iter->end) {
2024         const union ofp_action *a = iter->pos;
2025         unsigned int len = ntohs(a->header.len);
2026         iter->pos += len / OFP_ACTION_ALIGN;
2027         return a;
2028     } else {
2029         return NULL;
2030     }
2031 }
2032
2033 static void
2034 normalize_match(struct ofp_match *m)
2035 {
2036     enum { OFPFW_NW = (OFPFW_NW_SRC_ALL | OFPFW_NW_DST_ALL | OFPFW_NW_PROTO
2037                        | OFPFW_NW_TOS) };
2038     enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
2039     uint32_t wc;
2040
2041     wc = ntohl(m->wildcards) & OFPFW_ALL;
2042     if (wc & OFPFW_DL_TYPE) {
2043         /* Can't sensibly match on network or transport headers if the
2044          * data link type is unknown. */
2045         wc |= OFPFW_NW | OFPFW_TP;
2046     } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2047         if (wc & OFPFW_NW_PROTO) {
2048             /* Can't sensibly match on transport headers if the network
2049              * protocol is unknown. */
2050             wc |= OFPFW_TP;
2051         } else if (m->nw_proto != IPPROTO_TCP &&
2052                    m->nw_proto != IPPROTO_UDP &&
2053                    m->nw_proto != IPPROTO_ICMP) {
2054             /* Transport layer fields will always be extracted as zeros, so we
2055              * can do an exact-match on those values.  */
2056             wc &= ~OFPFW_TP;
2057             m->tp_src = m->tp_dst = 0;
2058         }
2059     } else if (m->dl_type != htons(ETH_TYPE_ARP) &&
2060                m->dl_type != htons(ETH_TYPE_IPV6)) {
2061         /* Network and transport layer fields will always be extracted as
2062          * zeros, so we can do an exact-match on those values. */
2063         wc &= ~(OFPFW_NW | OFPFW_TP);
2064         m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
2065         m->tp_src = m->tp_dst = 0;
2066     }
2067     m->wildcards = htonl(wc);
2068 }
2069
2070 /* Returns a string that describes 'match' in a very literal way, without
2071  * interpreting its contents except in a very basic fashion.  The returned
2072  * string is intended to be fixed-length, so that it is easy to see differences
2073  * between two such strings if one is put above another.  This is useful for
2074  * describing changes made by normalize_match().
2075  *
2076  * The caller must free the returned string (with free()). */
2077 char *
2078 ofp_match_to_literal_string(const struct ofp_match *match)
2079 {
2080     return xasprintf("wildcards=%#10"PRIx32" "
2081                      " in_port=%5"PRId16" "
2082                      " dl_src="ETH_ADDR_FMT" "
2083                      " dl_dst="ETH_ADDR_FMT" "
2084                      " dl_vlan=%5"PRId16" "
2085                      " dl_vlan_pcp=%3"PRId8" "
2086                      " dl_type=%#6"PRIx16" "
2087                      " nw_tos=%#4"PRIx8" "
2088                      " nw_proto=%#4"PRIx16" "
2089                      " nw_src=%#10"PRIx32" "
2090                      " nw_dst=%#10"PRIx32" "
2091                      " tp_src=%5"PRId16" "
2092                      " tp_dst=%5"PRId16,
2093                      ntohl(match->wildcards),
2094                      ntohs(match->in_port),
2095                      ETH_ADDR_ARGS(match->dl_src),
2096                      ETH_ADDR_ARGS(match->dl_dst),
2097                      ntohs(match->dl_vlan),
2098                      match->dl_vlan_pcp,
2099                      ntohs(match->dl_type),
2100                      match->nw_tos,
2101                      match->nw_proto,
2102                      ntohl(match->nw_src),
2103                      ntohl(match->nw_dst),
2104                      ntohs(match->tp_src),
2105                      ntohs(match->tp_dst));
2106 }
2107
2108 static uint32_t
2109 vendor_code_to_id(uint8_t code)
2110 {
2111     switch (code) {
2112 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2113         OFPUTIL_VENDORS
2114 #undef OFPUTIL_VENDOR
2115     default:
2116         return UINT32_MAX;
2117     }
2118 }
2119
2120 static int
2121 vendor_id_to_code(uint32_t id)
2122 {
2123     switch (id) {
2124 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2125         OFPUTIL_VENDORS
2126 #undef OFPUTIL_VENDOR
2127     default:
2128         return -1;
2129     }
2130 }
2131
2132 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2133  * information taken from 'error', whose encoding must be as described in the
2134  * large comment in ofp-util.h.  If 'oh' is nonnull, then the error will use
2135  * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2136  * of 'oh'.
2137  *
2138  * Returns NULL if 'error' is not an OpenFlow error code. */
2139 struct ofpbuf *
2140 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2141 {
2142     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2143
2144     struct ofpbuf *buf;
2145     const void *data;
2146     size_t len;
2147     uint8_t vendor;
2148     uint16_t type;
2149     uint16_t code;
2150     ovs_be32 xid;
2151
2152     if (!is_ofp_error(error)) {
2153         /* We format 'error' with strerror() here since it seems likely to be
2154          * a system errno value. */
2155         VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2156                      error, strerror(error));
2157         return NULL;
2158     }
2159
2160     if (oh) {
2161         xid = oh->xid;
2162         data = oh;
2163         len = ntohs(oh->length);
2164         if (len > 64) {
2165             len = 64;
2166         }
2167     } else {
2168         xid = 0;
2169         data = NULL;
2170         len = 0;
2171     }
2172
2173     vendor = get_ofp_err_vendor(error);
2174     type = get_ofp_err_type(error);
2175     code = get_ofp_err_code(error);
2176     if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2177         struct ofp_error_msg *oem;
2178
2179         oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2180         oem->type = htons(type);
2181         oem->code = htons(code);
2182     } else {
2183         struct ofp_error_msg *oem;
2184         struct nx_vendor_error *nve;
2185         uint32_t vendor_id;
2186
2187         vendor_id = vendor_code_to_id(vendor);
2188         if (vendor_id == UINT32_MAX) {
2189             VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2190                          error, vendor);
2191             return NULL;
2192         }
2193
2194         oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2195                                 OFPT_ERROR, xid, &buf);
2196         oem->type = htons(NXET_VENDOR);
2197         oem->code = htons(NXVC_VENDOR_ERROR);
2198
2199         nve = (struct nx_vendor_error *)oem->data;
2200         nve->vendor = htonl(vendor_id);
2201         nve->type = htons(type);
2202         nve->code = htons(code);
2203     }
2204
2205     if (len) {
2206         buf->size -= len;
2207         ofpbuf_put(buf, data, len);
2208     }
2209
2210     return buf;
2211 }
2212
2213 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2214  * Open vSwitch internal error code in the format described in the large
2215  * comment in ofp-util.h.
2216  *
2217  * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2218  * to the payload starting from 'oh' and on failure it is set to 0. */
2219 int
2220 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2221 {
2222     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2223
2224     const struct ofp_error_msg *oem;
2225     uint16_t type, code;
2226     struct ofpbuf b;
2227     int vendor;
2228
2229     if (payload_ofs) {
2230         *payload_ofs = 0;
2231     }
2232     if (oh->type != OFPT_ERROR) {
2233         return EPROTO;
2234     }
2235
2236     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2237     oem = ofpbuf_try_pull(&b, sizeof *oem);
2238     if (!oem) {
2239         return EPROTO;
2240     }
2241
2242     type = ntohs(oem->type);
2243     code = ntohs(oem->code);
2244     if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2245         const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2246         if (!nve) {
2247             return EPROTO;
2248         }
2249
2250         vendor = vendor_id_to_code(ntohl(nve->vendor));
2251         if (vendor < 0) {
2252             VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2253                          ntohl(nve->vendor));
2254             return EPROTO;
2255         }
2256         type = ntohs(nve->type);
2257         code = ntohs(nve->code);
2258     } else {
2259         vendor = OFPUTIL_VENDOR_OPENFLOW;
2260     }
2261
2262     if (type >= 1024) {
2263         VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2264                      "supported maximum value 1023", type);
2265         return EPROTO;
2266     }
2267
2268     if (payload_ofs) {
2269         *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2270     }
2271     return ofp_mkerr_vendor(vendor, type, code);
2272 }
2273
2274 void
2275 ofputil_format_error(struct ds *s, int error)
2276 {
2277     if (is_errno(error)) {
2278         ds_put_cstr(s, strerror(error));
2279     } else {
2280         uint16_t type = get_ofp_err_type(error);
2281         uint16_t code = get_ofp_err_code(error);
2282         const char *type_s = ofp_error_type_to_string(type);
2283         const char *code_s = ofp_error_code_to_string(type, code);
2284
2285         ds_put_format(s, "type ");
2286         if (type_s) {
2287             ds_put_cstr(s, type_s);
2288         } else {
2289             ds_put_format(s, "%"PRIu16, type);
2290         }
2291
2292         ds_put_cstr(s, ", code ");
2293         if (code_s) {
2294             ds_put_cstr(s, code_s);
2295         } else {
2296             ds_put_format(s, "%"PRIu16, code);
2297         }
2298     }
2299 }
2300
2301 char *
2302 ofputil_error_to_string(int error)
2303 {
2304     struct ds s = DS_EMPTY_INITIALIZER;
2305     ofputil_format_error(&s, error);
2306     return ds_steal_cstr(&s);
2307 }
2308
2309 /* Attempts to pull 'actions_len' bytes from the front of 'b'.  Returns 0 if
2310  * successful, otherwise an OpenFlow error.
2311  *
2312  * If successful, the first action is stored in '*actionsp' and the number of
2313  * "union ofp_action" size elements into '*n_actionsp'.  Otherwise NULL and 0
2314  * are stored, respectively.
2315  *
2316  * This function does not check that the actions are valid (the caller should
2317  * do so, with validate_actions()).  The caller is also responsible for making
2318  * sure that 'b->data' is initially aligned appropriately for "union
2319  * ofp_action". */
2320 int
2321 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2322                      union ofp_action **actionsp, size_t *n_actionsp)
2323 {
2324     if (actions_len % OFP_ACTION_ALIGN != 0) {
2325         VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2326                      "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2327         goto error;
2328     }
2329
2330     *actionsp = ofpbuf_try_pull(b, actions_len);
2331     if (*actionsp == NULL) {
2332         VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2333                      "exceeds remaining message length (%zu)",
2334                      actions_len, b->size);
2335         goto error;
2336     }
2337
2338     *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2339     return 0;
2340
2341 error:
2342     *actionsp = NULL;
2343     *n_actionsp = 0;
2344     return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2345 }