datapath.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  62. .name = OVS_FLOW_MCGROUP,
  63. };
  64. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  65. .name = OVS_DATAPATH_MCGROUP,
  66. };
  67. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  68. .name = OVS_VPORT_MCGROUP,
  69. };
  70. /* Check if need to build a reply message.
  71. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  72. static bool ovs_must_notify(struct genl_info *info,
  73. const struct genl_multicast_group *grp)
  74. {
  75. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  76. netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
  77. }
  78. static void ovs_notify(struct genl_family *family,
  79. struct sk_buff *skb, struct genl_info *info)
  80. {
  81. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  82. 0, info->nlhdr, GFP_KERNEL);
  83. }
  84. /**
  85. * DOC: Locking:
  86. *
  87. * All writes e.g. Writes to device state (add/remove datapath, port, set
  88. * operations on vports, etc.), Writes to other state (flow table
  89. * modifications, set miscellaneous datapath parameters, etc.) are protected
  90. * by ovs_lock.
  91. *
  92. * Reads are protected by RCU.
  93. *
  94. * There are a few special cases (mostly stats) that have their own
  95. * synchronization but they nest under all of above and don't interact with
  96. * each other.
  97. *
  98. * The RTNL lock nests inside ovs_mutex.
  99. */
  100. static DEFINE_MUTEX(ovs_mutex);
  101. void ovs_lock(void)
  102. {
  103. mutex_lock(&ovs_mutex);
  104. }
  105. void ovs_unlock(void)
  106. {
  107. mutex_unlock(&ovs_mutex);
  108. }
  109. #ifdef CONFIG_LOCKDEP
  110. int lockdep_ovsl_is_held(void)
  111. {
  112. if (debug_locks)
  113. return lockdep_is_held(&ovs_mutex);
  114. else
  115. return 1;
  116. }
  117. #endif
  118. static struct vport *new_vport(const struct vport_parms *);
  119. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  120. const struct dp_upcall_info *);
  121. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  122. const struct dp_upcall_info *);
  123. /* Must be called with rcu_read_lock or ovs_mutex. */
  124. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  125. {
  126. struct datapath *dp = NULL;
  127. struct net_device *dev;
  128. rcu_read_lock();
  129. dev = dev_get_by_index_rcu(net, dp_ifindex);
  130. if (dev) {
  131. struct vport *vport = ovs_internal_dev_get_vport(dev);
  132. if (vport)
  133. dp = vport->dp;
  134. }
  135. rcu_read_unlock();
  136. return dp;
  137. }
  138. /* Must be called with rcu_read_lock or ovs_mutex. */
  139. static const char *ovs_dp_name(const struct datapath *dp)
  140. {
  141. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  142. return vport->ops->get_name(vport);
  143. }
  144. static int get_dpifindex(struct datapath *dp)
  145. {
  146. struct vport *local;
  147. int ifindex;
  148. rcu_read_lock();
  149. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  150. if (local)
  151. ifindex = netdev_vport_priv(local)->dev->ifindex;
  152. else
  153. ifindex = 0;
  154. rcu_read_unlock();
  155. return ifindex;
  156. }
  157. static void destroy_dp_rcu(struct rcu_head *rcu)
  158. {
  159. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  160. free_percpu(dp->stats_percpu);
  161. release_net(ovs_dp_get_net(dp));
  162. kfree(dp->ports);
  163. kfree(dp);
  164. }
  165. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  166. u16 port_no)
  167. {
  168. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  169. }
  170. /* Called with ovs_mutex or RCU read lock. */
  171. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  172. {
  173. struct vport *vport;
  174. struct hlist_head *head;
  175. head = vport_hash_bucket(dp, port_no);
  176. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  177. if (vport->port_no == port_no)
  178. return vport;
  179. }
  180. return NULL;
  181. }
  182. /* Called with ovs_mutex. */
  183. static struct vport *new_vport(const struct vport_parms *parms)
  184. {
  185. struct vport *vport;
  186. vport = ovs_vport_add(parms);
  187. if (!IS_ERR(vport)) {
  188. struct datapath *dp = parms->dp;
  189. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  190. hlist_add_head_rcu(&vport->dp_hash_node, head);
  191. }
  192. return vport;
  193. }
  194. void ovs_dp_detach_port(struct vport *p)
  195. {
  196. ASSERT_OVSL();
  197. /* First drop references to device. */
  198. hlist_del_rcu(&p->dp_hash_node);
  199. /* Then destroy it. */
  200. ovs_vport_del(p);
  201. }
  202. /* Must be called with rcu_read_lock. */
  203. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  204. {
  205. struct datapath *dp = p->dp;
  206. struct sw_flow *flow;
  207. struct dp_stats_percpu *stats;
  208. struct sw_flow_key key;
  209. u64 *stats_counter;
  210. u32 n_mask_hit;
  211. int error;
  212. stats = this_cpu_ptr(dp->stats_percpu);
  213. /* Extract flow from 'skb' into 'key'. */
  214. error = ovs_flow_extract(skb, p->port_no, &key);
  215. if (unlikely(error)) {
  216. kfree_skb(skb);
  217. return;
  218. }
  219. /* Look up flow. */
  220. flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
  221. if (unlikely(!flow)) {
  222. struct dp_upcall_info upcall;
  223. upcall.cmd = OVS_PACKET_CMD_MISS;
  224. upcall.key = &key;
  225. upcall.userdata = NULL;
  226. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  227. error = ovs_dp_upcall(dp, skb, &upcall);
  228. if (unlikely(error))
  229. kfree_skb(skb);
  230. else
  231. consume_skb(skb);
  232. stats_counter = &stats->n_missed;
  233. goto out;
  234. }
  235. OVS_CB(skb)->flow = flow;
  236. OVS_CB(skb)->pkt_key = &key;
  237. ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
  238. ovs_execute_actions(dp, skb);
  239. stats_counter = &stats->n_hit;
  240. out:
  241. /* Update datapath statistics. */
  242. u64_stats_update_begin(&stats->syncp);
  243. (*stats_counter)++;
  244. stats->n_mask_hit += n_mask_hit;
  245. u64_stats_update_end(&stats->syncp);
  246. }
  247. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  248. const struct dp_upcall_info *upcall_info)
  249. {
  250. struct dp_stats_percpu *stats;
  251. int err;
  252. if (upcall_info->portid == 0) {
  253. err = -ENOTCONN;
  254. goto err;
  255. }
  256. if (!skb_is_gso(skb))
  257. err = queue_userspace_packet(dp, skb, upcall_info);
  258. else
  259. err = queue_gso_packets(dp, skb, upcall_info);
  260. if (err)
  261. goto err;
  262. return 0;
  263. err:
  264. stats = this_cpu_ptr(dp->stats_percpu);
  265. u64_stats_update_begin(&stats->syncp);
  266. stats->n_lost++;
  267. u64_stats_update_end(&stats->syncp);
  268. return err;
  269. }
  270. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  271. const struct dp_upcall_info *upcall_info)
  272. {
  273. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  274. struct dp_upcall_info later_info;
  275. struct sw_flow_key later_key;
  276. struct sk_buff *segs, *nskb;
  277. int err;
  278. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  279. if (IS_ERR(segs))
  280. return PTR_ERR(segs);
  281. /* Queue all of the segments. */
  282. skb = segs;
  283. do {
  284. err = queue_userspace_packet(dp, skb, upcall_info);
  285. if (err)
  286. break;
  287. if (skb == segs && gso_type & SKB_GSO_UDP) {
  288. /* The initial flow key extracted by ovs_flow_extract()
  289. * in this case is for a first fragment, so we need to
  290. * properly mark later fragments.
  291. */
  292. later_key = *upcall_info->key;
  293. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  294. later_info = *upcall_info;
  295. later_info.key = &later_key;
  296. upcall_info = &later_info;
  297. }
  298. } while ((skb = skb->next));
  299. /* Free all of the segments. */
  300. skb = segs;
  301. do {
  302. nskb = skb->next;
  303. if (err)
  304. kfree_skb(skb);
  305. else
  306. consume_skb(skb);
  307. } while ((skb = nskb));
  308. return err;
  309. }
  310. static size_t key_attr_size(void)
  311. {
  312. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  313. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  314. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  315. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  316. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  317. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  318. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  319. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  320. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  321. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  322. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  323. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  324. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  325. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  326. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  327. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  328. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  329. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  330. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  331. }
  332. static size_t upcall_msg_size(const struct nlattr *userdata,
  333. unsigned int hdrlen)
  334. {
  335. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  336. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  337. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  338. /* OVS_PACKET_ATTR_USERDATA */
  339. if (userdata)
  340. size += NLA_ALIGN(userdata->nla_len);
  341. return size;
  342. }
  343. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  344. const struct dp_upcall_info *upcall_info)
  345. {
  346. struct ovs_header *upcall;
  347. struct sk_buff *nskb = NULL;
  348. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  349. struct nlattr *nla;
  350. struct genl_info info = {
  351. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  352. .snd_portid = upcall_info->portid,
  353. };
  354. size_t len;
  355. unsigned int hlen;
  356. int err, dp_ifindex;
  357. dp_ifindex = get_dpifindex(dp);
  358. if (!dp_ifindex)
  359. return -ENODEV;
  360. if (vlan_tx_tag_present(skb)) {
  361. nskb = skb_clone(skb, GFP_ATOMIC);
  362. if (!nskb)
  363. return -ENOMEM;
  364. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  365. if (!nskb)
  366. return -ENOMEM;
  367. nskb->vlan_tci = 0;
  368. skb = nskb;
  369. }
  370. if (nla_attr_size(skb->len) > USHRT_MAX) {
  371. err = -EFBIG;
  372. goto out;
  373. }
  374. /* Complete checksum if needed */
  375. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  376. (err = skb_checksum_help(skb)))
  377. goto out;
  378. /* Older versions of OVS user space enforce alignment of the last
  379. * Netlink attribute to NLA_ALIGNTO which would require extensive
  380. * padding logic. Only perform zerocopy if padding is not required.
  381. */
  382. if (dp->user_features & OVS_DP_F_UNALIGNED)
  383. hlen = skb_zerocopy_headlen(skb);
  384. else
  385. hlen = skb->len;
  386. len = upcall_msg_size(upcall_info->userdata, hlen);
  387. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  388. if (!user_skb) {
  389. err = -ENOMEM;
  390. goto out;
  391. }
  392. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  393. 0, upcall_info->cmd);
  394. upcall->dp_ifindex = dp_ifindex;
  395. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  396. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  397. BUG_ON(err);
  398. nla_nest_end(user_skb, nla);
  399. if (upcall_info->userdata)
  400. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  401. nla_len(upcall_info->userdata),
  402. nla_data(upcall_info->userdata));
  403. /* Only reserve room for attribute header, packet data is added
  404. * in skb_zerocopy() */
  405. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  406. err = -ENOBUFS;
  407. goto out;
  408. }
  409. nla->nla_len = nla_attr_size(skb->len);
  410. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  411. if (err)
  412. goto out;
  413. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  414. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  415. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  416. if (plen > 0)
  417. memset(skb_put(user_skb, plen), 0, plen);
  418. }
  419. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  420. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  421. user_skb = NULL;
  422. out:
  423. if (err)
  424. skb_tx_error(skb);
  425. kfree_skb(user_skb);
  426. kfree_skb(nskb);
  427. return err;
  428. }
  429. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  430. {
  431. struct ovs_header *ovs_header = info->userhdr;
  432. struct nlattr **a = info->attrs;
  433. struct sw_flow_actions *acts;
  434. struct sk_buff *packet;
  435. struct sw_flow *flow;
  436. struct datapath *dp;
  437. struct ethhdr *eth;
  438. int len;
  439. int err;
  440. err = -EINVAL;
  441. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  442. !a[OVS_PACKET_ATTR_ACTIONS])
  443. goto err;
  444. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  445. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  446. err = -ENOMEM;
  447. if (!packet)
  448. goto err;
  449. skb_reserve(packet, NET_IP_ALIGN);
  450. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  451. skb_reset_mac_header(packet);
  452. eth = eth_hdr(packet);
  453. /* Normally, setting the skb 'protocol' field would be handled by a
  454. * call to eth_type_trans(), but it assumes there's a sending
  455. * device, which we may not have. */
  456. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  457. packet->protocol = eth->h_proto;
  458. else
  459. packet->protocol = htons(ETH_P_802_2);
  460. /* Build an sw_flow for sending this packet. */
  461. flow = ovs_flow_alloc();
  462. err = PTR_ERR(flow);
  463. if (IS_ERR(flow))
  464. goto err_kfree_skb;
  465. err = ovs_flow_extract(packet, -1, &flow->key);
  466. if (err)
  467. goto err_flow_free;
  468. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  469. if (err)
  470. goto err_flow_free;
  471. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  472. err = PTR_ERR(acts);
  473. if (IS_ERR(acts))
  474. goto err_flow_free;
  475. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  476. &flow->key, 0, &acts);
  477. rcu_assign_pointer(flow->sf_acts, acts);
  478. if (err)
  479. goto err_flow_free;
  480. OVS_CB(packet)->flow = flow;
  481. OVS_CB(packet)->pkt_key = &flow->key;
  482. packet->priority = flow->key.phy.priority;
  483. packet->mark = flow->key.phy.skb_mark;
  484. rcu_read_lock();
  485. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  486. err = -ENODEV;
  487. if (!dp)
  488. goto err_unlock;
  489. local_bh_disable();
  490. err = ovs_execute_actions(dp, packet);
  491. local_bh_enable();
  492. rcu_read_unlock();
  493. ovs_flow_free(flow, false);
  494. return err;
  495. err_unlock:
  496. rcu_read_unlock();
  497. err_flow_free:
  498. ovs_flow_free(flow, false);
  499. err_kfree_skb:
  500. kfree_skb(packet);
  501. err:
  502. return err;
  503. }
  504. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  505. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  506. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  507. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  508. };
  509. static const struct genl_ops dp_packet_genl_ops[] = {
  510. { .cmd = OVS_PACKET_CMD_EXECUTE,
  511. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  512. .policy = packet_policy,
  513. .doit = ovs_packet_cmd_execute
  514. }
  515. };
  516. static struct genl_family dp_packet_genl_family = {
  517. .id = GENL_ID_GENERATE,
  518. .hdrsize = sizeof(struct ovs_header),
  519. .name = OVS_PACKET_FAMILY,
  520. .version = OVS_PACKET_VERSION,
  521. .maxattr = OVS_PACKET_ATTR_MAX,
  522. .netnsok = true,
  523. .parallel_ops = true,
  524. .ops = dp_packet_genl_ops,
  525. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  526. };
  527. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  528. struct ovs_dp_megaflow_stats *mega_stats)
  529. {
  530. int i;
  531. memset(mega_stats, 0, sizeof(*mega_stats));
  532. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  533. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  534. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  535. for_each_possible_cpu(i) {
  536. const struct dp_stats_percpu *percpu_stats;
  537. struct dp_stats_percpu local_stats;
  538. unsigned int start;
  539. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  540. do {
  541. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  542. local_stats = *percpu_stats;
  543. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  544. stats->n_hit += local_stats.n_hit;
  545. stats->n_missed += local_stats.n_missed;
  546. stats->n_lost += local_stats.n_lost;
  547. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  548. }
  549. }
  550. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  551. {
  552. return NLMSG_ALIGN(sizeof(struct ovs_header))
  553. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  554. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  555. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  556. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  557. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  558. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  559. }
  560. /* Called with ovs_mutex or RCU read lock. */
  561. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  562. struct sk_buff *skb, u32 portid,
  563. u32 seq, u32 flags, u8 cmd)
  564. {
  565. const int skb_orig_len = skb->len;
  566. struct nlattr *start;
  567. struct ovs_flow_stats stats;
  568. __be16 tcp_flags;
  569. unsigned long used;
  570. struct ovs_header *ovs_header;
  571. struct nlattr *nla;
  572. int err;
  573. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  574. if (!ovs_header)
  575. return -EMSGSIZE;
  576. ovs_header->dp_ifindex = dp_ifindex;
  577. /* Fill flow key. */
  578. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  579. if (!nla)
  580. goto nla_put_failure;
  581. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  582. if (err)
  583. goto error;
  584. nla_nest_end(skb, nla);
  585. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  586. if (!nla)
  587. goto nla_put_failure;
  588. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  589. if (err)
  590. goto error;
  591. nla_nest_end(skb, nla);
  592. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  593. if (used &&
  594. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  595. goto nla_put_failure;
  596. if (stats.n_packets &&
  597. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  598. goto nla_put_failure;
  599. if ((u8)ntohs(tcp_flags) &&
  600. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  601. goto nla_put_failure;
  602. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  603. * this is the first flow to be dumped into 'skb'. This is unusual for
  604. * Netlink but individual action lists can be longer than
  605. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  606. * The userspace caller can always fetch the actions separately if it
  607. * really wants them. (Most userspace callers in fact don't care.)
  608. *
  609. * This can only fail for dump operations because the skb is always
  610. * properly sized for single flows.
  611. */
  612. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  613. if (start) {
  614. const struct sw_flow_actions *sf_acts;
  615. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  616. err = ovs_nla_put_actions(sf_acts->actions,
  617. sf_acts->actions_len, skb);
  618. if (!err)
  619. nla_nest_end(skb, start);
  620. else {
  621. if (skb_orig_len)
  622. goto error;
  623. nla_nest_cancel(skb, start);
  624. }
  625. } else if (skb_orig_len)
  626. goto nla_put_failure;
  627. return genlmsg_end(skb, ovs_header);
  628. nla_put_failure:
  629. err = -EMSGSIZE;
  630. error:
  631. genlmsg_cancel(skb, ovs_header);
  632. return err;
  633. }
  634. /* May not be called with RCU read lock. */
  635. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  636. struct genl_info *info,
  637. bool always)
  638. {
  639. struct sk_buff *skb;
  640. if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
  641. return NULL;
  642. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  643. if (!skb)
  644. return ERR_PTR(-ENOMEM);
  645. return skb;
  646. }
  647. /* Called with ovs_mutex. */
  648. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  649. int dp_ifindex,
  650. struct genl_info *info, u8 cmd,
  651. bool always)
  652. {
  653. struct sk_buff *skb;
  654. int retval;
  655. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  656. always);
  657. if (IS_ERR_OR_NULL(skb))
  658. return skb;
  659. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  660. info->snd_portid, info->snd_seq, 0,
  661. cmd);
  662. BUG_ON(retval < 0);
  663. return skb;
  664. }
  665. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  666. {
  667. struct nlattr **a = info->attrs;
  668. struct ovs_header *ovs_header = info->userhdr;
  669. struct sw_flow *flow, *new_flow;
  670. struct sw_flow_mask mask;
  671. struct sk_buff *reply;
  672. struct datapath *dp;
  673. struct sw_flow_actions *acts;
  674. struct sw_flow_match match;
  675. int error;
  676. /* Must have key and actions. */
  677. error = -EINVAL;
  678. if (!a[OVS_FLOW_ATTR_KEY])
  679. goto error;
  680. if (!a[OVS_FLOW_ATTR_ACTIONS])
  681. goto error;
  682. /* Most of the time we need to allocate a new flow, do it before
  683. * locking.
  684. */
  685. new_flow = ovs_flow_alloc();
  686. if (IS_ERR(new_flow)) {
  687. error = PTR_ERR(new_flow);
  688. goto error;
  689. }
  690. /* Extract key. */
  691. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  692. error = ovs_nla_get_match(&match,
  693. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  694. if (error)
  695. goto err_kfree_flow;
  696. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  697. /* Validate actions. */
  698. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  699. error = PTR_ERR(acts);
  700. if (IS_ERR(acts))
  701. goto err_kfree_flow;
  702. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  703. 0, &acts);
  704. if (error) {
  705. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  706. goto err_kfree_acts;
  707. }
  708. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  709. if (IS_ERR(reply)) {
  710. error = PTR_ERR(reply);
  711. goto err_kfree_acts;
  712. }
  713. ovs_lock();
  714. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  715. if (unlikely(!dp)) {
  716. error = -ENODEV;
  717. goto err_unlock_ovs;
  718. }
  719. /* Check if this is a duplicate flow */
  720. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  721. if (likely(!flow)) {
  722. rcu_assign_pointer(new_flow->sf_acts, acts);
  723. /* Put flow in bucket. */
  724. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  725. if (unlikely(error)) {
  726. acts = NULL;
  727. goto err_unlock_ovs;
  728. }
  729. if (unlikely(reply)) {
  730. error = ovs_flow_cmd_fill_info(new_flow,
  731. ovs_header->dp_ifindex,
  732. reply, info->snd_portid,
  733. info->snd_seq, 0,
  734. OVS_FLOW_CMD_NEW);
  735. BUG_ON(error < 0);
  736. }
  737. ovs_unlock();
  738. } else {
  739. struct sw_flow_actions *old_acts;
  740. /* Bail out if we're not allowed to modify an existing flow.
  741. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  742. * because Generic Netlink treats the latter as a dump
  743. * request. We also accept NLM_F_EXCL in case that bug ever
  744. * gets fixed.
  745. */
  746. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  747. | NLM_F_EXCL))) {
  748. error = -EEXIST;
  749. goto err_unlock_ovs;
  750. }
  751. /* The unmasked key has to be the same for flow updates. */
  752. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  753. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  754. if (!flow) {
  755. error = -ENOENT;
  756. goto err_unlock_ovs;
  757. }
  758. }
  759. /* Update actions. */
  760. old_acts = ovsl_dereference(flow->sf_acts);
  761. rcu_assign_pointer(flow->sf_acts, acts);
  762. if (unlikely(reply)) {
  763. error = ovs_flow_cmd_fill_info(flow,
  764. ovs_header->dp_ifindex,
  765. reply, info->snd_portid,
  766. info->snd_seq, 0,
  767. OVS_FLOW_CMD_NEW);
  768. BUG_ON(error < 0);
  769. }
  770. ovs_unlock();
  771. ovs_nla_free_flow_actions(old_acts);
  772. ovs_flow_free(new_flow, false);
  773. }
  774. if (reply)
  775. ovs_notify(&dp_flow_genl_family, reply, info);
  776. return 0;
  777. err_unlock_ovs:
  778. ovs_unlock();
  779. kfree_skb(reply);
  780. err_kfree_acts:
  781. kfree(acts);
  782. err_kfree_flow:
  783. ovs_flow_free(new_flow, false);
  784. error:
  785. return error;
  786. }
  787. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  788. {
  789. struct nlattr **a = info->attrs;
  790. struct ovs_header *ovs_header = info->userhdr;
  791. struct sw_flow_key key, masked_key;
  792. struct sw_flow *flow;
  793. struct sw_flow_mask mask;
  794. struct sk_buff *reply = NULL;
  795. struct datapath *dp;
  796. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  797. struct sw_flow_match match;
  798. int error;
  799. /* Extract key. */
  800. error = -EINVAL;
  801. if (!a[OVS_FLOW_ATTR_KEY])
  802. goto error;
  803. ovs_match_init(&match, &key, &mask);
  804. error = ovs_nla_get_match(&match,
  805. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  806. if (error)
  807. goto error;
  808. /* Validate actions. */
  809. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  810. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  811. error = PTR_ERR(acts);
  812. if (IS_ERR(acts))
  813. goto error;
  814. ovs_flow_mask_key(&masked_key, &key, &mask);
  815. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  816. &masked_key, 0, &acts);
  817. if (error) {
  818. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  819. goto err_kfree_acts;
  820. }
  821. }
  822. /* Can allocate before locking if have acts. */
  823. if (acts) {
  824. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  825. if (IS_ERR(reply)) {
  826. error = PTR_ERR(reply);
  827. goto err_kfree_acts;
  828. }
  829. }
  830. ovs_lock();
  831. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  832. if (unlikely(!dp)) {
  833. error = -ENODEV;
  834. goto err_unlock_ovs;
  835. }
  836. /* Check that the flow exists. */
  837. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  838. if (unlikely(!flow)) {
  839. error = -ENOENT;
  840. goto err_unlock_ovs;
  841. }
  842. /* Update actions, if present. */
  843. if (likely(acts)) {
  844. old_acts = ovsl_dereference(flow->sf_acts);
  845. rcu_assign_pointer(flow->sf_acts, acts);
  846. if (unlikely(reply)) {
  847. error = ovs_flow_cmd_fill_info(flow,
  848. ovs_header->dp_ifindex,
  849. reply, info->snd_portid,
  850. info->snd_seq, 0,
  851. OVS_FLOW_CMD_NEW);
  852. BUG_ON(error < 0);
  853. }
  854. } else {
  855. /* Could not alloc without acts before locking. */
  856. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  857. info, OVS_FLOW_CMD_NEW, false);
  858. if (unlikely(IS_ERR(reply))) {
  859. error = PTR_ERR(reply);
  860. goto err_unlock_ovs;
  861. }
  862. }
  863. /* Clear stats. */
  864. if (a[OVS_FLOW_ATTR_CLEAR])
  865. ovs_flow_stats_clear(flow);
  866. ovs_unlock();
  867. if (reply)
  868. ovs_notify(&dp_flow_genl_family, reply, info);
  869. if (old_acts)
  870. ovs_nla_free_flow_actions(old_acts);
  871. return 0;
  872. err_unlock_ovs:
  873. ovs_unlock();
  874. kfree_skb(reply);
  875. err_kfree_acts:
  876. kfree(acts);
  877. error:
  878. return error;
  879. }
  880. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  881. {
  882. struct nlattr **a = info->attrs;
  883. struct ovs_header *ovs_header = info->userhdr;
  884. struct sw_flow_key key;
  885. struct sk_buff *reply;
  886. struct sw_flow *flow;
  887. struct datapath *dp;
  888. struct sw_flow_match match;
  889. int err;
  890. if (!a[OVS_FLOW_ATTR_KEY]) {
  891. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  892. return -EINVAL;
  893. }
  894. ovs_match_init(&match, &key, NULL);
  895. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  896. if (err)
  897. return err;
  898. ovs_lock();
  899. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  900. if (!dp) {
  901. err = -ENODEV;
  902. goto unlock;
  903. }
  904. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  905. if (!flow) {
  906. err = -ENOENT;
  907. goto unlock;
  908. }
  909. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  910. OVS_FLOW_CMD_NEW, true);
  911. if (IS_ERR(reply)) {
  912. err = PTR_ERR(reply);
  913. goto unlock;
  914. }
  915. ovs_unlock();
  916. return genlmsg_reply(reply, info);
  917. unlock:
  918. ovs_unlock();
  919. return err;
  920. }
  921. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  922. {
  923. struct nlattr **a = info->attrs;
  924. struct ovs_header *ovs_header = info->userhdr;
  925. struct sw_flow_key key;
  926. struct sk_buff *reply;
  927. struct sw_flow *flow;
  928. struct datapath *dp;
  929. struct sw_flow_match match;
  930. int err;
  931. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  932. ovs_match_init(&match, &key, NULL);
  933. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  934. if (unlikely(err))
  935. return err;
  936. }
  937. ovs_lock();
  938. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  939. if (unlikely(!dp)) {
  940. err = -ENODEV;
  941. goto unlock;
  942. }
  943. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  944. err = ovs_flow_tbl_flush(&dp->table);
  945. goto unlock;
  946. }
  947. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  948. if (unlikely(!flow)) {
  949. err = -ENOENT;
  950. goto unlock;
  951. }
  952. ovs_flow_tbl_remove(&dp->table, flow);
  953. ovs_unlock();
  954. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  955. info, false);
  956. if (likely(reply)) {
  957. if (likely(!IS_ERR(reply))) {
  958. rcu_read_lock(); /*To keep RCU checker happy. */
  959. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  960. reply, info->snd_portid,
  961. info->snd_seq, 0,
  962. OVS_FLOW_CMD_DEL);
  963. rcu_read_unlock();
  964. BUG_ON(err < 0);
  965. ovs_notify(&dp_flow_genl_family, reply, info);
  966. } else {
  967. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  968. }
  969. }
  970. ovs_flow_free(flow, true);
  971. return 0;
  972. unlock:
  973. ovs_unlock();
  974. return err;
  975. }
  976. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  977. {
  978. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  979. struct table_instance *ti;
  980. struct datapath *dp;
  981. rcu_read_lock();
  982. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  983. if (!dp) {
  984. rcu_read_unlock();
  985. return -ENODEV;
  986. }
  987. ti = rcu_dereference(dp->table.ti);
  988. for (;;) {
  989. struct sw_flow *flow;
  990. u32 bucket, obj;
  991. bucket = cb->args[0];
  992. obj = cb->args[1];
  993. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  994. if (!flow)
  995. break;
  996. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  997. NETLINK_CB(cb->skb).portid,
  998. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  999. OVS_FLOW_CMD_NEW) < 0)
  1000. break;
  1001. cb->args[0] = bucket;
  1002. cb->args[1] = obj;
  1003. }
  1004. rcu_read_unlock();
  1005. return skb->len;
  1006. }
  1007. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1008. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1009. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1010. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1011. };
  1012. static const struct genl_ops dp_flow_genl_ops[] = {
  1013. { .cmd = OVS_FLOW_CMD_NEW,
  1014. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1015. .policy = flow_policy,
  1016. .doit = ovs_flow_cmd_new
  1017. },
  1018. { .cmd = OVS_FLOW_CMD_DEL,
  1019. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1020. .policy = flow_policy,
  1021. .doit = ovs_flow_cmd_del
  1022. },
  1023. { .cmd = OVS_FLOW_CMD_GET,
  1024. .flags = 0, /* OK for unprivileged users. */
  1025. .policy = flow_policy,
  1026. .doit = ovs_flow_cmd_get,
  1027. .dumpit = ovs_flow_cmd_dump
  1028. },
  1029. { .cmd = OVS_FLOW_CMD_SET,
  1030. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1031. .policy = flow_policy,
  1032. .doit = ovs_flow_cmd_set,
  1033. },
  1034. };
  1035. static struct genl_family dp_flow_genl_family = {
  1036. .id = GENL_ID_GENERATE,
  1037. .hdrsize = sizeof(struct ovs_header),
  1038. .name = OVS_FLOW_FAMILY,
  1039. .version = OVS_FLOW_VERSION,
  1040. .maxattr = OVS_FLOW_ATTR_MAX,
  1041. .netnsok = true,
  1042. .parallel_ops = true,
  1043. .ops = dp_flow_genl_ops,
  1044. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1045. .mcgrps = &ovs_dp_flow_multicast_group,
  1046. .n_mcgrps = 1,
  1047. };
  1048. static size_t ovs_dp_cmd_msg_size(void)
  1049. {
  1050. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1051. msgsize += nla_total_size(IFNAMSIZ);
  1052. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1053. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1054. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1055. return msgsize;
  1056. }
  1057. /* Called with ovs_mutex or RCU read lock. */
  1058. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1059. u32 portid, u32 seq, u32 flags, u8 cmd)
  1060. {
  1061. struct ovs_header *ovs_header;
  1062. struct ovs_dp_stats dp_stats;
  1063. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1064. int err;
  1065. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1066. flags, cmd);
  1067. if (!ovs_header)
  1068. goto error;
  1069. ovs_header->dp_ifindex = get_dpifindex(dp);
  1070. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1071. if (err)
  1072. goto nla_put_failure;
  1073. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1074. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1075. &dp_stats))
  1076. goto nla_put_failure;
  1077. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1078. sizeof(struct ovs_dp_megaflow_stats),
  1079. &dp_megaflow_stats))
  1080. goto nla_put_failure;
  1081. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1082. goto nla_put_failure;
  1083. return genlmsg_end(skb, ovs_header);
  1084. nla_put_failure:
  1085. genlmsg_cancel(skb, ovs_header);
  1086. error:
  1087. return -EMSGSIZE;
  1088. }
  1089. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1090. {
  1091. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1092. }
  1093. /* Called with rcu_read_lock or ovs_mutex. */
  1094. static struct datapath *lookup_datapath(struct net *net,
  1095. struct ovs_header *ovs_header,
  1096. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1097. {
  1098. struct datapath *dp;
  1099. if (!a[OVS_DP_ATTR_NAME])
  1100. dp = get_dp(net, ovs_header->dp_ifindex);
  1101. else {
  1102. struct vport *vport;
  1103. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1104. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1105. }
  1106. return dp ? dp : ERR_PTR(-ENODEV);
  1107. }
  1108. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1109. {
  1110. struct datapath *dp;
  1111. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1112. if (IS_ERR(dp))
  1113. return;
  1114. WARN(dp->user_features, "Dropping previously announced user features\n");
  1115. dp->user_features = 0;
  1116. }
  1117. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1118. {
  1119. if (a[OVS_DP_ATTR_USER_FEATURES])
  1120. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1121. }
  1122. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1123. {
  1124. struct nlattr **a = info->attrs;
  1125. struct vport_parms parms;
  1126. struct sk_buff *reply;
  1127. struct datapath *dp;
  1128. struct vport *vport;
  1129. struct ovs_net *ovs_net;
  1130. int err, i;
  1131. err = -EINVAL;
  1132. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1133. goto err;
  1134. reply = ovs_dp_cmd_alloc_info(info);
  1135. if (!reply)
  1136. return -ENOMEM;
  1137. err = -ENOMEM;
  1138. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1139. if (dp == NULL)
  1140. goto err_free_reply;
  1141. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1142. /* Allocate table. */
  1143. err = ovs_flow_tbl_init(&dp->table);
  1144. if (err)
  1145. goto err_free_dp;
  1146. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1147. if (!dp->stats_percpu) {
  1148. err = -ENOMEM;
  1149. goto err_destroy_table;
  1150. }
  1151. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1152. GFP_KERNEL);
  1153. if (!dp->ports) {
  1154. err = -ENOMEM;
  1155. goto err_destroy_percpu;
  1156. }
  1157. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1158. INIT_HLIST_HEAD(&dp->ports[i]);
  1159. /* Set up our datapath device. */
  1160. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1161. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1162. parms.options = NULL;
  1163. parms.dp = dp;
  1164. parms.port_no = OVSP_LOCAL;
  1165. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1166. ovs_dp_change(dp, a);
  1167. /* So far only local changes have been made, now need the lock. */
  1168. ovs_lock();
  1169. vport = new_vport(&parms);
  1170. if (IS_ERR(vport)) {
  1171. err = PTR_ERR(vport);
  1172. if (err == -EBUSY)
  1173. err = -EEXIST;
  1174. if (err == -EEXIST) {
  1175. /* An outdated user space instance that does not understand
  1176. * the concept of user_features has attempted to create a new
  1177. * datapath and is likely to reuse it. Drop all user features.
  1178. */
  1179. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1180. ovs_dp_reset_user_features(skb, info);
  1181. }
  1182. goto err_destroy_ports_array;
  1183. }
  1184. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1185. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1186. BUG_ON(err < 0);
  1187. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1188. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1189. ovs_unlock();
  1190. ovs_notify(&dp_datapath_genl_family, reply, info);
  1191. return 0;
  1192. err_destroy_ports_array:
  1193. ovs_unlock();
  1194. kfree(dp->ports);
  1195. err_destroy_percpu:
  1196. free_percpu(dp->stats_percpu);
  1197. err_destroy_table:
  1198. ovs_flow_tbl_destroy(&dp->table, false);
  1199. err_free_dp:
  1200. release_net(ovs_dp_get_net(dp));
  1201. kfree(dp);
  1202. err_free_reply:
  1203. kfree_skb(reply);
  1204. err:
  1205. return err;
  1206. }
  1207. /* Called with ovs_mutex. */
  1208. static void __dp_destroy(struct datapath *dp)
  1209. {
  1210. int i;
  1211. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1212. struct vport *vport;
  1213. struct hlist_node *n;
  1214. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1215. if (vport->port_no != OVSP_LOCAL)
  1216. ovs_dp_detach_port(vport);
  1217. }
  1218. list_del_rcu(&dp->list_node);
  1219. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1220. * all ports in datapath are destroyed first before freeing datapath.
  1221. */
  1222. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1223. /* RCU destroy the flow table */
  1224. ovs_flow_tbl_destroy(&dp->table, true);
  1225. call_rcu(&dp->rcu, destroy_dp_rcu);
  1226. }
  1227. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1228. {
  1229. struct sk_buff *reply;
  1230. struct datapath *dp;
  1231. int err;
  1232. reply = ovs_dp_cmd_alloc_info(info);
  1233. if (!reply)
  1234. return -ENOMEM;
  1235. ovs_lock();
  1236. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1237. err = PTR_ERR(dp);
  1238. if (IS_ERR(dp))
  1239. goto err_unlock_free;
  1240. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1241. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1242. BUG_ON(err < 0);
  1243. __dp_destroy(dp);
  1244. ovs_unlock();
  1245. ovs_notify(&dp_datapath_genl_family, reply, info);
  1246. return 0;
  1247. err_unlock_free:
  1248. ovs_unlock();
  1249. kfree_skb(reply);
  1250. return err;
  1251. }
  1252. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1253. {
  1254. struct sk_buff *reply;
  1255. struct datapath *dp;
  1256. int err;
  1257. reply = ovs_dp_cmd_alloc_info(info);
  1258. if (!reply)
  1259. return -ENOMEM;
  1260. ovs_lock();
  1261. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1262. err = PTR_ERR(dp);
  1263. if (IS_ERR(dp))
  1264. goto err_unlock_free;
  1265. ovs_dp_change(dp, info->attrs);
  1266. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1267. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1268. BUG_ON(err < 0);
  1269. ovs_unlock();
  1270. ovs_notify(&dp_datapath_genl_family, reply, info);
  1271. return 0;
  1272. err_unlock_free:
  1273. ovs_unlock();
  1274. kfree_skb(reply);
  1275. return err;
  1276. }
  1277. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1278. {
  1279. struct sk_buff *reply;
  1280. struct datapath *dp;
  1281. int err;
  1282. reply = ovs_dp_cmd_alloc_info(info);
  1283. if (!reply)
  1284. return -ENOMEM;
  1285. rcu_read_lock();
  1286. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1287. if (IS_ERR(dp)) {
  1288. err = PTR_ERR(dp);
  1289. goto err_unlock_free;
  1290. }
  1291. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1292. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1293. BUG_ON(err < 0);
  1294. rcu_read_unlock();
  1295. return genlmsg_reply(reply, info);
  1296. err_unlock_free:
  1297. rcu_read_unlock();
  1298. kfree_skb(reply);
  1299. return err;
  1300. }
  1301. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1302. {
  1303. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1304. struct datapath *dp;
  1305. int skip = cb->args[0];
  1306. int i = 0;
  1307. rcu_read_lock();
  1308. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1309. if (i >= skip &&
  1310. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1311. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1312. OVS_DP_CMD_NEW) < 0)
  1313. break;
  1314. i++;
  1315. }
  1316. rcu_read_unlock();
  1317. cb->args[0] = i;
  1318. return skb->len;
  1319. }
  1320. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1321. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1322. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1323. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1324. };
  1325. static const struct genl_ops dp_datapath_genl_ops[] = {
  1326. { .cmd = OVS_DP_CMD_NEW,
  1327. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1328. .policy = datapath_policy,
  1329. .doit = ovs_dp_cmd_new
  1330. },
  1331. { .cmd = OVS_DP_CMD_DEL,
  1332. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1333. .policy = datapath_policy,
  1334. .doit = ovs_dp_cmd_del
  1335. },
  1336. { .cmd = OVS_DP_CMD_GET,
  1337. .flags = 0, /* OK for unprivileged users. */
  1338. .policy = datapath_policy,
  1339. .doit = ovs_dp_cmd_get,
  1340. .dumpit = ovs_dp_cmd_dump
  1341. },
  1342. { .cmd = OVS_DP_CMD_SET,
  1343. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1344. .policy = datapath_policy,
  1345. .doit = ovs_dp_cmd_set,
  1346. },
  1347. };
  1348. static struct genl_family dp_datapath_genl_family = {
  1349. .id = GENL_ID_GENERATE,
  1350. .hdrsize = sizeof(struct ovs_header),
  1351. .name = OVS_DATAPATH_FAMILY,
  1352. .version = OVS_DATAPATH_VERSION,
  1353. .maxattr = OVS_DP_ATTR_MAX,
  1354. .netnsok = true,
  1355. .parallel_ops = true,
  1356. .ops = dp_datapath_genl_ops,
  1357. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1358. .mcgrps = &ovs_dp_datapath_multicast_group,
  1359. .n_mcgrps = 1,
  1360. };
  1361. /* Called with ovs_mutex or RCU read lock. */
  1362. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1363. u32 portid, u32 seq, u32 flags, u8 cmd)
  1364. {
  1365. struct ovs_header *ovs_header;
  1366. struct ovs_vport_stats vport_stats;
  1367. int err;
  1368. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1369. flags, cmd);
  1370. if (!ovs_header)
  1371. return -EMSGSIZE;
  1372. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1373. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1374. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1375. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1376. vport->ops->get_name(vport)))
  1377. goto nla_put_failure;
  1378. ovs_vport_get_stats(vport, &vport_stats);
  1379. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1380. &vport_stats))
  1381. goto nla_put_failure;
  1382. if (ovs_vport_get_upcall_portids(vport, skb))
  1383. goto nla_put_failure;
  1384. err = ovs_vport_get_options(vport, skb);
  1385. if (err == -EMSGSIZE)
  1386. goto error;
  1387. return genlmsg_end(skb, ovs_header);
  1388. nla_put_failure:
  1389. err = -EMSGSIZE;
  1390. error:
  1391. genlmsg_cancel(skb, ovs_header);
  1392. return err;
  1393. }
  1394. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1395. {
  1396. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1397. }
  1398. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1399. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1400. u32 seq, u8 cmd)
  1401. {
  1402. struct sk_buff *skb;
  1403. int retval;
  1404. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1405. if (!skb)
  1406. return ERR_PTR(-ENOMEM);
  1407. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1408. BUG_ON(retval < 0);
  1409. return skb;
  1410. }
  1411. /* Called with ovs_mutex or RCU read lock. */
  1412. static struct vport *lookup_vport(struct net *net,
  1413. struct ovs_header *ovs_header,
  1414. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1415. {
  1416. struct datapath *dp;
  1417. struct vport *vport;
  1418. if (a[OVS_VPORT_ATTR_NAME]) {
  1419. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1420. if (!vport)
  1421. return ERR_PTR(-ENODEV);
  1422. if (ovs_header->dp_ifindex &&
  1423. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1424. return ERR_PTR(-ENODEV);
  1425. return vport;
  1426. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1427. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1428. if (port_no >= DP_MAX_PORTS)
  1429. return ERR_PTR(-EFBIG);
  1430. dp = get_dp(net, ovs_header->dp_ifindex);
  1431. if (!dp)
  1432. return ERR_PTR(-ENODEV);
  1433. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1434. if (!vport)
  1435. return ERR_PTR(-ENODEV);
  1436. return vport;
  1437. } else
  1438. return ERR_PTR(-EINVAL);
  1439. }
  1440. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1441. {
  1442. struct nlattr **a = info->attrs;
  1443. struct ovs_header *ovs_header = info->userhdr;
  1444. struct vport_parms parms;
  1445. struct sk_buff *reply;
  1446. struct vport *vport;
  1447. struct datapath *dp;
  1448. u32 port_no;
  1449. int err;
  1450. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1451. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1452. return -EINVAL;
  1453. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1454. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1455. if (port_no >= DP_MAX_PORTS)
  1456. return -EFBIG;
  1457. reply = ovs_vport_cmd_alloc_info();
  1458. if (!reply)
  1459. return -ENOMEM;
  1460. ovs_lock();
  1461. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1462. err = -ENODEV;
  1463. if (!dp)
  1464. goto exit_unlock_free;
  1465. if (port_no) {
  1466. vport = ovs_vport_ovsl(dp, port_no);
  1467. err = -EBUSY;
  1468. if (vport)
  1469. goto exit_unlock_free;
  1470. } else {
  1471. for (port_no = 1; ; port_no++) {
  1472. if (port_no >= DP_MAX_PORTS) {
  1473. err = -EFBIG;
  1474. goto exit_unlock_free;
  1475. }
  1476. vport = ovs_vport_ovsl(dp, port_no);
  1477. if (!vport)
  1478. break;
  1479. }
  1480. }
  1481. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1482. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1483. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1484. parms.dp = dp;
  1485. parms.port_no = port_no;
  1486. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1487. vport = new_vport(&parms);
  1488. err = PTR_ERR(vport);
  1489. if (IS_ERR(vport))
  1490. goto exit_unlock_free;
  1491. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1492. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1493. BUG_ON(err < 0);
  1494. ovs_unlock();
  1495. ovs_notify(&dp_vport_genl_family, reply, info);
  1496. return 0;
  1497. exit_unlock_free:
  1498. ovs_unlock();
  1499. kfree_skb(reply);
  1500. return err;
  1501. }
  1502. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1503. {
  1504. struct nlattr **a = info->attrs;
  1505. struct sk_buff *reply;
  1506. struct vport *vport;
  1507. int err;
  1508. reply = ovs_vport_cmd_alloc_info();
  1509. if (!reply)
  1510. return -ENOMEM;
  1511. ovs_lock();
  1512. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1513. err = PTR_ERR(vport);
  1514. if (IS_ERR(vport))
  1515. goto exit_unlock_free;
  1516. if (a[OVS_VPORT_ATTR_TYPE] &&
  1517. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1518. err = -EINVAL;
  1519. goto exit_unlock_free;
  1520. }
  1521. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1522. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1523. if (err)
  1524. goto exit_unlock_free;
  1525. }
  1526. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1527. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1528. err = ovs_vport_set_upcall_portids(vport, ids);
  1529. if (err)
  1530. goto exit_unlock_free;
  1531. }
  1532. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1533. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1534. BUG_ON(err < 0);
  1535. ovs_unlock();
  1536. ovs_notify(&dp_vport_genl_family, reply, info);
  1537. return 0;
  1538. exit_unlock_free:
  1539. ovs_unlock();
  1540. kfree_skb(reply);
  1541. return err;
  1542. }
  1543. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1544. {
  1545. struct nlattr **a = info->attrs;
  1546. struct sk_buff *reply;
  1547. struct vport *vport;
  1548. int err;
  1549. reply = ovs_vport_cmd_alloc_info();
  1550. if (!reply)
  1551. return -ENOMEM;
  1552. ovs_lock();
  1553. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1554. err = PTR_ERR(vport);
  1555. if (IS_ERR(vport))
  1556. goto exit_unlock_free;
  1557. if (vport->port_no == OVSP_LOCAL) {
  1558. err = -EINVAL;
  1559. goto exit_unlock_free;
  1560. }
  1561. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1562. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1563. BUG_ON(err < 0);
  1564. ovs_dp_detach_port(vport);
  1565. ovs_unlock();
  1566. ovs_notify(&dp_vport_genl_family, reply, info);
  1567. return 0;
  1568. exit_unlock_free:
  1569. ovs_unlock();
  1570. kfree_skb(reply);
  1571. return err;
  1572. }
  1573. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1574. {
  1575. struct nlattr **a = info->attrs;
  1576. struct ovs_header *ovs_header = info->userhdr;
  1577. struct sk_buff *reply;
  1578. struct vport *vport;
  1579. int err;
  1580. reply = ovs_vport_cmd_alloc_info();
  1581. if (!reply)
  1582. return -ENOMEM;
  1583. rcu_read_lock();
  1584. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1585. err = PTR_ERR(vport);
  1586. if (IS_ERR(vport))
  1587. goto exit_unlock_free;
  1588. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1589. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1590. BUG_ON(err < 0);
  1591. rcu_read_unlock();
  1592. return genlmsg_reply(reply, info);
  1593. exit_unlock_free:
  1594. rcu_read_unlock();
  1595. kfree_skb(reply);
  1596. return err;
  1597. }
  1598. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1599. {
  1600. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1601. struct datapath *dp;
  1602. int bucket = cb->args[0], skip = cb->args[1];
  1603. int i, j = 0;
  1604. rcu_read_lock();
  1605. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1606. if (!dp) {
  1607. rcu_read_unlock();
  1608. return -ENODEV;
  1609. }
  1610. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1611. struct vport *vport;
  1612. j = 0;
  1613. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1614. if (j >= skip &&
  1615. ovs_vport_cmd_fill_info(vport, skb,
  1616. NETLINK_CB(cb->skb).portid,
  1617. cb->nlh->nlmsg_seq,
  1618. NLM_F_MULTI,
  1619. OVS_VPORT_CMD_NEW) < 0)
  1620. goto out;
  1621. j++;
  1622. }
  1623. skip = 0;
  1624. }
  1625. out:
  1626. rcu_read_unlock();
  1627. cb->args[0] = i;
  1628. cb->args[1] = j;
  1629. return skb->len;
  1630. }
  1631. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1632. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1633. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1634. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1635. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1636. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1637. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1638. };
  1639. static const struct genl_ops dp_vport_genl_ops[] = {
  1640. { .cmd = OVS_VPORT_CMD_NEW,
  1641. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1642. .policy = vport_policy,
  1643. .doit = ovs_vport_cmd_new
  1644. },
  1645. { .cmd = OVS_VPORT_CMD_DEL,
  1646. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1647. .policy = vport_policy,
  1648. .doit = ovs_vport_cmd_del
  1649. },
  1650. { .cmd = OVS_VPORT_CMD_GET,
  1651. .flags = 0, /* OK for unprivileged users. */
  1652. .policy = vport_policy,
  1653. .doit = ovs_vport_cmd_get,
  1654. .dumpit = ovs_vport_cmd_dump
  1655. },
  1656. { .cmd = OVS_VPORT_CMD_SET,
  1657. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1658. .policy = vport_policy,
  1659. .doit = ovs_vport_cmd_set,
  1660. },
  1661. };
  1662. struct genl_family dp_vport_genl_family = {
  1663. .id = GENL_ID_GENERATE,
  1664. .hdrsize = sizeof(struct ovs_header),
  1665. .name = OVS_VPORT_FAMILY,
  1666. .version = OVS_VPORT_VERSION,
  1667. .maxattr = OVS_VPORT_ATTR_MAX,
  1668. .netnsok = true,
  1669. .parallel_ops = true,
  1670. .ops = dp_vport_genl_ops,
  1671. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1672. .mcgrps = &ovs_dp_vport_multicast_group,
  1673. .n_mcgrps = 1,
  1674. };
  1675. static struct genl_family * const dp_genl_families[] = {
  1676. &dp_datapath_genl_family,
  1677. &dp_vport_genl_family,
  1678. &dp_flow_genl_family,
  1679. &dp_packet_genl_family,
  1680. };
  1681. static void dp_unregister_genl(int n_families)
  1682. {
  1683. int i;
  1684. for (i = 0; i < n_families; i++)
  1685. genl_unregister_family(dp_genl_families[i]);
  1686. }
  1687. static int dp_register_genl(void)
  1688. {
  1689. int err;
  1690. int i;
  1691. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1692. err = genl_register_family(dp_genl_families[i]);
  1693. if (err)
  1694. goto error;
  1695. }
  1696. return 0;
  1697. error:
  1698. dp_unregister_genl(i);
  1699. return err;
  1700. }
  1701. static int __net_init ovs_init_net(struct net *net)
  1702. {
  1703. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1704. INIT_LIST_HEAD(&ovs_net->dps);
  1705. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1706. return 0;
  1707. }
  1708. static void __net_exit ovs_exit_net(struct net *net)
  1709. {
  1710. struct datapath *dp, *dp_next;
  1711. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1712. ovs_lock();
  1713. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1714. __dp_destroy(dp);
  1715. ovs_unlock();
  1716. cancel_work_sync(&ovs_net->dp_notify_work);
  1717. }
  1718. static struct pernet_operations ovs_net_ops = {
  1719. .init = ovs_init_net,
  1720. .exit = ovs_exit_net,
  1721. .id = &ovs_net_id,
  1722. .size = sizeof(struct ovs_net),
  1723. };
  1724. static int __init dp_init(void)
  1725. {
  1726. int err;
  1727. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1728. pr_info("Open vSwitch switching datapath\n");
  1729. err = ovs_internal_dev_rtnl_link_register();
  1730. if (err)
  1731. goto error;
  1732. err = ovs_flow_init();
  1733. if (err)
  1734. goto error_unreg_rtnl_link;
  1735. err = ovs_vport_init();
  1736. if (err)
  1737. goto error_flow_exit;
  1738. err = register_pernet_device(&ovs_net_ops);
  1739. if (err)
  1740. goto error_vport_exit;
  1741. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1742. if (err)
  1743. goto error_netns_exit;
  1744. err = dp_register_genl();
  1745. if (err < 0)
  1746. goto error_unreg_notifier;
  1747. return 0;
  1748. error_unreg_notifier:
  1749. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1750. error_netns_exit:
  1751. unregister_pernet_device(&ovs_net_ops);
  1752. error_vport_exit:
  1753. ovs_vport_exit();
  1754. error_flow_exit:
  1755. ovs_flow_exit();
  1756. error_unreg_rtnl_link:
  1757. ovs_internal_dev_rtnl_link_unregister();
  1758. error:
  1759. return err;
  1760. }
  1761. static void dp_cleanup(void)
  1762. {
  1763. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1764. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1765. unregister_pernet_device(&ovs_net_ops);
  1766. rcu_barrier();
  1767. ovs_vport_exit();
  1768. ovs_flow_exit();
  1769. ovs_internal_dev_rtnl_link_unregister();
  1770. }
  1771. module_init(dp_init);
  1772. module_exit(dp_cleanup);
  1773. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1774. MODULE_LICENSE("GPL");