223int main(
int argc,
char *argv[])
229 int namespace_num = 1;
230 float over_provision_percent = -1;
231 int i32_over_provision_percent = 0;
239 int allowance_count = 0;
240 float pending_allowance = -1.0f;
241 int i32_allowance = 0;
244 int current_qos_mode = -1;
245 bool has_zero_allowance =
false;
246 float total_all_namespaces = 0.0f;
249 while ((opt =
getopt(argc, argv,
"d:D:n:p:q:s:a:chv")) != EOF)
257 printf(
"Release ver: %s\n"
267 if (lstat(device_namespace, &sb) != 0 ||
268 (sb.st_mode & S_IFMT) != S_IFBLK)
270 fprintf(stderr,
"ERROR: Only block device is supported! "
271 "%s is not block device!\n", device_namespace);
278 if (pending_allowance >= 0.0f)
280 ns_id = get_ns_id_from_path(device_namespace_OP);
283 fprintf(stderr,
"ERROR: Cannot get namespace ID from -D path %s\n", device_namespace_OP);
286 allowances.namespace_id = ns_id;
287 allowances.allowance = pending_allowance;
289 pending_allowance = -1.0f;
297 fprintf(stderr,
"ERROR: The number of namespace cannot "
305 if (over_provision_percent > 100 || over_provision_percent < 0)
307 fprintf(stderr,
"ERROR: Overprovision percent cannot "
308 "exceed 100%% or become negative\n");
319 "ERROR: QoS mode %d not supported\n",
328 fprintf(stderr,
"ERROR: Invalid SR-IOV device index: %d\n",
338 if (allowance < 0.0f || allowance > 100.0f)
340 fprintf(stderr,
"ERROR: Allowance %.2f out of range [0-100]\n", allowance);
343 if (device_namespace_OP[0] !=
'\0')
345 ns_id = get_ns_id_from_path(device_namespace_OP);
348 fprintf(stderr,
"ERROR: Cannot get namespace ID from -D path %s\n", device_namespace_OP);
351 allowances.namespace_id = ns_id;
352 allowances.allowance = allowance;
356 pending_allowance = allowance;
359 fprintf(stderr,
"ERROR: Invalid option: %c\n", opt);
364 if (pending_allowance >= 0.0f)
366 fprintf(stderr,
"ERROR: -a requires -D (e.g. -d /dev/nvme0n1 -a 80 -D /dev/nvme0n2)\n");
370 if (allowance_count > 0)
372 if (allowances.allowance == 0.0f)
373 has_zero_allowance =
true;
376 if (ns_handle == NI_INVALID_DEVICE_HANDLE)
378 fprintf(stderr,
"ERROR: Failed to open device %s\n", device_namespace_OP);
386 fprintf(stderr,
"ERROR: Failed to query device capability for %s\n", device_namespace_OP);
393 fprintf(stderr,
"ERROR: Failed to query QoS info for %s\n", device_namespace_OP);
397 if (has_zero_allowance)
399 current_qos_mode = (uint8_t)(qos_header.
ui16QosState & 0xFF);
403 fprintf(stderr,
"ERROR: 0%% allowance only allowed with QoS mode 2 (current mode: %d)\n",
415 fprintf(stderr,
"ERROR: Failed to query QoS namespace info for %s\n", device_namespace_OP);
420 total_all_namespaces = total_all_namespaces - qos_ns_info.
fAllowance + allowances.allowance;
422 if (total_all_namespaces > 100.0f)
424 fprintf(stderr,
"ERROR: Total allowance will be %.2f%% (exceeds 100%%)\n", total_all_namespaces);
427 if (total_all_namespaces < 100.0f)
428 fprintf(stderr,
"INFO: Total allowance will be %.2f%% (under 100%%)\n", total_all_namespaces);
430 memcpy(&i32_allowance, &allowances.allowance,
sizeof(int32_t));
434 fprintf(stderr,
"ERROR: Failed to set allowance for namespace %d\n", allowances.namespace_id);
437 printf(
"Set namespace %d allowance to %.2f%%\n",
438 allowances.namespace_id, allowances.allowance);
439 printf(
"Total allowance across all namespaces: %.2f%%\n", total_all_namespaces);
443 if (device_namespace[0] ==
'\0')
445 fprintf(stderr,
"ERROR: missing argument for -d\n");
448 if (strlen(device_namespace) < 3 ||
449 strcmp(device_namespace + strlen(device_namespace) - 2,
"n1") != 0)
451 fprintf(stderr,
"ERROR: Invalid device name %s, need n1, no vf\n", device_namespace);
457 if (namespace_num != 1 || sriov_index || over_provision_percent != -1)
460 "ERROR: QoS mode -q mutually exclusive of namespace and "
467 printf(
"QoS mode setting succeed with number of %d\n", qos_mode);
472 if (over_provision_percent != -1)
474 if (device_namespace_OP[0] ==
'\0')
476 fprintf(stderr,
"ERROR: missing argument for -D\n");
479 if (namespace_num != 1 || sriov_index || qos_mode != -1)
482 "ERROR: Overprovision percent -p mutually exclusive of "
483 "namespace and SR-IOV and QOS mode\n");
486 memcpy(&i32_over_provision_percent, &over_provision_percent,
sizeof(int32_t));
488 i32_over_provision_percent);
491 printf(
"Overprovision percent setting succeed with number of %f\n",
492 over_provision_percent);
497 if (persistence) namespace_num += 256;
501 printf(
"Namespace setting succeed with number of %d and SR-IOV "
503 namespace_num, sriov_index);