'scatterlist'에 해당되는 글 2건

 scatterlist 관련한 코드의 예시를 살펴보기 위해 커널 소스를 뒤져 보았는데 생각외로 광범위하게 사용되고 있어서 조금 놀랐다. 

 scatterlist 에 대해 알아보기 시작한 것이 crypto 쪽 작업 때문이었는데 crypto 뿐 아니라 filesystem, video driver, frame buffer 등등 분야를 가리지 않고 광범위하게 사용되고 있음을 확인 할 수 있었다. 

 그도 그럴것이 scatterlist 라는 것이 DMA 를 위한 것이니 DMA 를 사용하는 것에서는 모두 사용이 되고 있는 것이 당연할 것이다.

 무엇보다 scatterlist 의 가장 간단한 example 를 찾기 위해 구글을 뒤졌는데 아래와 같은 소스가 보기 쉽다는 답변을 확인 하였다.

 security/seclvl.c
 linux/fs/ecryptfs/ : eCryptfs file system 
 linux/drivers/staging/rtl8187se/ieee80211 : 802.11 wireless stack 

이중에서 가장 쉬운 코드가  security/seclvl.c 라는 설명이 있었는데 아쉽게도 kernel-2.6.18 이후론 없어진 것 같다. 
그래도 참조용으로 코드를 아래 정리해 보았다.
 

static int
plaintext_to_sha1(unsigned char *hash, const char *plaintext, unsigned int len)
{
  struct crypto_tfm *tfm;
  struct scatterlist sg;
  if (len > PAGE_SIZE) {
    seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d "
            "characters).  Largest possible is %lu "
            "bytes.\n", len, PAGE_SIZE);
    return -EINVAL;
  }
  tfm = crypto_alloc_tfm("sha1", CRYPTO_TFM_REQ_MAY_SLEEP);
  if (tfm == NULL) {
    seclvl_printk(0, KERN_ERR,
            "Failed to load transform for SHA1\n");
    return -EINVAL;
  }
  sg_init_one(&sg, (u8 *)plaintext, len);
  crypto_digest_init(tfm);
  crypto_digest_update(tfm, &sg, 1);
  crypto_digest_final(tfm, hash);
  crypto_free_tfm(tfm);
  return 0; 
} 



아래 코드도 참조용으로 유용한 것으로 생각된다. (출처 : http://stackoverflow.com/questions/3869028/how-to-use-cryptoapi-in-the-linux-kernel-2-6)

struct crypto_hash *tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
if (tfm == NULL)
    fail;
char *output_buf = kmalloc(crypto_hash_digestsize(tfm), GFP_KERNEL);
if (output_buf == NULL)
    fail;
struct scatterlist sg[2];
struct hash_desc desc = {.tfm = tfm};
ret = crypto_hash_init(&desc);
if (ret != 0)
    fail;
sg_init_table(sg, ARRAY_SIZE(sg));
sg_set_buf(&sg[0], "Hello", 5);
sg_set_buf(&sg[1], " World", 6);
ret = crypto_hash_digest(&desc, sg, 11, output_buf);
if (ret != 0)
    fail;

 
  참고로 scatterlist example 은 아니지만 암호화 테스트용 샘플 코드로써 암호화 API 를 사용하는 법을 쉽게 볼 수 있는 코드도 있다. 

  crypto/tcrypt.c


 
블로그 이미지

김유석0

,
넥스지에서 주어진 업무가 Crypto 기능을 Sync 에서 ASync 로 변경하는 일이어서 관련 문서들을 살펴보고 있다. 

Documentation/crypto/api-intro.txt 를 살펴보고 있는데 아래와 같이 시작을 하고 있다.


Scatterlist Cryptographic API

InTRODUCTION
The Scatterlist Crypto API takes page vectors(scatterlists) as arguments, and works directly on pages. In some cases(e.g. ECB mode ciphers), this will allow for pages to be encrypted in-place with no copying.
 
 
Scatter/gather 는 함께 움직이는 개념이다. 

Scatter 의 사전적 의미는
  scatter [skt]   단어장에 추가
  1.…을 뿌리다, 흩뿌리다, 살포하다, [씨]를 뿌리다[on, over, with ].
  2.흩어지다, 뿔뿔이 헤어지다, 분산하다.
  
 이고,

Gather 의 사전적 의미는

  gather
 [g]   단어장에 추가
  1.모으다, 끌어[긁어]모으다(in, up, together); [물건]을 하나하나 줍다.
 

DMA 전송에 있어서 데이터가 물리 메모리 상에 일렬로 정렬되어 있는 것은 아주 중요한 요소이다.

 커널 영역에서 메모리 관리를 할 때는 어렵긴 하지만(오버헤드가 크지만) 물리 메모리에 일렬로 정렬해서 사용하는 것이 가능하지만 사용자 영역에서는 이렇게 할 수가 없다.
 
 커널 영역에서 메모리를 물리 영역에 일렬로 정리해서 쓰는것이 가능하다고 했지만 여기 저기 흩어져 있는(Scatter) 메모리를 일렬로 정렬되어 있는 것처럼 사용할 수 있다면 좀더 편한 관리가 가능할 것이다. 

 다시 정리하면 Scatter/Gather 의 개념은 여기 저기 흩어져 있는 메모리(Scatter) 를 논리적으로 모아서(Gather) 연속된 물리적 메모리처럼 사용하는 것을 의미하며 Scatterlist 는 여기 저기 흩어져 있는 메모리(Scatter) 의 모음(array) 라고 보면 된다.

 DMA 전송에 사용되는 개념이다. 

    struct scatterlist{
      struct page *page;
      unsigned int offset;
      dma_addr_t dma_address;
      unsigned int length;
    } 

위 구조체를 보면 알겠지만 하나의 흩어져 있는 메모리의 정보를 담을 수 있도록 되어 있고 위 구조체가 여러개 모이면 마치 하나의 연속된 메모리처럼 동작시킬 수 있게 된다. 

아래 파일은 Scatter list 에 대한 간략한 diagram 이다. 
 

'development' 카테고리의 다른 글

[crypto] ASync crypto 와 Sync crypto api 함수명 특징  (0) 2011.10.10
[linux_kernel] scatterlist example  (0) 2011.09.27
[HW] TTL 과 RS232 에 대한 정리  (0) 2011.09.26
[HW] TTL Level 과 CMOS Level  (0) 2011.09.26
[crypto] IPSec  (0) 2011.09.23
블로그 이미지

김유석0

,