Ignore:
Timestamp:
Jan 13, 2021, 12:36:17 AM (4 years ago)
Author:
alain
Message:

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_socket.c

    r670 r683  
    4545
    4646#if DEBUG_SYS_SOCKET
    47 static char* socket_cmd_type_str( uint32_t type )
     47static char* socket_user_cmd_str( uint32_t type )
    4848{
    4949    if     ( type == SOCK_CREATE      ) return "CREATE";
     
    5454    else if( type == SOCK_SEND        ) return "SEND";
    5555    else if( type == SOCK_RECV        ) return "RECV";
     56    else if( type == SOCK_SENDTO      ) return "SENDTO";
     57    else if( type == SOCK_RECVFROM    ) return "RECVFROM";
    5658    else                                return "undefined";
    5759}
     
    7981
    8082#if DEBUG_SYS_SOCKET
     83char kbuf[64];
    8184if( DEBUG_SYS_SOCKET < (uint32_t)tm_start )
    82 printk("\n[%s] thread[%x,%x] enter / %s / a1 %x / a2 %x / a3 %x / cycle %d\n",
    83 __FUNCTION__, process->pid, this->trdid, socket_cmd_type_str(cmd),
     85printk("\n[%s] thread[%x,%x] enter for %s / a1 %x / a2 %x / a3 %x / cycle %d\n",
     86__FUNCTION__, process->pid, this->trdid, socket_user_cmd_str(cmd),
    8487arg1, arg2, arg3, (uint32_t)tm_start );
    8588#endif
     
    97100
    98101#if DEBUG_SYSCALLS_ERROR
    99 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    100 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / domain %d =! AF_INET\n",
    101 __FUNCTION__ , process->pid , this->trdid , domain );
     102printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / domain %d =! AF_INET / cycle %d\n",
     103__FUNCTION__ , process->pid , this->trdid , domain , (uint32_t)tm_start );
    102104#endif
    103105                this->errno = EINVAL;
     
    110112
    111113#if DEBUG_SYSCALLS_ERROR
    112 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    113 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / illegal socket type\n",
    114 __FUNCTION__ , process->pid , this->trdid );
     114printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / illegal socket type / cycle %d\n",
     115__FUNCTION__ , process->pid , this->trdid  , (uint32_t)tm_start);
    115116#endif
    116117                this->errno = EINVAL;
     
    126127
    127128#if DEBUG_SYSCALLS_ERROR
    128 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    129 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / cannot create socket\n",
    130 __FUNCTION__ , process->pid , this->trdid );
     129printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / cannot create socket / cycle %d\n",
     130__FUNCTION__ , process->pid , this->trdid  , (uint32_t)tm_start);
    131131#endif
    132132                this->errno = EINVAL;
     
    148148
    149149#if DEBUG_SYSCALLS_ERROR
    150 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    151 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / socket address %x unmapped\n",
    152 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
     150printk("\n[ERROR] in %s : thread[%x,%x] / BIND / socket address %x unmapped / cycle %d\n",
     151__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
    153152#endif
    154153                this->errno = EINVAL;
     
    171170
    172171#if DEBUG_SYSCALLS_ERROR
    173 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    174 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / cannot access socket[%x,%d]\n",
    175 __FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid );
     172printk("\n[ERROR] in %s : thread[%x,%x] / BIND / cannot access socket[%x,%d] / cycle %d\n",
     173__FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid , (uint32_t)tm_start );
    176174#endif
    177175                this->errno = EINVAL;
     
    195193
    196194#if DEBUG_SYSCALLS_ERROR
    197 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    198 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d]\n",
    199 __FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid );
     195printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d] / cycle %d\n",
     196__FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid , (uint32_t)tm_start );
    200197#endif
    201198                this->errno = EINVAL;
     
    217214
    218215#if DEBUG_SYSCALLS_ERROR
    219 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    220 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped\n",
    221 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
     216printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped / cycle %d\n",
     217__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
    222218#endif
    223219                this->errno = EINVAL;
     
    239235
    240236#if DEBUG_SYSCALLS_ERROR
    241 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    242 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d]\n",
    243 __FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid );
     237printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d] / cycle %d\n",
     238__FUNCTION__ , process->pid , this->trdid ,  process->pid, fdid , (uint32_t)tm_start );
    244239#endif
    245240                this->errno = EINVAL;
     
    261256
    262257#if DEBUG_SYSCALLS_ERROR
    263 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    264 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped\n",
    265 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
     258printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped / cycle %d\n",
     259__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
    266260#endif
    267261                this->errno = EINVAL;
     
    275269                                 &k_sockaddr.sin_port );
    276270
    277             if( ret )
    278             {
    279 
    280 #if DEBUG_SYSCALLS_ERROR
    281 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    282 printk("\n[ERROR] in %s : thread[%x,%x] / ACCEPT / cannot access socket[%x,%d]\n",
    283 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid );
     271            if( ret < 0 )
     272            {
     273
     274#if DEBUG_SYSCALLS_ERROR
     275printk("\n[ERROR] in %s : thread[%x,%x] / ACCEPT / cannot access socket[%x,%d] / cycle %d\n",
     276__FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start );
    284277#endif
    285278                this->errno = EINVAL;
     
    305298
    306299#if DEBUG_SYSCALLS_ERROR
    307 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    308 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / buffer %x unmapped\n",
    309 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
    310 #endif
    311                 this->errno = EINVAL;
    312                 ret = -1;
    313                 break;
    314             }
    315 
    316             // check length
    317             if( length == 0 )
    318             {
    319 
    320 #if DEBUG_SYSCALLS_ERROR
    321 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    322 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / buffer length is 0\n",
    323 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
    324 #endif
    325                 this->errno = EINVAL;
    326                 ret = -1;
    327                 break;
    328             }
    329 
    330             // cal relevant relevant socket function
    331             ret = socket_send( fdid , u_buf , length );
    332 
    333             if( ret < 0 )
    334             {
    335 
    336 #if DEBUG_SYSCALLS_ERROR
    337 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    338 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / cannot access socket[%x,%d] \n",
    339 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid );
    340 #endif
    341                 this->errno = EINVAL;
    342             }
     300printk("\n[ERROR] in %s : thread[%x,%x] / SEND / u_buf %x unmapped / cycle %d\n",
     301__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
     302#endif
     303                this->errno = EINVAL;
     304                ret = -1;
     305                break;
     306            }
     307
     308            // check length argument
     309            if( (length == 0) || (length > (1<<CONFIG_SOCK_TX_BUF_ORDER)) )
     310            {
     311
     312#if DEBUG_SYSCALLS_ERROR
     313printk("\n[ERROR] in %s : thread[%x,%x] / SEND / bad buffer length %d / cycle %d\n",
     314__FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start );
     315#endif
     316                this->errno = EINVAL;
     317                ret = -1;
     318                break;
     319            }
     320
     321            // cal relevant socket function
     322            ret = socket_send( fdid,
     323                               u_buf,
     324                               length );
     325            if( ret < 0 )
     326            {
     327
     328#if DEBUG_SYSCALLS_ERROR
     329printk("\n[ERROR] in %s : thread[%x,%x] / SEND / cannot access socket[%x,%d] / cycle %d\n",
     330__FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start );
     331#endif
     332                this->errno = EINVAL;
     333            }
     334
     335#if DEBUG_SYS_SOCKET
     336if( DEBUG_SYS_SOCKET < (uint32_t)tm_start )
     337{
     338    hal_copy_from_uspace( XPTR( local_cxy , &kbuf ) , u_buf , ret );
     339    printk("\n[%s] thread[%x,%x] send %d bytes <%s>\n",
     340    __FUNCTION__, process->pid, this->trdid , ret, kbuf );
     341}
     342#endif
    343343            break;
    344344        }
     
    355355
    356356#if DEBUG_SYSCALLS_ERROR
    357 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    358 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / buffer %x unmapped\n",
    359 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
    360 #endif
    361                 this->errno = EINVAL;
    362                 ret = -1;
    363                 break;
    364             }
    365 
    366             // check length
    367             if( length == 0 )
    368             {
    369 
    370 #if DEBUG_SYSCALLS_ERROR
    371 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    372 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / buffer length is 0\n",
    373 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 );
     357printk("\n[ERROR] in %s : thread[%x,%x] / RECV / u_buf %x unmapped / cycle %d\n",
     358__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
     359#endif
     360                this->errno = EINVAL;
     361                ret = -1;
     362                break;
     363            }
     364
     365            // check length argument
     366            if( (length == 0) || (length > (1<<CONFIG_SOCK_RX_BUF_ORDER)) )
     367            {
     368
     369#if DEBUG_SYSCALLS_ERROR
     370printk("\n[ERROR] in %s : thread[%x,%x] / RECV / bad buffer length %d / cycle %d\n",
     371__FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start );
    374372#endif
    375373                this->errno = EINVAL;
     
    379377
    380378            // cal relevant kernel socket function
    381             ret =  socket_recv( fdid , u_buf , length );
    382 
    383             if( ret < 0 )
    384             {
    385 
    386 #if DEBUG_SYSCALLS_ERROR
    387 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    388 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / cannot access socket[%x,%d] \n",
    389 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid );
    390 #endif
    391                 this->errno = EINVAL;
    392             }
     379            ret =  socket_recv( fdid,
     380                                u_buf,
     381                                length );
     382            if( ret < 0 )
     383            {
     384
     385#if DEBUG_SYSCALLS_ERROR
     386printk("\n[ERROR] in %s : thread[%x,%x] / RECV / cannot access socket[%x,%d] / cycle %d\n",
     387__FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start );
     388#endif
     389                this->errno = EINVAL;
     390            }
     391
     392#if DEBUG_SYS_SOCKET
     393if( DEBUG_SYS_SOCKET < (uint32_t)tm_start )
     394{
     395    hal_copy_from_uspace( XPTR( local_cxy , &kbuf ) , u_buf , ret );
     396    printk("\n[%s] thread[%x,%x] received %d bytes <%s>\n",
     397    __FUNCTION__, process->pid, this->trdid , ret, kbuf );
     398}
     399#endif
     400            break;
     401        }
     402        /////////////////
     403        case SOCK_SENDTO:
     404        {
     405            sockaddr_in_t k_remote_addr;
     406
     407            uint32_t      fdid          = (uint32_t)arg1 & 0x0000FFFF;
     408            uint32_t      length        = (uint32_t)arg1 >> 16;
     409            uint8_t     * u_buf         = (uint8_t *)(intptr_t)arg2;
     410            sockaddr_t  * u_remote_addr = (sockaddr_t *)(intptr_t)arg3;
     411
     412            // check u_buf mapped in user space
     413            if( vmm_get_vseg( process , (intptr_t)arg2 , &vseg ) )
     414            {
     415
     416#if DEBUG_SYSCALLS_ERROR
     417printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / u_buf %x unmapped / cycle %d\n",
     418__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
     419#endif
     420                this->errno = EINVAL;
     421                ret = -1;
     422                break;
     423            }
     424
     425            // check u_remote_addr mapped in user space
     426            if( vmm_get_vseg( process , (intptr_t)arg3 , &vseg ) )
     427            {
     428
     429#if DEBUG_SYSCALLS_ERROR
     430printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / u_remote_addr %x unmapped / cycle %d\n",
     431__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg3 , (uint32_t)tm_start );
     432#endif
     433                this->errno = EINVAL;
     434                ret = -1;
     435                break;
     436            }
     437
     438            // check length argument
     439            if( (length == 0) || (length > (1<<CONFIG_SOCK_TX_BUF_ORDER)) )
     440            {
     441
     442#if DEBUG_SYSCALLS_ERROR
     443printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / bad length %d / cycle %d\n",
     444__FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start );
     445#endif
     446                this->errno = EINVAL;
     447                ret = -1;
     448                break;
     449            }
     450
     451            // make a kernel copy of the sockaddr_t structure
     452            hal_copy_from_uspace( XPTR( local_cxy , &k_remote_addr ),
     453                                  u_remote_addr, sizeof(sockaddr_t) );
     454
     455            // cal relevant socket function
     456            ret = socket_sendto( fdid,
     457                                 u_buf,
     458                                 length,
     459                                 k_remote_addr.sin_addr,
     460                                 k_remote_addr.sin_port );
     461            if( ret < 0 )
     462            {
     463
     464#if DEBUG_SYSCALLS_ERROR
     465printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / cannot access socket[%x,%d] / cycle %d\n",
     466__FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start );
     467#endif
     468                this->errno = EINVAL;
     469            }
     470
     471            break;
     472        }
     473        ///////////////////
     474        case SOCK_RECVFROM:
     475        {
     476            sockaddr_in_t k_remote_addr;
     477
     478            uint32_t      fdid          = (uint32_t)arg1 & 0x0000FFFF;
     479            uint32_t      length        = (uint32_t)arg1 >> 16;
     480            uint8_t     * u_buf         = (uint8_t *)(intptr_t)arg2;
     481            sockaddr_t  * u_remote_addr = (sockaddr_t *)(intptr_t)arg3;
     482
     483            // check buffer is mapped in user space
     484            if( vmm_get_vseg( process , (intptr_t)arg2 , &vseg ) )
     485            {
     486
     487#if DEBUG_SYSCALLS_ERROR
     488printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / u_buf %x unmapped / cycle %d\n",
     489__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start );
     490#endif
     491                this->errno = EINVAL;
     492                ret = -1;
     493                break;
     494            }
     495
     496            // check u_remote_addr mapped in user space
     497            if( vmm_get_vseg( process , (intptr_t)arg3 , &vseg ) )
     498            {
     499
     500#if DEBUG_SYSCALLS_ERROR
     501printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / u_remote_addr %x unmapped / cycle %d\n",
     502__FUNCTION__ , process->pid , this->trdid , (intptr_t)arg3 , (uint32_t)tm_start );
     503#endif
     504                this->errno = EINVAL;
     505                ret = -1;
     506                break;
     507            }
     508
     509            // check length argument
     510            if( (length == 0) || (length > (1<<CONFIG_SOCK_RX_BUF_ORDER)) )
     511            {
     512
     513#if DEBUG_SYSCALLS_ERROR
     514printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / bad length %d / cycle %d\n",
     515__FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start );
     516#endif
     517                this->errno = EINVAL;
     518                ret = -1;
     519                break;
     520            }
     521
     522            // make a kernel copy of the sockaddr_t structure
     523            hal_copy_from_uspace( XPTR( local_cxy , &k_remote_addr ),
     524                                  u_remote_addr, sizeof(sockaddr_t) );
     525
     526            // cal relevant socket function
     527            ret = socket_recvfrom( fdid,
     528                                   u_buf,
     529                                   length,
     530                                   k_remote_addr.sin_addr,
     531                                   k_remote_addr.sin_port );
     532            if( ret < 0 )
     533            {
     534
     535#if DEBUG_SYSCALLS_ERROR
     536printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / cannot access socket[%x,%d] / cycle %d\n",
     537__FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start );
     538#endif
     539                this->errno = EINVAL;
     540            }
     541
    393542            break;
    394543        }
     
    398547
    399548#if DEBUG_SYSCALLS_ERROR
    400 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start )
    401 printk("\n[ERROR] in %s : thread[%x,%x] / undefined socket operation %d\n",
    402 __FUNCTION__ , process->pid , this->trdid , cmd );
     549printk("\n[ERROR] in %s : thread[%x,%x] / undefined socket operation %d / cycle %d\n",
     550__FUNCTION__ , process->pid , this->trdid , cmd , (uint32_t)tm_start );
    403551#endif
    404552            this->errno = EINVAL;
     
    413561
    414562#if DEBUG_SYS_SOCKET
    415 if( DEBUG_SYS_SOCKET < tm_end )
    416 printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
    417 __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
     563printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n",
     564__FUNCTION__, process->pid, this->trdid, socket_user_cmd_str(cmd), (uint32_t)tm_end );
    418565#endif
    419566
Note: See TracChangeset for help on using the changeset viewer.