16
16
#include <libbpf.h>
17
17
#include <sys/types.h>
18
18
#include <sys/stat.h>
19
+ #include <sys/mman.h>
19
20
#include <unistd.h>
20
21
21
22
#include "btf.h"
@@ -261,14 +262,16 @@ static int codegen(const char *template, ...)
261
262
static int do_skeleton (int argc , char * * argv )
262
263
{
263
264
char header_guard [MAX_OBJ_NAME_LEN + sizeof ("__SKEL_H__" )];
264
- size_t i , map_cnt = 0 , prog_cnt = 0 ;
265
- char obj_name [MAX_OBJ_NAME_LEN ];
265
+ size_t i , map_cnt = 0 , prog_cnt = 0 , file_sz , mmap_sz ;
266
+ DECLARE_LIBBPF_OPTS (bpf_object_open_opts , opts );
267
+ char obj_name [MAX_OBJ_NAME_LEN ], * obj_data ;
268
+ struct bpf_object * obj = NULL ;
266
269
const char * file , * ident ;
267
270
struct bpf_program * prog ;
268
- struct bpf_object * obj ;
271
+ int fd , len , err = -1 ;
269
272
struct bpf_map * map ;
270
273
struct btf * btf ;
271
- int err = -1 ;
274
+ struct stat st ;
272
275
273
276
if (!REQ_ARGS (1 )) {
274
277
usage ();
@@ -281,14 +284,31 @@ static int do_skeleton(int argc, char **argv)
281
284
return -1 ;
282
285
}
283
286
284
- obj = bpf_object__open_file (file , NULL );
285
- if (IS_ERR (obj )) {
286
- p_err ("failed to open BPF object file: %ld" , PTR_ERR (obj ));
287
+ if (stat (file , & st )) {
288
+ p_err ("failed to stat() %s: %s" , file , strerror (errno ));
287
289
return -1 ;
288
290
}
289
-
291
+ file_sz = st .st_size ;
292
+ mmap_sz = roundup (file_sz , sysconf (_SC_PAGE_SIZE ));
293
+ fd = open (file , O_RDONLY );
294
+ if (fd < 0 ) {
295
+ p_err ("failed to open() %s: %s" , file , strerror (errno ));
296
+ return -1 ;
297
+ }
298
+ obj_data = mmap (NULL , mmap_sz , PROT_READ , MAP_PRIVATE , fd , 0 );
299
+ if (obj_data == MAP_FAILED ) {
300
+ obj_data = NULL ;
301
+ p_err ("failed to mmap() %s: %s" , file , strerror (errno ));
302
+ goto out ;
303
+ }
290
304
get_obj_name (obj_name , file );
291
- get_header_guard (header_guard , obj_name );
305
+ opts .object_name = obj_name ;
306
+ obj = bpf_object__open_mem (obj_data , file_sz , & opts );
307
+ if (IS_ERR (obj )) {
308
+ obj = NULL ;
309
+ p_err ("failed to open BPF object file: %ld" , PTR_ERR (obj ));
310
+ goto out ;
311
+ }
292
312
293
313
bpf_object__for_each_map (map , obj ) {
294
314
ident = get_map_ident (map );
@@ -303,8 +323,11 @@ static int do_skeleton(int argc, char **argv)
303
323
prog_cnt ++ ;
304
324
}
305
325
326
+ get_header_guard (header_guard , obj_name );
306
327
codegen ("\
307
328
\n\
329
+ /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\
330
+ \n\
308
331
/* THIS FILE IS AUTOGENERATED! */ \n\
309
332
#ifndef %2$s \n\
310
333
#define %2$s \n\
@@ -356,19 +379,95 @@ static int do_skeleton(int argc, char **argv)
356
379
\n\
357
380
}; \n\
358
381
\n\
359
- static inline struct bpf_object_skeleton * \n\
360
- %1$s__create_skeleton(struct %1$s *obj, struct bpf_embed_data *embed)\n\
382
+ static void \n\
383
+ %1$s__destroy(struct %1$s *obj) \n\
384
+ { \n\
385
+ if (!obj) \n\
386
+ return; \n\
387
+ if (obj->skeleton) \n\
388
+ bpf_object__destroy_skeleton(obj->skeleton);\n\
389
+ free(obj); \n\
390
+ } \n\
391
+ \n\
392
+ static inline int \n\
393
+ %1$s__create_skeleton(struct %1$s *obj); \n\
394
+ \n\
395
+ static inline struct %1$s * \n\
396
+ %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
397
+ { \n\
398
+ struct %1$s *obj; \n\
399
+ \n\
400
+ obj = calloc(1, sizeof(*obj)); \n\
401
+ if (!obj) \n\
402
+ return NULL; \n\
403
+ if (%1$s__create_skeleton(obj)) \n\
404
+ goto err; \n\
405
+ if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
406
+ goto err; \n\
407
+ \n\
408
+ return obj; \n\
409
+ err: \n\
410
+ %1$s__destroy(obj); \n\
411
+ return NULL; \n\
412
+ } \n\
413
+ \n\
414
+ static inline struct %1$s * \n\
415
+ %1$s__open(void) \n\
416
+ { \n\
417
+ return %1$s__open_opts(NULL); \n\
418
+ } \n\
419
+ \n\
420
+ static inline int \n\
421
+ %1$s__load(struct %1$s *obj) \n\
422
+ { \n\
423
+ return bpf_object__load_skeleton(obj->skeleton); \n\
424
+ } \n\
425
+ \n\
426
+ static inline struct %1$s * \n\
427
+ %1$s__open_and_load(void) \n\
428
+ { \n\
429
+ struct %1$s *obj; \n\
430
+ \n\
431
+ obj = %1$s__open(); \n\
432
+ if (!obj) \n\
433
+ return NULL; \n\
434
+ if (%1$s__load(obj)) { \n\
435
+ %1$s__destroy(obj); \n\
436
+ return NULL; \n\
437
+ } \n\
438
+ return obj; \n\
439
+ } \n\
440
+ \n\
441
+ static inline int \n\
442
+ %1$s__attach(struct %1$s *obj) \n\
443
+ { \n\
444
+ return bpf_object__attach_skeleton(obj->skeleton); \n\
445
+ } \n\
446
+ \n\
447
+ static inline void \n\
448
+ %1$s__detach(struct %1$s *obj) \n\
449
+ { \n\
450
+ return bpf_object__detach_skeleton(obj->skeleton); \n\
451
+ } \n\
452
+ " ,
453
+ obj_name
454
+ );
455
+
456
+ codegen ("\
457
+ \n\
458
+ \n\
459
+ static inline int \n\
460
+ %1$s__create_skeleton(struct %1$s *obj) \n\
361
461
{ \n\
362
462
struct bpf_object_skeleton *s; \n\
363
463
\n\
364
464
s = calloc(1, sizeof(*s)); \n\
365
465
if (!s) \n\
366
- return NULL; \n\
466
+ return -1; \n\
467
+ obj->skeleton = s; \n\
367
468
\n\
368
469
s->sz = sizeof(*s); \n\
369
470
s->name = \"%1$s\"; \n\
370
- s->data = embed->data; \n\
371
- s->data_sz = embed->size; \n\
372
471
s->obj = &obj->obj; \n\
373
472
" ,
374
473
obj_name
@@ -438,90 +537,45 @@ static int do_skeleton(int argc, char **argv)
438
537
codegen ("\
439
538
\n\
440
539
\n\
441
- return s; \n\
442
- err: \n\
443
- bpf_object__destroy_skeleton(s); \n\
444
- return NULL; \n\
445
- } \n\
446
- \n\
447
- static void \n\
448
- %1$s__destroy(struct %1$s *obj) \n\
449
- { \n\
450
- if (!obj) \n\
451
- return; \n\
452
- if (obj->skeleton) \n\
453
- bpf_object__destroy_skeleton(obj->skeleton);\n\
454
- free(obj); \n\
455
- } \n\
456
- \n\
457
- static inline struct %1$s * \n\
458
- %1$s__open_opts(struct bpf_embed_data *embed, const struct bpf_object_open_opts *opts)\n\
459
- { \n\
460
- struct %1$s *obj; \n\
461
- \n\
462
- obj = calloc(1, sizeof(*obj)); \n\
463
- if (!obj) \n\
464
- return NULL; \n\
465
- \n\
466
- obj->skeleton = %1$s__create_skeleton(obj, embed); \n\
467
- if (!obj->skeleton) \n\
468
- goto err; \n\
469
- \n\
470
- if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
471
- goto err; \n\
540
+ s->data_sz = %d; \n\
541
+ s->data = \"\\ \n\
542
+ " ,
543
+ file_sz );
544
+
545
+ /* embed contents of BPF object file */
546
+ for (i = 0 , len = 0 ; i < file_sz ; i ++ ) {
547
+ int w = obj_data [i ] ? 4 : 2 ;
548
+
549
+ len += w ;
550
+ if (len > 78 ) {
551
+ printf ("\\\n" );
552
+ len = w ;
553
+ }
554
+ if (!obj_data [i ])
555
+ printf ("\\0" );
556
+ else
557
+ printf ("\\x%02x" , (unsigned char )obj_data [i ]);
558
+ }
559
+
560
+ codegen ("\
561
+ \n\
562
+ \"; \n\
472
563
\n\
473
- return obj ; \n\
564
+ return 0 ; \n\
474
565
err: \n\
475
- %1$s__destroy(obj); \n\
476
- return NULL; \n\
477
- } \n\
478
- \n\
479
- static inline struct %1$s * \n\
480
- %1$s__open(struct bpf_embed_data *embed) \n\
481
- { \n\
482
- return %1$s__open_opts(embed, NULL); \n\
483
- } \n\
484
- \n\
485
- static inline int \n\
486
- %1$s__load(struct %1$s *obj) \n\
487
- { \n\
488
- return bpf_object__load_skeleton(obj->skeleton); \n\
489
- } \n\
490
- \n\
491
- static inline struct %1$s * \n\
492
- %1$s__open_and_load(struct bpf_embed_data *embed) \n\
493
- { \n\
494
- struct %1$s *obj; \n\
495
- \n\
496
- obj = %1$s__open(embed); \n\
497
- if (!obj) \n\
498
- return NULL; \n\
499
- if (%1$s__load(obj)) { \n\
500
- %1$s__destroy(obj); \n\
501
- return NULL; \n\
502
- } \n\
503
- return obj; \n\
504
- } \n\
505
- \n\
506
- static inline int \n\
507
- %1$s__attach(struct %1$s *obj) \n\
508
- { \n\
509
- return bpf_object__attach_skeleton(obj->skeleton); \n\
510
- } \n\
511
- \n\
512
- static inline void \n\
513
- %1$s__detach(struct %1$s *obj) \n\
514
- { \n\
515
- return bpf_object__detach_skeleton(obj->skeleton); \n\
566
+ bpf_object__destroy_skeleton(s); \n\
567
+ return -1; \n\
516
568
} \n\
517
569
\n\
518
570
#endif /* %2$s */ \n\
519
571
" ,
520
- obj_name , header_guard
521
- );
572
+ obj_name , header_guard );
522
573
err = 0 ;
523
574
out :
524
575
bpf_object__close (obj );
576
+ if (obj_data )
577
+ munmap (obj_data , mmap_sz );
578
+ close (fd );
525
579
return err ;
526
580
}
527
581
0 commit comments