ASR_BASE
Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/external/subpack/net/tgt/Makefile b/external/subpack/net/tgt/Makefile
new file mode 100644
index 0000000..6abaae1
--- /dev/null
+++ b/external/subpack/net/tgt/Makefile
@@ -0,0 +1,53 @@
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=tgt
+PKG_VERSION:=1.0.80
+PKG_RELEASE:=1
+
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
+PKG_SOURCE_URL:=https://codeload.github.com/fujita/tgt/tar.gz/v$(PKG_VERSION)?
+PKG_HASH:=e2255482f1a2797c115a6d545c37428b10a4ba6276a2af787da5378fa4e34e75
+
+PKG_MAINTAINER:=Maxim Storchak <m.storchak@gmail.com>
+PKG_LICENSE:=GPL-2.0-only
+PKG_CPE_ID:=cpe:/a:zaal:tgt
+
+PKG_USE_MIPS16:=0
+PKG_INSTALL:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/tgt
+ SECTION:=net
+ CATEGORY:=Network
+ URL:=http://stgt.sourceforge.net/
+ TITLE:=userspace iSCSI target
+ DEPENDS:=+libpthread +libaio
+endef
+
+define Package/tgt/description
+The Linux target framework (tgt) is a user space SCSI target framework
+that supports the iSCSI and iSER transport protocols and that also
+supports multiple methods for accessing block storage. Tgt consists of
+a user-space daemon and user-space tools.
+endef
+
+define Build/Compile
+ $(call Build/Compile/Default,programs)
+endef
+
+define Package/tgt/conffiles
+/etc/config/tgt
+endef
+
+define Package/tgt/install
+ $(INSTALL_DIR) $(1)/etc/config $(1)/etc/init.d $(1)/usr/sbin
+ $(INSTALL_DATA) ./files/tgt.config $(1)/etc/config/tgt
+ $(INSTALL_BIN) ./files/tgt.init $(1)/etc/init.d/tgt
+ $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/tgt{d,adm} $(1)/usr/sbin/
+endef
+
+$(eval $(call BuildPackage,tgt))
diff --git a/external/subpack/net/tgt/files/tgt.config b/external/subpack/net/tgt/files/tgt.config
new file mode 100644
index 0000000..069f594
--- /dev/null
+++ b/external/subpack/net/tgt/files/tgt.config
@@ -0,0 +1,67 @@
+config options 'tgt'
+# iothreads limits number of worker threads per rdwr target, default is 16
+# which seems to be too much for an avarage router
+ option iothreads '2'
+# option nop_count '3'
+# option nop_interval '1'
+# option logging '0'
+# list portal '[::1]'
+# list portal '127.0.0.1:3261'
+# list portal '0.0.0.0:3262'
+# list portal '[::]:3263'
+
+config target 1
+ option name 'iqn.2012-06.org.openwrt:target1'
+# list allow_name 'iqn.1994-05.org.example:fedcba987654'
+# list allow_address '192.168.2.128/27'
+
+#config target 2
+# option name 'iqn.2012-06.org.openwrt:t2'
+
+# all options are set to default, except for the device
+# lun "name" is constructed as TGTID_LUN
+#config lun 1_1
+# option 'device' '/dev/sda'
+# type of scsi device. available options: disk, cd, pt (sg passthrough)
+# option 'type' 'disk'
+# backing store access method: rdwr (read-write), aio (async IO), sg (for pt type only, device must be /dev/sgN)
+# option 'bstype' 'aio'
+# set sync and/or direct flags when opening device, affect only rdwr
+# option 'sync' '0'
+# option 'direct' '0'
+# block size for lun, default is 512
+# option 'blocksize' 4096
+# override SCSI mode page, see tgtadm man page for details
+# option 'mode_page' 'string'
+# vendor, product, revision, SCSI ID and SCSI Serial number
+# option 'vendor_id' 'string'
+# option 'product_id' 'string'
+# option 'product_rev' 'string'
+# option 'scsi_id' 'string'
+# option 'scsi_sn' 'string'
+# refuse write attempts. applies only to disk type
+# option 'readonly' '0'
+# Disk devices default to non-removable, cd - to removable
+# option 'removable' '0'
+# 0 = Classic sense format, 1 = Support descriptor format.
+# option 'sense_format' '0'
+# Rotaion rate: 0: not reported, 1: non-rotational medium (SSD), 2-1024: reserverd, 1025+: "Nominal rotation rate"
+# option 'rotation_rate' '0'
+
+#config lun 2_1
+# option device /mnt/iscsi.img
+
+#config lun 2_2
+# option device /dev/sdc
+
+#config account
+# list target 1
+# list target 2
+# option user "username1"
+# option password "pass1"
+
+#config account
+# option target 2
+# option user "user2"
+# option password "pwd2"
+# option outgoing 1
diff --git a/external/subpack/net/tgt/files/tgt.init b/external/subpack/net/tgt/files/tgt.init
new file mode 100755
index 0000000..594cdf8
--- /dev/null
+++ b/external/subpack/net/tgt/files/tgt.init
@@ -0,0 +1,251 @@
+#!/bin/sh /etc/rc.common
+START=91
+STOP=10
+
+extra_command "show" "Show current configuration of tgtd"
+
+NAME=tgt
+PROG=/usr/sbin/tgtd
+USE_PROCD=1
+
+tgtadm="/usr/sbin/tgtadm --lld iscsi"
+logger="logger -p daemon.err -s -t $NAME"
+
+validate_lun_section() {
+ uci_load_validate tgt lun "$1" "$2" \
+ 'device:or(file, device)' \
+ 'type:or("disk", "cd", "pt"):disk' \
+ 'bstype:or("rdwr", "aio", "sg"):rdwr' \
+ 'sync:bool:0' \
+ 'direct:bool:0' \
+ 'blocksize:uinteger' \
+ 'mode_page:string' \
+ 'product_id:string' \
+ 'product_rev:string' \
+ 'readonly:bool:0' \
+ 'removable:bool' \
+ 'scsi_id:string' \
+ 'scsi_sn:string' \
+ 'sense_format:range(0, 1)' \
+ 'vendor_id:string' \
+ 'rotation_rate:uinteger'
+}
+
+handle_lun() {
+ local tgt_lun=$1
+ local my_tgtid=${tgt_lun%_*}
+ local lun=${tgt_lun#*_}
+
+ [ "$my_tgtid" -eq "$tgtid" ] || return 0
+
+ [ "$2" = 0 ] || {
+ $logger "Validation failed for LUN $tgt_lun"
+ return 1
+ }
+ [ "$device" ] || {
+ $logger "Device is required for target $tgt_lun"
+ return 1
+ }
+
+ if [ "$sync" -ne 0 ] || [ "$direct" -ne 0 ]; then
+ local bsoflags
+ [ "$sync" -ne 0 ] && bsoflags="sync"
+ [ "$direct" -ne 0 ] && bsoflags="direct"
+ [ "$sync" -ne 0 ] && [ "$direct" -ne 0 ] && bsoflags="sync:direct"
+ bsoflags="--bsoflags $bsoflags"
+ fi
+
+ blocksize=${blocksize+--blocksize=$blocksize}
+ local params='' i
+ for i in mode_page product_id product_rev readonly removable scsi_id scsi_sn sense_format vendor_id rotation_rate; do
+ eval params=\${$i+$i=\$$i,}\$params
+ done
+
+ local _tgtadm="$tgtadm --mode logicalunit --tid $tgtid --lun $lun"
+ $_tgtadm --op new --backing-store "$device" --device-type "$type" --bstype "$bstype" $bsoflags $blocksize || {
+ $logger "Failed to create lun $tgt_lun"
+ return 1
+ }
+ $_tgtadm --op update --param $params || {
+ $logger "Failed to update lun $tgt_lun"
+ return 1
+ }
+}
+
+validate_account_section () {
+ uci_load_validate tgt account "$1" "$2" \
+ 'target:list(uinteger)' \
+ 'user:string' \
+ 'password:string' \
+ 'outgoing:bool:0'
+}
+
+handle_account() {
+ local _tgtadm="$tgtadm --mode account"
+
+ [ "$2" = 0 ] || {
+ $logger "Validation failed for account ${user:-$1}"
+ return 1
+ }
+ [ "$user" ] || {
+ $logger "User is required for account $1. Run 'uci show tgt.$1' and check options"
+ return 1
+ }
+ [ "$target" ] || {
+ $logger "Target is required for account $user"
+ return 1
+ }
+ [ "$password" ] || {
+ $logger "Password is required for account $user"
+ return 1
+ }
+ $_tgtadm --op new --user "$user" --password "$password" || {
+ $logger "Failed to create user $username"
+ return 1
+ }
+}
+
+bind_account_to_target() {
+ local _tgtadm="$tgtadm --mode account"
+
+ [ "$2" = 0 ] || {
+ $logger "Validation failed for account ${user:-$1}"
+ return 1
+ }
+
+ [ "$outgoing" -ne 0 ] && outgoing=--outgoing || outgoing=""
+ local t
+ for t in $target; do
+ [ "$t" -eq "$tgtid" ] && {
+ $_tgtadm --op bind --tid "$tgtid" --user "$user" $outgoing || {
+ $logger "Failed to bind user $username to target $tgtid"
+ return 1
+ }
+ }
+ done
+ return 0
+}
+
+validate_target_section() {
+ uci_load_validate tgt target "$1" "$2" \
+ 'name:string:iqn.2012-06.org.openwrt' \
+ 'allow_address:list(string):ALL' \
+ 'allow_name:list(string)'
+}
+
+handle_target() {
+ local tgtid=$1
+ local _tgtadm="$tgtadm --mode target"
+
+ [ "$tgtid" -ge 0 ] || return 1
+ [ "$2" = 0 ] || {
+ $logger "Validation failed for target $tgtid"
+ return 1
+ }
+ $_tgtadm --op new --tid "$tgtid" --targetname "$name" || {
+ $logger "Failed to create target $tgtid"
+ return 1
+ }
+ local i
+ for i in $allow_address; do
+ $_tgtadm --op bind --tid "$tgtid" --initiator-address "$i" || {
+ $logger "Failed to set allow $i to connect to target $tgtid"
+ return 1
+ }
+ done
+ for i in $allow_name; do
+ $_tgtadm --op bind --tid "$tgtid" --initiator-name "$i" || {
+ $logger "Failed to set allow $i to connect to target $tgtid"
+ return 1
+ }
+ done
+ config_foreach validate_lun_section lun handle_lun || return 1
+ config_foreach validate_account_section account bind_account_to_target || return 1
+}
+
+configure() {
+ config_load $NAME
+ $tgtadm --mode sys --op update --name State -v offline || {
+ $logger "Failed to set system state to Offline"
+ return 1
+ }
+ config_foreach validate_account_section account handle_account || return 1
+ config_foreach validate_target_section target handle_target || return 1
+ $tgtadm --mode sys --op update --name State -v ready || {
+ $logger "Failed to set system state to Ready"
+ return 1
+ }
+ return 0
+}
+
+validate_tgt_section() {
+ uci_load_validate tgt options "$1" "$2" \
+ 'iothreads:uinteger' \
+ 'portal:list(string)' \
+ 'nop_interval:uinteger' \
+ 'nop_count:uinteger' \
+ 'logging:bool:0'
+}
+
+start_tgt_instance() {
+ local fg_flag=-f
+ [ "$2" = 0 ] || {
+ $logger "Validation failed for tgt options"
+ return 1
+ }
+ procd_open_instance
+ procd_set_param command $PROG
+ [ "$logging" -eq 1 ] && fg_flag=-D
+ procd_append_param command "$fg_flag"
+ [ "$iothreads" ] && procd_append_param command -t "$iothreads"
+ [ "$portal$nop_interval$nop_count" ] && {
+ local iscsi="" i
+ for i in nop_interval nop_count; do
+ eval iscsi=\${$i+$i=\$$i,}\$iscsi
+ done
+ for i in $portal; do
+ iscsi="portal=$i,$iscsi"
+ done
+ procd_append_param command --iscsi "$iscsi"
+ }
+ procd_set_param respawn
+ procd_close_instance
+ logger -p daemon.info -t "$NAME" -s "Configuration will be loaded in seconds"
+ ( sleep 5; configure || { stop_service; exit 1; } ) &
+}
+
+start_service() {
+ validate_tgt_section tgt start_tgt_instance
+}
+
+stop_service() {
+ $tgtadm --mode sys --op update --name State -v offline || {
+ $logger "Failed to set system state to Offline"
+ return 1
+ }
+ $tgtadm --mode target --op show \
+ | awk '$1 == "Target" {sub(/:/,"",$2); print $2}' \
+ | xargs -r -n1 $tgtadm --mode target --op delete --force --tid
+ $tgtadm --mode sys --op delete
+ procd_kill tgt
+}
+
+reload_service() {
+ stop_service
+ start_service
+}
+
+service_triggers() {
+ procd_add_reload_trigger "tgt"
+
+ procd_open_validate
+ validate_tgt_section
+ validate_account_section
+ validate_target_section
+ validate_lun_section
+ procd_close_validate
+}
+
+show() {
+ $tgtadm --mode target --op show
+}
diff --git a/external/subpack/net/tgt/patches/020-usr_Makefile.patch b/external/subpack/net/tgt/patches/020-usr_Makefile.patch
new file mode 100644
index 0000000..99c0d82
--- /dev/null
+++ b/external/subpack/net/tgt/patches/020-usr_Makefile.patch
@@ -0,0 +1,49 @@
+--- a/usr/Makefile
++++ b/usr/Makefile
+@@ -1,11 +1,11 @@
+ sbindir ?= $(PREFIX)/sbin
+ libdir ?= $(PREFIX)/lib/tgt
+
+-ifneq ($(shell test -e /usr/include/linux/signalfd.h && echo 1),)
++ifneq ($(shell test -n $(shell find $(STAGING_DIR)/usr/include -name "signalfd.h" | head -n1) && echo 1),)
+ CFLAGS += -DUSE_SIGNALFD
+ endif
+
+-ifneq ($(shell test -n $(shell find /usr/include -name "timerfd.h" | head -n1) && echo 1),)
++ifneq ($(shell test -n $(shell find $(STAGING_DIR)/usr/include -name "timerfd.h" | head -n1) && echo 1),)
+ CFLAGS += -DUSE_TIMERFD
+ endif
+
+@@ -25,7 +25,7 @@ ifneq ($(SD_NOTIFY),)
+ CFLAGS += -DUSE_SYSTEMD
+ endif
+
+-ifneq ($(shell test -e /usr/include/sys/eventfd.h && test -e /usr/include/libaio.h && echo 1),)
++ifneq ($(shell test -n $(shell find $(STAGING_DIR)/usr/include -name "eventfd.h" | head -n1) && test -n $(shell find $(STAGING_DIR)/usr/include -name "libaio.h" | head -n1) && echo 1),)
+ CFLAGS += -DUSE_EVENTFD
+ TGTD_OBJS += bs_aio.o
+ LIBS += -laio
+@@ -55,7 +55,7 @@ ifneq ($(SD_NOTIFY),)
+ LIBS += -lsystemd
+ endif
+
+-PROGRAMS += tgtd tgtadm tgtimg
++PROGRAMS += tgtd tgtadm
+ TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \
+ concat_buf.o parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o \
+ ssc.o libssc.o bs_rdwr.o bs_ssc.o \
+@@ -82,14 +82,6 @@ tgtadm: $(TGTADM_OBJS)
+
+ -include $(TGTADM_DEP)
+
+-TGTIMG_OBJS = tgtimg.o libssc.o libcrc32c.o
+-TGTIMG_DEP = $(TGTIMG_OBJS:.o=.d)
+-
+-tgtimg: $(TGTIMG_OBJS)
+- $(CC) $^ -o $@
+-
+--include $(TGTIMG_DEP)
+-
+ %.o: %.c
+ $(CC) -c $(CFLAGS) $*.c -o $*.o
+ @$(CC) -MM $(CFLAGS) -MF $*.d -MT $*.o $*.c
diff --git a/external/subpack/net/tgt/patches/030-Makefile.patch b/external/subpack/net/tgt/patches/030-Makefile.patch
new file mode 100644
index 0000000..6ac58f5
--- /dev/null
+++ b/external/subpack/net/tgt/patches/030-Makefile.patch
@@ -0,0 +1,11 @@
+--- a/Makefile
++++ b/Makefile
+@@ -64,7 +64,7 @@ clean-conf:
+ $(MAKE) -C conf clean
+
+ .PHONY: install
+-install: install-programs install-doc install-conf install-scripts
++install: install-programs
+
+ .PHONY: rpm
+ rpm:
diff --git a/external/subpack/net/tgt/patches/100-musl-compat.patch b/external/subpack/net/tgt/patches/100-musl-compat.patch
new file mode 100644
index 0000000..3d142c2
--- /dev/null
+++ b/external/subpack/net/tgt/patches/100-musl-compat.patch
@@ -0,0 +1,26 @@
+--- a/usr/tgtd.h
++++ b/usr/tgtd.h
+@@ -9,6 +9,10 @@
+ #include <systemd/sd-daemon.h>
+ #endif
+
++#ifndef __WORDSIZE
++#include <sys/reg.h>
++#endif
++
+ struct concat_buf;
+
+ #define NR_SCSI_OPCODES 256
+--- a/usr/util.h
++++ b/usr/util.h
+@@ -16,6 +16,10 @@
+ #include <limits.h>
+ #include <linux/types.h>
+
++#ifndef __WORDSIZE
++#include <sys/reg.h>
++#endif
++
+ #include "be_byteshift.h"
+
+ #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
diff --git a/external/subpack/net/tgt/test.sh b/external/subpack/net/tgt/test.sh
new file mode 100644
index 0000000..7d0162e
--- /dev/null
+++ b/external/subpack/net/tgt/test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+tgtd -V | grep "$2"