• Andrey Ignatov's avatar
    bpf: Introduce bpf_strtol and bpf_strtoul helpers · d7a4cb9b
    Andrey Ignatov authored
    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.
    Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    d7a4cb9b
helpers.c 11.4 KB