2 * Copyright (C) 2015 - 2016 Red Hat, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Gris Ge <fge@redhat.com>
18 * Todd Gill <tgill@redhat.com>
28 #include "libdmmp/libdmmp.h"
29 #include "libdmmp_private.h"
31 #define _DMMP_SHOW_PGS_CMD "show groups raw format %w|%g|%p|%t|%s"
32 #define _DMMP_SHOW_PG_INDEX_WWID 0
33 #define _DMMP_SHOW_PG_INDEX_PG_ID 1
34 #define _DMMP_SHOW_PG_INDEX_PRI 2
35 #define _DMMP_SHOW_PG_INDEX_STATUS 3
36 #define _DMMP_SHOW_PG_INDEX_SELECTOR 4
38 struct dmmp_path_group {
40 /* ^ pgindex of struct path, will be used for path group switch */
44 uint32_t dmmp_p_count;
45 struct dmmp_path **dmmp_ps;
48 static const struct _num_str_conv _DMMP_PATH_GROUP_STATUS_CONV[] = {
49 {DMMP_PATH_GROUP_STATUS_UNKNOWN, "undef"},
50 {DMMP_PATH_GROUP_STATUS_ACTIVE, "active"},
51 {DMMP_PATH_GROUP_STATUS_DISABLED, "disabled"},
52 {DMMP_PATH_GROUP_STATUS_ENABLED, "enabled"},
55 _dmmp_str_func_gen(dmmp_path_group_status_str, uint32_t, pg_status,
56 _DMMP_PATH_GROUP_STATUS_CONV);
57 _dmmp_str_conv_func_gen(_dmmp_path_group_status_str_conv, ctx, pg_status_str,
58 uint32_t, DMMP_PATH_GROUP_STATUS_UNKNOWN,
59 _DMMP_PATH_GROUP_STATUS_CONV);
61 _dmmp_getter_func_gen(dmmp_path_group_id_get, struct dmmp_path_group, dmmp_pg,
63 _dmmp_getter_func_gen(dmmp_path_group_status_get, struct dmmp_path_group,
64 dmmp_pg, status, uint32_t);
65 _dmmp_getter_func_gen(dmmp_path_group_priority_get, struct dmmp_path_group,
66 dmmp_pg, priority, uint32_t);
67 _dmmp_getter_func_gen(dmmp_path_group_selector_get, struct dmmp_path_group,
68 dmmp_pg, selector, const char *);
69 _dmmp_array_free_func_gen(_dmmp_path_group_array_free, struct dmmp_path_group,
70 _dmmp_path_group_free);
73 struct dmmp_path_group *_dmmp_path_group_new(void)
75 struct dmmp_path_group *dmmp_pg = NULL;
77 dmmp_pg = (struct dmmp_path_group *)
78 malloc(sizeof(struct dmmp_path_group));
80 if (dmmp_pg != NULL) {
81 dmmp_pg->id = _DMMP_PATH_GROUP_ID_UNKNOWN;
82 dmmp_pg->status = DMMP_PATH_GROUP_STATUS_UNKNOWN;
83 dmmp_pg->priority = 0;
84 dmmp_pg->selector = NULL;
85 dmmp_pg->dmmp_p_count = 0;
86 dmmp_pg->dmmp_ps = NULL;
90 int _dmmp_path_group_update(struct dmmp_context *ctx,
91 struct dmmp_path_group *dmmp_pg,
92 json_object *j_obj_pg)
96 int priority_int = -1 ;
97 const char *status_str = NULL;
98 const char *selector = NULL;
99 struct array_list *ar_ps = NULL;
102 struct dmmp_path *dmmp_p = NULL;
105 assert(dmmp_pg != NULL);
106 assert(j_obj_pg != NULL);
108 _json_obj_get_value(ctx, j_obj_pg, status_str, "dm_st",
109 json_type_string, json_object_get_string, rc, out);
111 _json_obj_get_value(ctx, j_obj_pg, selector, "selector",
112 json_type_string, json_object_get_string, rc, out);
114 _json_obj_get_value(ctx, j_obj_pg, priority_int, "pri",
115 json_type_int, json_object_get_int, rc, out);
117 _json_obj_get_value(ctx, j_obj_pg, id, "group",
118 json_type_int, json_object_get_int, rc, out);
120 dmmp_pg->priority = (priority_int <= 0) ? 0 : priority_int & UINT32_MAX;
122 _dmmp_null_or_empty_str_check(ctx, status_str, rc, out);
123 _dmmp_null_or_empty_str_check(ctx, selector, rc, out);
125 dmmp_pg->selector = strdup(selector);
126 _dmmp_alloc_null_check(ctx, dmmp_pg->selector, rc, out);
130 if (dmmp_pg->id == _DMMP_PATH_GROUP_ID_UNKNOWN) {
132 _error(ctx, "BUG: Got unknown(%d) path group ID",
133 _DMMP_PATH_GROUP_ID_UNKNOWN);
137 dmmp_pg->status = _dmmp_path_group_status_str_conv(ctx, status_str);
139 _json_obj_get_value(ctx, j_obj_pg, ar_ps, "paths",
140 json_type_array, json_object_get_array, rc, out);
142 ar_ps_len = array_list_length(ar_ps);
145 _error(ctx, "BUG: Got negative length for ar_ps");
148 else if (ar_ps_len == 0)
151 dmmp_pg->dmmp_p_count = ar_ps_len & UINT32_MAX;
153 dmmp_pg->dmmp_ps = (struct dmmp_path **)
154 malloc(sizeof(struct dmmp_path *) * dmmp_pg->dmmp_p_count);
155 _dmmp_alloc_null_check(ctx, dmmp_pg->dmmp_ps, rc, out);
156 for (; i < dmmp_pg->dmmp_p_count; ++i)
157 dmmp_pg->dmmp_ps[i] = NULL;
159 for (i = 0; i < dmmp_pg->dmmp_p_count; ++i) {
160 dmmp_p = _dmmp_path_new();
161 _dmmp_alloc_null_check(ctx, dmmp_p, rc, out);
162 dmmp_pg->dmmp_ps[i] = dmmp_p;
163 _good(_dmmp_path_update(ctx, dmmp_p,
164 array_list_get_idx(ar_ps, i)),
168 _debug(ctx, "Got path group id: %" PRIu32 "", dmmp_pg->id);
169 _debug(ctx, "Got path group priority: %" PRIu32 "", dmmp_pg->priority);
170 _debug(ctx, "Got path group status: %s(%" PRIu32 ")",
171 dmmp_path_group_status_str(dmmp_pg->status), dmmp_pg->status);
172 _debug(ctx, "Got path group selector: '%s'", dmmp_pg->selector);
176 _dmmp_path_group_free(dmmp_pg);
180 void _dmmp_path_group_free(struct dmmp_path_group *dmmp_pg)
187 free((char *) dmmp_pg->selector);
189 if (dmmp_pg->dmmp_ps != NULL) {
190 for (i = 0; i < dmmp_pg->dmmp_p_count; ++i) {
191 _dmmp_path_free(dmmp_pg->dmmp_ps[i]);
193 free(dmmp_pg->dmmp_ps);
198 void dmmp_path_array_get(struct dmmp_path_group *mp_pg,
199 struct dmmp_path ***mp_paths,
200 uint32_t *dmmp_p_count)
202 assert(mp_pg != NULL);
203 assert(mp_paths != NULL);
204 assert(dmmp_p_count != NULL);
206 *mp_paths = mp_pg->dmmp_ps;
207 *dmmp_p_count = mp_pg->dmmp_p_count;