mem: instrument allocator for ASan
[dpdk.git] / doc / guides / sample_app_ug / ipv4_multicast.rst
index 7c6e8b1..f87f7be 100644 (file)
@@ -97,14 +97,11 @@ The IPv4 Multicast sample application uses three memory pools.
 Two of the pools are for indirect buffers used for packet duplication purposes.
 Memory pools for indirect buffers are initialized differently from the memory pool for direct buffers:
 
-.. code-block:: c
-
-    packet_pool = rte_pktmbuf_pool_create("packet_pool", NB_PKT_MBUF, 32,
-                       0, PKT_MBUF_DATA_SIZE, rte_socket_id());
-    header_pool = rte_pktmbuf_pool_create("header_pool", NB_HDR_MBUF, 32,
-                       0, HDR_MBUF_DATA_SIZE, rte_socket_id());
-    clone_pool = rte_pktmbuf_pool_create("clone_pool", NB_CLONE_MBUF, 32,
-                       0, 0, rte_socket_id());
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Create the mbuf pools. 8<
+    :end-before: >8 End of create mbuf pools.
+    :dedent: 1
 
 The reason for this is because indirect buffers are not supposed to hold any packet data and
 therefore can be initialized with lower amount of reserved memory for each buffer.
@@ -114,27 +111,10 @@ Hash Initialization
 
 The hash object is created and loaded with the pre-configured entries read from a global array:
 
-.. code-block:: c
-
-    static int
-
-    init_mcast_hash(void)
-    {
-        uint32_t i;
-        mcast_hash_params.socket_id = rte_socket_id();
-
-        mcast_hash = rte_fbk_hash_create(&mcast_hash_params);
-        if (mcast_hash == NULL){
-            return -1;
-        }
-
-        for (i = 0; i < N_MCAST_GROUPS; i ++){
-            if (rte_fbk_hash_add_key(mcast_hash, mcast_group_table[i].ip, mcast_group_table[i].port_mask) < 0) {
-                       return -1;
-            }
-        }
-        return 0;
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Hash object is created and loaded. 8<
+    :end-before: >8 End of hash object is created and loaded.
 
 Forwarding
 ~~~~~~~~~~
@@ -142,106 +122,62 @@ Forwarding
 All forwarding is done inside the mcast_forward() function.
 Firstly, the Ethernet* header is removed from the packet and the IPv4 address is extracted from the IPv4 header:
 
-.. code-block:: c
-
-    /* Remove the Ethernet header from the input packet */
-
-    iphdr = (struct rte_ipv4_hdr *)rte_pktmbuf_adj(m, sizeof(struct rte_ether_hdr));
-    RTE_ASSERT(iphdr != NULL);
-    dest_addr = rte_be_to_cpu_32(iphdr->dst_addr);
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Remove the Ethernet header from the input packet. 8<
+    :end-before: >8 End of removing the Ethernet header from the input packet.
+    :dedent: 1
 
 Then, the packet is checked to see if it has a multicast destination address and
 if the routing table has any ports assigned to the destination address:
 
-.. code-block:: c
-
-    if (!RTE_IS_IPV4_MCAST(dest_addr) ||
-       (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 ||
-       (port_mask = hash & enabled_port_mask) == 0) {
-           rte_pktmbuf_free(m);
-           return;
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Check valid multicast address. 8<
+    :end-before: >8 End of valid multicast address check.
+    :dedent: 1
 
 Then, the number of ports in the destination portmask is calculated with the help of the bitcnt() function:
 
-.. code-block:: c
-
-    /* Get number of bits set. */
-
-    static inline uint32_t bitcnt(uint32_t v)
-    {
-        uint32_t n;
-
-        for (n = 0; v != 0; v &= v - 1, n++)
-           ;
-        return n;
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Get number of bits set. 8<
+    :end-before: >8 End of getting number of bits set.
 
 This is done to determine which forwarding algorithm to use.
 This is explained in more detail in the next section.
 
 Thereafter, a destination Ethernet address is constructed:
 
-.. code-block:: c
-
-    /* construct destination Ethernet address */
-
-    dst_eth_addr = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr);
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Construct destination ethernet address. 8<
+    :end-before: >8 End of constructing destination ethernet address.
+    :dedent: 1
 
 Since Ethernet addresses are also part of the multicast process, each outgoing packet carries the same destination Ethernet address.
 The destination Ethernet address is constructed from the lower 23 bits of the multicast group OR-ed
 with the Ethernet address 01:00:5e:00:00:00, as per RFC 1112:
 
-.. code-block:: c
-
-    #define ETHER_ADDR_FOR_IPV4_MCAST(x) \
-        (rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16)
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Construct Ethernet multicast address from IPv4 multicast Address. 8<
+    :end-before: >8 End of Construction of multicast address from IPv4 multicast address.
 
 Then, packets are dispatched to the destination ports according to the portmask associated with a multicast group:
 
-.. code-block:: c
-
-    for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) {
-        /* Prepare output packet and send it out. */
-
-        if ((port_mask & 1) != 0) {
-            if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL))
-                mcast_send_pkt(mc, &dst_eth_addr.as_addr, qconf, port);
-            else if (use_clone == 0)
-                 rte_pktmbuf_free(m);
-       }
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Packets dispatched to destination ports. 8<
+    :end-before: >8 End of packets dispatched to destination ports.
+    :dedent: 1
 
 The actual packet transmission is done in the mcast_send_pkt() function:
 
-.. code-block:: c
-
-    static inline void mcast_send_pkt(struct rte_mbuf *pkt, struct rte_ether_addr *dest_addr, struct lcore_queue_conf *qconf, uint16_t port)
-    {
-        struct rte_ether_hdr *ethdr;
-        uint16_t len;
-
-        /* Construct Ethernet header. */
-
-        ethdr = (struct rte_ether_hdr *)rte_pktmbuf_prepend(pkt, (uint16_t) sizeof(*ethdr));
-
-        RTE_ASSERT(ethdr != NULL);
-
-        rte_ether_addr_copy(dest_addr, &ethdr->d_addr);
-        rte_ether_addr_copy(&ports_eth_addr[port], &ethdr->s_addr);
-        ethdr->ether_type = rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV4);
-
-        /* Put new packet into the output queue */
-
-        len = qconf->tx_mbufs[port].len;
-        qconf->tx_mbufs[port].m_table[len] = pkt;
-        qconf->tx_mbufs[port].len = ++len;
-
-        /* Transmit packets */
-
-        if (unlikely(MAX_PKT_BURST == len))
-            send_burst(qconf, port);
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Write new Ethernet header to outgoing packets. 8<
+    :end-before: >8 End of writing new Ethernet headers.
 
 Buffer Cloning
 ~~~~~~~~~~~~~~
@@ -281,45 +217,15 @@ As the number of outgoing ports (and/or input segments) grows, the second approa
 Depending on the number of segments or the number of ports in the outgoing portmask,
 either the first (with cloning) or the second (without cloning) approach is taken:
 
-.. code-block:: c
-
-    use_clone = (port_num <= MCAST_CLONE_PORTS && m->pkt.nb_segs <= MCAST_CLONE_SEGS);
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: Should we use rte_pktmbuf_clone() or not. 8<
+    :end-before: >8 End of using rte_pktmbuf_clone().
+    :dedent: 1
 
 It is the mcast_out_pkt() function that performs the packet duplication (either with or without actually cloning the buffers):
 
-.. code-block:: c
-
-    static inline struct rte_mbuf *mcast_out_pkt(struct rte_mbuf *pkt, int use_clone)
-    {
-        struct rte_mbuf *hdr;
-
-        /* Create new mbuf for the header. */
-
-        if (unlikely ((hdr = rte_pktmbuf_alloc(header_pool)) == NULL))
-            return NULL;
-
-        /* If requested, then make a new clone packet. */
-
-        if (use_clone != 0 && unlikely ((pkt = rte_pktmbuf_clone(pkt, clone_pool)) == NULL)) {
-            rte_pktmbuf_free(hdr);
-            return NULL;
-        }
-
-        /* prepend new header */
-
-        hdr->pkt.next = pkt;
-
-        /* update header's fields */
-
-        hdr->pkt.pkt_len = (uint16_t)(hdr->pkt.data_len + pkt->pkt.pkt_len);
-        hdr->pkt.nb_segs = pkt->pkt.nb_segs + 1;
-
-        /* copy metadata from source packet */
-
-        hdr->pkt.in_port = pkt->pkt.in_port;
-        hdr->pkt.vlan_macip = pkt->pkt.vlan_macip;
-        hdr->pkt.hash = pkt->pkt.hash;
-        rte_mbuf_sanity_check(hdr, RTE_MBUF_PKT, 1);
-
-        return hdr;
-    }
+.. literalinclude:: ../../../examples/ipv4_multicast/main.c
+    :language: c
+    :start-after: mcast_out_pkt 8<
+    :end-before: >8 End of mcast_out_kt.