vconn: Move OpenFlow utility functions into new file ofp-util.c.
[cascardo/ovs.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 #ifndef OFP_UTIL_H
18 #define OFP_UTIL_H 1
19
20 #include <assert.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include "flow.h"
24
25 struct ofpbuf;
26 struct ofp_action_header;
27
28 /* OpenFlow protocol utility functions. */
29 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
30 void *make_openflow_xid(size_t openflow_len, uint8_t type,
31                         uint32_t xid, struct ofpbuf **);
32 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
33 void *put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
34                        struct ofpbuf *);
35 void update_openflow_length(struct ofpbuf *);
36 struct ofpbuf *make_flow_mod(uint16_t command, const flow_t *,
37                              size_t actions_len);
38 struct ofpbuf *make_add_flow(const flow_t *, uint32_t buffer_id,
39                              uint16_t max_idle, size_t actions_len);
40 struct ofpbuf *make_del_flow(const flow_t *);
41 struct ofpbuf *make_add_simple_flow(const flow_t *,
42                                     uint32_t buffer_id, uint16_t out_port,
43                                     uint16_t max_idle);
44 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
45                               uint8_t reason,
46                               const struct ofpbuf *payload, int max_send_len);
47 struct ofpbuf *make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
48                                uint16_t in_port,
49                                const struct ofp_action_header *,
50                                size_t n_actions);
51 struct ofpbuf *make_buffered_packet_out(uint32_t buffer_id,
52                                         uint16_t in_port, uint16_t out_port);
53 struct ofpbuf *make_unbuffered_packet_out(const struct ofpbuf *packet,
54                                           uint16_t in_port, uint16_t out_port);
55 struct ofpbuf *make_echo_request(void);
56 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
57 int check_ofp_message(const struct ofp_header *, uint8_t type, size_t size);
58 int check_ofp_message_array(const struct ofp_header *, uint8_t type,
59                             size_t size, size_t array_elt_size,
60                             size_t *n_array_elts);
61 int check_ofp_packet_out(const struct ofp_header *, struct ofpbuf *data,
62                          int *n_actions, int max_ports);
63
64 struct flow_stats_iterator {
65     const uint8_t *pos, *end;
66 };
67 const struct ofp_flow_stats *flow_stats_first(struct flow_stats_iterator *,
68                                               const struct ofp_stats_reply *);
69 const struct ofp_flow_stats *flow_stats_next(struct flow_stats_iterator *);
70
71 struct actions_iterator {
72     const union ofp_action *pos, *end;
73 };
74 const union ofp_action *actions_first(struct actions_iterator *,
75                                       const union ofp_action *,
76                                       size_t n_actions);
77 const union ofp_action *actions_next(struct actions_iterator *);
78 int validate_actions(const union ofp_action *, size_t n_actions,
79                      int max_ports);
80
81 void normalize_match(struct ofp_match *);
82
83 static inline int
84 ofp_mkerr(uint16_t type, uint16_t code)
85 {
86     assert(type > 0 && type <= 0x7fff);
87     return (type << 16) | code;
88 }
89
90 #endif /* ofp-util.h */