Fix soft checksum calculation
[samplevnf.git] / VNFs / DPPD-PROX / handle_gen.c
index 643c61c..4bf2e6e 100644 (file)
@@ -13,7 +13,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 */
-
 #include <rte_mbuf.h>
 #include <pcap.h>
 #include <string.h>
@@ -353,9 +352,10 @@ static void task_gen_apply_accur_pos(struct task_gen *task, uint8_t *pkt_hdr, ui
        *(uint32_t *)(pkt_hdr + task->accur_pos) = accuracy;
 }
 
-static void task_gen_apply_sig(struct task_gen *task, uint8_t *pkt_hdr)
+static void task_gen_apply_sig(struct task_gen *task, struct pkt_template *dst)
 {
-       *(uint32_t *)(pkt_hdr + task->sig_pos) = task->sig;
+       if (task->sig_pos)
+               *(uint32_t *)(dst->buf + task->sig_pos) = task->sig;
 }
 
 static void task_gen_apply_all_accur_pos(struct task_gen *task, struct rte_mbuf **mbufs, uint8_t **pkt_hdr, uint32_t count)
@@ -372,16 +372,6 @@ static void task_gen_apply_all_accur_pos(struct task_gen *task, struct rte_mbuf
        }
 }
 
-static void task_gen_apply_all_sig(struct task_gen *task, struct rte_mbuf **mbufs, uint8_t **pkt_hdr, uint32_t count)
-{
-       if (!task->sig_pos)
-               return;
-
-       for (uint16_t j = 0; j < count; ++j) {
-               task_gen_apply_sig(task, pkt_hdr[j]);
-       }
-}
-
 static void task_gen_apply_unique_id(struct task_gen *task, uint8_t *pkt_hdr, const struct unique_id *id)
 {
        struct unique_id *dst = (struct unique_id *)(pkt_hdr + task->packet_id_pos);
@@ -670,7 +660,6 @@ static int handle_gen_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uin
        task_gen_build_packets(task, new_pkts, pkt_hdr, send_bulk);
        task_gen_apply_all_random_fields(task, pkt_hdr, send_bulk);
        task_gen_apply_all_accur_pos(task, new_pkts, pkt_hdr, send_bulk);
-       task_gen_apply_all_sig(task, new_pkts, pkt_hdr, send_bulk);
        task_gen_apply_all_unique_id(task, new_pkts, pkt_hdr, send_bulk);
 
        uint64_t tsc_before_tx;
@@ -679,6 +668,20 @@ static int handle_gen_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uin
        task_gen_checksum_packets(task, new_pkts, pkt_hdr, send_bulk);
        ret = task->base.tx_pkt(&task->base, new_pkts, send_bulk, out);
        task_gen_store_accuracy(task, send_bulk, tsc_before_tx);
+
+       // If we failed to send some packets, we need to do some clean-up:
+
+       if (unlikely(ret)) {
+               // We need re-use the packets indexes not being sent
+               // Hence non-sent packets will not be considered as lost by the receiver when it looks at
+               // packet ids. This should also increase the percentage of packets used for latency measurements
+               task->pkt_queue_index -= ret;
+
+               // In case of failures, the estimate about when we can send next packet (earliest_tsc_next_pkt) is wrong
+               // This would result in under-estimated latency (up to 0 or negative)
+               uint64_t bulk_duration = task_gen_calc_bulk_duration(task, ret);
+               task->earliest_tsc_next_pkt -= bulk_duration;
+       }
        return ret;
 }
 
@@ -908,6 +911,7 @@ static void task_gen_reset_pkt_templates_content(struct task_gen *task)
                src = &task->pkt_template_orig[i];
                dst = &task->pkt_template[i];
                memcpy(dst->buf, src->buf, dst->len);
+               task_gen_apply_sig(task, dst);
        }
 }
 
@@ -1284,11 +1288,14 @@ static void init_task_gen(struct task_base *tbase, struct task_args *targ)
                plog_info("\tPort %u: max link speed is %ld Mbps\n",
                        (uint8_t)(task->port - prox_port_cfg), 8 * bytes_per_hz / 1000000);
        }
+       // There are cases where hz estimate might be slighly over-estimated
+       // This results in too much extrapolation
+       // Only account for 99% of extrapolation to handle cases with up to 1% error clocks
        for (unsigned int i = 0; i < task->max_frame_size * MAX_PKT_BURST ; i++) {
                if (bytes_per_hz == UINT64_MAX)
                        task->bytes_to_tsc[i] = 0;
                else
-                       task->bytes_to_tsc[i] = (task->hz * i) / bytes_per_hz;
+                       task->bytes_to_tsc[i] = (task->hz * i * 0.99) / bytes_per_hz;
        }
 
        if (!strcmp(targ->pcap_file, "")) {