File: block-status-payload.c

package info (click to toggle)
libnbd 1.24.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,956 kB
  • sloc: ansic: 55,063; ml: 12,326; sh: 8,817; python: 4,757; makefile: 3,036; perl: 165; cpp: 24
file content (242 lines) | stat: -rw-r--r-- 7,557 bytes parent folder | download | duplicates (2)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* Test interaction with qemu using block status payload filtering. */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <errno.h>

#include <libnbd.h>

#include "array-size.h"

static const char *contexts[] = {
  "base:allocation",
  "qemu:allocation-depth",
  "qemu:dirty-bitmap:bitmap0",
  "qemu:dirty-bitmap:bitmap1",
};

static int
cb (void *opaque, const char *metacontext, uint64_t offset,
    nbd_extent *entries, size_t len, int *error)
{
  /* Adjust seen according to which context was visited */
  unsigned int *seen = opaque;
  size_t i;

  for (i = 0; i < ARRAY_SIZE (contexts); i++)
    if (strcmp (contexts[i], metacontext) == 0)
      break;
  *seen |= 1 << i;
  return 0;
}

static char **
list (unsigned int use)
{
  static const char *array[ARRAY_SIZE (contexts) + 1];
  size_t i, j;

  assert (use < 1 << ARRAY_SIZE (contexts));
  for (i = j = 0; i < ARRAY_SIZE (contexts); i++)
    if (use & (1 << i))
      array[j++] = contexts[i];
  array[j] = NULL;
  return (char **) array;
}

int
main (int argc, char *argv[])
{
  struct nbd_handle *nbd;
  int64_t exportsize;
  uint64_t bytes_sent;
  unsigned int seen;
  size_t i;
  int r;

  if (argc < 2) {
    fprintf (stderr, "%s qemu-nbd [args ...]\n", argv[0]);
    exit (EXIT_FAILURE);
  }

  nbd = nbd_create ();
  if (nbd == NULL) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  assert (ARRAY_SIZE (contexts) == 4);
  for (i = 0; i < ARRAY_SIZE (contexts); i++) {
    if (nbd_add_meta_context (nbd, contexts[i]) == -1) {
      fprintf (stderr, "%s\n", nbd_get_error ());
      exit (EXIT_FAILURE);
    }
  }

  if (nbd_connect_systemd_socket_activation (nbd, &argv[1]) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  r = nbd_can_block_status_payload (nbd);
  if (r == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  if (r != 1) {
    fprintf (stderr, "expecting block status payload support from qemu\n");
    exit (EXIT_FAILURE);
  }

  exportsize = nbd_get_size (nbd);
  if (exportsize == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  /* An unfiltered call should see all four contexts */
  seen = 0;
  if (nbd_block_status_64 (nbd, exportsize, 0,
                           (nbd_extent64_callback) { .callback = cb,
                                                     .user_data = &seen },
                           0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  assert (seen == 0xf);

  /* Filtering with all contexts listed, same effect as unfilitered call */
  seen = 0;
  if (nbd_block_status_filter (nbd, exportsize, 0, list (0xf),
                               (nbd_extent64_callback) { .callback = cb,
                                                         .user_data = &seen },
                               0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  assert (seen == 0xf);

  /* Filtering with just two out of four contexts; test optional flag */
  seen = 0;
  if (nbd_block_status_filter (nbd, exportsize, 0, list (0x5),
                               (nbd_extent64_callback) { .callback = cb,
                                                         .user_data = &seen },
                               LIBNBD_CMD_FLAG_PAYLOAD_LEN) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  assert (seen == 0x5);

  /* Filtering with one context, near end of file (to make sure the
   * payload length isn't confused with the effect length)
   */
  seen = 0;
  if (nbd_block_status_filter (nbd, 1, exportsize - 1, list (0x2),
                               (nbd_extent64_callback) { .callback = cb,
                                                         .user_data = &seen },
                               0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  assert (seen == 0x2);

  /* Filtering with no contexts - pointless, so qemu rejects it */
  bytes_sent = nbd_stats_bytes_sent (nbd);
  seen = 0;
  if (nbd_block_status_filter (nbd, exportsize, 0, list (0x0),
                               (nbd_extent64_callback) { .callback = cb,
                                                         .user_data = &seen },
                               0) != -1) {
    fprintf (stderr, "expecting block status failure\n");
    exit (EXIT_FAILURE);
  }
  assert (seen == 0x0);
  if (nbd_get_errno () != EINVAL) {
    fprintf (stderr, "expecting EINVAL after block status failure\n");
    exit (EXIT_FAILURE);
  }
  if (nbd_stats_bytes_sent (nbd) <= bytes_sent) {
    fprintf (stderr, "expecting server-side rejection of bad request\n");
    exit (EXIT_FAILURE);
  }

  /* Giving unknown string triggers EINVAL from libnbd */
  bytes_sent = nbd_stats_bytes_sent (nbd);
  seen = 0;
  {
    const char *bogus[] = { "qemu:dirty-bitmap:bitmap2", NULL };
    if (nbd_block_status_filter (nbd, exportsize, 0, (char **) bogus,
                                 (nbd_extent64_callback) { .callback = cb,
                                                           .user_data = &seen },
                                 0) != -1) {
      fprintf (stderr, "expecting block status failure\n");
      exit (EXIT_FAILURE);
    }
  }
  if (nbd_get_errno () != EINVAL) {
    fprintf (stderr, "expecting EINVAL after block status failure\n");
    exit (EXIT_FAILURE);
  }
  assert (seen == 0x0);
  if (nbd_stats_bytes_sent (nbd) != bytes_sent) {
    fprintf (stderr, "expecting client-side rejection of bad request\n");
    exit (EXIT_FAILURE);
  }

  /* Giving same string twice triggers EINVAL from qemu */
  seen = 0;
  {
    const char *dupes[] = { "base:allocation", "base:allocation", NULL };
    if (nbd_block_status_filter (nbd, exportsize, 0, (char **) dupes,
                                 (nbd_extent64_callback) { .callback = cb,
                                                           .user_data = &seen },
                                 0) != -1) {
      fprintf (stderr, "expecting block status failure\n");
      exit (EXIT_FAILURE);
    }
  }
  if (nbd_get_errno () != EINVAL) {
    fprintf (stderr, "expecting EINVAL after block status failure\n");
    exit (EXIT_FAILURE);
  }
  assert (seen == 0x0);
  if (nbd_stats_bytes_sent (nbd) <= bytes_sent) {
    fprintf (stderr, "expecting server-side rejection of bad request\n");
    exit (EXIT_FAILURE);
  }

  /* Done */
  if (nbd_shutdown (nbd, 0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  nbd_close (nbd);

  exit (EXIT_SUCCESS);
}