2 * Copyright (c) 2013, 2014 Alexandru Copot <alex.mihai.c@gmail.com>, with support from IXIA.
3 * Copyright (c) 2013, 2014 Daniel Baluta <dbaluta@ixiacom.com>
4 * Copyright (c) 2014 Nicira, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "fail-open.h"
25 #include "ofp-actions.h"
29 #include "ofproto-provider.h"
31 #include "poll-loop.h"
38 #include "openvswitch/vconn.h"
39 #include "openvswitch/vlog.h"
43 VLOG_DEFINE_THIS_MODULE(bundles);
51 struct hmap_node node; /* In struct ofconn's "bundles" hmap. */
54 enum bundle_state state;
56 /* List of 'struct bundle_message's */
57 struct ovs_list msg_list;
60 struct bundle_message {
61 struct ofp_header *msg;
62 struct ovs_list node; /* Element in 'struct ofp_bundles's msg_list */
66 bundle_hash(uint32_t id)
68 return hash_int(id, 0);
71 static struct ofp_bundle *
72 ofp_bundle_find(struct hmap *bundles, uint32_t id)
74 struct ofp_bundle *bundle;
76 HMAP_FOR_EACH_IN_BUCKET(bundle, node, bundle_hash(id), bundles) {
77 if (bundle->id == id) {
85 static struct ofp_bundle *
86 ofp_bundle_create(uint32_t id, uint16_t flags)
88 struct ofp_bundle *bundle;
90 bundle = xmalloc(sizeof(*bundle));
93 bundle->flags = flags;
95 list_init(&bundle->msg_list);
101 ofp_bundle_remove(struct ofconn *ofconn, struct ofp_bundle *item)
103 struct bundle_message *msg;
104 struct hmap *bundles;
106 LIST_FOR_EACH_POP (msg, node, &item->msg_list) {
111 bundles = ofconn_get_bundles(ofconn);
112 hmap_remove(bundles, &item->node);
118 ofp_bundle_remove_all(struct ofconn *ofconn)
120 struct ofp_bundle *b, *next;
121 struct hmap *bundles;
123 bundles = ofconn_get_bundles(ofconn);
125 HMAP_FOR_EACH_SAFE (b, next, node, bundles) {
126 ofp_bundle_remove(ofconn, b);
131 ofp_bundle_open(struct ofconn *ofconn, uint32_t id, uint16_t flags)
133 struct hmap *bundles;
134 struct ofp_bundle *bundle;
136 bundles = ofconn_get_bundles(ofconn);
137 bundle = ofp_bundle_find(bundles, id);
140 VLOG_INFO("Bundle %x already exists.", id);
141 ofp_bundle_remove(ofconn, bundle);
143 return OFPERR_OFPBFC_BAD_ID;
146 /* XXX: Check the limit of open bundles */
148 bundle = ofp_bundle_create(id, flags);
149 bundle->state = BS_OPEN;
151 bundles = ofconn_get_bundles(ofconn);
152 hmap_insert(bundles, &bundle->node, bundle_hash(id));
158 ofp_bundle_close(struct ofconn *ofconn, uint32_t id, uint16_t flags)
160 struct hmap *bundles;
161 struct ofp_bundle *bundle;
163 bundles = ofconn_get_bundles(ofconn);
164 bundle = ofp_bundle_find(bundles, id);
167 return OFPERR_OFPBFC_BAD_ID;
170 if (bundle->state == BS_CLOSED) {
171 ofp_bundle_remove(ofconn, bundle);
172 return OFPERR_OFPBFC_BUNDLE_CLOSED;
175 if (bundle->flags != flags) {
176 ofp_bundle_remove(ofconn, bundle);
177 return OFPERR_OFPBFC_BAD_FLAGS;
180 bundle->state = BS_CLOSED;
185 ofp_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags)
187 struct hmap *bundles;
188 struct ofp_bundle *bundle;
189 enum ofperr error = 0;
190 struct bundle_message *msg;
192 bundles = ofconn_get_bundles(ofconn);
193 bundle = ofp_bundle_find(bundles, id);
196 return OFPERR_OFPBFC_BAD_ID;
198 if (bundle->flags != flags) {
199 error = OFPERR_OFPBFC_BAD_FLAGS;
201 LIST_FOR_EACH (msg, node, &bundle->msg_list) {
202 /* XXX: actual commit */
203 error = OFPERR_OFPBFC_MSG_FAILED;
207 ofp_bundle_remove(ofconn, bundle);
212 ofp_bundle_discard(struct ofconn *ofconn, uint32_t id)
214 struct hmap *bundles;
215 struct ofp_bundle *bundle;
217 bundles = ofconn_get_bundles(ofconn);
218 bundle = ofp_bundle_find(bundles, id);
221 return OFPERR_OFPBFC_BAD_ID;
224 ofp_bundle_remove(ofconn, bundle);
230 ofp_bundle_add_message(struct ofconn *ofconn, struct ofputil_bundle_add_msg *badd)
232 struct hmap *bundles;
233 struct ofp_bundle *bundle;
234 struct bundle_message *bmsg;
236 bundles = ofconn_get_bundles(ofconn);
237 bundle = ofp_bundle_find(bundles, badd->bundle_id);
240 bundle = ofp_bundle_create(badd->bundle_id, badd->flags);
241 bundle->state = BS_OPEN;
243 bundles = ofconn_get_bundles(ofconn);
244 hmap_insert(bundles, &bundle->node, bundle_hash(badd->bundle_id));
245 } else if (bundle->state == BS_CLOSED) {
246 ofp_bundle_remove(ofconn, bundle);
247 return OFPERR_OFPBFC_BUNDLE_CLOSED;
248 } else if (badd->flags != bundle->flags) {
249 ofp_bundle_remove(ofconn, bundle);
250 return OFPERR_OFPBFC_BAD_FLAGS;
253 bmsg = xmalloc(sizeof *bmsg);
254 bmsg->msg = xmemdup(badd->msg, ntohs(badd->msg->length));
255 list_push_back(&bundle->msg_list, &bmsg->node);