229int main(
int argc,
char *argv[])
235 int namespace_num = 1;
236 float over_provision_percent = -1;
237 int i32_over_provision_percent = 0;
245 int allowance_count = 0;
246 float pending_allowance = -1.0f;
247 int i32_allowance = 0;
250 int current_qos_mode = -1;
251 bool has_zero_allowance =
false;
253 float total_all_namespaces = 0.0f;
256 while ((opt =
getopt(argc, argv,
"d:D:n:p:q:s:a:chv")) != EOF)
264 printf(
"Release ver: %s\n"
274 if (lstat(device_namespace, &sb) != 0 ||
275 (sb.st_mode & S_IFMT) != S_IFBLK)
277 (void)fprintf(stderr,
"ERROR: Only block device is supported! "
278 "%s is not block device!\n", device_namespace);
285 if (pending_allowance >= 0.0f)
287 ns_id = get_ns_id_from_path(device_namespace_OP);
290 (void)fprintf(stderr,
"ERROR: Cannot get namespace ID from -D path %s\n", device_namespace_OP);
293 allowances.namespace_id = ns_id;
294 allowances.allowance = pending_allowance;
296 pending_allowance = -1.0f;
304 "ERROR: %s(): invalid value '%s' for -n\n",
308 namespace_num = (int)tmp_val;
311 (void)fprintf(stderr,
"ERROR: The number of namespace cannot "
326 over_provision_percent = (float)tmp_overprov;
327 if (over_provision_percent > 100 || over_provision_percent < 0)
329 (void)fprintf(stderr,
"ERROR: Overprovision percent cannot "
330 "exceed 100%% or become negative\n");
340 "ERROR: %s(): invalid value '%s' for -q\n",
344 qos_mode = (int)tmp_val;
348 (void)fprintf(stderr,
349 "ERROR: QoS mode %d not supported\n",
358 "ERROR: %s(): invalid value '%s' for -s\n",
362 sriov_index = (int)tmp_val;
365 (void)fprintf(stderr,
"ERROR: Invalid SR-IOV device index: %d\n",
375 double tmp_allowance;
382 allowance = (float)tmp_allowance;
383 if (allowance < 0.0f || allowance > 100.0f)
385 (void)fprintf(stderr,
"ERROR: Allowance %.2f out of range [0-100]\n", allowance);
388 if (device_namespace_OP[0] !=
'\0')
390 ns_id = get_ns_id_from_path(device_namespace_OP);
393 (void)fprintf(stderr,
"ERROR: Cannot get namespace ID from -D path %s\n", device_namespace_OP);
396 allowances.namespace_id = ns_id;
397 allowances.allowance = allowance;
401 pending_allowance = allowance;
405 (void)fprintf(stderr,
"ERROR: Invalid option: %c\n", opt);
410 if (pending_allowance >= 0.0f)
412 (void)fprintf(stderr,
"ERROR: -a requires -D (e.g. -d /dev/nvme0n1 -a 80 -D /dev/nvme0n2)\n");
416 if (allowance_count > 0)
418 if (allowances.allowance == 0.0f)
419 has_zero_allowance =
true;
422 if (ns_handle == NI_INVALID_DEVICE_HANDLE)
424 (void)fprintf(stderr,
"ERROR: Failed to open device %s\n", device_namespace_OP);
432 (void)fprintf(stderr,
"ERROR: Failed to query device capability for %s\n", device_namespace_OP);
439 (void)fprintf(stderr,
"ERROR: Failed to query QoS info for %s\n", device_namespace_OP);
443 if (has_zero_allowance)
445 current_qos_mode = (uint8_t)(qos_header.
ui16QosState & 0xFF);
449 (void)fprintf(stderr,
"ERROR: 0%% allowance only allowed with QoS mode 2 (current mode: %d)\n",
461 (void)fprintf(stderr,
"ERROR: Failed to query QoS namespace info for %s\n", device_namespace_OP);
466 total_all_namespaces = total_all_namespaces - qos_ns_info.
fAllowance + allowances.allowance;
468 if (total_all_namespaces > 100.0f)
470 (void)fprintf(stderr,
"ERROR: Total allowance will be %.2f%% (exceeds 100%%)\n", total_all_namespaces);
473 if (total_all_namespaces < 100.0f)
474 (void)fprintf(stderr,
"INFO: Total allowance will be %.2f%% (under 100%%)\n", total_all_namespaces);
476 memcpy(&i32_allowance, &allowances.allowance,
sizeof(int32_t));
480 (void)fprintf(stderr,
"ERROR: Failed to set allowance for namespace %d\n", allowances.namespace_id);
483 printf(
"Set namespace %d allowance to %.2f%%\n",
484 allowances.namespace_id, allowances.allowance);
485 printf(
"Total allowance across all namespaces: %.2f%%\n", total_all_namespaces);
489 if (device_namespace[0] ==
'\0')
491 (void)fprintf(stderr,
"ERROR: missing argument for -d\n");
494 if (strlen(device_namespace) < 3 ||
495 strcmp(device_namespace + strlen(device_namespace) - 2,
"n1") != 0)
497 (void)fprintf(stderr,
"ERROR: Invalid device name %s, need n1, no vf\n", device_namespace);
503 if (namespace_num != 1 || sriov_index || over_provision_percent != -1)
505 (void)fprintf(stderr,
506 "ERROR: QoS mode -q mutually exclusive of namespace and "
513 printf(
"QoS mode setting succeed with number of %d\n", qos_mode);
518 if (over_provision_percent != -1)
520 if (device_namespace_OP[0] ==
'\0')
522 (void)fprintf(stderr,
"ERROR: missing argument for -D\n");
525 if (namespace_num != 1 || sriov_index || qos_mode != -1)
527 (void)fprintf(stderr,
528 "ERROR: Overprovision percent -p mutually exclusive of "
529 "namespace and SR-IOV and QOS mode\n");
532 memcpy(&i32_over_provision_percent, &over_provision_percent,
sizeof(int32_t));
534 i32_over_provision_percent);
537 printf(
"Overprovision percent setting succeed with number of %f\n",
538 over_provision_percent);
543 if (persistence) namespace_num += 256;
547 printf(
"Namespace setting succeed with number of %d and SR-IOV "
549 namespace_num, sriov_index);