From owner-svn-src-all@FreeBSD.ORG Wed Jun 18 17:35:41 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 7AD0FE15; Wed, 18 Jun 2014 17:35:41 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 5B9CC2CD0; Wed, 18 Jun 2014 17:35:41 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s5IHZfl1069475; Wed, 18 Jun 2014 17:35:41 GMT (envelope-from trasz@svn.freebsd.org) Received: (from trasz@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s5IHZeOg069471; Wed, 18 Jun 2014 17:35:40 GMT (envelope-from trasz@svn.freebsd.org) Message-Id: <201406181735.s5IHZeOg069471@svn.freebsd.org> From: Edward Tomasz Napierala Date: Wed, 18 Jun 2014 17:35:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r267613 - in head: sys/dev/iscsi usr.sbin/iscsid X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jun 2014 17:35:41 -0000 Author: trasz Date: Wed Jun 18 17:35:40 2014 New Revision: 267613 URL: http://svnweb.freebsd.org/changeset/base/267613 Log: Implement redirection handling in initiator. Sponsored by: The FreeBSD Foundation Modified: head/sys/dev/iscsi/iscsi.c head/sys/dev/iscsi/iscsi_ioctl.h head/usr.sbin/iscsid/iscsid.c head/usr.sbin/iscsid/login.c Modified: head/sys/dev/iscsi/iscsi.c ============================================================================== --- head/sys/dev/iscsi/iscsi.c Wed Jun 18 17:21:38 2014 (r267612) +++ head/sys/dev/iscsi/iscsi.c Wed Jun 18 17:35:40 2014 (r267613) @@ -1589,6 +1589,33 @@ iscsi_sanitize_session_conf(struct iscsi isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0'; } +static bool +iscsi_valid_session_conf(const struct iscsi_session_conf *isc) +{ + + if (isc->isc_initiator[0] == '\0') { + ISCSI_DEBUG("empty isc_initiator"); + return (false); + } + + if (isc->isc_target_addr[0] == '\0') { + ISCSI_DEBUG("empty isc_target_addr"); + return (false); + } + + if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) { + ISCSI_DEBUG("non-empty isc_target for discovery session"); + return (false); + } + + if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) { + ISCSI_DEBUG("empty isc_target for non-discovery session"); + return (false); + } + + return (true); +} + static int iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) { @@ -1597,22 +1624,12 @@ iscsi_ioctl_session_add(struct iscsi_sof int error; iscsi_sanitize_session_conf(&isa->isa_conf); + if (iscsi_valid_session_conf(&isa->isa_conf) == false) + return (EINVAL); is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK); memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf)); - if (is->is_conf.isc_initiator[0] == '\0' || - is->is_conf.isc_target_addr[0] == '\0') { - free(is, M_ISCSI); - return (EINVAL); - } - - if ((is->is_conf.isc_discovery != 0 && is->is_conf.isc_target[0] != 0) || - (is->is_conf.isc_discovery == 0 && is->is_conf.isc_target[0] == 0)) { - free(is, M_ISCSI); - return (EINVAL); - } - sx_xlock(&sc->sc_lock); /* @@ -1769,7 +1786,38 @@ iscsi_ioctl_session_list(struct iscsi_so return (0); } - + +static int +iscsi_ioctl_session_modify(struct iscsi_softc *sc, + struct iscsi_session_modify *ism) +{ + struct iscsi_session *is; + + iscsi_sanitize_session_conf(&ism->ism_conf); + if (iscsi_valid_session_conf(&ism->ism_conf) == false) + return (EINVAL); + + sx_xlock(&sc->sc_lock); + TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { + ISCSI_SESSION_LOCK(is); + if (is->is_id == ism->ism_session_id) + break; + ISCSI_SESSION_UNLOCK(is); + } + if (is == NULL) { + sx_xunlock(&sc->sc_lock); + return (ESRCH); + } + sx_xunlock(&sc->sc_lock); + + memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf)); + ISCSI_SESSION_UNLOCK(is); + + iscsi_session_reconnect(is); + + return (0); +} + static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td) @@ -1808,6 +1856,9 @@ iscsi_ioctl(struct cdev *dev, u_long cmd case ISCSISLIST: return (iscsi_ioctl_session_list(sc, (struct iscsi_session_list *)arg)); + case ISCSISMODIFY: + return (iscsi_ioctl_session_modify(sc, + (struct iscsi_session_modify *)arg)); default: return (EINVAL); } Modified: head/sys/dev/iscsi/iscsi_ioctl.h ============================================================================== --- head/sys/dev/iscsi/iscsi_ioctl.h Wed Jun 18 17:21:38 2014 (r267612) +++ head/sys/dev/iscsi/iscsi_ioctl.h Wed Jun 18 17:35:40 2014 (r267613) @@ -202,8 +202,15 @@ struct iscsi_session_list { int isl_spare[4]; }; +struct iscsi_session_modify { + unsigned int ism_session_id; + struct iscsi_session_conf ism_conf; + int ism_spare[4]; +}; + #define ISCSISADD _IOW('I', 0x11, struct iscsi_session_add) #define ISCSISREMOVE _IOW('I', 0x12, struct iscsi_session_remove) #define ISCSISLIST _IOWR('I', 0x13, struct iscsi_session_list) +#define ISCSISMODIFY _IOWR('I', 0x14, struct iscsi_session_modify) #endif /* !ISCSI_IOCTL_H */ Modified: head/usr.sbin/iscsid/iscsid.c ============================================================================== --- head/usr.sbin/iscsid/iscsid.c Wed Jun 18 17:21:38 2014 (r267612) +++ head/usr.sbin/iscsid/iscsid.c Wed Jun 18 17:35:40 2014 (r267613) @@ -304,10 +304,10 @@ capsicate(struct connection *conn) cap_rights_t rights; #ifdef ICL_KERNEL_PROXY const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, - ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE }; + ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; #else const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, - ISCSISREMOVE }; + ISCSISREMOVE, ISCSISMODIFY }; #endif cap_rights_init(&rights, CAP_IOCTL); Modified: head/usr.sbin/iscsid/login.c ============================================================================== --- head/usr.sbin/iscsid/login.c Wed Jun 18 17:21:38 2014 (r267612) +++ head/usr.sbin/iscsid/login.c Wed Jun 18 17:35:40 2014 (r267613) @@ -30,6 +30,7 @@ */ #include +#include #include #include #include @@ -158,6 +159,60 @@ login_target_error_str(int class, int de } } +static void +kernel_modify(const struct connection *conn, const char *target_address) +{ + struct iscsi_session_modify ism; + int error; + + memset(&ism, 0, sizeof(ism)); + ism.ism_session_id = conn->conn_session_id; + memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf)); + strlcpy(ism.ism_conf.isc_target_addr, target_address, + sizeof(ism.ism_conf.isc_target)); + error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism); + if (error != 0) { + log_err(1, "failed to redirect to %s: ISCSISMODIFY", + target_address); + } +} + +/* + * XXX: The way it works is suboptimal; what should happen is described + * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would + * be much more complicated: we would need to keep "dependencies" + * for sessions, so that, in case described in draft and using draft + * terminology, we would have three sessions: one for discovery, + * one for initial target portal, and one for redirect portal. + * This would allow us to "backtrack" on connection failure, + * as described in draft. + */ +static void +login_handle_redirection(struct connection *conn, struct pdu *response) +{ + struct iscsi_bhs_login_response *bhslr; + struct keys *response_keys; + const char *target_address; + + bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; + assert (bhslr->bhslr_status_class == 1); + + response_keys = keys_new(); + keys_load(response_keys, response); + + target_address = keys_find(response_keys, "TargetAddress"); + if (target_address == NULL) + log_errx(1, "received redirection without TargetAddress"); + if (target_address[0] == '\0') + log_errx(1, "received redirection with empty TargetAddress"); + if (strlen(target_address) >= + sizeof(conn->conn_conf.isc_target_addr) - 1) + log_errx(1, "received TargetAddress is too long"); + + log_debugx("received redirection to \"%s\"", target_address); + kernel_modify(conn, target_address); +} + static struct pdu * login_receive(struct connection *conn) { @@ -184,6 +239,11 @@ login_receive(struct connection *conn) if (bhslr->bhslr_version_active != 0x00) log_errx(1, "received Login PDU with unsupported " "Version-active 0x%x", bhslr->bhslr_version_active); + if (bhslr->bhslr_status_class == 1) { + login_handle_redirection(conn, response); + log_debugx("redirection handled; exiting"); + exit(0); + } if (bhslr->bhslr_status_class != 0) { errorstr = login_target_error_str(bhslr->bhslr_status_class, bhslr->bhslr_status_detail);