forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request iovisor#1840 from oriolarcas/feature/footer-header
Added new footer.h header where BPF_LICENSE is set if not defined
- Loading branch information
Showing
11 changed files
with
295 additions
and
87 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
R"********( | ||
/* | ||
* Copyright (c) 2018 Clevernet, Inc. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
#ifndef BPF_LICENSE | ||
/* No license defined, using GPL | ||
* You can define your own BPF_LICENSE in your C code */ | ||
#define BPF_LICENSE GPL | ||
#endif | ||
#define ___LICENSE(s) #s | ||
#define __LICENSE(s) ___LICENSE(s) | ||
#define _LICENSE __LICENSE(BPF_LICENSE) | ||
char _license[] SEC("license") = _LICENSE; | ||
|
||
)********" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) 2018 Clevernet, Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License") | ||
|
||
import unittest | ||
from bcc import BPF | ||
|
||
class TestLicense(unittest.TestCase): | ||
gpl_only_text = """ | ||
#include <uapi/linux/ptrace.h> | ||
struct gpl_s { | ||
u64 ts; | ||
}; | ||
BPF_PERF_OUTPUT(events); | ||
int license_program(struct pt_regs *ctx) { | ||
struct gpl_s data = {}; | ||
data.ts = bpf_ktime_get_ns(); | ||
events.perf_submit(ctx, &data, sizeof(data)); | ||
return 0; | ||
} | ||
""" | ||
|
||
proprietary_text = """ | ||
#include <uapi/linux/ptrace.h> | ||
struct key_t { | ||
u64 ip; | ||
u32 pid; | ||
u32 uid; | ||
char comm[16]; | ||
}; | ||
BPF_HASH(counts, struct key_t); | ||
int license_program(struct pt_regs *ctx) { | ||
struct key_t key = {}; | ||
u64 zero = 0 , *val; | ||
u64 pid = bpf_get_current_pid_tgid(); | ||
u32 uid = bpf_get_current_uid_gid(); | ||
key.ip = PT_REGS_IP(ctx); | ||
key.pid = pid & 0xFFFFFFFF; | ||
key.uid = uid & 0xFFFFFFFF; | ||
bpf_get_current_comm(&(key.comm), 16); | ||
val = counts.lookup_or_init(&key, &zero); // update counter | ||
(*val)++; | ||
return 0; | ||
} | ||
""" | ||
|
||
def license(self, lic): | ||
return ''' | ||
#define BPF_LICENSE %s | ||
''' % (lic) | ||
|
||
def load_bpf_code(self, bpf_code): | ||
event_name = bpf_code.get_syscall_fnname("read") | ||
bpf_code.attach_kprobe(event=event_name, fn_name="license_program") | ||
bpf_code.detach_kprobe(event=event_name) | ||
|
||
def test_default(self): | ||
b = BPF(text=self.gpl_only_text) | ||
self.load_bpf_code(b) | ||
|
||
def test_gpl_helper_macro(self): | ||
b = BPF(text=self.gpl_only_text + self.license('GPL')) | ||
self.load_bpf_code(b) | ||
|
||
def test_proprietary_macro(self): | ||
b = BPF(text=self.proprietary_text + self.license('Proprietary')) | ||
self.load_bpf_code(b) | ||
|
||
def test_gpl_compatible_macro(self): | ||
b = BPF(text=self.gpl_only_text + self.license('Dual BSD/GPL')) | ||
self.load_bpf_code(b) | ||
|
||
def test_proprietary_words_macro(self): | ||
b = BPF(text=self.proprietary_text + self.license('Proprietary license')) | ||
self.load_bpf_code(b) | ||
|
||
@unittest.expectedFailure | ||
def test_cflags_fail(self): | ||
b = BPF(text=self.gpl_only_text, cflags=["-DBPF_LICENSE=GPL"]) | ||
self.load_bpf_code(b) | ||
|
||
@unittest.expectedFailure | ||
def test_cflags_macro_fail(self): | ||
b = BPF(text=self.gpl_only_text + self.license('GPL'), cflags=["-DBPF_LICENSE=GPL"]) | ||
self.load_bpf_code(b) | ||
|
||
@unittest.expectedFailure | ||
def test_empty_fail_macro(self): | ||
b = BPF(text=self.gpl_only_text + self.license('')) | ||
self.load_bpf_code(b) | ||
|
||
@unittest.expectedFailure | ||
def test_proprietary_fail_macro(self): | ||
b = BPF(text=self.gpl_only_text + self.license('Proprietary license')) | ||
self.load_bpf_code(b) | ||
|
||
@unittest.expectedFailure | ||
def test_proprietary_cflags_fail(self): | ||
b = BPF(text=self.proprietary_text, cflags=["-DBPF_LICENSE=Proprietary"]) | ||
self.load_bpf_code(b) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |