Openvswitch原理与代码分析(6):用户态流表flow table的操作

时间:2023-03-09 06:19:09
Openvswitch原理与代码分析(6):用户态流表flow table的操作

当内核无法查找到流表项的时候,则会通过upcall来调用用户态ovs-vswtichd中的flow table。

会调用ofproto-dpif-upcall.c中的udpif_upcall_handler函数。

  1. static
    void *
  2. udpif_upcall_handler(void *arg)
  3. {
  4.     struct handler *handler = arg;
  5.     struct udpif *udpif = handler->udpif;
  6.     while (!latch_is_set(&handler->udpif->exit_latch)) {
  7.         if (recv_upcalls(handler)) {
  8.             poll_immediate_wake();
  9.         } else {
  10.             dpif_recv_wait(udpif->dpif, handler->handler_id);
  11.             latch_wait(&udpif->exit_latch);
  12.         }
  13.         poll_block();
  14.     }
  15.     return NULL;
  16. }

 

会调用static size_t recv_upcalls(struct handler *handler)

在这个函数里面

(1) 首先读取upcall调用static int upcall_receive(struct upcall *upcall, const struct dpif_backer *backer, const struct dp_packet *packet, enum dpif_upcall_type type, const struct nlattr *userdata, const struct flow *flow, const unsigned int mru, const ovs_u128 *ufid, const unsigned pmd_id)

(2) 其次提取包头调用void flow_extract(struct dp_packet *packet, struct flow *flow),提取出的flow如下:

  1. struct flow {
  2.     /* Metadata */
  3.     struct flow_tnl tunnel; /* Encapsulating tunnel parameters. */
  4.     ovs_be64 metadata; /* OpenFlow Metadata. */
  5.     uint32_t regs[FLOW_N_REGS]; /* Registers. */
  6.     uint32_t skb_priority; /* Packet priority for QoS. */
  7.     uint32_t pkt_mark; /* Packet mark. */
  8.     uint32_t dp_hash; /* Datapath computed hash value. The exact
  9.                                  * computation is opaque to the user space. */
  10.     union flow_in_port in_port; /* Input port.*/
  11.     uint32_t recirc_id; /* Must be exact match. */
  12.     uint16_t ct_state; /* Connection tracking state. */
  13.     uint16_t ct_zone; /* Connection tracking zone. */
  14.     uint32_t ct_mark; /* Connection mark.*/
  15.     uint8_t pad1[4]; /* Pad to 64 bits. */
  16.     ovs_u128 ct_label; /* Connection label. */
  17.     uint32_t conj_id; /* Conjunction ID. */
  18.     ofp_port_t actset_output; /* Output port in action set. */
  19.     uint8_t pad2[2]; /* Pad to 64 bits. */
  20.     /* L2, Order the same as in the Ethernet header! (64-bit aligned) */
  21.     struct eth_addr dl_dst; /* Ethernet destination address. */
  22.     struct eth_addr dl_src; /* Ethernet source address. */
  23.     ovs_be16 dl_type; /* Ethernet frame type. */
  24.     ovs_be16 vlan_tci; /* If 802.1Q, TCI | VLAN_CFI; otherwise 0. */
  25.     ovs_be32 mpls_lse[ROUND_UP(FLOW_MAX_MPLS_LABELS, 2)]; /* MPLS label stack
  26.                                                              (with padding). */
  27.     /* L3 (64-bit aligned) */
  28.     ovs_be32 nw_src; /* IPv4 source address. */
  29.     ovs_be32 nw_dst; /* IPv4 destination address. */
  30.     struct
    in6_addr ipv6_src; /* IPv6 source address. */
  31.     struct
    in6_addr ipv6_dst; /* IPv6 destination address. */
  32.     ovs_be32 ipv6_label; /* IPv6 flow label. */
  33.     uint8_t nw_frag; /* FLOW_FRAG_* flags. */
  34.     uint8_t nw_tos; /* IP ToS (including DSCP and ECN). */
  35.     uint8_t nw_ttl; /* IP TTL/Hop Limit. */
  36.     uint8_t nw_proto; /* IP protocol or low 8 bits of ARP opcode. */
  37.     struct
    in6_addr nd_target; /* IPv6 neighbor discovery (ND) target. */
  38.     struct eth_addr arp_sha; /* ARP/ND source hardware address. */
  39.     struct eth_addr arp_tha; /* ARP/ND target hardware address. */
  40.     ovs_be16 tcp_flags; /* TCP flags. With L3 to avoid matching L4. */
  41.     ovs_be16 pad3; /* Pad to 64 bits. */
  42.     /* L4 (64-bit aligned) */
  43.     ovs_be16 tp_src; /* TCP/UDP/SCTP source port/ICMP type. */
  44.     ovs_be16 tp_dst; /* TCP/UDP/SCTP destination port/ICMP code. */
  45.     ovs_be32 igmp_group_ip4; /* IGMP group IPv4 address.
  46.                                  * Keep last for BUILD_ASSERT_DECL below. */
  47. };

 

(3) 然后调用static int process_upcall(struct udpif *udpif, struct upcall *upcall, struct ofpbuf *odp_actions, struct flow_wildcards *wc)来处理upcall。

 

对于MISS_UPCALL,调用static void upcall_xlate(struct udpif *udpif, struct upcall *upcall, struct ofpbuf *odp_actions, struct flow_wildcards *wc)

  1. switch (classify_upcall(upcall->type, userdata)) {
  2. case MISS_UPCALL:
  3.     upcall_xlate(udpif, upcall, odp_actions, wc);
  4.     return 0;

 

会调用enum xlate_error xlate_actions(struct xlate_in *xin, struct xlate_out *xout)

在这个函数里面,会在flow table里面查找rule

ctx.rule = rule_dpif_lookup_from_table( ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc, ctx.xin->resubmit_stats, &ctx.table_id, flow->in_port.ofp_port, true, true);

找到rule之后,调用static void do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len, struct xlate_ctx *ctx)在这个函数里面,根据action的不同,修改flow的内容。

  1. switch (a->type) {
  2. case OFPACT_OUTPUT:
  3.     xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
  4.                         ofpact_get_OUTPUT(a)->max_len, true);
  5.     break;
  6. case OFPACT_SET_VLAN_VID:
  7.     wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
  8.     if (flow->vlan_tci & htons(VLAN_CFI) ||
  9.         ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
  10.         flow->vlan_tci &= ~htons(VLAN_VID_MASK);
  11.         flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
  12.                            | htons(VLAN_CFI));
  13.     }
  14.     break;
  15. case OFPACT_SET_ETH_SRC:
  16.     WC_MASK_FIELD(wc, dl_src);
  17.     flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
  18.     break;
  19. case OFPACT_SET_ETH_DST:
  20.     WC_MASK_FIELD(wc, dl_dst);
  21.     flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
  22.     break;
  23. case OFPACT_SET_IPV4_SRC:
  24.     CHECK_MPLS_RECIRCULATION();
  25.     if (flow->dl_type == htons(ETH_TYPE_IP)) {
  26.         memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
  27.         flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
  28.     }
  29.     break;
  30. case OFPACT_SET_IPV4_DST:
  31.     CHECK_MPLS_RECIRCULATION();
  32.     if (flow->dl_type == htons(ETH_TYPE_IP)) {
  33.         memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
  34.         flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
  35.     }
  36.     break;
  37. case OFPACT_SET_L4_SRC_PORT:
  38.     CHECK_MPLS_RECIRCULATION();
  39.     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
  40.         memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
  41.         memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
  42.         flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
  43.     }
  44.     break;
  45. case OFPACT_SET_L4_DST_PORT:
  46.     CHECK_MPLS_RECIRCULATION();
  47.     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
  48.         memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
  49.         memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
  50.         flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
  51.     }
  52.     break;

 

 

(4) 最后调用static void handle_upcalls(struct udpif *udpif, struct upcall *upcalls, size_t n_upcalls)将flow rule添加到内核中的datapath

他会调用void dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops),他会调用dpif->dpif_class->operate(dpif, ops, chunk);

会调用dpif_netlink_operate()

  1. static
    void
  2. dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
  3. {
  4.     struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
  5.     while (n_ops > 0) {
  6.         size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
  7.         ops += chunk;
  8.         n_ops -= chunk;
  9.     }
  10. }

 

在static size_t dpif_netlink_operate__(struct dpif_netlink *dpif, struct dpif_op **ops, size_t n_ops)中,有以下的代码:

  1. switch (op->type) {
  2. case DPIF_OP_FLOW_PUT:
  3.     put = &op->u.flow_put;
  4.     dpif_netlink_init_flow_put(dpif, put, &flow);
  5.     if (put->stats) {
  6.         flow.nlmsg_flags |= NLM_F_ECHO;
  7.         aux->txn.reply = &aux->reply;
  8.     }
  9.     dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
  10.     break;
  11. case DPIF_OP_FLOW_DEL:
  12.     del = &op->u.flow_del;
  13.     dpif_netlink_init_flow_del(dpif, del, &flow);
  14.     if (del->stats) {
  15.         flow.nlmsg_flags |= NLM_F_ECHO;
  16.         aux->txn.reply = &aux->reply;
  17.     }
  18.     dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
  19.     break;

 

会调用netlink修改内核中datapath的规则。