Improve RecordVal JSON formatting

No need to create a record introspection table each time when all
the required information can be obtained directly in the RecordVal
and RecordType objects.  Besides the additional overhead, using such
a table will re-order the fields arbitrarily instead of using the
order in which they're defined.
This commit is contained in:
Jon Siwek 2019-09-30 19:04:55 -07:00
parent d258ebe5ca
commit a34ade4876
4 changed files with 25 additions and 24 deletions

View file

@ -498,6 +498,12 @@ public:
return decl && decl->FindAttr(ATTR_DEPRECATED) != 0;
}
bool FieldHasAttr(int field, attr_tag at) const
{
const TypeDecl* decl = FieldDecl(field);
return decl && decl->FindAttr(at) != 0;
}
string GetFieldDeprecationWarning(int field, bool has_check) const;
protected:

View file

@ -582,37 +582,32 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
{
j = ZeekJson::object();
auto* rval = val->AsRecordVal();
TableVal* fields = rval->GetRecordFields();
auto* field_indexes = fields->ConvertToPureList();
int num_indexes = field_indexes->Length();
auto rt = rval->Type()->AsRecordType();
for ( int i = 0; i < num_indexes; ++i )
for ( auto i = 0; i < rt->NumFields(); ++i )
{
Val* key = field_indexes->Index(i);
auto* key_field = fields->Lookup(key)->AsRecordVal();
auto field_name = rt->FieldName(i);
std::string key_string;
auto* key_val = key->AsStringVal();
string key_string;
if ( re->MatchAnywhere(key_val->AsString()) != 0 )
if ( re->MatchAnywhere(field_name) != 0 )
{
StringVal blank("");
key_val = key_val->Substitute(re, &blank, 0)->AsStringVal();
StringVal fn_val(field_name);
auto key_val = fn_val.Substitute(re, &blank, 0)->AsStringVal();
key_string = key_val->ToStdString();
delete key_val;
Unref(key_val);
}
else
key_string = key_val->ToStdString();
key_string = field_name;
Val* value = key_field->Lookup("value", true);
Val* value = rval->LookupWithDefault(i);
if ( value && ( ! only_loggable || key_field->Lookup("log")->AsBool() ) )
if ( value && ( ! only_loggable || rt->FieldHasAttr(i, ATTR_LOG) ) )
j[key_string] = BuildJSON(value, only_loggable, re);
Unref(value);
}
delete fields;
delete field_indexes;
break;
}

View file

@ -1,22 +1,22 @@
http://127.0.0.1:8080/stats/flowentry/clear/42
http://127.0.0.1:8080/stats/flowentry/add
{"priority":0,"idle_timeout":0,"hard_timeout":0,"match":{},"actions":[{"port":3,"type":"OUTPUT"},{"port":7,"type":"OUTPUT"}],"cookie":4398046511105,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511105,"idle_timeout":0,"hard_timeout":0,"priority":0,"flags":0,"match":{},"actions":[{"type":"OUTPUT","port":3},{"type":"OUTPUT","port":7}]}
Flow_mod_success
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"74.53.140.153/32","tp_dst":25,"tp_src":1470,"nw_proto":6,"dl_type":2048,"nw_src":"10.10.1.4/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"10.10.1.4/32","nw_dst":"74.53.140.153/32","tp_src":1470,"tp_dst":25},"actions":[]}
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"10.10.1.4/32","tp_dst":1470,"tp_src":25,"nw_proto":6,"dl_type":2048,"nw_src":"74.53.140.153/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"74.53.140.153/32","nw_dst":"10.10.1.4/32","tp_src":25,"tp_dst":1470},"actions":[]}
Flow_mod_success
Flow_mod_success
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"192.168.133.102/32","tp_dst":25,"tp_src":49648,"nw_proto":6,"dl_type":2048,"nw_src":"192.168.133.100/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"192.168.133.100/32","nw_dst":"192.168.133.102/32","tp_src":49648,"tp_dst":25},"actions":[]}
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"192.168.133.100/32","tp_dst":49648,"tp_src":25,"nw_proto":6,"dl_type":2048,"nw_src":"192.168.133.102/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"192.168.133.102/32","nw_dst":"192.168.133.100/32","tp_src":25,"tp_dst":49648},"actions":[]}
Flow_mod_success
Flow_mod_success
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"17.167.150.73/32","tp_dst":443,"tp_src":49655,"nw_proto":6,"dl_type":2048,"nw_src":"192.168.133.100/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"192.168.133.100/32","nw_dst":"17.167.150.73/32","tp_src":49655,"tp_dst":443},"actions":[]}
http://127.0.0.1:8080/stats/flowentry/add
{"priority":5,"idle_timeout":30,"hard_timeout":0,"match":{"nw_dst":"192.168.133.100/32","tp_dst":49655,"tp_src":443,"nw_proto":6,"dl_type":2048,"nw_src":"17.167.150.73/32"},"actions":[],"cookie":4398046511146,"flags":0,"dpid":42}
{"dpid":42,"cookie":4398046511146,"idle_timeout":30,"hard_timeout":0,"priority":5,"flags":0,"match":{"dl_type":2048,"nw_proto":6,"nw_src":"17.167.150.73/32","nw_dst":"192.168.133.100/32","tp_src":443,"tp_dst":49655},"actions":[]}
Flow_mod_success
Flow_mod_success

View file

@ -19,7 +19,7 @@ true
"fe80::/64"
"Red"
"/^?(^abcd)$?/"
{"s":"test","c":100}
{"c":100,"s":"test"}
{"s":"test"}
{"s":"test"}
{"m":{"s":"test"}}