Freezer: make kernel threads nonfreezable by default
[safe/jmp/linux-2.6] / net / core / pktgen.c
index 895739f..bca787f 100644 (file)
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/kernel.h>
-#include <linux/smp_lock.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <net/checksum.h>
 #include <net/ipv6.h>
 #include <net/addrconf.h>
+#ifdef CONFIG_XFRM
+#include <net/xfrm.h>
+#endif
 #include <asm/byteorder.h>
 #include <linux/rcupdate.h>
 #include <asm/bitops.h>
 #define F_MPLS_RND    (1<<8)   /* Random MPLS labels */
 #define F_VID_RND     (1<<9)   /* Random VLAN ID */
 #define F_SVID_RND    (1<<10)  /* Random SVLAN ID */
+#define F_FLOW_SEQ    (1<<11)  /* Sequential flows */
+#define F_IPSEC_ON    (1<<12)  /* ipsec on for flows */
 
 /* Thread control flag bits */
 #define T_TERMINATE   (1<<0)
@@ -208,8 +212,15 @@ static struct proc_dir_entry *pg_proc_dir = NULL;
 struct flow_state {
        __be32 cur_daddr;
        int count;
+#ifdef CONFIG_XFRM
+       struct xfrm_state *x;
+#endif
+       __u32 flags;
 };
 
+/* flow flag bits */
+#define F_INIT   (1<<0)                /* flow has been initialized */
+
 struct pktgen_dev {
        /*
         * Try to keep frequent/infrequent used vars. separated.
@@ -229,6 +240,7 @@ struct pktgen_dev {
 
        int min_pkt_size;       /* = ETH_ZLEN; */
        int max_pkt_size;       /* = ETH_ZLEN; */
+       int pkt_overhead;       /* overhead for MPLS, VLANs, IPSEC etc */
        int nfrags;
        __u32 delay_us;         /* Default delay */
        __u32 delay_ns;
@@ -342,7 +354,11 @@ struct pktgen_dev {
        unsigned cflows;        /* Concurrent flows (config) */
        unsigned lflow;         /* Flow length  (config) */
        unsigned nflows;        /* accumulated flows (stats) */
-
+       unsigned curfl;         /* current sequenced flow (state)*/
+#ifdef CONFIG_XFRM
+       __u8    ipsmode;                /* IPSEC mode (config) */
+       __u8    ipsproto;               /* IPSEC type (config) */
+#endif
        char result[512];
 };
 
@@ -645,7 +661,7 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
        if (pkt_dev->nr_labels) {
                unsigned i;
                seq_printf(seq, "     mpls: ");
-               for(i = 0; i < pkt_dev->nr_labels; i++)
+               for (i = 0; i < pkt_dev->nr_labels; i++)
                        seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
                                   i == pkt_dev->nr_labels-1 ? "\n" : ", ");
        }
@@ -691,6 +707,18 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
        if (pkt_dev->flags & F_MPLS_RND)
                seq_printf(seq,  "MPLS_RND  ");
 
+       if (pkt_dev->cflows) {
+               if (pkt_dev->flags & F_FLOW_SEQ)
+                       seq_printf(seq,  "FLOW_SEQ  "); /*in sequence flows*/
+               else
+                       seq_printf(seq,  "FLOW_RND  ");
+       }
+
+#ifdef CONFIG_XFRM
+       if (pkt_dev->flags & F_IPSEC_ON)
+               seq_printf(seq,  "IPSEC  ");
+#endif
+
        if (pkt_dev->flags & F_MACSRC_RND)
                seq_printf(seq, "MACSRC_RND  ");
 
@@ -750,7 +778,7 @@ static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32
        int i = 0;
        *num = 0;
 
-       for(; i < maxlen; i++) {
+       for (; i < maxlen; i++) {
                char c;
                *num <<= 4;
                if (get_user(c, &user_buffer[i]))
@@ -786,7 +814,7 @@ static int count_trail_chars(const char __user * user_buffer,
                        break;
                default:
                        goto done;
-               };
+               }
        }
 done:
        return i;
@@ -829,7 +857,7 @@ static int strn_len(const char __user * user_buffer, unsigned int maxlen)
                        break;
                default:
                        break;
-               };
+               }
        }
 done_str:
        return i;
@@ -858,7 +886,7 @@ static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
                n++;
                if (n >= MAX_MPLS_LABELS)
                        return -E2BIG;
-       } while(c == ',');
+       } while (c == ',');
 
        pkt_dev->nr_labels = n;
        return i;
@@ -1182,6 +1210,14 @@ static ssize_t pktgen_if_write(struct file *file,
                else if (strcmp(f, "!SVID_RND") == 0)
                        pkt_dev->flags &= ~F_SVID_RND;
 
+               else if (strcmp(f, "FLOW_SEQ") == 0)
+                       pkt_dev->flags |= F_FLOW_SEQ;
+
+#ifdef CONFIG_XFRM
+               else if (strcmp(f, "IPSEC") == 0)
+                       pkt_dev->flags |= F_IPSEC_ON;
+#endif
+
                else if (strcmp(f, "!IPV6") == 0)
                        pkt_dev->flags &= ~F_IPV6;
 
@@ -1190,7 +1226,7 @@ static ssize_t pktgen_if_write(struct file *file,
                                "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
                                f,
                                "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
-                               "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
+                               "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
                        return count;
                }
                sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
@@ -1487,7 +1523,7 @@ static ssize_t pktgen_if_write(struct file *file,
                if (len < 0) { return len; }
                i += len;
                offset = sprintf(pg_result, "OK: mpls=");
-               for(n = 0; n < pkt_dev->nr_labels; n++)
+               for (n = 0; n < pkt_dev->nr_labels; n++)
                        offset += sprintf(pg_result + offset,
                                          "%08x%s", ntohl(pkt_dev->labels[n]),
                                          n == pkt_dev->nr_labels-1 ? "" : ",");
@@ -1906,7 +1942,7 @@ static int pktgen_device_event(struct notifier_block *unused,
        case NETDEV_UNREGISTER:
                pktgen_mark_device(dev->name);
                break;
-       };
+       }
 
        return NOTIFY_DONE;
 }
@@ -2076,6 +2112,70 @@ static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
        pkt_dev->idle_acc += now - start;
 }
 
+static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
+{
+       pkt_dev->pkt_overhead = 0;
+       pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
+       pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
+       pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
+}
+
+static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
+{
+
+       if (pkt_dev->flows[flow].flags & F_INIT)
+               return 1;
+       else
+               return 0;
+}
+
+static inline int f_pick(struct pktgen_dev *pkt_dev)
+{
+       int flow = pkt_dev->curfl;
+
+       if (pkt_dev->flags & F_FLOW_SEQ) {
+               if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
+                       /* reset time */
+                       pkt_dev->flows[flow].count = 0;
+                       pkt_dev->curfl += 1;
+                       if (pkt_dev->curfl >= pkt_dev->cflows)
+                               pkt_dev->curfl = 0; /*reset */
+               }
+       } else {
+               flow = random32() % pkt_dev->cflows;
+
+               if (pkt_dev->flows[flow].count > pkt_dev->lflow)
+                       pkt_dev->flows[flow].count = 0;
+       }
+
+       return pkt_dev->curfl;
+}
+
+
+#ifdef CONFIG_XFRM
+/* If there was already an IPSEC SA, we keep it as is, else
+ * we go look for it ...
+*/
+inline
+void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
+{
+       struct xfrm_state *x = pkt_dev->flows[flow].x;
+       if (!x) {
+               /*slow path: we dont already have xfrm_state*/
+               x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
+                                       (xfrm_address_t *)&pkt_dev->cur_saddr,
+                                       AF_INET,
+                                       pkt_dev->ipsmode,
+                                       pkt_dev->ipsproto, 0);
+               if (x) {
+                       pkt_dev->flows[flow].x = x;
+                       set_pkt_overhead(pkt_dev);
+                       pkt_dev->pkt_overhead+=x->props.header_len;
+               }
+
+       }
+}
+#endif
 /* Increment/randomize headers according to flags and current values
  * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  */
@@ -2085,12 +2185,8 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
        __u32 imx;
        int flow = 0;
 
-       if (pkt_dev->cflows) {
-               flow = random32() % pkt_dev->cflows;
-
-               if (pkt_dev->flows[flow].count > pkt_dev->lflow)
-                       pkt_dev->flows[flow].count = 0;
-       }
+       if (pkt_dev->cflows)
+               flow = f_pick(pkt_dev);
 
        /*  Deal with source MAC */
        if (pkt_dev->src_mac_count > 1) {
@@ -2148,7 +2244,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
 
        if (pkt_dev->flags & F_MPLS_RND) {
                unsigned i;
-               for(i = 0; i < pkt_dev->nr_labels; i++)
+               for (i = 0; i < pkt_dev->nr_labels; i++)
                        if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
                                pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
                                             ((__force __be32)random32() &
@@ -2206,7 +2302,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                        pkt_dev->cur_saddr = htonl(t);
                }
 
-               if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
+               if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
                        pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
                } else {
                        imn = ntohl(pkt_dev->daddr_min);
@@ -2236,8 +2332,13 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
                                }
                        }
                        if (pkt_dev->cflows) {
+                               pkt_dev->flows[flow].flags |= F_INIT;
                                pkt_dev->flows[flow].cur_daddr =
                                    pkt_dev->cur_daddr;
+#ifdef CONFIG_XFRM
+                               if (pkt_dev->flags & F_IPSEC_ON)
+                                       get_ipsec_sa(pkt_dev, flow);
+#endif
                                pkt_dev->nflows++;
                        }
                }
@@ -2278,10 +2379,95 @@ static void mod_cur_headers(struct pktgen_dev *pkt_dev)
        pkt_dev->flows[flow].count++;
 }
 
+
+#ifdef CONFIG_XFRM
+static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
+{
+       struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
+       int err = 0;
+       struct iphdr *iph;
+
+       if (!x)
+               return 0;
+       /* XXX: we dont support tunnel mode for now until
+        * we resolve the dst issue */
+       if (x->props.mode != XFRM_MODE_TRANSPORT)
+               return 0;
+
+       spin_lock(&x->lock);
+       iph = ip_hdr(skb);
+
+       err = x->mode->output(x, skb);
+       if (err)
+               goto error;
+       err = x->type->output(x, skb);
+       if (err)
+               goto error;
+
+       x->curlft.bytes +=skb->len;
+       x->curlft.packets++;
+       spin_unlock(&x->lock);
+
+error:
+       spin_unlock(&x->lock);
+       return err;
+}
+
+static inline void free_SAs(struct pktgen_dev *pkt_dev)
+{
+       if (pkt_dev->cflows) {
+               /* let go of the SAs if we have them */
+               int i = 0;
+               for (;  i < pkt_dev->nflows; i++){
+                       struct xfrm_state *x = pkt_dev->flows[i].x;
+                       if (x) {
+                               xfrm_state_put(x);
+                               pkt_dev->flows[i].x = NULL;
+                       }
+               }
+       }
+}
+
+static inline int process_ipsec(struct pktgen_dev *pkt_dev,
+                             struct sk_buff *skb, __be16 protocol)
+{
+       if (pkt_dev->flags & F_IPSEC_ON) {
+               struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
+               int nhead = 0;
+               if (x) {
+                       int ret;
+                       __u8 *eth;
+                       nhead = x->props.header_len - skb_headroom(skb);
+                       if (nhead >0) {
+                               ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
+                               if (ret < 0) {
+                                       printk("Error expanding ipsec packet %d\n",ret);
+                                       return 0;
+                               }
+                       }
+
+                       /* ipsec is not expecting ll header */
+                       skb_pull(skb, ETH_HLEN);
+                       ret = pktgen_output_ipsec(skb, pkt_dev);
+                       if (ret) {
+                               printk("Error creating ipsec packet %d\n",ret);
+                               kfree_skb(skb);
+                               return 0;
+                       }
+                       /* restore ll */
+                       eth = (__u8 *) skb_push(skb, ETH_HLEN);
+                       memcpy(eth, pkt_dev->hh, 12);
+                       *(u16 *) & eth[12] = protocol;
+               }
+       }
+       return 1;
+}
+#endif
+
 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
 {
        unsigned i;
-       for(i = 0; i < pkt_dev->nr_labels; i++) {
+       for (i = 0; i < pkt_dev->nr_labels; i++) {
                *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
        }
        mpls--;
@@ -2324,9 +2510,7 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
 
        datalen = (odev->hard_header_len + 16) & ~0xf;
        skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
-                       pkt_dev->nr_labels*sizeof(u32) +
-                       VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
-                       GFP_ATOMIC);
+                       pkt_dev->pkt_overhead, GFP_ATOMIC);
        if (!skb) {
                sprintf(pkt_dev->result, "No memory");
                return NULL;
@@ -2341,7 +2525,7 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
                mpls_push(mpls, pkt_dev);
 
        if (pkt_dev->vlan_id != 0xffff) {
-               if(pkt_dev->svlan_id != 0xffff) {
+               if (pkt_dev->svlan_id != 0xffff) {
                        svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
                        *svlan_tci = build_tci(pkt_dev->svlan_id,
                                               pkt_dev->svlan_cfi,
@@ -2357,15 +2541,19 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
                *vlan_encapsulated_proto = htons(ETH_P_IP);
        }
 
-       iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
-       udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
+       skb->network_header = skb->tail;
+       skb->transport_header = skb->network_header + sizeof(struct iphdr);
+       skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
+
+       iph = ip_hdr(skb);
+       udph = udp_hdr(skb);
 
        memcpy(eth, pkt_dev->hh, 12);
        *(__be16 *) & eth[12] = protocol;
 
        /* Eth + IPh + UDPh + mpls */
        datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
-                 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
+                 pkt_dev->pkt_overhead;
        if (datalen < sizeof(struct pktgen_hdr))
                datalen = sizeof(struct pktgen_hdr);
 
@@ -2387,12 +2575,10 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
        iph->check = 0;
        iph->check = ip_fast_csum((void *)iph, iph->ihl);
        skb->protocol = protocol;
-       skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
-               VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
+       skb->mac_header = (skb->network_header - ETH_HLEN -
+                          pkt_dev->pkt_overhead);
        skb->dev = odev;
        skb->pkt_type = PACKET_HOST;
-       skb->nh.iph = iph;
-       skb->h.uh = udph;
 
        if (pkt_dev->nfrags <= 0)
                pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
@@ -2461,6 +2647,11 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
                pgh->tv_usec = htonl(timestamp.tv_usec);
        }
 
+#ifdef CONFIG_XFRM
+       if (!process_ipsec(pkt_dev, skb, protocol))
+               return NULL;
+#endif
+
        return skb;
 }
 
@@ -2660,9 +2851,7 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        mod_cur_headers(pkt_dev);
 
        skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
-                       pkt_dev->nr_labels*sizeof(u32) +
-                       VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
-                       GFP_ATOMIC);
+                       pkt_dev->pkt_overhead, GFP_ATOMIC);
        if (!skb) {
                sprintf(pkt_dev->result, "No memory");
                return NULL;
@@ -2677,7 +2866,7 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
                mpls_push(mpls, pkt_dev);
 
        if (pkt_dev->vlan_id != 0xffff) {
-               if(pkt_dev->svlan_id != 0xffff) {
+               if (pkt_dev->svlan_id != 0xffff) {
                        svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
                        *svlan_tci = build_tci(pkt_dev->svlan_id,
                                               pkt_dev->svlan_cfi,
@@ -2693,8 +2882,12 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
                *vlan_encapsulated_proto = htons(ETH_P_IPV6);
        }
 
-       iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
-       udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
+       skb->network_header = skb->tail;
+       skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
+       skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
+
+       iph = ipv6_hdr(skb);
+       udph = udp_hdr(skb);
 
        memcpy(eth, pkt_dev->hh, 12);
        *(__be16 *) & eth[12] = protocol;
@@ -2702,7 +2895,7 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        /* Eth + IPh + UDPh + mpls */
        datalen = pkt_dev->cur_pkt_size - 14 -
                  sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
-                 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
+                 pkt_dev->pkt_overhead;
 
        if (datalen < sizeof(struct pktgen_hdr)) {
                datalen = sizeof(struct pktgen_hdr);
@@ -2731,13 +2924,11 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
        ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
        ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
 
-       skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
-               VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
+       skb->mac_header = (skb->network_header - ETH_HLEN -
+                          pkt_dev->pkt_overhead);
        skb->protocol = protocol;
        skb->dev = odev;
        skb->pkt_type = PACKET_HOST;
-       skb->nh.ipv6h = iph;
-       skb->h.uh = udph;
 
        if (pkt_dev->nfrags <= 0)
                pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
@@ -2852,6 +3043,7 @@ static void pktgen_run(struct pktgen_thread *t)
                        pkt_dev->started_at = getCurUs();
                        pkt_dev->next_tx_us = getCurUs();       /* Transmit immediately */
                        pkt_dev->next_tx_ns = 0;
+                       set_pkt_overhead(pkt_dev);
 
                        strcpy(pkt_dev->result, "Starting");
                        started++;
@@ -3134,7 +3326,9 @@ static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
                }
        }
 
-       if (netif_queue_stopped(odev) || need_resched()) {
+       if ((netif_queue_stopped(odev) ||
+            netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) ||
+            need_resched()) {
                idle_start = getCurUs();
 
                if (!netif_running(odev)) {
@@ -3149,7 +3343,8 @@ static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
 
                pkt_dev->idle_acc += getCurUs() - idle_start;
 
-               if (netif_queue_stopped(odev)) {
+               if (netif_queue_stopped(odev) ||
+                   netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
                        pkt_dev->next_tx_us = getCurUs();       /* TODO */
                        pkt_dev->next_tx_ns = 0;
                        goto out;       /* Try the next interface */
@@ -3176,7 +3371,8 @@ static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
        }
 
        netif_tx_lock_bh(odev);
-       if (!netif_queue_stopped(odev)) {
+       if (!netif_queue_stopped(odev) &&
+           !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
 
                atomic_inc(&(pkt_dev->skb->users));
              retry_now:
@@ -3269,6 +3465,8 @@ static int pktgen_thread_worker(void *arg)
 
        set_current_state(TASK_INTERRUPTIBLE);
 
+       set_freezable();
+
        while (!kthread_should_stop()) {
                pkt_dev = next_to_run(t);
 
@@ -3441,11 +3639,18 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
        }
        pkt_dev->entry->proc_fops = &pktgen_if_fops;
        pkt_dev->entry->data = pkt_dev;
+#ifdef CONFIG_XFRM
+       pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
+       pkt_dev->ipsproto = IPPROTO_ESP;
+#endif
 
        return add_dev_to_thread(t, pkt_dev);
 out2:
        dev_put(pkt_dev->odev);
 out1:
+#ifdef CONFIG_XFRM
+       free_SAs(pkt_dev);
+#endif
        if (pkt_dev->flows)
                vfree(pkt_dev->flows);
        kfree(pkt_dev);
@@ -3540,6 +3745,9 @@ static int pktgen_remove_device(struct pktgen_thread *t,
        if (pkt_dev->entry)
                remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
 
+#ifdef CONFIG_XFRM
+       free_SAs(pkt_dev);
+#endif
        if (pkt_dev->flows)
                vfree(pkt_dev->flows);
        kfree(pkt_dev);