History

bpf: Introduce bpf_strtol and bpf_strtoul helpers

Add bpf_strtol and bpf_strtoul to convert a string to long and unsigned long correspondingly. It's similar to user space strtol(3) and strtoul(3) with a few changes to the API: * instead of NUL-terminated C string the helpers expect buffer and buffer length; * resulting long or unsigned long is returned in a separate result-argument; * return value is used to indicate success or failure, on success number of consumed bytes is returned that can be used to identify position to read next if the buffer is expected to contain multiple integers; * instead of *base* argument, *flags* is used that provides base in 5 LSB, other bits are reserved for future use; * number of supported bases is limited. Documentation for the new helpers is provided in bpf.h UAPI. The helpers are made available to BPF_PROG_TYPE_CGROUP_SYSCTL programs to be able to convert string input to e.g. "ulongvec" output. E.g. "net/ipv4/tcp_mem" consists of three ulong integers. They can be parsed by calling to bpf_strtoul three times. Implementation notes: Implementation includes "../../lib/kstrtox.h" to reuse integer parsing functions. It's done exactly same way as fs/proc/base.c already does. Unfortunately existing kstrtoX function can't be used directly since they fail if any invalid character is present right after integer in the string. Existing simple_strtoX functions can't be used either since they're obsolete and don't handle overflow properly.

Andrey Ignatov committed 7 years ago

Commit d7a4cb91 parent 57c3bb74 files · +187 −1Browse files

Showing the changes to include/uapi/linux (1 of 4 files). Show all

Unified · Split

include/uapi/linux/bpf.h+50−1
@@ -2575,6 +2575,53 @@ union bpf_attr { · 2574 unmodified lines
25752575 * **-E2BIG** if the *buf_len* is too big.
25762576 *
25772577 * **-EINVAL** if sysctl is being read.
2578+ *
2579+ * int bpf_strtol(const char *buf, size_t buf_len, u64 flags, long *res)
2580+ * Description
2581+ * Convert the initial part of the string from buffer *buf* of
2582+ * size *buf_len* to a long integer according to the given base
2583+ * and save the result in *res*.
2584+ *
2585+ * The string may begin with an arbitrary amount of white space
2586+ * (as determined by isspace(3)) followed by a single optional '-'
2587+ * sign.
2588+ *
2589+ * Five least significant bits of *flags* encode base, other bits
2590+ * are currently unused.
2591+ *
2592+ * Base must be either 8, 10, 16 or 0 to detect it automatically
2593+ * similar to user space strtol(3).
2594+ * Return
2595+ * Number of characters consumed on success. Must be positive but
2596+ * no more than buf_len.
2597+ *
2598+ * **-EINVAL** if no valid digits were found or unsupported base
2599+ * was provided.
2600+ *
2601+ * **-ERANGE** if resulting value was out of range.
2602+ *
2603+ * int bpf_strtoul(const char *buf, size_t buf_len, u64 flags, unsigned long *res)
2604+ * Description
2605+ * Convert the initial part of the string from buffer *buf* of
2606+ * size *buf_len* to an unsigned long integer according to the
2607+ * given base and save the result in *res*.
2608+ *
2609+ * The string may begin with an arbitrary amount of white space
2610+ * (as determined by isspace(3)).
2611+ *
2612+ * Five least significant bits of *flags* encode base, other bits
2613+ * are currently unused.
2614+ *
2615+ * Base must be either 8, 10, 16 or 0 to detect it automatically
2616+ * similar to user space strtoul(3).
2617+ * Return
2618+ * Number of characters consumed on success. Must be positive but
2619+ * no more than buf_len.
2620+ *
2621+ * **-EINVAL** if no valid digits were found or unsupported base
2622+ * was provided.
2623+ *
2624+ * **-ERANGE** if resulting value was out of range.
25782625 */
25792626 #define __BPF_FUNC_MAPPER(FN) \
25802627 FN(unspec), \
@@ -2681,7 +2728,9 @@ union bpf_attr { · 100 unmodified lines
26812728 FN(sysctl_get_name), \
26822729 FN(sysctl_get_current_value), \
26832730 FN(sysctl_get_new_value), \
2684− FN(sysctl_set_new_value),
2731+ FN(sysctl_set_new_value), \
2732+ FN(strtol), \
2733+ FN(strtoul),
26852734
26862735 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
26872736 * function eBPF program intends to call