4.3OVSDB部分数据结构分析 该部分描述了OVSDB比较重要的数据结构的内容。 4.3.1 json格式描述 /* Type of a JSON value. / enum json_type { JSON_NULL, / null / JSON_FALSE, / false / JSON_TRUE, / true / JSON_OBJECT, / {“a”: b, “c”: d, …} / JSON_ARRAY, / [1, 2, 3, …] / JSON_INTEGER, / 123. / JSON_REAL, / 123.456. / JSON_STRING, / “…” */ JSON_N_TYPES }; 这个也是ovsdb数据库操作的使用格式描述举例,ovsdb数据库的操作均是基于该格式来进行操作的,所有的json格式转换处理的接口函数均在json.c文件中。
4.3.2 hmap结构及其相关 /* A hash map. */ struct hmap { struct hmap_node buckets; / Must point to ‘one’ iff ‘mask’ == 0. / struct hmap_node one; size_t mask; size_t n; }; / A hash map node, to be embedded inside the data structure being mapped. / struct hmap_node { size_t hash; / Hash value. / struct hmap_node next; / Next in linked list. / }; struct shash { struct hmap map; }; 一个struct hmap_node 的list是由相同hash的hmap_node组成,用buckets来指向,buckets是struct hmap_node的指针数组。其中buckets数组的第一个指针初始化存放在struct hmap_node one里面。mask表示哈希值的取模,通过hash & mask得到hmap_node所在的buckets下标索引。n表示hmap里当前所有struct hmap_node的个数。一般来说,hmap的node个数不应该超过mask的2倍,如果超过了,需要调整mask值,在hmap_expand_at函数中进行调整。 Hmap结构使用hmap_init、hmap_destroy来进行相应的创建初始化和删除,在ovsdb代码中一般使用shash结构对hmap进行封装并使用,相应的封装函数为shash_init和shash_destroy。
4.3.3 server_config结构 struct server_config { struct sset *remotes; struct shash *all_dbs; FILE *config_tmpfile; struct ovsdb_jsonrpc_server *jsonrpc; }; Remotes:存放DB相关的参数,其实主要存储了DB的name,以hash方式存储; All_dbs:将结构体db(该结构体在ovsdb_file_open函数中做好了实例化操作)作为hash参数放入到其中,以hash方式存储; Config_tmpfile:存放DB相关的参数的临时文件,文件内容包括了remotes和dbfilename; Jsonrpc:存放被server服务的DB,最后将上面的db结构中的ovsdb结构体放入到jsonrpc->up.dbs中,以hash方式存储;另外其中的remotes存放的是ovsdb_jsonrpc_remote结构,以hash方式存储在node下的data中;
4.3.4 ovsdb_jsonrpc_remote结构 struct ovsdb_jsonrpc_remote { struct ovsdb_jsonrpc_server *server; struct pstream listener; / Listener, if passive. / struct ovs_list sessions; / List of "struct ovsdb_jsonrpc_session"s. */ uint8_t dscp; }; Server:该结构和ovsdb_jsonrpc_remote相互关联,首先该结构和ovsdb_jsonrpc_remote中的server是同一块区域,其次ovsdb_jsonrpc_remote结构会以void *data的形式添加到ovsdb_jsonrpc_server的remotes哈希map结构的节点中; Listener:跟我们的连接的方式(ptcp、punix、pssl等)有关系的一个pstream变量,该变量和client获取的方式类似; Sessions:ovsdb_jsonrpc_sessions结构的链表,将ovsdb_jsonrpc_session中的node节点插入到该链表中;
4.3.5 ovsdb_jsonrpc_server结构 struct ovsdb_jsonrpc_server { struct ovsdb_server up; unsigned int n_sessions, max_sessions; struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */ }; up:目前没有发现该结构体的用途,因为只有初始化了,但是实际并没有做赋值操作,在注释中显示的是跟ovsdb关联的数据结构体(该结构的实际用途就是将ovsdb结构以hash方式存储在该ovsdb_server中,参见上面的server_config结构的说明); n_sessions, max_sessions:表示了连接的数量和最多连接的数量,默认的链接个数的最大值为300; remotes:该结构用来以哈希方式存储ovsdb_jsonrpc_remote的结构,可以参见上面的ovsdb_jsonrpc_remote结构的描述。
4.3.6 ovsdb_server结构 struct ovsdb_server { struct shash dbs; /* Maps from a db name to a “struct ovsdb *”. / struct hmap locks; / Contains "struct ovsdb_lock"s indexed by name. */ }; dbs:该结构中存储了从ovsdb中读取的相关数据值; locks:跟ovsdb的lock相关的变量。
4.3.7 ovsdb_jsonrpc_session结构 struct ovsdb_jsonrpc_session { struct ovs_list node; /* Element in remote’s sessions list. */ struct ovsdb_session up; struct ovsdb_jsonrpc_remote *remote;
/* Triggers. */ struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */ /* Monitors. */ struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */ /* Network connectivity. */ struct jsonrpc_session *js; /* JSON-RPC session. */ unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */}; node:该节点为remote session的元素,最终该节点会被挂接到ovsdb_jsonrpc_remote结构中的sessions中去; up:该结构和ovsdb_server类似,只是该节点表示client; remote:该结构和ovsdb_jsonrpc_remote关联起来; triggers:ovsdb_jsonrpc_trigger结构体的hash map; monitors:ovsdb_jsonrpc_monitor结构体的hash map; js:jsonrpc的session,该结构会和初始化好的jsonrpc_session关联起来; js_seqno:用于存储上次的jsonrpc_session_get_seqno值,初始化时为0。
4.3.8 jsonrpc_session结构 struct jsonrpc_session { struct reconnect *reconnect; struct jsonrpc *rpc; struct stream *stream; struct pstream *pstream; int last_error; unsigned int seqno; uint8_t dscp; }; Reconnect:暂时不知道用途; rpc:jsonrpc相关的结构,该结构会在client的描述中进行描述说明; stream:active stream connection相关的结构; pstream:passive listener相关的结构; last_error:应该是用于错误信息,暂时不知道其用途; seqno:用于存储上次的jsonrpc_session_get_seqno值,初始化时为0; sdcp:暂时不知道其用途。
4.3.9 ovsdb_session结构 struct ovsdb_session { struct ovsdb_server server; struct ovs_list completions;/ Completed triggers. / struct hmap waiters; / "ovsdb_lock_waiter *"s by lock name. */ }; server:该结构会和ovsdb_jsonrpc_server中的ovsdb_server关联起来; completions:从注释来看用于trigger的链表,暂时没有发现用途; waiters:从注释来看用于ovsdb_lock_waiter的链表,暂时没有发现用途。
4.3.10 json_array结构 struct json_array { size_t n, n_allocated; struct json **elems; }; Json array的存储结构; n,n_allocated:包含有的json array的element的个数和分配的array的个数; elems:json array的元素指针;
4.3.11 json结构 struct json { enum json_type type; union { struct shash object; / Contains "struct json *"s. */ struct json_array array; long long int integer; double real; char *string; } u; }; Type:json的type类型; object:json object的存储结构; array:json array的存储结构; integer:json int的存储结构; real:json real的存储结构; string:json string的存储结构。
4.3.12 ovsdb_idl结构 struct ovsdb_idl { const struct ovsdb_idl_class *class; struct jsonrpc_session *session; struct shash table_by_name; struct ovsdb_idl_table tables; / Contains "struct ovsdb_idl_table "s./ unsigned int change_seqno; bool verify_write_only;
/* Session state. */ unsigned int state_seqno; enum ovsdb_idl_state state; struct json *request_id; /* Database locking. */ char *lock_name; /* Name of lock we need, NULL if none. */ bool has_lock; /* Has db server told us we have the lock? */ bool is_lock_contended; /* Has db server told us we can't get lock? */ struct json *lock_request_id; /* JSON-RPC ID of in-flight lock request. */ /* Transaction support. */ struct ovsdb_idl_txn *txn; struct hmap outstanding_txns;}; class:ovsdb_idl_class,初始化好的全局变量; session:用于jsonrpc会话的结构; table_by_name:用于连接ovsdb_idl_talbe的shash结构; tables:包含了idl的table; change_seqno:用于描述table内容发生变更的sequence number,每次变更会做自加操作; verify_write_only:暂不明白用途; state_suqno:用于session连接的state,每次connect或者disconnect都会做修改,与session的sequence number保持一致; state:暂不明白用途; request_id:jsonrpc的request id值; lock_name:用于lock机制; has_lock:ovsdb server告知client是否拥有lock; is_lock_contended:ovsdb server告知lock是否connected; lock_request_id:获取lock时的request id值; txn:用于transaction的结构; outstanding_txns:每次transaction完成后将ovsdb_idl_txn的hmap_node insert其中;
4.3.13 ovsdb_idl_txn结构 struct ovsdb_idl_txn { struct hmap_node hmap_node; struct json *request_id; struct ovsdb_idl *idl; struct hmap txn_rows; enum ovsdb_idl_txn_status status; char *error; bool dry_run; struct ds comment;
/* Increments. */ const char *inc_table; const char *inc_column; struct uuid inc_row; unsigned int inc_index; int64_t inc_new_value; /* Inserted rows. */ struct hmap inserted_rows; /* Contains "struct ovsdb_idl_txn_insert"s. */}; hmap_node:insert到ovsdb_idl的outstanding_txns的节点; request id:用于jsonrpc request id值; idl:用于和ovsdb_idl关联的结构; txn_rows:将ovsdb_idl_row insert其中的hmap结构; status:暂不明白用途; error:赞不明白用途; dry_run:暂不明白其用途; inc_table:暂不明白用途 inc_column:暂不明白用途; inc_row:暂不明白用途; inc_index:暂不明白用途; inc_new_value:暂不明白用途; inserted_rows:当有新的row需要insert到database的时候,会生成ovsdb_idl_txn_insert结构并将其hmap_node insert其中;
4.3.14 ovsdb_idl_row结构 struct ovsdb_idl_row { struct hmap_node hmap_node; /* In struct ovsdb_idl_table’s ‘rows’. / struct uuid uuid; / Row “_uuid” field. / struct ovs_list src_arcs; / Forward arcs (ovsdb_idl_arc.src_node). / struct ovs_list dst_arcs; / Backward arcs (ovsdb_idl_arc.dst_node). */ struct ovsdb_idl_table table; / Containing table. */ struct ovsdb_datum old; / Committed data (null if orphaned). */
/* Transactional data. */ struct ovsdb_datum *new; /* Modified data (null to delete row). */ unsigned long int *prereqs; /* Bitmap of columns to verify in "old". */ unsigned long int *written; /* Bitmap of columns from "new" to write. */ struct hmap_node txn_node; /* Node in ovsdb_idl_txn's list. */ unsigned int change_seqno[OVSDB_IDL_CHANGE_MAX]; struct ovs_list track_node;}; hmap_node:insert到ovsdb_idl_table的rows的节点; uuid:每一行的uuid值,实际上是维护的本地的uuid值,它与ovsdb server的uuid值不同,但是每次commit之后,它就与ovsdb_server的值同步了; src_arcs:暂不明白其用途; dst_arcs:暂不明白其用途; table:与ovsdb_idl_table关联起来; old:用于存储row中的数据,但是commit之后,该值是与database中的值保持同步的; new:用于transaction的数据,该值是即将commit到database server的数据,transaction之后会destroy掉; prereqs:暂不明白其用途; written:用于commit时的bitmap值; txn_node:insert到ovsdb_idl_txn中的txn_rows结构的节点; change_seqno:暂不明白其用途; track_node:暂不明白其用途;
4.3.15 ovsdb_idl_table结构 struct ovsdb_idl_table { const struct ovsdb_idl_table_class class; unsigned char modes; / OVSDB_IDL_ bitmasks, indexed by column. / bool need_table; / Monitor table even if no columns are selected * for replication. / struct shash columns; / Contains "const struct ovsdb_idl_column *"s. / struct hmap rows; / Contains "struct ovsdb_idl_row"s. */ struct ovsdb_idl idl; / Containing idl. / unsigned int change_seqno[OVSDB_IDL_CHANGE_MAX]; struct ovs_list track_list; / Tracked rows (ovsdb_idl_row.track_node). */ }; Class:每个table的class结构,根据schema定义好的全局变量; modes:暂不明白其用途; need_table:用于monitor时的标志位,调用ovsdb_idl_add_table时生效; Columns:将ovsdb_idl_column以column name为索引插入到其中的结构; rows:将ovsdb_idl_row的hmap_node插入其中的结构; idl:ovsdb_idl结构,和其关联起来; change_seqno:暂不明白其用途; track_list:暂不明白其用途;
4.3.16 ovsdb_idl_column结构 struct ovsdb_idl_column { char *name; struct ovsdb_type type; bool mutable; void (*parse)(struct ovsdb_idl_row *, const struct ovsdb_datum *); void (*unparse)(struct ovsdb_idl_row *); }; Name:column的name; Type:column的type; Mutable:数据库选项; (*parse)(struct ovsdb_idl_row *, const struct ovsdb_datum *):根据schema定义好的函数; (*unparse)(struct ovsdb_idl_row *):根据schema定义好的函数;
4.3.17 ovsdb_idl_class结构 struct ovsdb_idl_class { const char database; / for this database. */ const struct ovsdb_idl_table_class *tables; size_t n_tables; }; Database:数据库的name; Tables:对应的table的class; n_tables:每个database中table的数量; 该结构是全局定义好的一个数组结构;
4.3.18 ovsdb_idl_table_class结构 struct ovsdb_idl_table_class { char *name; bool is_root; const struct ovsdb_idl_column *columns; size_t n_columns; size_t allocation_size; void (*row_init)(struct ovsdb_idl_row *); }; Name:table的name; is_root:is_root标志位; columns:和ovsdb_idl_column关联的结构; n_columns:table中对应的column的数量; allocation_size:分配的column的数量; (*row_init)(struct ovsdb_idl_row *):初始化ovsdb_idl_row的函数;
4.3.19 ovsdb_datum结构 struct ovsdb_datum { unsigned int n; /* Number of ‘keys’ and ‘values’. */ union ovsdb_atom keys; / Each of the ovsdb_type’s ‘key_type’. */ union ovsdb_atom values; / Each of the ovsdb_type’s ‘value_type’. */ }; N:key vlaue的数量; Keys:对应的每一个column的type类型; Values:每一个column对应的值;
