1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  *
21  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 /*
26  * IPMP administrative interfaces (see PSARC/2007/272).
27  */
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 
36 #include "ipmp_impl.h"
37 #include "ipmp_mpathd.h"
38 #include "ipmp_admin.h"
39 
40 static int
41 ipmp_command(ipmp_handle_t handle, const void *req, uint_t reqsize)
42 {
43 	ipmp_state_t	*statep = (ipmp_state_t *)handle;
44 	mi_result_t	result;
45 	struct timeval	end;
46 	int		save_errno;
47 	int		retval;
48 
49 	if (gettimeofday(&end, NULL) == -1)
50 		return (IPMP_FAILURE);
51 	end.tv_sec += IPMP_REQTIMEOUT;
52 
53 	assert(statep->st_fd == -1);
54 	retval = ipmp_connect(&statep->st_fd);
55 	if (retval != IPMP_SUCCESS)
56 		return (retval);
57 
58 	retval = ipmp_write(statep->st_fd, req, reqsize);
59 	if (retval != IPMP_SUCCESS)
60 		goto out;
61 
62 	retval = ipmp_read(statep->st_fd, &result, sizeof (result), &end);
63 	if (retval != IPMP_SUCCESS)
64 		goto out;
65 
66 	errno = result.me_sys_error;
67 	retval = result.me_mpathd_error;
68 out:
69 	save_errno = errno;
70 	(void) close(statep->st_fd);
71 	statep->st_fd = -1;
72 	errno = save_errno;
73 	return (retval);
74 }
75 
76 int
77 ipmp_offline(ipmp_handle_t handle, const char *ifname, uint_t minred)
78 {
79 	mi_offline_t mio;
80 
81 	mio.mio_command = MI_OFFLINE;
82 	mio.mio_min_redundancy = minred;
83 	(void) strlcpy(mio.mio_ifname, ifname, LIFNAMSIZ);
84 	return (ipmp_command(handle, &mio, sizeof (mio)));
85 }
86 
87 int
88 ipmp_undo_offline(ipmp_handle_t handle, const char *ifname)
89 {
90 	mi_undo_offline_t miu;
91 
92 	miu.miu_command = MI_UNDO_OFFLINE;
93 	(void) strlcpy(miu.miu_ifname, ifname, LIFNAMSIZ);
94 	return (ipmp_command(handle, &miu, sizeof (miu)));
95 }
96 
97 int
98 ipmp_ping_daemon(ipmp_handle_t handle)
99 {
100 	mi_ping_t mip;
101 
102 	mip.mip_command = MI_PING;
103 	return (ipmp_command(handle, &mip, sizeof (mip)));
104 }
105