@@ -101,30 +101,26 @@ class MyMemoryManager : public SectionMemoryManager {
101
101
map<string, tuple<uint8_t *, uintptr_t >> *sections_;
102
102
};
103
103
104
- BPFModule::BPFModule (unsigned flags)
105
- : flags_(flags), ctx_(new LLVMContext) {
104
+ BPFModule::BPFModule (unsigned flags, TableStorage *ts )
105
+ : flags_(flags), ctx_(new LLVMContext), ts_(ts) {
106
106
InitializeNativeTarget ();
107
107
InitializeNativeTargetAsmPrinter ();
108
108
LLVMInitializeBPFTarget ();
109
109
LLVMInitializeBPFTargetMC ();
110
110
LLVMInitializeBPFTargetInfo ();
111
111
LLVMInitializeBPFAsmPrinter ();
112
112
LLVMLinkInMCJIT (); /* call empty function to force linking of MCJIT */
113
+ if (!ts_) {
114
+ local_ts_ = createSharedTableStorage ();
115
+ ts_ = &*local_ts_;
116
+ }
113
117
}
114
118
115
119
BPFModule::~BPFModule () {
116
120
engine_.reset ();
117
121
rw_engine_.reset ();
118
122
ctx_.reset ();
119
- if (tables_) {
120
- for (auto table : *tables_) {
121
- if (table.is_shared ) {
122
- SharedTables::instance ()->remove_fd (table.name );
123
- } else if (!table.is_extern ) {
124
- close (table.fd );
125
- }
126
- }
127
- }
123
+ ts_->DeletePrefix (Path ({std::to_string ((uintptr_t )this )}));
128
124
}
129
125
130
126
static void debug_printf (Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) {
@@ -326,7 +322,8 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
326
322
// load an entire c file as a module
327
323
int BPFModule::load_cfile (const string &file, bool in_memory, const char *cflags[], int ncflags) {
328
324
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
329
- if (clang_loader_->parse (&mod_, &tables_, file, in_memory, cflags, ncflags))
325
+ if (clang_loader_->parse (&mod_, *ts_, file, in_memory, cflags, ncflags,
326
+ std::to_string ((uintptr_t )this )))
330
327
return -1 ;
331
328
return 0 ;
332
329
}
@@ -338,7 +335,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags
338
335
// build an ExecutionEngine.
339
336
int BPFModule::load_includes (const string &text) {
340
337
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
341
- if (clang_loader_->parse (&mod_, &tables_ , text, true , nullptr , 0 ))
338
+ if (clang_loader_->parse (&mod_, *ts_ , text, true , nullptr , 0 , " " ))
342
339
return -1 ;
343
340
return 0 ;
344
341
}
@@ -352,7 +349,10 @@ int BPFModule::annotate() {
352
349
auto m = make_unique<Module>(" sscanf" , *ctx_);
353
350
354
351
size_t id = 0 ;
355
- for (auto &table : *tables_) {
352
+ Path path ({std::to_string ((uintptr_t )this )});
353
+ for (auto it = ts_->lower_bound (path), up = ts_->upper_bound (path); it != up; ++it) {
354
+ TableDesc &table = it->second ;
355
+ tables_.push_back (&it->second );
356
356
table_names_[table.name ] = id++;
357
357
GlobalValue *gvar = mod_->getNamedValue (table.name );
358
358
if (!gvar) continue ;
@@ -507,9 +507,7 @@ unsigned BPFModule::kern_version() const {
507
507
return *(unsigned *)get<0 >(section->second );
508
508
}
509
509
510
- size_t BPFModule::num_tables () const {
511
- return tables_->size ();
512
- }
510
+ size_t BPFModule::num_tables () const { return tables_.size (); }
513
511
514
512
size_t BPFModule::table_id (const string &name) const {
515
513
auto it = table_names_.find (name);
@@ -522,46 +520,52 @@ int BPFModule::table_fd(const string &name) const {
522
520
}
523
521
524
522
int BPFModule::table_fd (size_t id) const {
525
- if (id >= tables_->size ()) return -1 ;
526
- return (*tables_)[id].fd ;
523
+ if (id >= tables_.size ())
524
+ return -1 ;
525
+ return tables_[id]->fd ;
527
526
}
528
527
529
528
int BPFModule::table_type (const string &name) const {
530
529
return table_type (table_id (name));
531
530
}
532
531
533
532
int BPFModule::table_type (size_t id) const {
534
- if (id >= tables_->size ()) return -1 ;
535
- return (*tables_)[id].type ;
533
+ if (id >= tables_.size ())
534
+ return -1 ;
535
+ return tables_[id]->type ;
536
536
}
537
537
538
538
size_t BPFModule::table_max_entries (const string &name) const {
539
539
return table_max_entries (table_id (name));
540
540
}
541
541
542
542
size_t BPFModule::table_max_entries (size_t id) const {
543
- if (id >= tables_->size ()) return 0 ;
544
- return (*tables_)[id].max_entries ;
543
+ if (id >= tables_.size ())
544
+ return 0 ;
545
+ return tables_[id]->max_entries ;
545
546
}
546
547
547
548
int BPFModule::table_flags (const string &name) const {
548
549
return table_flags (table_id (name));
549
550
}
550
551
551
552
int BPFModule::table_flags (size_t id) const {
552
- if (id >= tables_->size ()) return -1 ;
553
- return (*tables_)[id].flags ;
553
+ if (id >= tables_.size ())
554
+ return -1 ;
555
+ return tables_[id]->flags ;
554
556
}
555
557
556
558
const char * BPFModule::table_name (size_t id) const {
557
- if (id >= tables_->size ()) return nullptr ;
558
- return (*tables_)[id].name .c_str ();
559
+ if (id >= tables_.size ())
560
+ return nullptr ;
561
+ return tables_[id]->name .c_str ();
559
562
}
560
563
561
564
const char * BPFModule::table_key_desc (size_t id) const {
562
565
if (b_loader_) return nullptr ;
563
- if (id >= tables_->size ()) return nullptr ;
564
- return (*tables_)[id].key_desc .c_str ();
566
+ if (id >= tables_.size ())
567
+ return nullptr ;
568
+ return tables_[id]->key_desc .c_str ();
565
569
}
566
570
567
571
const char * BPFModule::table_key_desc (const string &name) const {
@@ -570,24 +574,27 @@ const char * BPFModule::table_key_desc(const string &name) const {
570
574
571
575
const char * BPFModule::table_leaf_desc (size_t id) const {
572
576
if (b_loader_) return nullptr ;
573
- if (id >= tables_->size ()) return nullptr ;
574
- return (*tables_)[id].leaf_desc .c_str ();
577
+ if (id >= tables_.size ())
578
+ return nullptr ;
579
+ return tables_[id]->leaf_desc .c_str ();
575
580
}
576
581
577
582
const char * BPFModule::table_leaf_desc (const string &name) const {
578
583
return table_leaf_desc (table_id (name));
579
584
}
580
585
size_t BPFModule::table_key_size (size_t id) const {
581
- if (id >= tables_->size ()) return 0 ;
582
- return (*tables_)[id].key_size ;
586
+ if (id >= tables_.size ())
587
+ return 0 ;
588
+ return tables_[id]->key_size ;
583
589
}
584
590
size_t BPFModule::table_key_size (const string &name) const {
585
591
return table_key_size (table_id (name));
586
592
}
587
593
588
594
size_t BPFModule::table_leaf_size (size_t id) const {
589
- if (id >= tables_->size ()) return 0 ;
590
- return (*tables_)[id].leaf_size ;
595
+ if (id >= tables_.size ())
596
+ return 0 ;
597
+ return tables_[id]->leaf_size ;
591
598
}
592
599
size_t BPFModule::table_leaf_size (const string &name) const {
593
600
return table_leaf_size (table_id (name));
@@ -603,8 +610,9 @@ struct TableIterator {
603
610
};
604
611
605
612
int BPFModule::table_key_printf (size_t id, char *buf, size_t buflen, const void *key) {
606
- if (id >= tables_->size ()) return -1 ;
607
- const TableDesc &desc = (*tables_)[id];
613
+ if (id >= tables_.size ())
614
+ return -1 ;
615
+ const TableDesc &desc = *tables_[id];
608
616
if (!desc.key_snprintf ) {
609
617
fprintf (stderr, " Key snprintf not available\n " );
610
618
return -1 ;
@@ -627,8 +635,9 @@ int BPFModule::table_key_printf(size_t id, char *buf, size_t buflen, const void
627
635
}
628
636
629
637
int BPFModule::table_leaf_printf (size_t id, char *buf, size_t buflen, const void *leaf) {
630
- if (id >= tables_->size ()) return -1 ;
631
- const TableDesc &desc = (*tables_)[id];
638
+ if (id >= tables_.size ())
639
+ return -1 ;
640
+ const TableDesc &desc = *tables_[id];
632
641
if (!desc.leaf_snprintf ) {
633
642
fprintf (stderr, " Key snprintf not available\n " );
634
643
return -1 ;
@@ -651,8 +660,9 @@ int BPFModule::table_leaf_printf(size_t id, char *buf, size_t buflen, const void
651
660
}
652
661
653
662
int BPFModule::table_key_scanf (size_t id, const char *key_str, void *key) {
654
- if (id >= tables_->size ()) return -1 ;
655
- const TableDesc &desc = (*tables_)[id];
663
+ if (id >= tables_.size ())
664
+ return -1 ;
665
+ const TableDesc &desc = *tables_[id];
656
666
if (!desc.key_sscanf ) {
657
667
fprintf (stderr, " Key sscanf not available\n " );
658
668
return -1 ;
@@ -672,8 +682,9 @@ int BPFModule::table_key_scanf(size_t id, const char *key_str, void *key) {
672
682
}
673
683
674
684
int BPFModule::table_leaf_scanf (size_t id, const char *leaf_str, void *leaf) {
675
- if (id >= tables_->size ()) return -1 ;
676
- const TableDesc &desc = (*tables_)[id];
685
+ if (id >= tables_.size ())
686
+ return -1 ;
687
+ const TableDesc &desc = *tables_[id];
677
688
if (!desc.leaf_sscanf ) {
678
689
fprintf (stderr, " Key sscanf not available\n " );
679
690
return -1 ;
@@ -714,7 +725,8 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) {
714
725
return rc;
715
726
716
727
b_loader_.reset (new BLoader (flags_));
717
- if (int rc = b_loader_->parse (&*mod_, filename, proto_filename, &tables_))
728
+ if (int rc =
729
+ b_loader_->parse (&*mod_, filename, proto_filename, *ts_, std::to_string ((uintptr_t )this )))
718
730
return rc;
719
731
if (int rc = annotate ())
720
732
return rc;
0 commit comments