From patchwork Fri Dec 3 11:09:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ronnie sahlberg X-Patchwork-Id: 74124 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 07A0C1007D9 for ; Fri, 3 Dec 2010 22:55:27 +1100 (EST) Received: from localhost ([127.0.0.1]:41434 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POTby-0002s0-BP for incoming@patchwork.ozlabs.org; Fri, 03 Dec 2010 06:15:34 -0500 Received: from [140.186.70.92] (port=58566 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POTXB-00019b-Fq for qemu-devel@nongnu.org; Fri, 03 Dec 2010 06:10:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1POTXA-0002Ws-1h for qemu-devel@nongnu.org; Fri, 03 Dec 2010 06:10:37 -0500 Received: from mail-yx0-f173.google.com ([209.85.213.173]:61819) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1POTX9-0002Wo-Rs for qemu-devel@nongnu.org; Fri, 03 Dec 2010 06:10:36 -0500 Received: by yxl31 with SMTP id 31so5074889yxl.4 for ; Fri, 03 Dec 2010 03:10:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:received:from:to:cc:subject :date:message-id:x-mailer:in-reply-to:references; bh=WUlGhphh+WH9xDliUetkMSCBF+qzRjdtcXApKAWJmlE=; b=nlQm/c8+/EpQjQ4KG6mmbT7J0vl6bXxIDqZxSwax00e7cEtqzscr/RY+U65eW0M3ac HSSfGTviqq9roN6uBtxRlapTlDnYTjcAuBZPdDEQ0OKwUXP1fL3taTHWsJWwx4VBBWqx gjrrNwhr8ZV3hAngRKQfAPc2/Nm1+LOO5SSyM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=OPiCmZGvXADuzIYssoZaVMacwm69/1ksS1PeVH0qAsz4/hjpQwB94mkhj4JBZUDrc5 VTAob6l0Y+9lBTFGcpJGAg+2R2jB6B0yXqZHmS9NWbSJw/qLEhQpTNm35gbFggwvc52Q a5Ynjs8CAL8kC53o12spX/xdOmdr8k/6zu54s= Received: by 10.151.158.12 with SMTP id k12mr3481264ybo.377.1291374635404; Fri, 03 Dec 2010 03:10:35 -0800 (PST) Received: from ronniesahlberg@gmail.com (CPE-121-216-183-74.lnse2.ken.bigpond.net.au [121.216.183.74]) by mx.google.com with ESMTPS id j4sm121370yha.37.2010.12.03.03.10.32 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 03 Dec 2010 03:10:35 -0800 (PST) Received: by ronniesahlberg@gmail.com (sSMTP sendmail emulation); Fri, 03 Dec 2010 22:10:07 +1100 From: ronniesahlberg@gmail.com To: qemu-devel@nongnu.org Date: Fri, 3 Dec 2010 22:09:40 +1100 Message-Id: <1291374593-17448-2-git-send-email-ronniesahlberg@gmail.com> X-Mailer: git-send-email 1.7.3.1 In-Reply-To: <1291374593-17448-1-git-send-email-ronniesahlberg@gmail.com> References: <1291374593-17448-1-git-send-email-ronniesahlberg@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: Ronnie Sahlberg Subject: [Qemu-devel] [PATCH 01/14] ./block/iscsi/init.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Ronnie Sahlberg iscsi client library : init.c This file contains functions to create a iscsi context, destroy a context, error reporting api, as well as basic functions to manipulate properties of an iscsi context. ... ./block/iscsi/ contains a copy of a general purpose iscsi client library which is aimed at providing a clientside api for iscsi for both qemu/kvm as well as otther scsi related utilities. As such, there is need to make merging across various consumers, qemu/kvm being one of many here, as easy as possible when features are added to the library. As such, no consumer/qemu specific code is used in this library as well as coding guidelined might not be adhered to 100% It is the intention that this library will be useful for many and that iscsi use spawned from this will flourish. Signed-off-by: Ronnie Sahlberg --- block/iscsi/init.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 215 insertions(+), 0 deletions(-) create mode 100644 block/iscsi/init.c diff --git a/block/iscsi/init.c b/block/iscsi/init.c new file mode 100644 index 0000000..c6ed347 --- /dev/null +++ b/block/iscsi/init.c @@ -0,0 +1,215 @@ +/* + Copyright (C) 2010 by Ronnie Sahlberg + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "iscsi.h" +#include "iscsi-private.h" +#include "slist.h" + + +struct iscsi_context * +iscsi_create_context(const char *initiator_name) +{ + struct iscsi_context *iscsi; + + iscsi = malloc(sizeof(struct iscsi_context)); + if (iscsi == NULL) { + return NULL; + } + + bzero(iscsi, sizeof(struct iscsi_context)); + + iscsi->initiator_name = strdup(initiator_name); + if (iscsi->initiator_name == NULL) { + free(iscsi); + return NULL; + } + + iscsi->fd = -1; + + /* use a "random" isid */ + srandom(getpid() ^ time(NULL)); + iscsi_set_random_isid(iscsi); + + return iscsi; +} + +int +iscsi_set_random_isid(struct iscsi_context *iscsi) +{ + iscsi->isid[0] = 0x80; + iscsi->isid[1] = random()&0xff; + iscsi->isid[2] = random()&0xff; + iscsi->isid[3] = random()&0xff; + iscsi->isid[4] = 0; + iscsi->isid[5] = 0; + + return 0; +} + +int +iscsi_set_alias(struct iscsi_context *iscsi, const char *alias) +{ + if (iscsi->is_loggedin != 0) { + iscsi_set_error(iscsi, "Already logged in when adding alias\n"); + return -2; + } + + if (iscsi->alias != NULL) { + free(discard_const(iscsi->alias)); + iscsi->alias = NULL; + } + + iscsi->alias = strdup(alias); + if (iscsi->alias == NULL) { + iscsi_set_error(iscsi, "Failed to allocate alias name\n"); + return -3; + } + + return 0; +} + +int +iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name) +{ + if (iscsi->is_loggedin != 0) { + iscsi_set_error(iscsi, "Already logged in when adding " + "targetname\n"); + return -2; + } + + if (iscsi->target_name != NULL) { + free(discard_const(iscsi->target_name)); + iscsi->target_name = NULL; + } + + iscsi->target_name = strdup(target_name); + if (iscsi->target_name == NULL) { + iscsi_set_error(iscsi, "Failed to allocate target name\n"); + return -3; + } + + return 0; +} + +int +iscsi_destroy_context(struct iscsi_context *iscsi) +{ + struct iscsi_pdu *pdu; + + if (iscsi == NULL) { + return 0; + } + if (iscsi->initiator_name != NULL) { + free(discard_const(iscsi->initiator_name)); + iscsi->initiator_name = NULL; + } + if (iscsi->target_name != NULL) { + free(discard_const(iscsi->target_name)); + iscsi->target_name = NULL; + } + if (iscsi->alias != NULL) { + free(discard_const(iscsi->alias)); + iscsi->alias = NULL; + } + if (iscsi->fd != -1) { + iscsi_disconnect(iscsi); + } + + if (iscsi->inbuf != NULL) { + free(iscsi->inbuf); + iscsi->inbuf = NULL; + iscsi->insize = 0; + iscsi->inpos = 0; + } + + while ((pdu = iscsi->outqueue)) { + SLIST_REMOVE(&iscsi->outqueue, pdu); + pdu->callback(iscsi, SCSI_STATUS_CANCELLED, NULL, + pdu->private_data); + iscsi_free_pdu(iscsi, pdu); + } + while ((pdu = iscsi->waitpdu)) { + SLIST_REMOVE(&iscsi->waitpdu, pdu); + pdu->callback(iscsi, SCSI_STATUS_CANCELLED, NULL, + pdu->private_data); + iscsi_free_pdu(iscsi, pdu); + } + + if (iscsi->error_string != NULL) { + free(iscsi->error_string); + } + + free(iscsi); + + return 0; +} + + + +void +iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...) +{ + va_list ap; + char *str; + + va_start(ap, error_string); + if (vasprintf(&str, error_string, ap) < 0) { + /* not much we can do here */ + } + if (iscsi->error_string != NULL) { + free(iscsi->error_string); + } + iscsi->error_string = str; + va_end(ap); +} + + +char * +iscsi_get_error(struct iscsi_context *iscsi) +{ + return iscsi->error_string; +} + +int +iscsi_set_header_digest(struct iscsi_context *iscsi, + enum iscsi_header_digest header_digest) +{ + if (iscsi->is_loggedin) { + iscsi_set_error(iscsi, "trying to set header digest while " + "logged in\n"); + return -2; + } + + iscsi->want_header_digest = header_digest; + + return 0; +} + +int +iscsi_is_logged_in(struct iscsi_context *iscsi) +{ + return iscsi->is_loggedin; +}