]>
Commit | Line | Data |
---|---|---|
717c7fc5 JP |
1 | /* Copyright (c) 2015 Nicira, Inc. |
2 | * | |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. | |
14 | */ | |
15 | ||
16 | #include <config.h> | |
17 | #include "chassis.h" | |
18 | ||
19 | #include "lib/poll-loop.h" | |
20 | #include "lib/util.h" | |
21 | #include "lib/vswitch-idl.h" | |
22 | #include "openvswitch/vlog.h" | |
23 | #include "ovn/ovn-sb-idl.h" | |
24 | #include "ovn-controller.h" | |
25 | ||
26 | VLOG_DEFINE_THIS_MODULE(chassis); | |
27 | ||
28 | void | |
29 | chassis_init(struct controller_ctx *ctx) | |
30 | { | |
31 | ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_open_vswitch); | |
32 | ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_open_vswitch_col_external_ids); | |
33 | } | |
34 | ||
35 | static void | |
36 | register_chassis(struct controller_ctx *ctx, | |
37 | const struct sbrec_chassis *chassis_rec, | |
38 | const char *encap_type, const char *encap_ip) | |
39 | { | |
40 | struct sbrec_encap *encap_rec; | |
41 | int retval = TXN_TRY_AGAIN; | |
42 | struct ovsdb_idl_txn *txn; | |
43 | ||
44 | txn = ovsdb_idl_txn_create(ctx->ovnsb_idl); | |
45 | ovsdb_idl_txn_add_comment(txn, | |
46 | "ovn-controller: registering chassis '%s'", | |
47 | ctx->chassis_name); | |
48 | ||
49 | if (!chassis_rec) { | |
50 | chassis_rec = sbrec_chassis_insert(txn); | |
51 | sbrec_chassis_set_name(chassis_rec, ctx->chassis_name); | |
52 | } | |
53 | ||
54 | encap_rec = sbrec_encap_insert(txn); | |
55 | ||
56 | sbrec_encap_set_type(encap_rec, encap_type); | |
57 | sbrec_encap_set_ip(encap_rec, encap_ip); | |
58 | ||
59 | sbrec_chassis_set_encaps(chassis_rec, &encap_rec, 1); | |
60 | ||
61 | retval = ovsdb_idl_txn_commit_block(txn); | |
62 | if (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) { | |
63 | VLOG_INFO("Problem registering chassis: %s", | |
64 | ovsdb_idl_txn_status_to_string(retval)); | |
65 | poll_immediate_wake(); | |
66 | } | |
67 | ovsdb_idl_txn_destroy(txn); | |
68 | } | |
69 | ||
70 | void | |
71 | chassis_run(struct controller_ctx *ctx) | |
72 | { | |
73 | const struct sbrec_chassis *chassis_rec; | |
74 | const struct ovsrec_open_vswitch *cfg; | |
75 | const char *encap_type, *encap_ip; | |
76 | static bool inited = false; | |
77 | ||
78 | SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->ovnsb_idl) { | |
79 | if (!strcmp(chassis_rec->name, ctx->chassis_name)) { | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
84 | /* xxx Need to support more than one encap. Also need to support | |
85 | * xxx encap options. */ | |
86 | cfg = ovsrec_open_vswitch_first(ctx->ovs_idl); | |
87 | if (!cfg) { | |
88 | VLOG_INFO("No Open_vSwitch row defined."); | |
89 | return; | |
90 | } | |
91 | ||
92 | encap_type = smap_get(&cfg->external_ids, "ovn-encap-type"); | |
93 | encap_ip = smap_get(&cfg->external_ids, "ovn-encap-ip"); | |
94 | if (!encap_type || !encap_ip) { | |
95 | VLOG_INFO("Need to specify an encap type and ip"); | |
96 | return; | |
97 | } | |
98 | ||
99 | if (chassis_rec) { | |
100 | int i; | |
101 | ||
102 | for (i = 0; i < chassis_rec->n_encaps; i++) { | |
103 | if (!strcmp(chassis_rec->encaps[i]->type, encap_type) | |
104 | && !strcmp(chassis_rec->encaps[i]->ip, encap_ip)) { | |
105 | /* Nothing changed. */ | |
106 | inited = true; | |
107 | return; | |
108 | } else if (!inited) { | |
109 | VLOG_WARN("Chassis config changing on startup, make sure " | |
110 | "multiple chassis are not configured : %s/%s->%s/%s", | |
111 | chassis_rec->encaps[i]->type, | |
112 | chassis_rec->encaps[i]->ip, | |
113 | encap_type, encap_ip); | |
114 | } | |
115 | ||
116 | } | |
117 | } | |
118 | ||
119 | register_chassis(ctx, chassis_rec, encap_type, encap_ip); | |
120 | inited = true; | |
121 | } | |
122 | ||
123 | void | |
124 | chassis_destroy(struct controller_ctx *ctx) | |
125 | { | |
126 | int retval = TXN_TRY_AGAIN; | |
127 | ||
128 | ovs_assert(ctx->ovnsb_idl); | |
129 | ||
130 | while (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) { | |
131 | const struct sbrec_chassis *chassis_rec; | |
132 | struct ovsdb_idl_txn *txn; | |
133 | ||
134 | SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->ovnsb_idl) { | |
135 | if (!strcmp(chassis_rec->name, ctx->chassis_name)) { | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | if (!chassis_rec) { | |
141 | return; | |
142 | } | |
143 | ||
144 | txn = ovsdb_idl_txn_create(ctx->ovnsb_idl); | |
145 | ovsdb_idl_txn_add_comment(txn, | |
146 | "ovn-controller: unregistering chassis '%s'", | |
147 | ctx->chassis_name); | |
148 | sbrec_chassis_delete(chassis_rec); | |
149 | ||
150 | retval = ovsdb_idl_txn_commit_block(txn); | |
151 | if (retval == TXN_ERROR) { | |
152 | VLOG_INFO("Problem unregistering chassis: %s", | |
153 | ovsdb_idl_txn_status_to_string(retval)); | |
154 | } | |
155 | ovsdb_idl_txn_destroy(txn); | |
156 | } | |
157 | } |