| 71 | } |
| 72 | |
| 73 | CommandResponse BPF::CommandAdd(const bess::pb::BPFArg &arg) { |
| 74 | for (const auto &f : arg.filters()) { |
| 75 | if (f.gate() < 0 || f.gate() >= MAX_GATES) { |
| 76 | return CommandFailure(EINVAL, "Invalid gate"); |
| 77 | } |
| 78 | |
| 79 | bess::utils::Filter filter; |
| 80 | filter.priority = f.priority(); |
| 81 | filter.gate = f.gate(); |
| 82 | filter.exp = f.filter(); |
| 83 | |
| 84 | struct bpf_program il; |
| 85 | if (pcap_compile_nopcap(SNAPLEN, DLT_EN10MB, // Ethernet |
| 86 | &il, filter.exp.c_str(), |
| 87 | 1, // optimize (IL only) |
| 88 | PCAP_NETMASK_UNKNOWN) == -1) { |
| 89 | return CommandFailure(EINVAL, "BPF compilation error"); |
| 90 | } |
| 91 | |
| 92 | #ifdef __x86_64 |
| 93 | filter.func = |
| 94 | bess::utils::bpf_jit_compile(il.bf_insns, il.bf_len, &filter.mmap_size); |
| 95 | pcap_freecode(&il); |
| 96 | if (!filter.func) { |
| 97 | return CommandFailure(ENOMEM, "BPF JIT compilation error"); |
| 98 | } |
| 99 | #else |
| 100 | filter.il_code = il; |
| 101 | #endif |
| 102 | |
| 103 | filters_.push_back(filter); |
| 104 | } |
| 105 | |
| 106 | std::sort(filters_.begin(), filters_.end(), |
| 107 | [](const bess::utils::Filter &a, const bess::utils::Filter &b) { |
| 108 | // descending order of priority number |
| 109 | return b.priority < a.priority; |
| 110 | }); |
| 111 | |
| 112 | return CommandSuccess(); |
| 113 | } |
| 114 | |
| 115 | CommandResponse BPF::CommandClear(const bess::pb::EmptyArg &) { |
| 116 | DeInit(); |
nothing calls this directly
no test coverage detected