sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libilb / common / ilb_hc.c
blobdc813320f23a94ced36651cdef4bcad337fa6825
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <stddef.h>
33 #include <libilb_impl.h>
34 #include <libilb.h>
37 * Create a health check, returning a health check handle upon success.
38 * Health check created will be recorded in persistent datastore.
40 ilb_status_t
41 ilb_create_hc(ilb_handle_t h, const ilb_hc_info_t *hc)
43 ilb_status_t rc;
44 ilb_comm_t *ic;
45 size_t ic_sz;
47 if (h == ILB_INVALID_HANDLE || hc == NULL || *hc->hci_name == '\0' ||
48 hc->hci_timeout < 0 || hc->hci_count < 0 ||
49 hc->hci_interval <= hc->hci_timeout * hc->hci_count)
50 return (ILB_STATUS_EINVAL);
52 if ((ic = i_ilb_alloc_req(ILBD_CREATE_HC, &ic_sz)) == NULL)
53 return (ILB_STATUS_ENOMEM);
55 (void) memcpy(&ic->ic_data, hc, sizeof (ilb_hc_info_t));
57 rc = i_ilb_do_comm(h, ic, ic_sz, ic, &ic_sz);
58 if (rc != ILB_STATUS_OK)
59 goto out;
61 if (ic->ic_cmd != ILBD_CMD_OK)
62 rc = *(ilb_status_t *)&ic->ic_data;
64 out:
65 free(ic);
66 return (rc);
70 * Given a health check handle, destroy the corresponding health check.
71 * Persistent datastore will be updated as well.
73 ilb_status_t
74 ilb_destroy_hc(ilb_handle_t h, const char *hcname)
76 ilb_status_t rc;
77 ilb_comm_t *ic;
78 size_t ic_sz;
80 if (h == ILB_INVALID_HANDLE || hcname == NULL || *hcname == '\0')
81 return (ILB_STATUS_EINVAL);
83 if ((ic = i_ilb_alloc_req(ILBD_DESTROY_HC, &ic_sz)) == NULL)
84 return (ILB_STATUS_ENOMEM);
86 (void) strlcpy((char *)&ic->ic_data, hcname, sizeof (ilbd_name_t));
88 rc = i_ilb_do_comm(h, ic, ic_sz, ic, &ic_sz);
89 if (rc != ILB_STATUS_OK)
90 goto out;
92 if (ic->ic_cmd != ILBD_CMD_OK)
93 rc = *(ilb_status_t *)&ic->ic_data;
95 out:
96 free(ic);
97 return (rc);
101 * Given a health check name, get hc info associated with this handle
103 ilb_status_t
104 ilb_get_hc_info(ilb_handle_t h, const char *name, ilb_hc_info_t *hcp)
106 ilb_status_t rc;
107 ilb_comm_t *ic, *rbuf;
108 size_t ic_sz, rbufsz;
110 if (h == ILB_INVALID_HANDLE || name == NULL || hcp == NULL)
111 return (ILB_STATUS_EINVAL);
113 if ((ic = i_ilb_alloc_req(ILBD_GET_HC_INFO, &ic_sz)) == NULL)
114 return (ILB_STATUS_ENOMEM);
115 rbufsz = sizeof (ilb_comm_t) + sizeof (ilb_hc_info_t);
116 if ((rbuf = malloc(rbufsz)) == NULL) {
117 free(ic);
118 return (ILB_STATUS_ENOMEM);
121 (void) strlcpy((char *)&ic->ic_data, name, sizeof (ilbd_name_t));
123 rc = i_ilb_do_comm(h, ic, ic_sz, rbuf, &rbufsz);
124 if (rc != ILB_STATUS_OK)
125 goto out;
127 if (rbuf->ic_cmd != ILBD_CMD_OK) {
128 rc = *(ilb_status_t *)&rbuf->ic_data;
129 goto out;
131 (void) memcpy(hcp, &rbuf->ic_data, sizeof (*hcp));
133 out:
134 free(ic);
135 free(rbuf);
136 return (rc);
140 * Walk through all health checks, will need if we implement list-hc
142 ilb_status_t
143 ilb_walk_hc(ilb_handle_t h, hc_walkerfunc_t func, void *arg)
145 ilb_status_t rc;
146 ilb_hc_info_t hc_info;
147 ilbd_namelist_t *hc_names;
148 ilb_comm_t ic, *rbuf;
149 size_t rbufsz;
150 int i;
152 rbufsz = ILBD_MSG_SIZE;
153 if ((rbuf = malloc(rbufsz)) == NULL)
154 return (ILB_STATUS_ENOMEM);
155 ic.ic_cmd = ILBD_RETRIEVE_HC_NAMES;
157 rc = i_ilb_do_comm(h, &ic, sizeof (ic), rbuf, &rbufsz);
158 if (rc != ILB_STATUS_OK)
159 goto out;
160 if (rbuf->ic_cmd != ILBD_CMD_OK) {
161 rc = *(ilb_status_t *)&rbuf->ic_data;
162 goto out;
165 hc_names = (ilbd_namelist_t *)&rbuf->ic_data;
166 for (i = 0; i < hc_names->ilbl_count; i++) {
167 rc = ilb_get_hc_info(h, hc_names->ilbl_name[i], &hc_info);
169 * Since getting the list of hc names and getting the info
170 * of each of them are not atomic, some hc objects may have
171 * been deleted. If this is the case, just skip them.
173 if (rc == ILB_STATUS_ENOENT) {
174 rc = ILB_STATUS_OK;
175 continue;
176 } else if (rc != ILB_STATUS_OK) {
177 break;
179 rc = func(h, &hc_info, arg);
182 out:
183 free(rbuf);
184 return (rc);
187 static ilb_status_t
188 ilb_get_hc_srvs(ilb_handle_t h, const char *rulename, ilb_comm_t **rbuf,
189 size_t *rbufsz)
191 ilb_status_t rc;
192 ilb_comm_t *ic, *tmp_rbuf;
193 size_t ic_sz;
195 if ((ic = i_ilb_alloc_req(ILBD_GET_HC_SRVS, &ic_sz)) == NULL)
196 return (ILB_STATUS_ENOMEM);
197 *rbufsz = ILBD_MSG_SIZE;
198 if ((tmp_rbuf = malloc(*rbufsz)) == NULL) {
199 free(ic);
200 return (ILB_STATUS_ENOMEM);
203 (void) strlcpy((char *)&ic->ic_data, rulename,
204 sizeof (ilbd_name_t));
206 rc = i_ilb_do_comm(h, ic, ic_sz, tmp_rbuf, rbufsz);
207 if (rc != ILB_STATUS_OK)
208 goto out;
210 if (tmp_rbuf->ic_cmd == ILBD_CMD_OK) {
211 *rbuf = tmp_rbuf;
212 return (rc);
214 rc = *(ilb_status_t *)&tmp_rbuf->ic_data;
215 out:
216 free(ic);
217 free(tmp_rbuf);
218 *rbuf = NULL;
219 return (rc);
222 ilb_status_t
223 ilb_walk_hc_srvs(ilb_handle_t h, hc_srvwalkerfunc_t fn, const char *rulename,
224 void *arg)
226 ilb_status_t rc;
227 ilb_hc_rule_srv_t *srvs;
228 int i, j;
229 ilb_comm_t *rbuf;
230 size_t rbufsz;
232 if (rulename != NULL) {
233 rc = ilb_get_hc_srvs(h, rulename, &rbuf, &rbufsz);
234 if (rc != ILB_STATUS_OK)
235 return (rc);
236 srvs = (ilb_hc_rule_srv_t *)&rbuf->ic_data;
237 for (i = 0; i < srvs->rs_num_srvs; i++) {
238 rc = fn(h, &srvs->rs_srvs[i], arg);
239 if (rc != ILB_STATUS_OK)
240 break;
242 free(rbuf);
243 } else {
244 ilbd_namelist_t *names;
245 ilb_comm_t *srv_rbuf;
246 size_t srv_rbufsz;
248 rc = i_ilb_retrieve_rule_names(h, &rbuf, &rbufsz);
249 if (rc != ILB_STATUS_OK)
250 return (rc);
251 names = (ilbd_namelist_t *)&rbuf->ic_data;
253 for (i = 0; i < names->ilbl_count; i++) {
254 rc = ilb_get_hc_srvs(h, names->ilbl_name[i],
255 &srv_rbuf, &srv_rbufsz);
257 /* Not all rules have HC, so reset the error to OK. */
258 if (rc == ILB_STATUS_RULE_NO_HC) {
259 rc = ILB_STATUS_OK;
260 continue;
261 } else if (rc != ILB_STATUS_OK) {
262 break;
265 srvs = (ilb_hc_rule_srv_t *)&srv_rbuf->ic_data;
266 for (j = 0; j < srvs->rs_num_srvs; j++) {
267 rc = fn(h, &srvs->rs_srvs[j], arg);
268 if (rc != ILB_STATUS_OK)
269 break;
271 free(srv_rbuf);
273 free(rbuf);
275 return (rc);