上一节我们讲了ovs-vswitchd,其中虚拟网桥初始化的时候,对调用内核模块来添加虚拟网卡。
我们从openvswitch内核模块的加载过程,来看这个过程。
在datapath/datapath.c中会调用module_init(dp_init);来初始化内核模块。
-
static int __init dp_init(void) -
-
-
-
BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); -
-
pr_info("Open vSwitch switching datapath %s\n", VERSION); -
-
-
-
-
-
err = action_fifos_init(); -
-
-
-
err = ovs_internal_dev_rtnl_link_register(); -
-
goto error_action_fifos_exit; -
-
-
-
goto error_unreg_rtnl_link; -
-
-
-
-
-
err = register_pernet_device(&ovs_net_ops); -
-
-
-
err = register_netdevice_notifier(&ovs_dp_device_notifier); -
-
-
-
-
-
goto error_unreg_notifier; -
-
err = dp_register_genl(); -
-
-
-
-
-
-
-
-
unregister_netdevice_notifier(&ovs_dp_device_notifier); -
-
unregister_pernet_device(&ovs_net_ops); -
-
-
-
-
-
ovs_internal_dev_rtnl_link_unregister(); -
-
-
-
-
-
-
|
其中比较重要的是调用了dp_register_genl(),这个就是注册netlink函数,从而ovs-vswitchd可以通过netlink调用内核。
-
static int dp_register_genl(void) -
-
-
-
-
for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { -
-
err = genl_register_family(dp_genl_families[i]); -
-
-
-
-
-
-
-
-
-
|
这里dp_genl_families由四个netlink的family组成
-
static struct genl_family *dp_genl_families[] = { -
&dp_datapath_genl_family, -
-
-
-
|
其中分别定义了以下的操作:
Family名称 | .name | .ops |
dp_datapath_genl_family | -
static struct genl_family dp_datapath_genl_family = { -
-
.hdrsize = sizeof(struct ovs_header), -
.name = OVS_DATAPATH_FAMILY, -
.version = OVS_DATAPATH_VERSION, -
.maxattr = OVS_DP_ATTR_MAX, -
-
-
.ops = dp_datapath_genl_ops, -
.n_ops = ARRAY_SIZE(dp_datapath_genl_ops), -
.mcgrps = &ovs_dp_datapath_multicast_group, -
-
| -
static struct genl_ops dp_datapath_genl_ops[] = { -
-
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
.policy = datapath_policy, -
-
-
-
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
.policy = datapath_policy, -
-
-
-
.flags = 0, /* OK for unprivileged users. */ -
.policy = datapath_policy, -
-
.dumpit = ovs_dp_cmd_dump -
-
-
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
.policy = datapath_policy, -
-
-
|
dp_vport_genl_family | -
struct genl_family dp_vport_genl_family = { -
-
.hdrsize = sizeof(struct ovs_header), -
.name = OVS_VPORT_FAMILY, -
.version = OVS_VPORT_VERSION, -
.maxattr = OVS_VPORT_ATTR_MAX, -
-
-
.ops = dp_vport_genl_ops, -
.n_ops = ARRAY_SIZE(dp_vport_genl_ops), -
.mcgrps = &ovs_dp_vport_multicast_group, -
-
| -
static struct genl_ops dp_vport_genl_ops[] = { -
{ .cmd = OVS_VPORT_CMD_NEW, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
.doit = ovs_vport_cmd_new -
-
{ .cmd = OVS_VPORT_CMD_DEL, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
.doit = ovs_vport_cmd_del -
-
{ .cmd = OVS_VPORT_CMD_GET, -
.flags = 0, /* OK for unprivileged users. */ -
-
.doit = ovs_vport_cmd_get, -
.dumpit = ovs_vport_cmd_dump -
-
{ .cmd = OVS_VPORT_CMD_SET, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
.doit = ovs_vport_cmd_set, -
-
|
dp_flow_genl_family | -
static struct genl_family dp_flow_genl_family = { -
-
.hdrsize = sizeof(struct ovs_header), -
-
.version = OVS_FLOW_VERSION, -
.maxattr = OVS_FLOW_ATTR_MAX, -
-
-
-
.n_ops = ARRAY_SIZE(dp_flow_genl_ops), -
.mcgrps = &ovs_dp_flow_multicast_group, -
-
| -
static struct genl_ops dp_flow_genl_ops[] = { -
{ .cmd = OVS_FLOW_CMD_NEW, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
-
-
{ .cmd = OVS_FLOW_CMD_DEL, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
-
-
{ .cmd = OVS_FLOW_CMD_GET, -
.flags = 0, /* OK for unprivileged users. */ -
-
.doit = ovs_flow_cmd_get, -
.dumpit = ovs_flow_cmd_dump -
-
{ .cmd = OVS_FLOW_CMD_SET, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
.doit = ovs_flow_cmd_set, -
-
|
dp_packet_genl_family | -
static struct genl_family dp_packet_genl_family = { -
-
.hdrsize = sizeof(struct ovs_header), -
.name = OVS_PACKET_FAMILY, -
.version = OVS_PACKET_VERSION, -
.maxattr = OVS_PACKET_ATTR_MAX, -
-
-
.ops = dp_packet_genl_ops, -
.n_ops = ARRAY_SIZE(dp_packet_genl_ops), -
| -
static struct genl_ops dp_packet_genl_ops[] = { -
{ .cmd = OVS_PACKET_CMD_EXECUTE, -
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ -
-
.doit = ovs_packet_cmd_execute -
-
|
如上一节中,ovs-vswitchd启动的时候,将虚拟网卡添加到虚拟交换机上的时候,会调用netlink的OVS_VPORT_CMD_NEW命令,因而会调用函数ovs_vport_cmd_new。
会调用static struct vport *new_vport(const struct vport_parms *parms)
会调用struct vport *ovs_vport_add(const struct vport_parms *parms)里面会调用vport = ops->create(parms);
ops是什么呢?在dp_init函数中会调用ovs_netdev_init,它会调用ovs_vport_ops_register(&ovs_netdev_vport_ops);
-
static struct vport_ops ovs_netdev_vport_ops = { -
.type = OVS_VPORT_TYPE_NETDEV, -
-
.destroy = netdev_destroy, -
-
|
所以ops->create会调用netdev_create,它会调用ovs_netdev_link,其中有下面的代码:
-
err = netdev_rx_handler_register(vport->dev, netdev_frame_hook, -
|
注册一个方法叫做netdev_frame_hook,每当网卡收到包的时候,就调用这个方法。
在下面的章节中,我们会从这个函数开始,解析整个网络包的处理过程。