File: test_extendedcopy_simple.c

package info (click to toggle)
libiscsi 1.20.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,080 kB
  • sloc: ansic: 40,928; xml: 749; sh: 689; makefile: 352
file content (217 lines) | stat: -rw-r--r-- 8,273 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
   Copyright (c) 2015 SanDisk Corp.

   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 <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

#include <CUnit/CUnit.h>

#include "iscsi.h"
#include "scsi-lowlevel.h"
#include "iscsi-test-cu.h"

void
test_extendedcopy_simple(void)
{
        int tgt_desc_len = 0, seg_desc_len = 0, offset = XCOPY_DESC_OFFSET, len;
        struct iscsi_data data;
        unsigned char *xcopybuf;
        unsigned int copied_blocks;
        unsigned char *buf1;
        unsigned char *buf2;
        uint64_t cp_dst_lba;

        copied_blocks = num_blocks / 2;
        if (copied_blocks > 2048)
                copied_blocks = 2048;
        buf1 = malloc(copied_blocks * block_size);
        buf2 = malloc(copied_blocks * block_size);

        logging(LOG_VERBOSE, LOG_BLANK_LINE);
        logging(LOG_VERBOSE,
                "Test EXTENDED COPY of %u blocks from start of LUN to end of LUN",
                copied_blocks);

        CHECK_FOR_DATALOSS;

        cp_dst_lba = num_blocks - copied_blocks;
        logging(LOG_VERBOSE, "Zero %u blocks at the end of the LUN (LBA:%llu)",
                copied_blocks, (unsigned long long)cp_dst_lba);
        memset(buf1, '\0', copied_blocks * block_size);
        WRITE16(sd, cp_dst_lba, copied_blocks * block_size,
                block_size, 0, 0, 0, 0, 0, buf1, EXPECT_STATUS_GOOD);
        logging(LOG_VERBOSE, "Write %u blocks of 'A' at LBA:0", copied_blocks);
        memset(buf1, 'A', copied_blocks * block_size);
        WRITE16(sd, 0, copied_blocks * block_size, block_size, 0, 0, 0, 0, 0,
                buf1, EXPECT_STATUS_GOOD);

        data.size = XCOPY_DESC_OFFSET +
                get_desc_len(IDENT_DESCR_TGT_DESCR) +
                get_desc_len(BLK_TO_BLK_SEG_DESCR);
        data.data = alloca(data.size);
        xcopybuf = data.data;
        memset(xcopybuf, 0, data.size);

        /* Initialize target descriptor list with one target descriptor */
        len = populate_tgt_desc(xcopybuf+offset, IDENT_DESCR_TGT_DESCR,
                        LU_ID_TYPE_LUN, 0, 0, 0, 0, sd);
        if (len < 0) {
                CU_FAIL("Populating target descriptor failed");
                goto free;
        }
        offset += len;
        tgt_desc_len = offset - XCOPY_DESC_OFFSET;

        /* Initialize segment descriptor list with one segment descriptor */
        offset += populate_seg_desc_b2b(xcopybuf+offset, 0, 0, 0, 0,
                        copied_blocks, 0, cp_dst_lba);
        seg_desc_len = offset - XCOPY_DESC_OFFSET - tgt_desc_len;

        /* Initialize the parameter list header */
        populate_param_header(xcopybuf, 1, 0, LIST_ID_USAGE_DISCARD, 0,
                        tgt_desc_len, seg_desc_len, 0);

        EXTENDEDCOPY(sd, &data, EXPECT_STATUS_GOOD);

        logging(LOG_VERBOSE, "Read %u blocks from end of the LUN",
                copied_blocks);
        READ16(sd, NULL, cp_dst_lba, copied_blocks * block_size,
               block_size, 0, 0, 0, 0, 0, buf2,
               EXPECT_STATUS_GOOD);

        if (memcmp(buf1, buf2, copied_blocks * block_size)) {
                CU_FAIL("Blocks were not copied correctly");
        }

free:
        free(buf1);
        free(buf2);
}

/* to save time, avoid copying more than 256M */
#define TEST_XCOPY_LARGE_CP_LEN_MAX (256 * 1024 * 1024)
void
test_extendedcopy_large(void)
{
        struct scsi_task *edl_task = NULL;
        struct scsi_copy_results_op_params *opp = NULL;
        uint32_t cp_len_bytes = 0;
        int i, tgt_desc_len = 0, seg_desc_len = 0, offset = XCOPY_DESC_OFFSET;
        int len;
        struct iscsi_data data;
        unsigned char *xcopybuf;
        unsigned int write_blocks;
        unsigned char *buf1;
        unsigned char *buf2;
        uint64_t cp_dst_lba;

        logging(LOG_VERBOSE, LOG_BLANK_LINE);
        logging(LOG_VERBOSE,
                "Test large EXTENDED COPY with type 0x0A segment descriptor");

        CHECK_FOR_DATALOSS;

        /* check for 0x0A type SD support, which offers 32-bit lengths */
        RECEIVE_COPY_RESULTS(&edl_task, sd, SCSI_COPY_RESULTS_OP_PARAMS, 0,
                             (void **)&opp, EXPECT_STATUS_GOOD);
        CU_ASSERT_NOT_EQUAL(opp, NULL);

        for (i = 0; i < opp->impl_desc_list_length; i++) {
                if (opp->imp_desc_type_codes[i] == BLK_TO_BLK_OFF_SEG_DESCR) {
                        cp_len_bytes = opp->max_segment_length;
                        break;
                }
        }
        scsi_free_scsi_task(edl_task);

        if (cp_len_bytes == 0) {
                logging(LOG_NORMAL,
                        "[SKIPPED] BLK_TO_BLK_OFF_SEG_DESCR not supported");
                CU_PASS("[SKIPPED] BLK_TO_BLK_OFF_SEG_DESCR not supported");
                return;
        }
        if (cp_len_bytes > TEST_XCOPY_LARGE_CP_LEN_MAX) {
                cp_len_bytes = TEST_XCOPY_LARGE_CP_LEN_MAX;
        }
        if (num_blocks < (cp_len_bytes / block_size) * 2) {
                logging(LOG_NORMAL,
                        "[SKIPPED] device too small to handle maxlen XCOPY");
                CU_PASS("[SKIPPED] device too small to handle maxlen XCOPY");
                return;
        }

        write_blocks = num_blocks / 2;
        if (write_blocks > 2048)
                write_blocks = 2048;
        buf1 = malloc(write_blocks * block_size);
        buf2 = malloc(write_blocks * block_size);

        cp_dst_lba = num_blocks - (cp_len_bytes / block_size);
        logging(LOG_VERBOSE, "Zero %u blocks at the end of the LUN (LBA:%llu)",
                write_blocks, (unsigned long long)cp_dst_lba);
        memset(buf1, '\0', write_blocks * block_size);
        WRITE16(sd, cp_dst_lba, write_blocks * block_size,
                block_size, 0, 0, 0, 0, 0, buf1, EXPECT_STATUS_GOOD);
        logging(LOG_VERBOSE, "Write %u blocks of 'B' at LBA:0", write_blocks);
        memset(buf1, 'B', write_blocks * block_size);
        WRITE16(sd, 0, write_blocks * block_size, block_size, 0, 0, 0, 0, 0,
                buf1, EXPECT_STATUS_GOOD);

        data.size = XCOPY_DESC_OFFSET +
                get_desc_len(IDENT_DESCR_TGT_DESCR) +
                get_desc_len(BLK_TO_BLK_OFF_SEG_DESCR);
        data.data = alloca(data.size);
        xcopybuf = data.data;
        memset(xcopybuf, 0, data.size);

        /* Initialize target descriptor list with one target descriptor */
        len = populate_tgt_desc(xcopybuf+offset, IDENT_DESCR_TGT_DESCR,
                        LU_ID_TYPE_LUN, 0, 0, 0, 0, sd);
        if (len < 0) {
                CU_FAIL("Populating target descriptor failed");
                goto free;
        }
        offset += len;
        tgt_desc_len = offset - XCOPY_DESC_OFFSET;

        /* Initialize segment descriptor list with one segment descriptor */
        offset += populate_seg_desc_b2b_off(xcopybuf+offset, 0, 0, 0,
                                  cp_len_bytes, 0, cp_dst_lba, 0, 0);
        seg_desc_len = offset - XCOPY_DESC_OFFSET - tgt_desc_len;

        /* Initialize the parameter list header */
        populate_param_header(xcopybuf, 1, 0, LIST_ID_USAGE_DISCARD, 0,
                        tgt_desc_len, seg_desc_len, 0);

        EXTENDEDCOPY(sd, &data, EXPECT_STATUS_GOOD);

        logging(LOG_VERBOSE, "Read %u blocks from end of the LUN",
                write_blocks);
        READ16(sd, NULL, cp_dst_lba, write_blocks * block_size,
               block_size, 0, 0, 0, 0, 0, buf2,
               EXPECT_STATUS_GOOD);

        if (memcmp(buf1, buf2, write_blocks * block_size)) {
                CU_FAIL("Blocks were not copied correctly");
        }

free:
        free(buf1);
        free(buf2);
}