2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/callout.h>
43 #include <sys/ioctl.h>
44 #include <sys/linker.h>
45 #include <sys/module.h>
46 #include <sys/queue.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_io.h>
63 #include <cam/ctl/ctl_backend.h>
64 #include <cam/ctl/ctl_ioctl.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
70 #ifdef ICL_KERNEL_PROXY
74 extern bool proxy_mode;
76 static int ctl_fd = 0;
81 int retval, saved_errno;
83 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84 if (ctl_fd < 0 && errno == ENOENT) {
86 retval = kldload("ctl");
88 ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
93 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
97 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
98 log_warn("couldn't load cfiscsi");
105 * Name/value pair used for per-LUN attributes.
110 STAILQ_ENTRY(cctl_lun_nv) links;
114 * Backend LUN information.
120 uint64_t size_blocks;
125 STAILQ_HEAD(,cctl_lun_nv) attr_list;
126 STAILQ_ENTRY(cctl_lun) links;
136 char *cfiscsi_target;
137 uint16_t cfiscsi_portal_group_tag;
138 char *ctld_portal_group_name;
139 STAILQ_HEAD(,cctl_lun_nv) attr_list;
140 STAILQ_ENTRY(cctl_port) links;
143 struct cctl_devlist_data {
145 STAILQ_HEAD(,cctl_lun) lun_list;
146 struct cctl_lun *cur_lun;
148 STAILQ_HEAD(,cctl_port) port_list;
149 struct cctl_port *cur_port;
151 struct sbuf *cur_sb[32];
155 cctl_start_element(void *user_data, const char *name, const char **attr)
158 struct cctl_devlist_data *devlist;
159 struct cctl_lun *cur_lun;
161 devlist = (struct cctl_devlist_data *)user_data;
162 cur_lun = devlist->cur_lun;
164 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
165 sizeof(devlist->cur_sb[0])))
166 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
167 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
169 devlist->cur_sb[devlist->level] = sbuf_new_auto();
170 if (devlist->cur_sb[devlist->level] == NULL)
171 log_err(1, "%s: unable to allocate sbuf", __func__);
173 if (strcmp(name, "lun") == 0) {
175 log_errx(1, "%s: improper lun element nesting",
178 cur_lun = calloc(1, sizeof(*cur_lun));
180 log_err(1, "%s: cannot allocate %zd bytes", __func__,
184 devlist->cur_lun = cur_lun;
186 STAILQ_INIT(&cur_lun->attr_list);
187 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
189 for (i = 0; attr[i] != NULL; i += 2) {
190 if (strcmp(attr[i], "id") == 0) {
191 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
193 log_errx(1, "%s: invalid LUN attribute %s = %s",
194 __func__, attr[i], attr[i+1]);
201 cctl_end_element(void *user_data, const char *name)
203 struct cctl_devlist_data *devlist;
204 struct cctl_lun *cur_lun;
207 devlist = (struct cctl_devlist_data *)user_data;
208 cur_lun = devlist->cur_lun;
210 if ((cur_lun == NULL)
211 && (strcmp(name, "ctllunlist") != 0))
212 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
214 if (devlist->cur_sb[devlist->level] == NULL)
215 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
216 devlist->level, name);
218 sbuf_finish(devlist->cur_sb[devlist->level]);
219 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
221 if (strlen(str) == 0) {
226 sbuf_delete(devlist->cur_sb[devlist->level]);
227 devlist->cur_sb[devlist->level] = NULL;
230 if (strcmp(name, "backend_type") == 0) {
231 cur_lun->backend_type = str;
233 } else if (strcmp(name, "lun_type") == 0) {
234 cur_lun->device_type = strtoull(str, NULL, 0);
235 } else if (strcmp(name, "size") == 0) {
236 cur_lun->size_blocks = strtoull(str, NULL, 0);
237 } else if (strcmp(name, "blocksize") == 0) {
238 cur_lun->blocksize = strtoul(str, NULL, 0);
239 } else if (strcmp(name, "serial_number") == 0) {
240 cur_lun->serial_number = str;
242 } else if (strcmp(name, "device_id") == 0) {
243 cur_lun->device_id = str;
245 } else if (strcmp(name, "ctld_name") == 0) {
246 cur_lun->ctld_name = str;
248 } else if (strcmp(name, "lun") == 0) {
249 devlist->cur_lun = NULL;
250 } else if (strcmp(name, "ctllunlist") == 0) {
253 struct cctl_lun_nv *nv;
255 nv = calloc(1, sizeof(*nv));
257 log_err(1, "%s: can't allocate %zd bytes for nv pair",
258 __func__, sizeof(*nv));
260 nv->name = checked_strdup(name);
264 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
271 cctl_start_pelement(void *user_data, const char *name, const char **attr)
274 struct cctl_devlist_data *devlist;
275 struct cctl_port *cur_port;
277 devlist = (struct cctl_devlist_data *)user_data;
278 cur_port = devlist->cur_port;
280 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
281 sizeof(devlist->cur_sb[0])))
282 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
283 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
285 devlist->cur_sb[devlist->level] = sbuf_new_auto();
286 if (devlist->cur_sb[devlist->level] == NULL)
287 log_err(1, "%s: unable to allocate sbuf", __func__);
289 if (strcmp(name, "targ_port") == 0) {
290 if (cur_port != NULL)
291 log_errx(1, "%s: improper port element nesting (%s)",
294 cur_port = calloc(1, sizeof(*cur_port));
295 if (cur_port == NULL)
296 log_err(1, "%s: cannot allocate %zd bytes", __func__,
299 devlist->num_ports++;
300 devlist->cur_port = cur_port;
302 STAILQ_INIT(&cur_port->attr_list);
303 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
305 for (i = 0; attr[i] != NULL; i += 2) {
306 if (strcmp(attr[i], "id") == 0) {
307 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
309 log_errx(1, "%s: invalid LUN attribute %s = %s",
310 __func__, attr[i], attr[i+1]);
317 cctl_end_pelement(void *user_data, const char *name)
319 struct cctl_devlist_data *devlist;
320 struct cctl_port *cur_port;
323 devlist = (struct cctl_devlist_data *)user_data;
324 cur_port = devlist->cur_port;
326 if ((cur_port == NULL)
327 && (strcmp(name, "ctlportlist") != 0))
328 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
330 if (devlist->cur_sb[devlist->level] == NULL)
331 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
332 devlist->level, name);
334 sbuf_finish(devlist->cur_sb[devlist->level]);
335 str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
337 if (strlen(str) == 0) {
342 sbuf_delete(devlist->cur_sb[devlist->level]);
343 devlist->cur_sb[devlist->level] = NULL;
346 if (strcmp(name, "frontend_type") == 0) {
347 cur_port->port_frontend = str;
349 } else if (strcmp(name, "port_name") == 0) {
350 cur_port->port_name = str;
352 } else if (strcmp(name, "physical_port") == 0) {
353 cur_port->pp = strtoul(str, NULL, 0);
354 } else if (strcmp(name, "virtual_port") == 0) {
355 cur_port->vp = strtoul(str, NULL, 0);
356 } else if (strcmp(name, "cfiscsi_target") == 0) {
357 cur_port->cfiscsi_target = str;
359 } else if (strcmp(name, "cfiscsi_state") == 0) {
360 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
361 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
362 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
363 } else if (strcmp(name, "ctld_portal_group_name") == 0) {
364 cur_port->ctld_portal_group_name = str;
366 } else if (strcmp(name, "targ_port") == 0) {
367 devlist->cur_port = NULL;
368 } else if (strcmp(name, "ctlportlist") == 0) {
371 struct cctl_lun_nv *nv;
373 nv = calloc(1, sizeof(*nv));
375 log_err(1, "%s: can't allocate %zd bytes for nv pair",
376 __func__, sizeof(*nv));
378 nv->name = checked_strdup(name);
382 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
389 cctl_char_handler(void *user_data, const XML_Char *str, int len)
391 struct cctl_devlist_data *devlist;
393 devlist = (struct cctl_devlist_data *)user_data;
395 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
399 conf_new_from_kernel(void)
401 struct conf *conf = NULL;
403 struct portal_group *pg;
408 struct ctl_lun_list list;
409 struct cctl_devlist_data devlist;
410 struct cctl_lun *lun;
411 struct cctl_port *port;
416 bzero(&devlist, sizeof(devlist));
417 STAILQ_INIT(&devlist.lun_list);
418 STAILQ_INIT(&devlist.port_list);
420 log_debugx("obtaining previously configured CTL luns from the kernel");
425 str = realloc(str, len);
427 log_err(1, "realloc");
429 bzero(&list, sizeof(list));
430 list.alloc_len = len;
431 list.status = CTL_LUN_LIST_NONE;
434 if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
435 log_warn("error issuing CTL_LUN_LIST ioctl");
440 if (list.status == CTL_LUN_LIST_ERROR) {
441 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
447 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
452 parser = XML_ParserCreate(NULL);
453 if (parser == NULL) {
454 log_warnx("unable to create XML parser");
459 XML_SetUserData(parser, &devlist);
460 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
461 XML_SetCharacterDataHandler(parser, cctl_char_handler);
463 retval = XML_Parse(parser, str, strlen(str), 1);
464 XML_ParserFree(parser);
467 log_warnx("XML_Parse failed");
474 str = realloc(str, len);
476 log_err(1, "realloc");
478 bzero(&list, sizeof(list));
479 list.alloc_len = len;
480 list.status = CTL_LUN_LIST_NONE;
483 if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
484 log_warn("error issuing CTL_PORT_LIST ioctl");
489 if (list.status == CTL_LUN_LIST_ERROR) {
490 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
496 if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
501 parser = XML_ParserCreate(NULL);
502 if (parser == NULL) {
503 log_warnx("unable to create XML parser");
508 XML_SetUserData(parser, &devlist);
509 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
510 XML_SetCharacterDataHandler(parser, cctl_char_handler);
512 retval = XML_Parse(parser, str, strlen(str), 1);
513 XML_ParserFree(parser);
516 log_warnx("XML_Parse failed");
523 STAILQ_FOREACH(port, &devlist.port_list, links) {
524 if (strcmp(port->port_frontend, "ha") == 0)
527 if (port->pp == 0 && port->vp == 0) {
528 name = checked_strdup(port->port_name);
529 } else if (port->vp == 0) {
530 retval = asprintf(&name, "%s/%d",
531 port->port_name, port->pp);
533 log_err(1, "asprintf");
535 retval = asprintf(&name, "%s/%d/%d",
536 port->port_name, port->pp, port->vp);
538 log_err(1, "asprintf");
541 if (port->cfiscsi_target == NULL) {
542 log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
543 port->port_id, name);
544 pp = pport_find(conf, name);
547 log_debugx("found new kernel port %u \"%s\"",
548 port->port_id, name);
550 pp = pport_new(conf, name, port->port_id);
552 log_warnx("pport_new failed");
558 if (port->cfiscsi_state != 1) {
559 log_debugx("CTL port %ju is not active (%d); ignoring",
560 (uintmax_t)port->port_id, port->cfiscsi_state);
564 targ = target_find(conf, port->cfiscsi_target);
567 log_debugx("found new kernel target %s for CTL port %ld",
568 port->cfiscsi_target, port->port_id);
570 targ = target_new(conf, port->cfiscsi_target);
572 log_warnx("target_new failed");
577 if (port->ctld_portal_group_name == NULL)
579 pg = portal_group_find(conf, port->ctld_portal_group_name);
582 log_debugx("found new kernel portal group %s for CTL port %ld",
583 port->ctld_portal_group_name, port->port_id);
585 pg = portal_group_new(conf, port->ctld_portal_group_name);
587 log_warnx("portal_group_new failed");
591 pg->pg_tag = port->cfiscsi_portal_group_tag;
592 cp = port_new(conf, targ, pg);
594 log_warnx("port_new failed");
597 cp->p_ctl_port = port->port_id;
601 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
602 struct cctl_lun_nv *nv;
604 if (lun->ctld_name == NULL) {
605 log_debugx("CTL lun %ju wasn't managed by ctld; "
606 "ignoring", (uintmax_t)lun->lun_id);
610 cl = lun_find(conf, lun->ctld_name);
612 log_warnx("found CTL lun %ju \"%s\", "
613 "also backed by CTL lun %d; ignoring",
614 (uintmax_t)lun->lun_id, lun->ctld_name,
619 log_debugx("found CTL lun %ju \"%s\"",
620 (uintmax_t)lun->lun_id, lun->ctld_name);
622 cl = lun_new(conf, lun->ctld_name);
624 log_warnx("lun_new failed");
627 lun_set_backend(cl, lun->backend_type);
628 lun_set_device_type(cl, lun->device_type);
629 lun_set_blocksize(cl, lun->blocksize);
630 lun_set_device_id(cl, lun->device_id);
631 lun_set_serial(cl, lun->serial_number);
632 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
633 lun_set_ctl_lun(cl, lun->lun_id);
635 STAILQ_FOREACH(nv, &lun->attr_list, links) {
636 if (strcmp(nv->name, "file") == 0 ||
637 strcmp(nv->name, "dev") == 0) {
638 lun_set_path(cl, nv->value);
641 o = option_new(&cl->l_options, nv->name, nv->value);
643 log_warnx("unable to add CTL lun option %s "
644 "for CTL lun %ju \"%s\"",
645 nv->name, (uintmax_t) lun->lun_id,
654 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
657 arg->namelen = strlen(name) + 1;
658 arg->name = __DECONST(char *, name);
659 arg->vallen = strlen(value) + 1;
660 arg->value = __DECONST(char *, value);
661 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
665 kernel_lun_add(struct lun *lun)
668 struct ctl_lun_req req;
669 int error, i, num_options;
671 bzero(&req, sizeof(req));
673 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
674 req.reqtype = CTL_LUNREQ_CREATE;
676 req.reqdata.create.blocksize_bytes = lun->l_blocksize;
678 if (lun->l_size != 0)
679 req.reqdata.create.lun_size_bytes = lun->l_size;
681 if (lun->l_ctl_lun >= 0) {
682 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
683 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
686 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
687 req.reqdata.create.device_type = lun->l_device_type;
689 if (lun->l_serial != NULL) {
690 strncpy(req.reqdata.create.serial_num, lun->l_serial,
691 sizeof(req.reqdata.create.serial_num));
692 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
695 if (lun->l_device_id != NULL) {
696 strncpy(req.reqdata.create.device_id, lun->l_device_id,
697 sizeof(req.reqdata.create.device_id));
698 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
701 if (lun->l_path != NULL) {
702 o = option_find(&lun->l_options, "file");
704 option_set(o, lun->l_path);
706 o = option_new(&lun->l_options, "file", lun->l_path);
711 o = option_find(&lun->l_options, "ctld_name");
713 option_set(o, lun->l_name);
715 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
719 o = option_find(&lun->l_options, "scsiname");
720 if (o == NULL && lun->l_scsiname != NULL) {
721 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
726 TAILQ_FOREACH(o, &lun->l_options, o_next)
729 req.num_be_args = num_options;
730 if (num_options > 0) {
731 req.be_args = malloc(num_options * sizeof(*req.be_args));
732 if (req.be_args == NULL) {
733 log_warn("error allocating %zd bytes",
734 num_options * sizeof(*req.be_args));
739 TAILQ_FOREACH(o, &lun->l_options, o_next) {
740 str_arg(&req.be_args[i], o->o_name, o->o_value);
743 assert(i == num_options);
746 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
749 log_warn("error issuing CTL_LUN_REQ ioctl");
753 switch (req.status) {
755 log_warnx("LUN creation error: %s", req.error_str);
757 case CTL_LUN_WARNING:
758 log_warnx("LUN creation warning: %s", req.error_str);
763 log_warnx("unknown LUN creation status: %d",
768 lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
773 kernel_lun_modify(struct lun *lun)
776 struct ctl_lun_req req;
777 int error, i, num_options;
779 bzero(&req, sizeof(req));
781 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
782 req.reqtype = CTL_LUNREQ_MODIFY;
784 req.reqdata.modify.lun_id = lun->l_ctl_lun;
785 req.reqdata.modify.lun_size_bytes = lun->l_size;
788 TAILQ_FOREACH(o, &lun->l_options, o_next)
791 req.num_be_args = num_options;
792 if (num_options > 0) {
793 req.be_args = malloc(num_options * sizeof(*req.be_args));
794 if (req.be_args == NULL) {
795 log_warn("error allocating %zd bytes",
796 num_options * sizeof(*req.be_args));
801 TAILQ_FOREACH(o, &lun->l_options, o_next) {
802 str_arg(&req.be_args[i], o->o_name, o->o_value);
805 assert(i == num_options);
808 error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
811 log_warn("error issuing CTL_LUN_REQ ioctl");
815 switch (req.status) {
817 log_warnx("LUN modification error: %s", req.error_str);
819 case CTL_LUN_WARNING:
820 log_warnx("LUN modification warning: %s", req.error_str);
825 log_warnx("unknown LUN modification status: %d",
834 kernel_lun_remove(struct lun *lun)
836 struct ctl_lun_req req;
838 bzero(&req, sizeof(req));
840 strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
841 req.reqtype = CTL_LUNREQ_RM;
843 req.reqdata.rm.lun_id = lun->l_ctl_lun;
845 if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
846 log_warn("error issuing CTL_LUN_REQ ioctl");
850 switch (req.status) {
852 log_warnx("LUN removal error: %s", req.error_str);
854 case CTL_LUN_WARNING:
855 log_warnx("LUN removal warning: %s", req.error_str);
860 log_warnx("unknown LUN removal status: %d", req.status);
868 kernel_handoff(struct connection *conn)
870 struct ctl_iscsi req;
872 bzero(&req, sizeof(req));
874 req.type = CTL_ISCSI_HANDOFF;
875 strlcpy(req.data.handoff.initiator_name,
876 conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
877 strlcpy(req.data.handoff.initiator_addr,
878 conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
879 if (conn->conn_initiator_alias != NULL) {
880 strlcpy(req.data.handoff.initiator_alias,
881 conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
883 memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
884 sizeof(req.data.handoff.initiator_isid));
885 strlcpy(req.data.handoff.target_name,
886 conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
887 if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
888 strlcpy(req.data.handoff.offload,
889 conn->conn_portal->p_portal_group->pg_offload,
890 sizeof(req.data.handoff.offload));
892 #ifdef ICL_KERNEL_PROXY
894 req.data.handoff.connection_id = conn->conn_socket;
896 req.data.handoff.socket = conn->conn_socket;
898 req.data.handoff.socket = conn->conn_socket;
900 req.data.handoff.portal_group_tag =
901 conn->conn_portal->p_portal_group->pg_tag;
902 if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
903 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
904 if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
905 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
906 req.data.handoff.cmdsn = conn->conn_cmdsn;
907 req.data.handoff.statsn = conn->conn_statsn;
908 req.data.handoff.max_recv_data_segment_length =
909 conn->conn_max_recv_data_segment_length;
910 req.data.handoff.max_send_data_segment_length =
911 conn->conn_max_send_data_segment_length;
912 req.data.handoff.max_burst_length = conn->conn_max_burst_length;
913 req.data.handoff.first_burst_length = conn->conn_first_burst_length;
914 req.data.handoff.immediate_data = conn->conn_immediate_data;
916 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
917 log_err(1, "error issuing CTL_ISCSI ioctl; "
918 "dropping connection");
921 if (req.status != CTL_ISCSI_OK) {
922 log_errx(1, "error returned from CTL iSCSI handoff request: "
923 "%s; dropping connection", req.error_str);
928 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
929 int *max_burst_length, int *first_burst_length)
931 struct ctl_iscsi req;
932 struct ctl_iscsi_limits_params *cilp;
934 bzero(&req, sizeof(req));
936 req.type = CTL_ISCSI_LIMITS;
937 cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
938 if (offload != NULL) {
939 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
942 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
943 log_err(1, "error issuing CTL_ISCSI ioctl; "
944 "dropping connection");
947 if (req.status != CTL_ISCSI_OK) {
948 log_errx(1, "error returned from CTL iSCSI limits request: "
949 "%s; dropping connection", req.error_str);
952 if (cilp->max_recv_data_segment_length != 0) {
953 *max_recv_dsl = cilp->max_recv_data_segment_length;
954 *max_send_dsl = cilp->max_recv_data_segment_length;
956 if (cilp->max_send_data_segment_length != 0)
957 *max_send_dsl = cilp->max_send_data_segment_length;
958 if (cilp->max_burst_length != 0)
959 *max_burst_length = cilp->max_burst_length;
960 if (cilp->first_burst_length != 0)
961 *first_burst_length = cilp->first_burst_length;
962 if (*max_burst_length < *first_burst_length)
963 *first_burst_length = *max_burst_length;
965 if (offload != NULL) {
966 log_debugx("Kernel limits for offload \"%s\" are "
967 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
968 "MaxBurstLength=%d, FirstBurstLength=%d",
969 offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
970 *first_burst_length);
972 log_debugx("Kernel limits are "
973 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
974 "MaxBurstLength=%d, FirstBurstLength=%d",
975 *max_recv_dsl, *max_send_dsl, *max_burst_length,
976 *first_burst_length);
981 kernel_port_add(struct port *port)
984 struct ctl_port_entry entry;
986 struct ctl_lun_map lm;
987 struct target *targ = port->p_target;
988 struct portal_group *pg = port->p_portal_group;
992 /* Create iSCSI port. */
993 if (port->p_portal_group) {
994 bzero(&req, sizeof(req));
995 strlcpy(req.driver, "iscsi", sizeof(req.driver));
996 req.reqtype = CTL_REQ_CREATE;
998 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1000 req.args = malloc(req.num_args * sizeof(*req.args));
1001 if (req.args == NULL)
1002 log_err(1, "malloc");
1004 req.args[n].namelen = sizeof("port_id");
1005 req.args[n].name = __DECONST(char *, "port_id");
1006 req.args[n].vallen = sizeof(port->p_ctl_port);
1007 req.args[n].value = &port->p_ctl_port;
1008 req.args[n++].flags = CTL_BEARG_WR;
1009 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
1010 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1011 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
1013 str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
1014 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
1015 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1016 str_arg(&req.args[n++], o->o_name, o->o_value);
1018 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1021 log_warn("error issuing CTL_PORT_REQ ioctl");
1024 if (req.status == CTL_LUN_ERROR) {
1025 log_warnx("error returned from port creation request: %s",
1029 if (req.status != CTL_LUN_OK) {
1030 log_warnx("unknown port creation request status %d",
1034 } else if (port->p_pport) {
1035 port->p_ctl_port = port->p_pport->pp_ctl_port;
1037 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1038 strlen(targ->t_name) == 20) {
1039 bzero(&entry, sizeof(entry));
1040 entry.port_type = CTL_PORT_NONE;
1041 entry.targ_port = port->p_ctl_port;
1042 entry.flags |= CTL_PORT_WWNN_VALID;
1043 entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1044 if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1045 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1049 /* Explicitly enable mapping to block any access except allowed. */
1050 lm.port = port->p_ctl_port;
1051 lm.plun = UINT32_MAX;
1053 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1055 log_warn("CTL_LUN_MAP ioctl failed");
1057 /* Map configured LUNs */
1058 for (i = 0; i < MAX_LUNS; i++) {
1059 if (targ->t_luns[i] == NULL)
1061 lm.port = port->p_ctl_port;
1063 lm.lun = targ->t_luns[i]->l_ctl_lun;
1064 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1066 log_warn("CTL_LUN_MAP ioctl failed");
1070 bzero(&entry, sizeof(entry));
1071 entry.targ_port = port->p_ctl_port;
1072 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1074 log_warn("CTL_ENABLE_PORT ioctl failed");
1082 kernel_port_update(struct port *port, struct port *oport)
1084 struct ctl_lun_map lm;
1085 struct target *targ = port->p_target;
1086 struct target *otarg = oport->p_target;
1090 /* Map configured LUNs and unmap others */
1091 for (i = 0; i < MAX_LUNS; i++) {
1092 lm.port = port->p_ctl_port;
1094 if (targ->t_luns[i] == NULL)
1095 lm.lun = UINT32_MAX;
1097 lm.lun = targ->t_luns[i]->l_ctl_lun;
1098 if (otarg->t_luns[i] == NULL)
1101 olun = otarg->t_luns[i]->l_ctl_lun;
1104 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1106 log_warn("CTL_LUN_MAP ioctl failed");
1112 kernel_port_remove(struct port *port)
1114 struct ctl_port_entry entry;
1115 struct ctl_lun_map lm;
1118 struct target *targ = port->p_target;
1119 struct portal_group *pg = port->p_portal_group;
1123 bzero(&entry, sizeof(entry));
1124 entry.targ_port = port->p_ctl_port;
1125 error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1127 log_warn("CTL_DISABLE_PORT ioctl failed");
1131 /* Remove iSCSI port. */
1132 if (port->p_portal_group) {
1133 bzero(&req, sizeof(req));
1134 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1135 req.reqtype = CTL_REQ_REMOVE;
1137 req.args = malloc(req.num_args * sizeof(*req.args));
1138 if (req.args == NULL)
1139 log_err(1, "malloc");
1140 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1141 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1142 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1143 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1146 log_warn("error issuing CTL_PORT_REQ ioctl");
1149 if (req.status == CTL_LUN_ERROR) {
1150 log_warnx("error returned from port removal request: %s",
1154 if (req.status != CTL_LUN_OK) {
1155 log_warnx("unknown port removal request status %d",
1160 /* Disable LUN mapping. */
1161 lm.port = port->p_ctl_port;
1162 lm.plun = UINT32_MAX;
1163 lm.lun = UINT32_MAX;
1164 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1166 log_warn("CTL_LUN_MAP ioctl failed");
1171 #ifdef ICL_KERNEL_PROXY
1173 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1175 struct ctl_iscsi req;
1177 bzero(&req, sizeof(req));
1179 req.type = CTL_ISCSI_LISTEN;
1180 req.data.listen.iser = iser;
1181 req.data.listen.domain = ai->ai_family;
1182 req.data.listen.socktype = ai->ai_socktype;
1183 req.data.listen.protocol = ai->ai_protocol;
1184 req.data.listen.addr = ai->ai_addr;
1185 req.data.listen.addrlen = ai->ai_addrlen;
1186 req.data.listen.portal_id = portal_id;
1188 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1189 log_err(1, "error issuing CTL_ISCSI ioctl");
1191 if (req.status != CTL_ISCSI_OK) {
1192 log_errx(1, "error returned from CTL iSCSI listen: %s",
1198 kernel_accept(int *connection_id, int *portal_id,
1199 struct sockaddr *client_sa, socklen_t *client_salen)
1201 struct ctl_iscsi req;
1202 struct sockaddr_storage ss;
1204 bzero(&req, sizeof(req));
1206 req.type = CTL_ISCSI_ACCEPT;
1207 req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1209 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1210 log_err(1, "error issuing CTL_ISCSI ioctl");
1212 if (req.status != CTL_ISCSI_OK) {
1213 log_errx(1, "error returned from CTL iSCSI accept: %s",
1217 *connection_id = req.data.accept.connection_id;
1218 *portal_id = req.data.accept.portal_id;
1219 *client_salen = req.data.accept.initiator_addrlen;
1220 memcpy(client_sa, &ss, *client_salen);
1224 kernel_send(struct pdu *pdu)
1226 struct ctl_iscsi req;
1228 bzero(&req, sizeof(req));
1230 req.type = CTL_ISCSI_SEND;
1231 req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1232 req.data.send.bhs = pdu->pdu_bhs;
1233 req.data.send.data_segment_len = pdu->pdu_data_len;
1234 req.data.send.data_segment = pdu->pdu_data;
1236 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1237 log_err(1, "error issuing CTL_ISCSI ioctl; "
1238 "dropping connection");
1241 if (req.status != CTL_ISCSI_OK) {
1242 log_errx(1, "error returned from CTL iSCSI send: "
1243 "%s; dropping connection", req.error_str);
1248 kernel_receive(struct pdu *pdu)
1250 struct connection *conn;
1251 struct ctl_iscsi req;
1253 conn = pdu->pdu_connection;
1254 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1255 if (pdu->pdu_data == NULL)
1256 log_err(1, "malloc");
1258 bzero(&req, sizeof(req));
1260 req.type = CTL_ISCSI_RECEIVE;
1261 req.data.receive.connection_id = conn->conn_socket;
1262 req.data.receive.bhs = pdu->pdu_bhs;
1263 req.data.receive.data_segment_len =
1264 conn->conn_max_recv_data_segment_length;
1265 req.data.receive.data_segment = pdu->pdu_data;
1267 if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1268 log_err(1, "error issuing CTL_ISCSI ioctl; "
1269 "dropping connection");
1272 if (req.status != CTL_ISCSI_OK) {
1273 log_errx(1, "error returned from CTL iSCSI receive: "
1274 "%s; dropping connection", req.error_str);
1279 #endif /* ICL_KERNEL_PROXY */
1282 * XXX: I CANT INTO LATIN
1285 kernel_capsicate(void)
1288 cap_rights_t rights;
1289 const unsigned long cmds[] = { CTL_ISCSI };
1291 cap_rights_init(&rights, CAP_IOCTL);
1292 error = cap_rights_limit(ctl_fd, &rights);
1293 if (error != 0 && errno != ENOSYS)
1294 log_err(1, "cap_rights_limit");
1296 error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds));
1298 if (error != 0 && errno != ENOSYS)
1299 log_err(1, "cap_ioctls_limit");
1301 error = cap_enter();
1302 if (error != 0 && errno != ENOSYS)
1303 log_err(1, "cap_enter");
1305 if (cap_sandboxed())
1306 log_debugx("Capsicum capability mode enabled");
1308 log_warnx("Capsicum capability mode not supported");