#!/usr/bin/perl -w # SPDX-License-Identifier: GPL-2.0-or-later # # Build a static ASN.1 Object Identified (OID) registry # # Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. # Written by David Howells (dhowells@redhat.com) # use strict; my @names = (); my @oids = (); if ($#ARGV != 1) { print STDERR "Format: ", $0, " \n"; exit(2); } # # Open the file to read from # open IN_FILE, "<$ARGV[0]" || die; while () { chomp; if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) { push @names, $1; push @oids, $2; } } close IN_FILE || die; # # Open the files to write into # open C_FILE, ">$ARGV[1]" or die; print C_FILE "/*\n"; print C_FILE " * Automatically generated by ", $0, ". Do not edit\n"; print C_FILE " */\n"; # # Split the data up into separate lists and also determine the lengths of the # encoded data arrays. # my @indices = (); my @lengths = (); my $total_length = 0; for (my $i = 0; $i <= $#names; $i++) { my $name = $names[$i]; my $oid = $oids[$i]; my @components = split(/[.]/, $oid); # Determine the encoded length of this OID my $size = $#components; for (my $loop = 2; $loop <= $#components; $loop++) { my $c = $components[$loop]; # We will base128 encode the number my $tmp = ($c == 0) ? 0 : int(log($c)/log(2)); $tmp = int($tmp / 7); $size += $tmp; } push @lengths, $size; push @indices, $total_length; $total_length += $size; } # # Emit the look-up-by-OID index table # print C_FILE "\n"; if ($total_length <= 255) { print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n"; } else { print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n"; } for (my $i = 0; $i <= $#names; $i++) { print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n" } print C_FILE "\t[OID__NR] = ", $total_length, "\n"; print C_FILE "};\n"; # # Encode the OIDs # my @encoded_oids = (); for (my $i = 0; $i <= $#names; $i++) { my @octets = (); my @components = split(/[.]/, $oids[$i]); push @octets, $components[0] * 40 + $components[1]; for (my $loop = 2; $loop <= $#components; $loop++) { my $c = $components[$loop]; # Base128 encode the number my $tmp = ($c == 0) ? 0 : int(log($c)/log(2)); $tmp = int($tmp / 7); for (; $tmp > 0; $tmp--) { push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80; } push @octets, $c & 0x7f; } push @encoded_oids, \@octets; } # # Create a hash value for each OID # my @hash_values = (); for (my $i = 0; $i <= $#names; $i++) { my @octets = @{$encoded_oids[$i]}; my $hash = $#octets; foreach (@octets) { $hash += $_ * 33; } $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash); push @hash_values, $hash & 0xff; } # # Emit the OID data # print C_FILE "\n"; print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n"; for (my $i = 0; $i <= $#names; $i++) { my @octets = @{$encoded_oids[$i]}; print C_FILE "\t"; print C_FILE $_, ", " foreach (@octets); print C_FILE "\t// ", $names[$i]; print C_FILE "\n"; } print C_FILE "};\n"; # # Build the search index table (ordered by length then hash then content) # my @index_table = ( 0 .. $#names ); @index_table = sort { my @octets_a = @{$encoded_oids[$a]}; my @octets_b = @{$encoded_oids[$b]}; return $hash_values[$a] <=> $hash_values[$b] if ($hash_values[$a] != $hash_values[$b]); return $#octets_a <=> $#octets_b if ($#octets_a != $#octets_b); for (my $i = $#octets_a; $i >= 0; $i--) { return $octets_a[$i] <=> $octets_b[$i] if ($octets_a[$i] != $octets_b[$i]); } return 0; } @index_table; # # Emit the search index and hash value table # print C_FILE "\n"; print C_FILE "static const struct {\n"; print C_FILE "\tunsigned char hash;\n"; if ($#names <= 255) { print C_FILE "\tenum OID oid : 8;\n"; } else { print C_FILE "\tenum OID oid : 16;\n"; } print C_FILE "} oid_search_table[OID__NR] = {\n"; for (my $i = 0; $i <= $#names; $i++) { my @octets = @{$encoded_oids[$index_table[$i]]}; printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ", $i, $hash_values[$index_table[$i]], $names[$index_table[$i]]); printf C_FILE "%02x", $_ foreach (@octets); print C_FILE "\n"; } print C_FILE "};\n"; # # Emit the OID debugging name table # #print C_FILE "\n"; #print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n"; # #for (my $i = 0; $i <= $#names; $i++) { # print C_FILE "\t\"", $names[$i], "\",\n" #} #print C_FILE "\t\"Unknown-OID\"\n"; #print C_FILE "};\n"; # # Polish off # close C_FILE or die; /bpf: Fix build of sockmap_ktls.cAlexei Starovoitov The selftests fails to build with: tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c: In function ‘test_sockmap_ktls_disconnect_after_delete’: tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c:72:37: error: ‘TCP_ULP’ undeclared (first use in this function) 72 | err = setsockopt(cli, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls")); | ^~~~~~~ Similar to commit that fixes build of sockmap_basic.c on systems with old /usr/include fix the build of sockmap_ktls.c Fixes: d1ba1204f2ee ("selftests/bpf: Test unhashing kTLS socket after removing from map") Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200219205514.3353788-1-ast@kernel.org 2020-02-19selftests/bpf: Change llvm flag -mcpu=probe to -mcpu=v3Yonghong Song The latest llvm supports cpu version v3, which is cpu version v1 plus some additional 64bit jmp insns and 32bit jmp insn support. In selftests/bpf Makefile, the llvm flag -mcpu=probe did runtime probe into the host system. Depending on compilation environments, it is possible that runtime probe may fail, e.g., due to memlock issue. This will cause generated code with cpu version v1. This may cause confusion as the same compiler and the same C code generates different byte codes in different environment. Let us change the llvm flag -mcpu=probe to -mcpu=v3 so the generated code will be the same regardless of the compilation environment. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200219004236.2291125-1-yhs@fb.com 2020-02-19selftests/bpf: Add bpf_read_branch_records() selftestDaniel Xu Add a selftest to test: * default bpf_read_branch_records() behavior * BPF_F_GET_BRANCH_RECORDS_SIZE flag behavior * error path on non branch record perf events * using helper to write to stack * using helper to write to global On host with hardware counter support: # ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED On host without hardware counter support (VM): # ./test_progs -t perf_branches #27/1 perf_branches_hw:OK #27/2 perf_branches_no_hw:OK #27 perf_branches:OK Summary: 1/2 PASSED, 1 SKIPPED, 0 FAILED Also sync tools/include/uapi/linux/bpf.h. Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200218030432.4600-3-dxu@dxuuu.xyz 2020-02-19selftests/bpf: Test unhashing kTLS socket after removing from mapJakub Sitnicki When a TCP socket gets inserted into a sockmap, its sk_prot callbacks get replaced with tcp_bpf callbacks built from regular tcp callbacks. If TLS gets enabled on the same socket, sk_prot callbacks get replaced once again, this time with kTLS callbacks built from tcp_bpf callbacks. Now, we allow removing a socket from a sockmap that has kTLS enabled. After removal, socket remains with kTLS configured. This is where things things get tricky. Since the socket has a set of sk_prot callbacks that are a mix of kTLS and tcp_bpf callbacks, we need to restore just the tcp_bpf callbacks to the original ones. At the moment, it comes down to the the unhash operation. We had a regression recently because tcp_bpf callbacks were not cleared in this particular scenario of removing a kTLS socket from a sockmap. It got fixed in commit 4da6a196f93b ("bpf: Sockmap/tls, during free we may call tcp_bpf_unhash() in loop"). Add a test that triggers the regression so that we don't reintroduce it in the future. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200217121530.754315-4-jakub@cloudflare.com