mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
Bump github.com/weaveworks/tcptracer-bpf/ and github.com/iovisor/gobpf/
This commit is contained in:
14
vendor/github.com/iovisor/gobpf/bpffs/COPYRIGHT.txt
generated
vendored
Normal file
14
vendor/github.com/iovisor/gobpf/bpffs/COPYRIGHT.txt
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
Copyright 2016 PLUMgrid
|
||||
Copyright 2016 Kinvolk
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
201
vendor/github.com/iovisor/gobpf/bpffs/LICENSE.txt
generated
vendored
Normal file
201
vendor/github.com/iovisor/gobpf/bpffs/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
44
vendor/github.com/iovisor/gobpf/bpffs/fs.go
generated
vendored
Normal file
44
vendor/github.com/iovisor/gobpf/bpffs/fs.go
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
package bpffs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const BPFFSPath = "/sys/fs/bpf"
|
||||
|
||||
var FsMagicBPFFS int32
|
||||
|
||||
func init() {
|
||||
// https://github.com/coreutils/coreutils/blob/v8.27/src/stat.c#L275
|
||||
// https://github.com/torvalds/linux/blob/v4.8/include/uapi/linux/magic.h#L80
|
||||
magic := uint32(0xCAFE4A11)
|
||||
// 0xCAFE4A11 overflows an int32, which is what's expected by Statfs_t.Type in 32bit platforms.
|
||||
// To avoid conditional compilation for all 32bit/64bit platforms, we use an unsafe cast
|
||||
FsMagicBPFFS = *(*int32)(unsafe.Pointer(&magic))
|
||||
}
|
||||
|
||||
// IsMounted checks if the BPF fs is mounted already
|
||||
func IsMounted() (bool, error) {
|
||||
var data syscall.Statfs_t
|
||||
if err := syscall.Statfs(BPFFSPath, &data); err != nil {
|
||||
return false, fmt.Errorf("cannot statfs %q: %v", BPFFSPath, err)
|
||||
}
|
||||
return int32(data.Type) == FsMagicBPFFS, nil
|
||||
}
|
||||
|
||||
// Mount mounts the BPF fs if not already mounted
|
||||
func Mount() error {
|
||||
mounted, err := IsMounted()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mounted {
|
||||
return nil
|
||||
}
|
||||
if err := syscall.Mount(BPFFSPath, BPFFSPath, "bpf", 0, ""); err != nil {
|
||||
return fmt.Errorf("error mounting %q: %v", BPFFSPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
164
vendor/github.com/iovisor/gobpf/elf/elf.go
generated
vendored
164
vendor/github.com/iovisor/gobpf/elf/elf.go
generated
vendored
@@ -26,13 +26,18 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/iovisor/gobpf/bpffs"
|
||||
)
|
||||
|
||||
/*
|
||||
#define _GNU_SOURCE
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -51,11 +56,16 @@ import (
|
||||
// from https://github.com/safchain/goebpf
|
||||
// Apache License, Version 2.0
|
||||
|
||||
#define BUF_SIZE_MAP_NS 256
|
||||
|
||||
typedef struct bpf_map_def {
|
||||
unsigned int type;
|
||||
unsigned int key_size;
|
||||
unsigned int value_size;
|
||||
unsigned int max_entries;
|
||||
unsigned int map_flags;
|
||||
unsigned int pinning;
|
||||
char namespace[BUF_SIZE_MAP_NS];
|
||||
} bpf_map_def;
|
||||
|
||||
typedef struct bpf_map {
|
||||
@@ -63,6 +73,8 @@ typedef struct bpf_map {
|
||||
bpf_map_def def;
|
||||
} bpf_map;
|
||||
|
||||
extern int bpf_pin_object(int fd, const char *pathname);
|
||||
|
||||
__u64 ptr_to_u64(void *ptr)
|
||||
{
|
||||
return (__u64) (unsigned long) ptr;
|
||||
@@ -113,9 +125,25 @@ static int bpf_create_map(enum bpf_map_type map_type, int key_size,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bpf_map *bpf_load_map(bpf_map_def *map_def)
|
||||
void create_bpf_obj_get(const char *pathname, void *attr)
|
||||
{
|
||||
union bpf_attr *ptr_bpf_attr;
|
||||
ptr_bpf_attr = (union bpf_attr *)attr;
|
||||
ptr_bpf_attr->pathname = ptr_to_u64((void *) pathname);
|
||||
}
|
||||
|
||||
int get_pinned_obj_fd(const char *path)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
create_bpf_obj_get(path, &attr);
|
||||
return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static bpf_map *bpf_load_map(bpf_map_def *map_def, const char *path)
|
||||
{
|
||||
bpf_map *map;
|
||||
struct stat st;
|
||||
int ret, do_pin = 0;
|
||||
|
||||
map = calloc(1, sizeof(bpf_map));
|
||||
if (map == NULL)
|
||||
@@ -123,14 +151,38 @@ static bpf_map *bpf_load_map(bpf_map_def *map_def)
|
||||
|
||||
memcpy(&map->def, map_def, sizeof(bpf_map_def));
|
||||
|
||||
switch (map_def->pinning) {
|
||||
case 1: // PIN_OBJECT_NS
|
||||
// TODO to be implemented
|
||||
return 0;
|
||||
case 2: // PIN_GLOBAL_NS
|
||||
if (stat(path, &st) == 0) {
|
||||
ret = get_pinned_obj_fd(path);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
map->fd = ret;
|
||||
return map;
|
||||
}
|
||||
do_pin = 1;
|
||||
}
|
||||
|
||||
map->fd = bpf_create_map(map_def->type,
|
||||
map_def->key_size,
|
||||
map_def->value_size,
|
||||
map_def->max_entries
|
||||
);
|
||||
|
||||
if (map->fd < 0)
|
||||
if (map->fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (do_pin) {
|
||||
ret = bpf_pin_object(map->fd, path);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
@@ -203,8 +255,8 @@ static int perf_event_open_map(int pid, int cpu, int group_fd, unsigned long fla
|
||||
attr.size = sizeof(struct perf_event_attr);
|
||||
attr.config = 10; // PERF_COUNT_SW_BPF_OUTPUT
|
||||
|
||||
return syscall(__NR_perf_event_open, &attr, pid, cpu,
|
||||
group_fd, flags);
|
||||
return syscall(__NR_perf_event_open, &attr, pid, cpu,
|
||||
group_fd, flags);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
@@ -243,40 +295,78 @@ func elfReadVersion(file *elf.File) (uint32, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func prepareBPFFS(namespace, name string) (string, error) {
|
||||
err := bpffs.Mount()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mapPath := filepath.Join(BPFFSPath, namespace, BPFDirGlobals, name)
|
||||
err = os.MkdirAll(filepath.Dir(mapPath), syscall.S_IRWXU)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating map directory %q: %v", filepath.Dir(mapPath), err)
|
||||
}
|
||||
return mapPath, nil
|
||||
}
|
||||
|
||||
func validMapNamespace(namespaceRaw *C.char) (string, error) {
|
||||
namespace := C.GoStringN(namespaceRaw, C.int(C.strnlen(namespaceRaw, C.BUF_SIZE_MAP_NS)))
|
||||
if namespace == "" || namespace == "." || namespace == ".." {
|
||||
return "", fmt.Errorf("namespace must not be %q", namespace)
|
||||
}
|
||||
if strings.Contains(namespace, "/") {
|
||||
return "", fmt.Errorf("no '/' allowed in namespace")
|
||||
}
|
||||
return namespace, nil
|
||||
}
|
||||
|
||||
func elfReadMaps(file *elf.File) (map[string]*Map, error) {
|
||||
maps := make(map[string]*Map)
|
||||
for sectionIdx, section := range file.Sections {
|
||||
if strings.HasPrefix(section.Name, "maps/") {
|
||||
data, err := section.Data()
|
||||
for _, section := range file.Sections {
|
||||
if !strings.HasPrefix(section.Name, "maps/") {
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := section.Data()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) != C.sizeof_struct_bpf_map_def {
|
||||
return nil, fmt.Errorf("only one map with size %d allowed per section", C.sizeof_struct_bpf_map_def)
|
||||
}
|
||||
|
||||
name := strings.TrimPrefix(section.Name, "maps/")
|
||||
|
||||
mapDef := (*C.bpf_map_def)(unsafe.Pointer(&data[0]))
|
||||
|
||||
var mapPathC *C.char
|
||||
if mapDef.pinning > 0 {
|
||||
namespace, err := validMapNamespace(&mapDef.namespace[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name := strings.TrimPrefix(section.Name, "maps/")
|
||||
|
||||
mapCount := len(data) / C.sizeof_struct_bpf_map_def
|
||||
for i := 0; i < mapCount; i++ {
|
||||
pos := i * C.sizeof_struct_bpf_map_def
|
||||
cm, err := C.bpf_load_map((*C.bpf_map_def)(unsafe.Pointer(&data[pos])))
|
||||
if cm == nil {
|
||||
return nil, fmt.Errorf("error while loading map %q: %v", section.Name, err)
|
||||
}
|
||||
|
||||
m := &Map{
|
||||
Name: name,
|
||||
SectionIdx: sectionIdx,
|
||||
Idx: i,
|
||||
m: cm,
|
||||
}
|
||||
|
||||
if oldMap, ok := maps[name]; ok {
|
||||
return nil, fmt.Errorf("duplicate map: %q (section %q) and %q (section %q)",
|
||||
oldMap.Name, file.Sections[oldMap.SectionIdx].Name,
|
||||
name, section.Name)
|
||||
}
|
||||
maps[name] = m
|
||||
mapPath, err := prepareBPFFS(namespace, name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing bpf fs: %v", err)
|
||||
}
|
||||
mapPathC = C.CString(mapPath)
|
||||
defer C.free(unsafe.Pointer(mapPathC))
|
||||
} else {
|
||||
mapPathC = nil
|
||||
}
|
||||
|
||||
cm, err := C.bpf_load_map(mapDef, mapPathC)
|
||||
if cm == nil {
|
||||
return nil, fmt.Errorf("error while loading map %q: %v", section.Name, err)
|
||||
}
|
||||
|
||||
if oldMap, ok := maps[name]; ok {
|
||||
return nil, fmt.Errorf("duplicate map: %q and %q", oldMap.Name, name)
|
||||
}
|
||||
maps[name] = &Map{
|
||||
Name: name,
|
||||
m: cm,
|
||||
}
|
||||
|
||||
}
|
||||
return maps, nil
|
||||
}
|
||||
@@ -598,7 +688,7 @@ func (b *Module) initializePerfMaps(parameters map[string]SectionParams) error {
|
||||
return fmt.Errorf("error enabling perf event: %v", err2)
|
||||
}
|
||||
|
||||
// assign perf fd tp map
|
||||
// assign perf fd to map
|
||||
ret, err := C.bpf_update_element(C.int(b.maps[name].m.fd), unsafe.Pointer(&cpu), unsafe.Pointer(&pmuFD), C.BPF_ANY)
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("cannot assign perf fd to map %q: %v (cpu %d)", name, err, cpu)
|
||||
@@ -617,10 +707,8 @@ func (b *Module) initializePerfMaps(parameters map[string]SectionParams) error {
|
||||
// Map represents a eBPF map. An eBPF map has to be declared in the
|
||||
// C file.
|
||||
type Map struct {
|
||||
Name string
|
||||
SectionIdx int
|
||||
Idx int
|
||||
m *C.bpf_map
|
||||
Name string
|
||||
m *C.bpf_map
|
||||
|
||||
// only for perf maps
|
||||
pmuFDs []C.int
|
||||
@@ -642,3 +730,7 @@ func (b *Module) IterMaps() <-chan *Map {
|
||||
func (b *Module) Map(name string) *Map {
|
||||
return b.maps[name]
|
||||
}
|
||||
|
||||
func (m *Map) Fd() int {
|
||||
return int(m.m.fd)
|
||||
}
|
||||
|
||||
123
vendor/github.com/iovisor/gobpf/elf/module.go
generated
vendored
123
vendor/github.com/iovisor/gobpf/elf/module.go
generated
vendored
@@ -25,14 +25,17 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#include <unistd.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include "include/bpf.h"
|
||||
#include <linux/perf_event.h>
|
||||
#include <linux/unistd.h>
|
||||
@@ -124,14 +127,13 @@ func writeKprobeEvent(probeType, eventName, funcName, maxactiveStr string) (int,
|
||||
kprobeEventsFileName := "/sys/kernel/debug/tracing/kprobe_events"
|
||||
f, err := os.OpenFile(kprobeEventsFileName, os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("cannot open kprobe_events: %v\n", err)
|
||||
return -1, fmt.Errorf("cannot open kprobe_events: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cmd := fmt.Sprintf("%s%s:%s %s\n", probeType, maxactiveStr, eventName, funcName)
|
||||
_, err = f.WriteString(cmd)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("cannot write %q to kprobe_events: %v\n", cmd, err)
|
||||
if _, err = f.WriteString(cmd); err != nil {
|
||||
return -1, fmt.Errorf("cannot write %q to kprobe_events: %v", cmd, err)
|
||||
}
|
||||
|
||||
kprobeIdFile := fmt.Sprintf("/sys/kernel/debug/tracing/events/kprobes/%s/id", eventName)
|
||||
@@ -140,12 +142,12 @@ func writeKprobeEvent(probeType, eventName, funcName, maxactiveStr string) (int,
|
||||
if os.IsNotExist(err) {
|
||||
return -1, kprobeIDNotExist
|
||||
}
|
||||
return -1, fmt.Errorf("cannot read kprobe id: %v\n", err)
|
||||
return -1, fmt.Errorf("cannot read kprobe id: %v", err)
|
||||
}
|
||||
|
||||
kprobeId, err := strconv.Atoi(strings.TrimSpace(string(kprobeIdBytes)))
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("invalid kprobe id: %v\n", err)
|
||||
return -1, fmt.Errorf("invalid kprobe id: %v", err)
|
||||
}
|
||||
|
||||
return kprobeId, nil
|
||||
@@ -257,8 +259,115 @@ func (b *Module) AttachCgroupProgram(cgroupProg *CgroupProgram, cgroupPath strin
|
||||
cgroupFd := C.int(f.Fd())
|
||||
ret, err := C.bpf_prog_attach(progFd, cgroupFd, uint32(attachType))
|
||||
if ret < 0 {
|
||||
return fmt.Errorf("failed to attach prog to cgroup %q: %v\n", cgroupPath, err)
|
||||
return fmt.Errorf("failed to attach prog to cgroup %q: %v", cgroupPath, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Module) Kprobe(name string) *Kprobe {
|
||||
return b.probes[name]
|
||||
}
|
||||
|
||||
func (kp *Kprobe) Fd() int {
|
||||
return kp.fd
|
||||
}
|
||||
|
||||
func disableKprobe(eventName string) error {
|
||||
kprobeEventsFileName := "/sys/kernel/debug/tracing/kprobe_events"
|
||||
f, err := os.OpenFile(kprobeEventsFileName, os.O_APPEND|os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot open kprobe_events: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
cmd := fmt.Sprintf("-:%s\n", eventName)
|
||||
if _, err = f.WriteString(cmd); err != nil {
|
||||
pathErr, ok := err.(*os.PathError)
|
||||
if ok && pathErr.Err == syscall.ENOENT {
|
||||
// This can happen when for example two modules
|
||||
// use the same elf object and both call `Close()`.
|
||||
// The second will encounter the error as the
|
||||
// probe already has been cleared by the first.
|
||||
return nil
|
||||
} else {
|
||||
return fmt.Errorf("cannot write %q to kprobe_events: %v", cmd, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Module) closeProbes() error {
|
||||
var funcName string
|
||||
for _, probe := range b.probes {
|
||||
if err := syscall.Close(probe.efd); err != nil {
|
||||
return fmt.Errorf("error closing perf event fd: %v", err)
|
||||
}
|
||||
if err := syscall.Close(probe.fd); err != nil {
|
||||
return fmt.Errorf("error closing probe fd: %v", err)
|
||||
}
|
||||
name := probe.Name
|
||||
isKretprobe := strings.HasPrefix(name, "kretprobe/")
|
||||
var err error
|
||||
if isKretprobe {
|
||||
funcName = strings.TrimPrefix(name, "kretprobe/")
|
||||
err = disableKprobe("r" + funcName)
|
||||
} else {
|
||||
funcName = strings.TrimPrefix(name, "kprobe/")
|
||||
err = disableKprobe("p" + funcName)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("error clearing probe: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Module) closeCgroupPrograms() error {
|
||||
for _, program := range b.cgroupPrograms {
|
||||
if err := syscall.Close(program.fd); err != nil {
|
||||
return fmt.Errorf("error closing cgroup program fd: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unpinMap(m *Map) error {
|
||||
if m.m.def.pinning == 0 {
|
||||
return nil
|
||||
}
|
||||
namespace := C.GoString(&m.m.def.namespace[0])
|
||||
mapPath := filepath.Join(BPFFSPath, namespace, BPFDirGlobals, m.Name)
|
||||
return syscall.Unlink(mapPath)
|
||||
}
|
||||
|
||||
func (b *Module) closeMaps() error {
|
||||
for _, m := range b.maps {
|
||||
if m.m.def.pinning > 0 {
|
||||
unpinMap(m)
|
||||
}
|
||||
for _, fd := range m.pmuFDs {
|
||||
if err := syscall.Close(int(fd)); err != nil {
|
||||
return fmt.Errorf("error closing perf event fd: %v", err)
|
||||
}
|
||||
}
|
||||
if err := syscall.Close(int(m.m.fd)); err != nil {
|
||||
return fmt.Errorf("error closing map fd: %v", err)
|
||||
}
|
||||
C.free(unsafe.Pointer(m.m))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close takes care of terminating all underlying BPF programs and structures
|
||||
func (b *Module) Close() error {
|
||||
if err := b.closeMaps(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.closeProbes(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.closeCgroupPrograms(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
4
vendor/github.com/iovisor/gobpf/elf/module_unsupported.go
generated
vendored
4
vendor/github.com/iovisor/gobpf/elf/module_unsupported.go
generated
vendored
@@ -40,6 +40,10 @@ func (b *Module) CgroupProgram(name string) *CgroupProgram {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Module) Kprobe(name string) *Kprobe {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Module) AttachProgram(cgroupProg *CgroupProgram, cgroupPath string, attachType AttachType) error {
|
||||
return fmt.Errorf("not supported")
|
||||
}
|
||||
|
||||
6
vendor/github.com/iovisor/gobpf/elf/perf.go
generated
vendored
6
vendor/github.com/iovisor/gobpf/elf/perf.go
generated
vendored
@@ -173,6 +173,12 @@ func (pm *PerfMap) PollStart() {
|
||||
|
||||
harvestLoop:
|
||||
for {
|
||||
select {
|
||||
case <-pm.pollStop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
var harvestCount C.int
|
||||
beforeHarvest := nowNanoseconds()
|
||||
for cpu := 0; cpu < cpuCount; cpu++ {
|
||||
|
||||
69
vendor/github.com/iovisor/gobpf/elf/pinning.go
generated
vendored
Normal file
69
vendor/github.com/iovisor/gobpf/elf/pinning.go
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
// +build linux
|
||||
|
||||
package elf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"unsafe"
|
||||
|
||||
"github.com/iovisor/gobpf/bpffs"
|
||||
)
|
||||
|
||||
/*
|
||||
#include <linux/unistd.h>
|
||||
#include <linux/bpf.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern __u64 ptr_to_u64(void *);
|
||||
|
||||
int bpf_pin_object(int fd, const char *pathname)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.pathname = ptr_to_u64((void *)pathname);
|
||||
attr.bpf_fd = fd;
|
||||
|
||||
return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
const (
|
||||
BPFDirGlobals = "globals" // as in iproute2's BPF_DIR_GLOBALS
|
||||
BPFFSPath = "/sys/fs/bpf/"
|
||||
)
|
||||
|
||||
func pinObject(fd int, namespace, object, name string) error {
|
||||
mounted, err := bpffs.IsMounted()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error checking if %q is mounted: %v", BPFFSPath, err)
|
||||
}
|
||||
if !mounted {
|
||||
return fmt.Errorf("bpf fs not mounted at %q", BPFFSPath)
|
||||
}
|
||||
mapPath := filepath.Join(BPFFSPath, namespace, object, name)
|
||||
err = os.MkdirAll(filepath.Dir(mapPath), 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating map directory %q: %v", filepath.Dir(mapPath), err)
|
||||
}
|
||||
err = os.RemoveAll(mapPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error removing old map file %q: %v", mapPath, err)
|
||||
}
|
||||
|
||||
mapPathC := C.CString(mapPath)
|
||||
defer C.free(unsafe.Pointer(mapPathC))
|
||||
|
||||
ret, err := C.bpf_pin_object(C.int(fd), mapPathC)
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("error pinning object to %q: %v", mapPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func PinObjectGlobal(fd int, namespace, name string) error {
|
||||
return pinObject(fd, namespace, BPFDirGlobals, name)
|
||||
}
|
||||
4
vendor/github.com/weaveworks/tcptracer-bpf/bpf_helpers.h
generated
vendored
4
vendor/github.com/weaveworks/tcptracer-bpf/bpf_helpers.h
generated
vendored
@@ -60,12 +60,16 @@ unsigned long long load_word(void *skb,
|
||||
/* a helper structure used by eBPF C program
|
||||
* to describe map attributes to elf_bpf loader
|
||||
*/
|
||||
#define BUF_SIZE_MAP_NS 256
|
||||
|
||||
struct bpf_map_def {
|
||||
unsigned int type;
|
||||
unsigned int key_size;
|
||||
unsigned int value_size;
|
||||
unsigned int max_entries;
|
||||
unsigned int map_flags;
|
||||
unsigned int pinning;
|
||||
char namespace[BUF_SIZE_MAP_NS];
|
||||
};
|
||||
|
||||
static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
|
||||
|
||||
4
vendor/github.com/weaveworks/tcptracer-bpf/pkg/tracer/tcptracer-ebpf.go
generated
vendored
4
vendor/github.com/weaveworks/tcptracer-bpf/pkg/tracer/tcptracer-ebpf.go
generated
vendored
File diff suppressed because one or more lines are too long
1
vendor/github.com/weaveworks/tcptracer-bpf/pkg/tracer/tracer.go
generated
vendored
1
vendor/github.com/weaveworks/tcptracer-bpf/pkg/tracer/tracer.go
generated
vendored
@@ -129,6 +129,7 @@ func (t *Tracer) Stop() {
|
||||
close(t.stopChan)
|
||||
t.perfMapIPV4.PollStop()
|
||||
t.perfMapIPV6.PollStop()
|
||||
t.m.Close()
|
||||
}
|
||||
|
||||
func initialize(module *bpflib.Module, eventMapName string, eventChan chan []byte, lostChan chan uint64) (*bpflib.PerfMap, error) {
|
||||
|
||||
18
vendor/github.com/weaveworks/tcptracer-bpf/tcptracer-bpf.c
generated
vendored
18
vendor/github.com/weaveworks/tcptracer-bpf/tcptracer-bpf.c
generated
vendored
@@ -24,6 +24,8 @@ struct bpf_map_def SEC("maps/tcp_event_ipv4") tcp_event_ipv4 = {
|
||||
.key_size = sizeof(int),
|
||||
.value_size = sizeof(__u32),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being the cpu number
|
||||
@@ -34,6 +36,8 @@ struct bpf_map_def SEC("maps/tcp_event_ipv6") tcp_event_ipv6 = {
|
||||
.key_size = sizeof(int),
|
||||
.value_size = sizeof(__u32),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* These maps are used to match the kprobe & kretprobe of connect */
|
||||
@@ -46,6 +50,8 @@ struct bpf_map_def SEC("maps/connectsock_ipv4") connectsock_ipv4 = {
|
||||
.key_size = sizeof(__u64),
|
||||
.value_size = sizeof(void *),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being a pid
|
||||
@@ -56,6 +62,8 @@ struct bpf_map_def SEC("maps/connectsock_ipv6") connectsock_ipv6 = {
|
||||
.key_size = sizeof(__u64),
|
||||
.value_size = sizeof(void *),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being an ipv4_tuple_t
|
||||
@@ -66,6 +74,8 @@ struct bpf_map_def SEC("maps/tuplepid_ipv4") tuplepid_ipv4 = {
|
||||
.key_size = sizeof(struct ipv4_tuple_t),
|
||||
.value_size = sizeof(struct pid_comm_t),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being an ipv6_tuple_t
|
||||
@@ -76,6 +86,8 @@ struct bpf_map_def SEC("maps/tuplepid_ipv6") tuplepid_ipv6 = {
|
||||
.key_size = sizeof(struct ipv6_tuple_t),
|
||||
.value_size = sizeof(struct pid_comm_t),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being a pid
|
||||
@@ -86,6 +98,8 @@ struct bpf_map_def SEC("maps/fdinstall_ret") fdinstall_ret = {
|
||||
.key_size = sizeof(__u64),
|
||||
.value_size = sizeof(unsigned int),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* This is a key/value store with the keys being a pid (tgid)
|
||||
@@ -96,6 +110,8 @@ struct bpf_map_def SEC("maps/fdinstall_pids") fdinstall_pids = {
|
||||
.key_size = sizeof(__u32),
|
||||
.value_size = sizeof(__u32),
|
||||
.max_entries = 1024,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
/* http://stackoverflow.com/questions/1001307/detecting-endianness-programmatically-in-a-c-program */
|
||||
@@ -132,6 +148,8 @@ struct bpf_map_def SEC("maps/tcptracer_status") tcptracer_status = {
|
||||
.key_size = sizeof(__u64),
|
||||
.value_size = sizeof(struct tcptracer_status_t),
|
||||
.max_entries = 1,
|
||||
.pinning = 0,
|
||||
.namespace = "",
|
||||
};
|
||||
|
||||
__attribute__((always_inline))
|
||||
|
||||
13
vendor/manifest
vendored
13
vendor/manifest
vendored
@@ -1016,11 +1016,20 @@
|
||||
"revision": "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75",
|
||||
"branch": "master"
|
||||
},
|
||||
{
|
||||
"importpath": "github.com/iovisor/gobpf/bpffs",
|
||||
"repository": "https://github.com/iovisor/gobpf",
|
||||
"vcs": "git",
|
||||
"revision": "aef06278f7e7142309a608d0e14e69cf94a5adae",
|
||||
"branch": "master",
|
||||
"path": "/bpffs",
|
||||
"notests": true
|
||||
},
|
||||
{
|
||||
"importpath": "github.com/iovisor/gobpf/elf",
|
||||
"repository": "https://github.com/iovisor/gobpf",
|
||||
"vcs": "git",
|
||||
"revision": "23f7ee81c1cc244d16ddc8110c2ec8b8a09d0448",
|
||||
"revision": "aef06278f7e7142309a608d0e14e69cf94a5adae",
|
||||
"branch": "master",
|
||||
"path": "/elf",
|
||||
"notests": true
|
||||
@@ -1462,7 +1471,7 @@
|
||||
"importpath": "github.com/weaveworks/tcptracer-bpf",
|
||||
"repository": "https://github.com/weaveworks/tcptracer-bpf",
|
||||
"vcs": "git",
|
||||
"revision": "783f088bbe3e91d4d23cf2f48072f80de2fd03fc",
|
||||
"revision": "a616ebc6c5ac196af743e5dfc621a52fce030f87",
|
||||
"branch": "master",
|
||||
"notests": true
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user