Eunomia  0.1.0
A lightweight eBPF-based CloudNative Monitor tool for Container Security and Observability
single_prog_update_skel.h
Go to the documentation of this file.
1 #ifndef __HOT_UPDATE_SKEL_H__
2 #define __HOT_UPDATE_SKEL_H__
3 
4 extern "C"
5 {
6 #include <bpf/libbpf.h>
7 #include <stdlib.h>
8 }
9 
10 #include "../base64.h"
11 #include "hot_update.h"
12 
14 {
15  struct bpf_object_skeleton *skeleton;
16  struct bpf_object *obj;
17  struct
18  {
19  struct bpf_map *rb;
20  } maps;
21  struct
22  {
23  struct bpf_program *handle_exec;
24  } progs;
25  struct
26  {
27  struct bpf_link *handle_exec;
28  } links;
29 };
30 
31 static void update_bpf__destroy(struct single_prog_update_bpf *obj)
32 {
33  if (!obj)
34  return;
35  if (obj->skeleton)
36  bpf_object__destroy_skeleton(obj->skeleton);
37  free(obj);
38 }
39 
40 static inline int create_single_prog_skeleton_from_json(
41  struct single_prog_update_bpf *obj,
42  struct ebpf_update_meta_data &ebpf_data)
43 {
44  struct bpf_object_skeleton *s;
45 
46  s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));
47  if (!s)
48  return -1;
49  obj->skeleton = s;
50 
51  s->sz = sizeof(*s);
52  s->name = ebpf_data.name.c_str();
53  s->obj = &obj->obj;
54 
55  /* maps */
56  s->map_cnt = ebpf_data.maps_name.size();
57  s->map_skel_sz = sizeof(*s->maps);
58  s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);
59  if (!s->maps)
60  goto err;
61 
62  for (int i = 0; i < s->map_cnt; i++)
63  {
64  s->maps[i].name = ebpf_data.maps_name[i].c_str();
65  s->maps[i].map = &obj->maps.rb;
66  }
67 
68  /* programs */
69  s->prog_cnt = ebpf_data.progs_name.size();
70  s->prog_skel_sz = sizeof(*s->progs);
71  s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);
72  if (!s->progs)
73  goto err;
74  for (int i = 0; i < s->prog_cnt; i++)
75  {
76  s->progs[i].name = ebpf_data.progs_name[i].c_str();
77  s->progs[i].prog = &obj->progs.handle_exec;
78  s->progs[i].link = &obj->links.handle_exec;
79  }
80 
81  s->data_sz = ebpf_data.data_sz;
82  ebpf_data.base64_decode_buffer = base64_decode((const unsigned char *)ebpf_data.data.c_str(), ebpf_data.data.size());
83  // s->data = ebpf_raw_data;
84  s->data = (void *)ebpf_data.base64_decode_buffer.data();
85 
86  return 0;
87 err:
88  bpf_object__destroy_skeleton(s);
89  return -1;
90 }
91 
92 static inline struct single_prog_update_bpf *single_prog_update_bpf__decode_open(struct ebpf_update_meta_data &ebpf_data)
93 {
95 
96  obj = (struct single_prog_update_bpf *)calloc(1, sizeof(*obj));
97  if (!obj)
98  return NULL;
99  if (create_single_prog_skeleton_from_json(obj, ebpf_data))
100  goto err;
101  if (bpf_object__open_skeleton(obj->skeleton, NULL))
102  goto err;
103 
104  return obj;
105 err:
106  update_bpf__destroy(obj);
107  return NULL;
108 }
109 
110 static inline int update_bpf__load(struct single_prog_update_bpf *obj)
111 {
112  return bpf_object__load_skeleton(obj->skeleton);
113 }
114 
115 static inline int update_bpf__attach(struct single_prog_update_bpf *obj)
116 {
117  return bpf_object__attach_skeleton(obj->skeleton);
118 }
119 
120 static inline void update_bpf__detach(struct single_prog_update_bpf *obj)
121 {
122  return bpf_object__detach_skeleton(obj->skeleton);
123 }
124 
125 #endif /* __HOT_UPDATE_SKEL_H__ */
single_prog_update_bpf::obj
struct bpf_object * obj
Definition: single_prog_update_skel.h:16
single_prog_update_bpf::handle_exec
struct bpf_link * handle_exec
Definition: single_prog_update_skel.h:27
ebpf_update_meta_data::name
std::string name
meta data
Definition: hot_update.h:16
ebpf_update_meta_data::progs_name
std::vector< std::string > progs_name
Definition: hot_update.h:18
ebpf_update_meta_data::data
std::string data
Definition: hot_update.h:20
single_prog_update_bpf::progs
struct single_prog_update_bpf::@1 progs
hot_update.h
ebpf_update_meta_data::base64_decode_buffer
std::vector< char > base64_decode_buffer
buffer to base 64 decode
Definition: hot_update.h:22
single_prog_update_bpf::maps
struct single_prog_update_bpf::@0 maps
single_prog_update_bpf::handle_exec
struct bpf_program * handle_exec
Definition: single_prog_update_skel.h:23
ebpf_update_meta_data::data_sz
int data_sz
Definition: hot_update.h:19
single_prog_update_bpf::rb
struct bpf_map * rb
Definition: single_prog_update_skel.h:19
single_prog_update_bpf
Definition: single_prog_update_skel.h:13
single_prog_update_bpf::links
struct single_prog_update_bpf::@2 links
ebpf_update_meta_data::maps_name
std::vector< std::string > maps_name
Definition: hot_update.h:17
ebpf_update_meta_data
Definition: hot_update.h:13
single_prog_update_bpf::skeleton
struct bpf_object_skeleton * skeleton
Definition: single_prog_update_skel.h:15