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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
33 #include <libilb_impl.h>
37 * Create a health check, returning a health check handle upon success.
38 * Health check created will be recorded in persistent datastore.
41 ilb_create_hc(ilb_handle_t h
, const ilb_hc_info_t
*hc
)
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
)
61 if (ic
->ic_cmd
!= ILBD_CMD_OK
)
62 rc
= *(ilb_status_t
*)&ic
->ic_data
;
70 * Given a health check handle, destroy the corresponding health check.
71 * Persistent datastore will be updated as well.
74 ilb_destroy_hc(ilb_handle_t h
, const char *hcname
)
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
)
92 if (ic
->ic_cmd
!= ILBD_CMD_OK
)
93 rc
= *(ilb_status_t
*)&ic
->ic_data
;
101 * Given a health check name, get hc info associated with this handle
104 ilb_get_hc_info(ilb_handle_t h
, const char *name
, ilb_hc_info_t
*hcp
)
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
) {
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
)
127 if (rbuf
->ic_cmd
!= ILBD_CMD_OK
) {
128 rc
= *(ilb_status_t
*)&rbuf
->ic_data
;
131 (void) memcpy(hcp
, &rbuf
->ic_data
, sizeof (*hcp
));
140 * Walk through all health checks, will need if we implement list-hc
143 ilb_walk_hc(ilb_handle_t h
, hc_walkerfunc_t func
, void *arg
)
146 ilb_hc_info_t hc_info
;
147 ilbd_namelist_t
*hc_names
;
148 ilb_comm_t ic
, *rbuf
;
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
)
160 if (rbuf
->ic_cmd
!= ILBD_CMD_OK
) {
161 rc
= *(ilb_status_t
*)&rbuf
->ic_data
;
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
) {
176 } else if (rc
!= ILB_STATUS_OK
) {
179 rc
= func(h
, &hc_info
, arg
);
188 ilb_get_hc_srvs(ilb_handle_t h
, const char *rulename
, ilb_comm_t
**rbuf
,
192 ilb_comm_t
*ic
, *tmp_rbuf
;
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
) {
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
)
210 if (tmp_rbuf
->ic_cmd
== ILBD_CMD_OK
) {
214 rc
= *(ilb_status_t
*)&tmp_rbuf
->ic_data
;
223 ilb_walk_hc_srvs(ilb_handle_t h
, hc_srvwalkerfunc_t fn
, const char *rulename
,
227 ilb_hc_rule_srv_t
*srvs
;
232 if (rulename
!= NULL
) {
233 rc
= ilb_get_hc_srvs(h
, rulename
, &rbuf
, &rbufsz
);
234 if (rc
!= ILB_STATUS_OK
)
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
)
244 ilbd_namelist_t
*names
;
245 ilb_comm_t
*srv_rbuf
;
248 rc
= i_ilb_retrieve_rule_names(h
, &rbuf
, &rbufsz
);
249 if (rc
!= ILB_STATUS_OK
)
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
) {
261 } else if (rc
!= ILB_STATUS_OK
) {
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
)