vconn: New functions vconn_verify_name(), pvconn_verify_name().
[sliver-openvswitch.git] / lib / vconn.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 VCONN_H
18 #define VCONN_H 1
19
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "flow.h"
26
27 struct ofpbuf;
28 struct ofp_action_header;
29 struct ofp_header;
30 struct ofp_match;
31 struct ofp_stats_reply;
32 struct pvconn;
33 struct vconn;
34
35 void vconn_usage(bool active, bool passive, bool bootstrap);
36
37 /* Active vconns: virtual connections to OpenFlow devices. */
38 int vconn_verify_name(const char *name);
39 int vconn_open(const char *name, int min_version, struct vconn **);
40 void vconn_close(struct vconn *);
41 const char *vconn_get_name(const struct vconn *);
42 uint32_t vconn_get_remote_ip(const struct vconn *);
43 uint16_t vconn_get_remote_port(const struct vconn *);
44 uint32_t vconn_get_local_ip(const struct vconn *);
45 uint16_t vconn_get_local_port(const struct vconn *);
46 int vconn_connect(struct vconn *);
47 int vconn_recv(struct vconn *, struct ofpbuf **);
48 int vconn_send(struct vconn *, struct ofpbuf *);
49 int vconn_recv_xid(struct vconn *, uint32_t xid, struct ofpbuf **);
50 int vconn_transact(struct vconn *, struct ofpbuf *, struct ofpbuf **);
51
52 int vconn_open_block(const char *name, int min_version, struct vconn **);
53 int vconn_send_block(struct vconn *, struct ofpbuf *);
54 int vconn_recv_block(struct vconn *, struct ofpbuf **);
55
56 enum vconn_wait_type {
57     WAIT_CONNECT,
58     WAIT_RECV,
59     WAIT_SEND
60 };
61 void vconn_wait(struct vconn *, enum vconn_wait_type);
62 void vconn_connect_wait(struct vconn *);
63 void vconn_recv_wait(struct vconn *);
64 void vconn_send_wait(struct vconn *);
65
66 /* Passive vconns: virtual listeners for incoming OpenFlow connections. */
67 int pvconn_verify_name(const char *name);
68 int pvconn_open(const char *name, struct pvconn **);
69 const char *pvconn_get_name(const struct pvconn *);
70 void pvconn_close(struct pvconn *);
71 int pvconn_accept(struct pvconn *, int min_version, struct vconn **);
72 void pvconn_wait(struct pvconn *);
73
74 /* OpenFlow protocol utility functions. */
75 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
76 void *make_openflow_xid(size_t openflow_len, uint8_t type,
77                         uint32_t xid, struct ofpbuf **);
78 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
79 void *put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
80                        struct ofpbuf *);
81 void update_openflow_length(struct ofpbuf *);
82 struct ofpbuf *make_flow_mod(uint16_t command, const flow_t *,
83                              size_t actions_len);
84 struct ofpbuf *make_add_flow(const flow_t *, uint32_t buffer_id,
85                              uint16_t max_idle, size_t actions_len);
86 struct ofpbuf *make_del_flow(const flow_t *);
87 struct ofpbuf *make_add_simple_flow(const flow_t *,
88                                     uint32_t buffer_id, uint16_t out_port,
89                                     uint16_t max_idle);
90 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
91                               uint8_t reason,
92                               const struct ofpbuf *payload, int max_send_len);
93 struct ofpbuf *make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
94                                uint16_t in_port,
95                                const struct ofp_action_header *,
96                                size_t n_actions);
97 struct ofpbuf *make_buffered_packet_out(uint32_t buffer_id,
98                                         uint16_t in_port, uint16_t out_port);
99 struct ofpbuf *make_unbuffered_packet_out(const struct ofpbuf *packet,
100                                           uint16_t in_port, uint16_t out_port);
101 struct ofpbuf *make_echo_request(void);
102 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
103 int check_ofp_message(const struct ofp_header *, uint8_t type, size_t size);
104 int check_ofp_message_array(const struct ofp_header *, uint8_t type,
105                             size_t size, size_t array_elt_size,
106                             size_t *n_array_elts);
107 int check_ofp_packet_out(const struct ofp_header *, struct ofpbuf *data,
108                          int *n_actions, int max_ports);
109
110 struct flow_stats_iterator {
111     const uint8_t *pos, *end;
112 };
113 const struct ofp_flow_stats *flow_stats_first(struct flow_stats_iterator *,
114                                               const struct ofp_stats_reply *);
115 const struct ofp_flow_stats *flow_stats_next(struct flow_stats_iterator *);
116
117 struct actions_iterator {
118     const union ofp_action *pos, *end;
119 };
120 const union ofp_action *actions_first(struct actions_iterator *,
121                                       const union ofp_action *,
122                                       size_t n_actions);
123 const union ofp_action *actions_next(struct actions_iterator *);
124 int validate_actions(const union ofp_action *, size_t n_actions,
125                      int max_ports);
126
127 void normalize_match(struct ofp_match *);
128
129 static inline int
130 ofp_mkerr(uint16_t type, uint16_t code)
131 {
132     assert(type > 0 && type <= 0x7fff);
133     return (type << 16) | code;
134 }
135
136 #endif /* vconn.h */