DevinLeamy commented on code in PR #501:
URL: https://github.com/apache/mesos/pull/501#discussion_r1517860015
##########
src/linux/ebpf.cpp:
##########
@@ -14,4 +14,99 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "linux/ebpf.hpp"
\ No newline at end of file
+#include "linux/ebpf.hpp"
+
+#include <errno.h>
+#include <linux/bpf.h>
+#include <sys/syscall.h>
+
+#include <cstdlib>
+#include <cstring>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "stout/foreach.hpp"
+#include "stout/os.hpp"
+
+using std::ostream;
+using std::string;
+using std::vector;
+
+namespace ebpf {
+
+namespace internal {
+
+// Wrapper around the `bpf` syscall.
+Try<int> syscall_bpf(int cmd, union bpf_attr* attr, size_t size)
+{
+ int result = syscall(__NR_bpf, cmd, attr, size);
+ if (result == -1) {
+ return Error(
+ "BFP syscall failed with error '" + string(std::strerror(errno)) + "'");
+ }
+ return result;
+}
+
+
+uint64_t pointer_to_int(const void* pointer)
+{
+ return (uint64_t)(unsigned long)pointer;
+}
+
+} // namespace internal {
+
+ProgramBuilder::ProgramBuilder(enum bpf_prog_type _type) : type(_type) {}
+
+
+void ProgramBuilder::append(std::vector<struct bpf_insn> instructions)
+{
+ program.insert(program.end(), instructions.begin(), instructions.end());
+}
+
+
+size_t ProgramBuilder::instructions() const { return program.size(); }
+
+
+Program ProgramBuilder::build()
+{
+ union bpf_attr attribute;
+ std::memset(&attribute, 0, sizeof(attribute));
+
+ attribute.prog_type = type;
+ attribute.insn_cnt = instructions();
+ attribute.insns = internal::pointer_to_int(program.data());
+ attribute.license = internal::pointer_to_int("Apache 2.0");
+
+ return Program{.attribute = attribute};
+}
+
+
+Try<int> load(const Program& program, bool logErrors)
+{
+ union bpf_attr attribute = program.attribute;
+ Try<int> fd = 0; // placeholder because Try<int>() DNE
+ string logs = "";
+
+ if (logErrors) {
+ const int LOG_SIZE = 8196;
+ const int LOG_LEVEL = 1;
+
+ char* log_buffer = (char*)malloc(LOG_SIZE);
+ attribute.log_level = LOG_LEVEL;
+ attribute.log_buf = internal::pointer_to_int(log_buffer);
+ attribute.log_size = LOG_SIZE;
+ fd = internal::syscall_bpf(BPF_PROG_LOAD, &attribute, sizeof(attribute));
+ logs = ": " + string(log_buffer);
+ } else {
+ fd = internal::syscall_bpf(BPF_PROG_LOAD, &attribute, sizeof(attribute));
+ }
+
+ if (fd.isError()) {
+ return Error("BPF syscall failed" + logs + ": " + fd.error());
+ }
Review Comment:
Updated to work similarly to how you described. We attempt and then retry
with logs if there's a validator error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]