DevinLeamy commented on code in PR #501:
URL: https://github.com/apache/mesos/pull/501#discussion_r1518170872


##########
src/linux/ebpf.cpp:
##########
@@ -14,4 +14,126 @@
 // 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 <linux/bpf.h>
+#include <sys/syscall.h>
+
+#include <string>
+#include <vector>
+
+#include "stout/error.hpp"
+#include "stout/try.hpp"
+
+using std::string;
+using std::vector;
+
+namespace ebpf {
+
+#define syscall_bpf(cmd, attr, size) (int) syscall(__NR_bpf, cmd, attr, size);
+
+// Wrapper around the BPF syscall.
+Try<int> bpf(int cmd, bpf_attr* attr, size_t size)
+{
+  int result = syscall_bpf(cmd, attr, size);
+  if (result == -1) {
+    ErrnoError error;
+    return Error("BPF syscall failed with error '" + error.message + "'");
+  }
+  return result;
+}
+
+
+Program::Program(bpf_prog_type _type) : type(_type) {}
+
+
+void Program::append(vector<bpf_insn>&& instructions)
+{
+  program.insert(
+    program.end(),
+    std::make_move_iterator(instructions.begin()),
+    std::make_move_iterator(instructions.end()));
+}
+
+
+bpf_attr Program::createAttribute() const
+{
+  bpf_attr attribute;
+  std::memset(&attribute, 0, sizeof(attribute));
+
+  attribute.prog_type = type;
+  attribute.insn_cnt = program.size();
+  attribute.insns = reinterpret_cast<uint64_t>(program.data());
+  attribute.license = reinterpret_cast<uint64_t>("Apache 2.0");
+
+  return attribute;
+}
+
+
+Try<int> load(const Program&& program)
+{
+  bpf_attr attribute = program.createAttribute();
+  string logs = "";
+
+  // The BPF system call is repeated based on whether it succeeds and the
+  // return value. If it succeeds, we don't retry. It it fails with 'EAGAIN'
+  // we retry with the same parameters. If it fails with 'EACCES' - a
+  // verification error - when we retry once with an additional buffer to
+  // capture the verifier error logs.
+  //
+  // We attempt a maximum of 3 times, to avoid an infinite loop if 'EAGAIN' is
+  // returned multiple times.
+  int attempts = 0;
+  bool retry = false;
+  bool retriedWithLogs = false;
+
+  Try<int> fd = 0; // '0' because Try<int> has no default constructor.
+  do {

Review Comment:
   Updated.



-- 
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]

Reply via email to