1#!/usr/bin/env bash 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright (C) 2017 Intel Corporation 4# All rights reserved. 5# 6set -e 7 8rootdir=$(readlink -f $(dirname $0))/.. 9source "$rootdir/scripts/common.sh" 10 11gen_subsystems=false 12gen_mode="local" 13gen_function="create_local_json_config" 14gen_args=() 15 16function usage() { 17 echo "Script for generating JSON configuration file for attaching" 18 echo "local userspace NVMe drives." 19 echo "Usage: ${0##*/} [OPTIONS]" 20 echo 21 echo "-h, --help Print help and exit" 22 echo " --mode Generate 'local' or 'remote' NVMe JSON configuration. Default is 'local'." 23 echo " Remote needs --trid option to be present." 24 echo " --trid Comma separated list target subsystem information containing transport type," 25 echo " IP addresses, port numbers and NQN names." 26 echo " Example: tcp:127.0.0.1:4420:nqn.2016-06.io.spdk:cnode1,tcp:127.0.0.1:4421:nqn.2016-06.io.spdk:cnode2" 27 echo " --json-with-subsystems Wrap bdev subsystem JSON configuration with higher level 'subsystems' dictionary." 28 echo "-n, --bdev-count Defines number of nvme bdevs to use in the configuration." 29 exit 0 30} 31 32function create_local_json_config() { 33 local bdev_json_cfg=() 34 local bdfs=() 35 local max_bdfs 36 37 bdfs=($(nvme_in_userspace)) 38 max_bdfs=$((bdev_count > 0 && bdev_count < ${#bdfs[@]} ? bdev_count : ${#bdfs[@]})) 39 40 for ((i = 0; i < max_bdfs; i++)); do 41 bdev_json_cfg+=("$( 42 cat <<- JSON 43 { 44 "method": "bdev_nvme_attach_controller", 45 "params": { 46 "trtype": "PCIe", 47 "name":"Nvme${i}", 48 "traddr":"${bdfs[i]}" 49 } 50 } 51 JSON 52 )") 53 done 54 55 local IFS="," 56 cat <<- JSON 57 { 58 "subsystem": "bdev", 59 "config": [ 60 ${bdev_json_cfg[*]} 61 ] 62 } 63 JSON 64} 65 66function create_remote_json_config() { 67 local trids 68 local bdev_json_cfg=() 69 70 IFS="," read -r -a trids <<< $1 71 for ((i = 0; i < ${#trids[@]}; i++)); do 72 local transport 73 local ip_addr 74 local svc_port 75 local nqn 76 77 IFS=":" read -r transport ip_addr svc_port nqn <<< ${trids[i]} 78 bdev_json_cfg+=("$( 79 cat <<- JSON 80 { 81 "method": "bdev_nvme_attach_controller", 82 "params": { 83 "trtype": "$transport", 84 "adrfam": "IPv4", 85 "name": "Nvme${i}", 86 "subnqn": "$nqn", 87 "traddr": "$ip_addr", 88 "trsvcid": "$svc_port" 89 } 90 } 91 JSON 92 )") 93 done 94 95 local IFS="," 96 cat <<- JSON 97 { 98 "subsystem": "bdev", 99 "config": [ 100 ${bdev_json_cfg[*]} 101 ] 102 } 103 JSON 104} 105 106while getopts 'hn:-:' optchar; do 107 case "$optchar" in 108 -) 109 case "$OPTARG" in 110 help) usage ;; 111 mode=*) 112 gen_mode="${OPTARG#*=}" 113 gen_function="create_${OPTARG#*=}_json_config" 114 ;; 115 trid=*) remote_trid="${OPTARG#*=}" ;; 116 json-with-subsystems) gen_subsystems=true ;; 117 bdev-count=*) bdev_count=${OPTARG#*=} ;; 118 *) echo "Invalid argument '$OPTARG'" && usage ;; 119 esac 120 ;; 121 h) usage ;; 122 n) bdev_count=$OPTARG ;; 123 *) echo "Invalid argument '$OPTARG'" && usage ;; 124 esac 125done 126 127if [[ "$gen_mode" == "remote" ]] && [[ -z "$remote_trid" ]]; then 128 echo "For $gen_mode --trid argument must be provided." 129 exit 1 130fi 131 132if [[ "$gen_mode" == "remote" ]]; then 133 gen_args+=("$remote_trid") 134fi 135 136bdev_json_cfg=$("$gen_function" "${gen_args[@]}") 137if [[ $gen_subsystems == true ]]; then 138 bdev_json_cfg=$( 139 cat <<- JSON 140 { 141 "subsystems": [ 142 $bdev_json_cfg 143 ] 144 } 145 JSON 146 ) 147fi 148 149echo "$bdev_json_cfg" 150